-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (63 loc) · 1.75 KB
/
main.go
File metadata and controls
75 lines (63 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/cretz/bine/tor"
)
func main() {
// Directory where the binary is being executed
currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting current directory: %v", err)
}
// Local server port
port := 8080
// Starts local HTTP server serving the current directory
go func() {
log.Printf("Serving %s at http://localhost:%d", currentDir, port)
http.Handle("/", http.FileServer(http.Dir(currentDir)))
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
log.Fatalf("HTTP server error: %v", err)
}
}()
// Starts embedded Tor
t, err := tor.Start(nil, &tor.StartConf{
DebugWriter: nil, // silences Tor log output
})
if err != nil {
log.Fatalf("Error starting Tor: %v", err)
}
defer t.Close()
// Waits for Tor to be ready
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
// Creates .onion service on port 80
hiddenService, err := t.Listen(ctx, &tor.ListenConf{
Version3: true,
RemotePorts: []int{80},
})
if err != nil {
log.Fatalf("Error creating .onion service: %v", err)
}
defer hiddenService.Close()
onionURL := hiddenService.ID + ".onion"
fmt.Println("🚀 Your site is available on the Tor network:")
fmt.Println(" http://" + onionURL)
// Routes .onion connections to the same local content
go func() {
log.Printf("Forwarding .onion to %s", currentDir)
if err := http.Serve(hiddenService, http.FileServer(http.Dir(currentDir))); err != nil {
log.Fatalf("Error serving .onion: %v", err)
}
}()
// Waits for Ctrl+C
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
fmt.Println("\nShutting down HTTP and .onion services...")
}