Debian Codegraph @

examples/simpleserver/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "log"
7 "net/http"
8 "time"
9
10 "github.com/cretz/bine/tor"
11 )
12
13 func main() {
14 if err := run(); err != nil {
15 log.Fatal(err)
16 }
17 }
18
19 func run() error {
20 // Start tor with default config (can set start conf's DebugWriter to os.Stdout for debug logs)
21 fmt.Println("Starting and registering onion service, please wait a couple of minutes...")
22 t, err := tor.Start(nil, nil)
23 if err != nil {
24 return err
25 }
26 defer t.Close()
27 // Add a handler
28 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
29 w.Write([]byte("Hello, Dark World!"))
30 })
31 // Wait at most a few minutes to publish the service
32 listenCtx, listenCancel := context.WithTimeout(context.Background(), 3*time.Minute)
33 defer listenCancel()
34 // Create an onion service to listen on 8080 but show as 80
35 onion, err := t.Listen(listenCtx, &tor.ListenConf{LocalPort: 8080, RemotePorts: []int{80}, Version3: true})
36 if err != nil {
37 return err
38 }
39 defer onion.Close()
40 // Serve on HTTP
41 fmt.Printf("Open Tor browser and navigate to http://%v.onion\n", onion.ID)
42 return http.Serve(onion, nil)
43 }