This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
404 lines (352 loc) · 9.09 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package main
import (
"context"
"crypto/ecdsa"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
xdg "github.com/adrg/xdg"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
badger "github.com/ipfs/go-ds-badger"
log "github.com/ipfs/go-log/v2"
crypto "github.com/libp2p/go-libp2p-core/crypto"
cli "github.com/urfave/cli/v2"
zap "go.uber.org/zap"
)
const appName = "gemini-ipfs-gateway"
func init() {
log.SetLogLevel("gemini", "info")
}
type key ecdsa.PrivateKey
func (k *key) MarshalJSON() ([]byte, error) {
b, err := x509.MarshalECPrivateKey((*ecdsa.PrivateKey)(k))
if err != nil {
return nil, err
}
return json.Marshal(b)
}
func (k *key) UnmarshalJSON(b []byte) error {
var b2 []byte
err := json.Unmarshal(b, &b2)
if err != nil {
return err
}
parsed, err := x509.ParseECPrivateKey(b2)
if err != nil {
return err
}
*k = key(*parsed)
return nil
}
type appConfig struct {
PrivateKey *key `json:"private_key"`
}
func main() {
dataFolder, err := xdg.DataFile(appName)
if err != nil {
logger.Error(err)
logger.Error("error determining default data folder. Defaulting to \"data\".")
dataFolder = "data"
}
configFolder, err := xdg.ConfigFile(appName)
if err != nil {
logger.Error(err)
logger.Error("error determining default config folder. Defaulting to \"config\".")
configFolder = "config"
}
app := &cli.App{
Name: appName,
Usage: "Access IPFS content using the Gemini Protocol",
HideHelpCommand: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "data",
Usage: "Storage folder",
Value: dataFolder,
TakesFile: true,
EnvVars: []string{"GIGW_DATA"},
},
&cli.StringFlag{
Name: "config",
Usage: "Config folder",
Value: configFolder,
TakesFile: true,
EnvVars: []string{"GIGW_CONFIG"},
},
&cli.StringSliceFlag{
Name: "listen",
Usage: "Listen on `ip:port`",
Value: cli.NewStringSlice("0.0.0.0:2021", "[::]:2022"),
EnvVars: []string{"GIGW_LISTEN"},
},
&cli.StringSliceFlag{
Name: "p2p-listen",
Usage: "Enable libp2p server mode and listen on `ip:port`",
Value: nil,
EnvVars: []string{"GIGW_P2P_LISTEN"},
},
&cli.BoolFlag{
Name: "tls",
Usage: "Enable TLS with self-signed certificate",
Value: false,
EnvVars: []string{"GIGW_TLS"},
},
&cli.BoolFlag{
Name: "inmem",
Usage: "Run fully in-memory, using a random identity",
Value: false,
EnvVars: []string{"GIGW_INMEM"},
},
&cli.BoolFlag{
Name: "v",
Usage: "Verbose (-vvv for full verbosity)",
Value: false,
},
&cli.BoolFlag{
Name: "vv",
Value: false,
Hidden: true,
},
&cli.BoolFlag{
Name: "vvv",
Value: false,
Hidden: true,
},
},
CustomAppHelpTemplate: `Usage: gemini-ipfs-gateway [FLAGS]
Flags:
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}{{end}}
`,
Action: run,
}
err = app.Run(os.Args)
if err != nil {
logger.Error(err)
fmt.Println("gemini-ipfs-gateway terminated abnormally")
}
}
func run(cctx *cli.Context) error {
ctx, cancel := context.WithCancel(cctx.Context)
defer cancel()
handleSigTerm(cancel)
setupVerbosity(cctx.Bool("v"), cctx.Bool("vv"), cctx.Bool("vvv"))
inMem := cctx.Bool("inmem")
err := setupConfigFolder(inMem, cctx.String("config"))
if err != nil {
return err
}
id, err := setupIdentity(inMem, cctx.String("config"))
if err != nil {
return err
}
crt, err := setupCertificate(
cctx.Bool("tls"),
inMem,
id,
cctx.String("config"),
cctx.StringSlice("listen"),
)
if err != nil {
return err
}
ds, err := setupStorage(inMem, cctx.String("data"))
if err != nil {
return err
}
cfg := Config{
Identity: id,
Certificate: crt,
GatewayListenAddresses: cctx.StringSlice("listen"),
PeerListenAddresses: cctx.StringSlice("p2p-listen"),
TLSListeners: cctx.Bool("tls"),
ReadTimeout: 0,
WriteTimeout: 5 * time.Second,
GatewayTimeout: 30 * time.Second,
}
gw, err := NewGateway(ctx, ds, cfg)
if err != nil {
return err
}
err = gw.ListenAndServe()
if err != nil {
return err
}
return nil
}
func handleSigTerm(cancel context.CancelFunc) {
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
)
go func() {
for {
s := <-signalChan
logger.Infof("Aborting on signal: %s", s)
cancel()
}
}()
}
func setupStorage(inMem bool, storagePath string) (ds.Batching, error) {
if inMem {
logger.Info("In-memory datastore enabled")
return dssync.MutexWrap(ds.NewMapDatastore()), nil
}
return badger.NewDatastore(storagePath, &badger.DefaultOptions)
}
func setupConfigFolder(inMem bool, configPath string) error {
if inMem {
return nil
}
_, err := os.Stat(configPath)
// Check config folder exists.
if os.IsNotExist(err) {
err = os.MkdirAll(configPath, 0700)
if err != nil {
return fmt.Errorf("error creating config folder: %w", err)
}
} else if err != nil {
return fmt.Errorf("error checking config folder: %w", err)
}
return nil
}
func setupIdentity(inMem bool, configPath string) (*ecdsa.PrivateKey, error) {
if inMem {
logger.Info("Using a new random in-memory private key")
return ecdsa.GenerateKey(crypto.ECDSACurve, rand.Reader)
}
// Open config file.
path := filepath.Join(configPath, "config.json")
f, err := os.Open(path)
if os.IsNotExist(err) {
pk, err := ecdsa.GenerateKey(crypto.ECDSACurve, rand.Reader)
if err != nil {
return nil, err
}
cfg := &appConfig{
PrivateKey: (*key)(pk),
}
w, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return nil, fmt.Errorf("error generating configuration file: %w", err)
}
defer w.Close()
enc := json.NewEncoder(w)
err = enc.Encode(cfg)
if err != nil {
return nil, fmt.Errorf("error writing configuration file (%s): %w", path, err)
}
logger.Infof("Stored new configuration and private key at %s", path)
return pk, nil
} else if err != nil {
return nil, fmt.Errorf("error opening configuration: %w", err)
}
defer f.Close()
dec := json.NewDecoder(f)
var cfg appConfig
err = dec.Decode(&cfg)
if err != nil {
return nil, fmt.Errorf("error parsing configuration (%s): %w", path, err)
}
logger.Infof("Private key loaded from %s", path)
return (*ecdsa.PrivateKey)(cfg.PrivateKey), nil
}
func setupCertificate(tlsEnabled, inMem bool, pk *ecdsa.PrivateKey, configPath string, addrs []string) (tls.Certificate, error) {
if !tlsEnabled {
return tls.Certificate{}, nil
}
if inMem {
// generate on the fly
crt, err := makeCertificate(pk, addrs)
if err != nil {
return tls.Certificate{}, fmt.Errorf("error generating x509 certificate: %w", err)
}
return crt, nil
}
certPath := filepath.Join(configPath, "cert.pem")
keyPath := filepath.Join(configPath, "key.pem")
_, errCert := os.Stat(certPath)
if errCert != nil && !os.IsNotExist(errCert) {
return tls.Certificate{}, fmt.Errorf("error trying to read %s: %w", certPath, errCert)
}
certExist := errCert == nil
_, errKey := os.Stat(keyPath)
if errKey != nil && !os.IsNotExist(errKey) {
return tls.Certificate{}, fmt.Errorf("error trying to read %s: %w", keyPath, errKey)
}
keyExist := errKey == nil
if keyExist && certExist {
logger.Infof("Loading custom gateway TLS certificate and key from %s and %s", certPath, keyPath)
return tls.LoadX509KeyPair(certPath, keyPath)
}
// Return using identity key
if certExist {
// Attempt to load cached certificate
pemCert, err := ioutil.ReadFile(certPath)
if err != nil {
return tls.Certificate{}, fmt.Errorf("error reading certificate: %w", err)
}
asnKey, err := x509.MarshalECPrivateKey(pk)
if err != nil {
return tls.Certificate{}, fmt.Errorf("marshaling private key: %w", err)
}
pemKey := pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: asnKey,
})
logger.Infof("Loading cached TLS certificate from %s", certPath)
return tls.X509KeyPair(pemCert, pemKey)
}
// Key not on disk. Cert not on disk.
// Create cert. Save it to disk.
crt, err := makeCertificate(pk, addrs)
if err != nil {
return tls.Certificate{}, fmt.Errorf("error generating x509 certificate: %w", err)
}
blk := &pem.Block{
Type: "CERTIFICATE",
Bytes: crt.Certificate[0],
}
w, err := os.OpenFile(certPath, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return tls.Certificate{}, fmt.Errorf("error creating certificate file: %w", err)
}
defer w.Close()
err = pem.Encode(w, blk)
if err != nil {
return tls.Certificate{}, fmt.Errorf("error writing certificate file: %w", err)
}
logger.Infof("Generated TLS certificate and saved it to %s", certPath)
return crt, nil
}
func setupVerbosity(v, vv, vvv bool) {
// Default
log.SetLogLevel("*", "error")
log.SetLogLevel("gemini", "info")
switch {
case vvv:
log.SetLogLevel("*", "debug")
case vv:
log.SetLogLevel("swarm2", "info")
fallthrough
case v:
logger.SugaredLogger = *(logger.SugaredLogger.
Desugar().
WithOptions(zap.AddCaller()).
Sugar())
log.SetLogLevel("gemini", "debug")
}
}