-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
386 lines (382 loc) · 12.4 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"context"
"crypto/ed25519"
"crypto/tls"
"encoding/base32"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"log"
mathRand "math/rand"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/anacrolix/bargle"
"github.com/anacrolix/envpprof"
"github.com/anacrolix/generics"
"github.com/anacrolix/missinggo/v2/iter"
"github.com/dgraph-io/ristretto"
_ "github.com/honeycombio/honeycomb-opentelemetry-go"
"github.com/honeycombio/opentelemetry-go-contrib/launcher"
"github.com/multiformats/go-base36"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"golang.org/x/crypto/acme/autocert"
)
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
main := bargle.Main{}
main.Defer(envpprof.Stop)
type encoderFunc func([]byte) string
encoderChoices := bargle.Choices[encoderFunc]{
"base64url": base64.URLEncoding.EncodeToString,
"base36lc": base36.EncodeToStringLc,
"base32hex": base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString,
"hex": hex.EncodeToString,
}
main.Positionals = append(main.Positionals,
bargle.Subcommand{Name: "proxy", Command: proxy()},
bargle.Subcommand{Name: "gencert", Command: genCert()},
bargle.Subcommand{Name: "extract-pubkey", Command: func() (cmd bargle.Command) {
encoderChoice := bargle.NewChoice(encoderChoices)
cmd.Options = append(cmd.Options, func() bargle.Param {
m := bargle.NewUnaryOption(encoderChoice)
m.AddLong("encoder")
m.SetDefault("base36lc")
return m.Make()
}())
var seed []byte
{
var encodedSeed string
m := &bargle.Positional{
Value: bargle.AutoUnmarshaler(&encodedSeed),
Name: "private-key",
Desc: "private key seed",
AfterParseFunc: func(ctx bargle.Context) (err error) {
seed, err = hex.DecodeString(encodedSeed)
if err != nil {
return
}
if len(seed) != ed25519.SeedSize {
return fmt.Errorf("expected %v bytes, got %v", ed25519.SeedSize, len(seed))
}
return nil
},
}
cmd.Positionals = append(cmd.Positionals, m)
}
cmd.DefaultAction = func() error {
privKey := ed25519.NewKeyFromSeed(seed)
fmt.Println(encoderChoice.Value()(privKey.Public().(ed25519.PublicKey)))
return nil
}
return
}()},
bargle.Subcommand{Name: "convert", Command: func() (cmd bargle.Command) {
cmd.Desc = "convert between different encodings used in btlink"
type decoderFunc func(string) ([]byte, error)
decoderChoice := bargle.NewChoice(bargle.Choices[decoderFunc]{
"hex": hex.DecodeString,
"base36": base36.DecodeString,
"base32hex": base32.HexEncoding.DecodeString,
})
decoderParam := bargle.NewUnaryOption(decoderChoice)
decoderParam.AddLong("from")
decoderParam.AddShort('f')
decoderParam.SetDefault("hex")
encoderChoice := &bargle.Choice[encoderFunc]{Choices: encoderChoices}
encoderParam := bargle.NewUnaryOption(encoderChoice)
encoderParam.AddLong("to")
encoderParam.AddShort('t')
encoderParam.SetRequired()
cmd.Options = append(cmd.Options,
decoderParam.Make(),
encoderParam.Make())
var input generics.Option[string]
cmd.Positionals = append(cmd.Positionals, &bargle.Positional{
Name: "input",
Desc: "data to decode",
Value: bargle.NewOption(&input, nil),
})
cmd.DefaultAction = func() error {
b, err := decoderChoice.Value()(input.Unwrap())
if err != nil {
return fmt.Errorf("error decoding input: %w", err)
}
fmt.Println(encoderChoice.Value()(b))
return nil
}
return
}()},
bargle.Subcommand{Name: "generate-keys", Command: func() (cmd bargle.Command) {
cmd.Desc = "search for progressively shorter valid public keys"
parallelism := runtime.NumCPU()
cmd.Options = append(cmd.Options, func() bargle.Param {
m := bargle.NewUnaryOption(bargle.AutoUnmarshaler(¶llelism))
m.AddLong("parallel")
return m.Make()
}())
cmd.DefaultAction = func() error {
var (
mu sync.Mutex
tries int64
shortest = ""
)
for range iter.N(parallelism) {
go func() {
getPair := func() func() ([]byte, []byte) {
if true {
seed := make([]byte, ed25519.SeedSize)
return func() ([]byte, []byte) {
mathRand.Read(seed)
privKey := ed25519.NewKeyFromSeed(seed)
pubKey := privKey[ed25519.SeedSize:]
return pubKey, seed
}
} else {
return func() ([]byte, []byte) {
pubKey, privKey, _ := ed25519.GenerateKey(nil)
return pubKey, privKey.Seed()
}
}
}()
for {
pubKey, seed := getPair()
base36PubKey := base36.EncodeToStringLc(pubKey)
mu.Lock()
if shortest == "" || len(base36PubKey) < len(shortest) {
shortest = base36PubKey
seedBase36Lc := base36.EncodeToStringLc(seed)
fmt.Printf("%s %s\n", base36PubKey, seedBase36Lc)
}
tries++
mu.Unlock()
}
}()
}
var lastTries int64
duration := time.Second
for {
time.Sleep(duration)
mu.Lock()
curTries := tries
mu.Unlock()
log.Printf("%v tries/s", (curTries-lastTries)/int64(duration/time.Second))
duration *= 2
lastTries = curTries
}
}
return
}()},
)
main.Run()
}
func proxy() (cmd bargle.Command) {
cmd.Desc = "run proxy/gateway combo"
var (
confluenceHost string
confluenceScheme string
httpPortInt uint16 = 42080
httpsPortInt uint16 = 44369
gatewayDomains []string
logRequestHeaders bool
)
opt := bargle.NewUnaryOption(bargle.AutoUnmarshaler(&confluenceHost))
opt.AddLong("confluence-host")
opt.AddShort('h')
opt.SetRequired()
cmd.Options = append(cmd.Options, opt.Make())
opt = bargle.NewUnaryOption(bargle.AutoUnmarshaler(&confluenceScheme))
opt.AddLong("confluence-scheme")
opt.AddShort('s')
opt.SetRequired()
cmd.Options = append(cmd.Options, opt.Make())
opt = bargle.NewUnaryOption(bargle.AutoUnmarshaler(&httpPortInt))
opt.AddLong("http-port")
cmd.Options = append(cmd.Options, opt.Make())
opt = bargle.NewUnaryOption(bargle.AutoUnmarshaler(&httpsPortInt))
opt.AddLong("https-port")
cmd.Options = append(cmd.Options, opt.Make())
opt = bargle.NewUnaryOption(bargle.AutoUnmarshaler(&gatewayDomains))
opt.AddLong("gateway-domain")
opt.AddShort('g')
opt.Description(`whitelist of domain roots to issue certificates for`)
cmd.Options = append(cmd.Options, opt.Make())
flagOpt := bargle.NewFlag(&logRequestHeaders)
flagOpt.AddLong("log-request-headers")
cmd.Options = append(cmd.Options, flagOpt.Make())
cmd.DefaultAction = func() error {
shutdownTelemetry, err := launcher.ConfigureOpenTelemetry()
if err != nil {
err = fmt.Errorf("configuring open telemetry: %w", err)
// Silly honeycomb helper has a newline in the error message. Not cool.
log.Printf("%q", err)
} else {
defer shutdownTelemetry()
}
confluenceClientCert, err := tls.LoadX509KeyPair("confluence.pem", "confluence.pem")
if err != nil {
log.Printf("error loading confluence client cert: %v", err)
}
dhtItemCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 30,
// So we can trigger this sooner for testing.
MaxCost: 3,
BufferItems: 64,
// Because we don't represent the cost of cache items using bytes, but ristretto will add
// the internal cost for the key in bytes.
IgnoreInternalCost: true,
OnExit: func(val interface{}) {
v := val.(*dhtItemCacheValue)
log.Printf("value removed from dht item cache [target=%v]", v.target)
},
})
if err != nil {
return fmt.Errorf("new dht item cache: %w", err)
}
infoCache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 10000,
MaxCost: 50e6,
BufferItems: 64,
})
if err != nil {
return fmt.Errorf("new info cache: %w", err)
}
handler := gatewayHandler{
confluence: confluenceHandler{
confluenceHost: confluenceHost,
confluenceScheme: confluenceScheme,
confluenceTransport: otelhttp.NewTransport(&http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{confluenceClientCert},
InsecureSkipVerify: true,
},
}),
},
dhtItemCache: dhtItemCache,
dirPageTemplate: htmlTemplates.Lookup("dir.html"),
metainfoCache: infoCache,
}
httpPort := strconv.FormatUint(uint64(httpPortInt), 10) // Make the default 42080
httpAddr := ":" + httpPort
httpsPort := strconv.FormatUint(uint64(httpsPortInt), 10) // Make sure default is 44369
httpsAddr := ":" + httpsPort
proxyMux := http.NewServeMux()
proxyMux.HandleFunc("/.btlink/proxy.pac", func(w http.ResponseWriter, r *http.Request) {
err := serveDynamicPac(w, r, httpPort, httpsPort)
if err != nil {
log.Printf("error serving dynamic pac: %v", err)
}
})
proxyMux.HandleFunc("/.btlink/rootca.pem", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "ca.pem")
})
autocertManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache("autocert-cache"),
HostPolicy: func(ctx context.Context, host string) error {
// Note that this doesn't match a specific proxy domain (which we can't just use
// Cloudflare for due to CONNECT). If the proxy domain isn't the same as one of the
// gateway domains, more configuration is required here. There's also no checking that
// the requested domain name is valid, which would reduce unnecessary certificate
// issuance. Lastly, if there is rate-limiting applied, it might be worth looking into
// ACME DNS challenges to obtain wildcard certificates.
for _, gd := range gatewayDomains {
if host == gd || strings.HasSuffix(host, "."+gd) {
return nil
}
}
return errors.New("no gateway domains matched")
},
Email: "[email protected]",
ShortSAN: "btlink.anacrolix.link",
}
proxyHandler := func(logPrefix string) http.Handler {
return otelhttp.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if logRequestHeaders {
log.Printf("%v: received request\n%s", logPrefix, requestLogString(r))
}
err := func() error {
// Connect can be passed to a HTTPS proxy endpoint. We want to handle this
// ourselves, so we loop it back too. This also works if we receive CONNECT over HTTPS.
if r.Method == http.MethodConnect {
// We serve TLS by looping back to the HTTPS handler on this host.
log.Printf("handling proxy request for %q", requestUrl(r))
handleConnect(w, "localhost"+httpsAddr, r)
return nil
}
if handler.ServeHTTP(w, r) {
return nil
}
log.Printf("handling proxy request for %q", requestUrl(r))
proxyMux.ServeHTTP(w, r)
return nil
}()
if err != nil {
log.Printf("%v: error in proxy handler: %v", logPrefix, err)
}
}),
logPrefix,
)
}
serverErrs := make(chan error, 2)
go func() {
log.Printf("starting http server at %q", httpAddr)
err := http.ListenAndServe(httpAddr, autocertManager.HTTPHandler(proxyHandler("http server")))
log.Printf("http server returned: %v", err)
serverErrs <- err
}()
if true {
go func() {
var certs []tls.Certificate
cert, err := tls.LoadX509KeyPair("wildcard.bt.pem", "ca.key")
if err != nil {
log.Printf("error loading bt wildcard cert: %v", err)
} else {
certs = append(certs, cert)
}
cert, err = tls.LoadX509KeyPair("localhost.pem", "ca.key")
if err != nil {
log.Printf("error loading localhost cert: %v", err)
} else {
certs = append(certs, cert)
}
tlsConfig := &tls.Config{
GetCertificate: func(info *tls.ClientHelloInfo) (_ *tls.Certificate, err error) {
for _, cert := range certs {
if info.SupportsCertificate(&cert) == nil {
return &cert, nil
}
}
started := time.Now()
defer func() {
elapsed := time.Since(started)
if elapsed > time.Second {
go log.Printf("getting certificate for %q took %v: %v", info.ServerName, elapsed, err)
}
}()
return autocertManager.GetCertificate(info)
},
}
s := http.Server{
Addr: httpsAddr,
Handler: proxyHandler("tls http server"),
TLSConfig: tlsConfig,
// TODO: Test this with Safari using HTTPS then falling back to HTTP proxying.
ReadHeaderTimeout: 5 * time.Second,
}
log.Printf("starting https server at %q", s.Addr)
err = s.ListenAndServeTLS("", "")
log.Printf("https server returned: %v", err)
serverErrs <- err
}()
}
return fmt.Errorf("server error: %w", <-serverErrs)
}
return
}