This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
/
listener.go
71 lines (61 loc) · 1.8 KB
/
listener.go
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
// Listener example.
//
// To the extent possible under law, Yawning Angel waived all copyright
// and related or neighboring rights to bulb, using the creative
// commons "cc0" public domain dedication. See LICENSE or
// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
package main
import (
"crypto/rand"
"crypto/rsa"
"io"
"log"
"net/http"
"github.com/yawning/bulb"
"github.com/yawning/bulb/utils/pkcs1"
)
func onionServer(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "hello, onion world!\n")
}
func main() {
// Connect to a running tor instance.
// * TCP: c, err := bulb.Dial("tcp4", "127.0.0.1:9051")
c, err := bulb.Dial("unix", "/var/run/tor/control")
if err != nil {
log.Fatalf("failed to connect to control port: %v", err)
}
defer c.Close()
// See what's really going on under the hood.
// Do not enable in production.
c.Debug(true)
// Authenticate with the control port. The password argument
// here can be "" if no password is set (CookieAuth, no auth).
if err := c.Authenticate("ExamplePassword"); err != nil {
log.Fatalf("Authentication failed: %v", err)
}
// Generate a private key and create a port 80 Onion Service.
//
// For one-shot services:` l, err := c.Listener(80, nil)` is considerably
// easier.
pk, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
log.Fatalf("Failed to generate RSA key")
}
id, err := pkcs1.OnionAddr(&pk.PublicKey)
if err != nil {
log.Fatalf("Failed to derive onion ID: %v", err)
}
log.Printf("Expected ID: %v", id)
cfg := &bulb.NewOnionConfig{
DiscardPK: true,
PrivateKey: pk,
}
l, err := c.NewListener(cfg, 80)
if err != nil {
log.Fatalf("Failed to get Listener: %v", err)
}
defer l.Close()
log.Printf("Listener: %s", l.Addr())
http.HandleFunc("/", onionServer)
http.Serve(l, nil)
}