arnaucube
arnaucube/tor-eth-online-shopPublic Self-hosted repository
tor-eth-online-shop/main.go
main.goRaw
 1package main
 2
 3import (
 4	"context"
 5	"fmt"
 6	"log"
 7	"net/http"
 8	"os"
 9	"time"
10
11	"github.com/cretz/bine/tor"
12	"github.com/ipsn/go-libtor"
13)
14
15func main() {
16	ReadConfig(".", "config")
17
18	// Start tor with some defaults + elevated verbosity
19	fmt.Println("Starting and registering onion service, please wait a bit...")
20	t, err := tor.Start(nil, &tor.StartConf{ProcessCreator: libtor.Creator, DebugWriter: os.Stderr})
21	if err != nil {
22		log.Panicf("Failed to start tor: %v", err)
23	}
24	defer t.Close()
25
26	// Wait at most a few minutes to publish the service
27	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
28	defer cancel()
29
30	// Create an onion service to listen on any port but show as 80
31	onion, err := t.Listen(ctx, &tor.ListenConf{RemotePorts: []int{80}})
32	if err != nil {
33		log.Panicf("Failed to create onion service: %v", err)
34	}
35	defer onion.Close()
36
37	fmt.Printf("Please open a Tor capable browser and navigate to http://%v.onion\n", onion.ID)
38
39	// connect to eth
40	// Ethereum
41	err = Web3Open()
42	if err != nil {
43		log.Fatal(err)
44	}
45
46	// Run a Hello-World HTTP service until terminated
47	http.Handle("/", http.FileServer(http.Dir("./www")))
48	http.HandleFunc("/api/purchase", handlePurchase)
49	http.HandleFunc("/api/txid", handleTxId)
50	http.Serve(onion, nil)
51}