-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathonionize.go
195 lines (178 loc) · 4.55 KB
/
onionize.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// onionize.go - onionize things.
//
// To the extent possible under law, Ivan Markin waived all copyright
// and related or neighboring rights to this module of onionize, using the creative
// commons "cc0" public domain dedication. See LICENSE or
// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
package onionize
import (
"crypto"
"crypto/rand"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"net/url"
"strings"
"github.com/nogoegst/bulb"
"github.com/nogoegst/fileserver"
"github.com/nogoegst/onionize/util"
"github.com/nogoegst/onionutil"
)
const slugLength = 16
type Parameters struct {
Pathspec string
Zip bool
Slug bool
ControlPath string
ControlPassword string
Passphrase string
Debug bool
IdentityKey crypto.PrivateKey
TLSConfig *tls.Config
NoOnion bool
StartTor bool
}
func generateSlug() (string, error) {
slugBin := make([]byte, (slugLength*5)/8+1)
_, err := rand.Read(slugBin)
if err != nil {
return "", err
}
return onionutil.Base32Encode(slugBin)[:slugLength], nil
}
func Onionize(p Parameters, linkChan chan<- url.URL) error {
// Run tor instance ourselves
if p.StartTor {
p.ControlPath = "tcp://127.0.0.1:9999"
torReady := make(chan struct{})
go func() {
err := runTor(torReady)
if err != nil {
log.Fatal(err)
}
}()
<-torReady
}
var handler http.Handler
var slug string
var err error
if p.Slug && !p.NoOnion {
slug, err = generateSlug()
if err != nil {
return fmt.Errorf("Unable to generate slug: %v", err)
}
}
useOnion := !p.NoOnion
link := url.URL{Path: "/"}
var c *bulb.Conn
nocfg := &bulb.NewOnionConfig{
DiscardPK: true,
AwaitForUpload: true,
}
if strings.HasPrefix(p.Pathspec, "http://") || strings.HasPrefix(p.Pathspec, "https://") {
target, err := url.Parse(p.Pathspec)
if err != nil {
return fmt.Errorf("Unable to parse target URL: %v", err)
}
handler = onionReverseHTTPProxy(target)
} else {
handler, err = fileserver.New(p.Pathspec, p.Zip, p.Debug)
if err != nil {
return err
}
}
server := &http.Server{Handler: subdomainSluggedHandler(handler, slug)}
listenAddress := "127.0.0.1:0"
if useOnion {
// Connect to a running tor instance
if p.ControlPath == "" {
p.ControlPath = "default://"
}
c, err = bulb.DialURL(p.ControlPath)
if err != nil {
return fmt.Errorf("Failed to connect to control socket: %v", err)
}
defer c.Close()
// See what's really going on under the hood
c.Debug(p.Debug)
// Authenticate with the control port
if err := c.Authenticate(p.ControlPassword); err != nil {
return fmt.Errorf("Authentication failed: %v", err)
}
// Derive onion service keymaterial from passphrase or generate a new one
if p.Passphrase != "" {
keyrd := util.KeystreamReader([]byte(p.Passphrase), []byte("onionize-keygen"))
privOnionKey, err := onionutil.GenerateOnionKey(keyrd, "current")
if err != nil {
return fmt.Errorf("Unable to generate onion key: %v", err)
}
nocfg.PrivateKey = privOnionKey
} else {
nocfg.PrivateKey = p.IdentityKey
}
} else {
tc, err := net.Dial("udp", "1.1.1.1:1")
if err != nil {
return err
}
defer tc.Close()
host, _, err := net.SplitHostPort(tc.LocalAddr().String())
if err != nil {
return err
}
listenAddress = host + ":0"
}
var listener net.Listener
rawListener, err := net.Listen("tcp4", listenAddress)
if err != nil {
return err
}
var virtPort uint16
if p.TLSConfig != nil {
listener = tls.NewListener(rawListener, p.TLSConfig)
link.Scheme = "https"
virtPort = uint16(443)
} else {
listener = rawListener
link.Scheme = "http"
virtPort = uint16(80)
}
if useOnion {
portSpec := bulb.OnionPortSpec{
VirtPort: virtPort,
Target: listener.Addr().String(),
}
nocfg.PortSpecs = []bulb.OnionPortSpec{portSpec}
oi, err := c.NewOnion(nocfg)
if err != nil {
return fmt.Errorf("Error occurred while creating an onion service: %v", err)
}
// Track if tor went down
// TODO: Signal from here to perform graceful shutdown and display a message
go func() {
for {
_, err := c.NextEvent()
if err != nil {
log.Fatalf("Lost connection to tor: %v", err)
}
}
}()
if slug != "" {
link.Host = fmt.Sprintf("%s.%s.onion", slug, oi.OnionID)
} else {
link.Host = fmt.Sprintf("%s.onion", oi.OnionID)
}
} else {
link.Host = listener.Addr().String()
}
// Return the link to the service
linkChan <- link
// Run a webservice
err = server.Serve(listener)
if err != nil {
return fmt.Errorf("Cannot serve HTTP: %v", err)
}
return nil
}