forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.go
982 lines (886 loc) · 30.1 KB
/
start.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
package cmd
import (
"context"
"crypto/rand"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"syscall"
"time"
routinghelpers "gx/ipfs/QmRCrPXk2oUwpK1Cj2FXrUotRpddUxz56setkny2gz13Cx/go-libp2p-routing-helpers"
libp2p "gx/ipfs/QmRxk6AUaGaKCfzS1xSNRojiAPd7h2ih8GuCdjJBF3Y6GK/go-libp2p"
dht "gx/ipfs/QmSY3nkMNLzh9GdbFKK5tT7YMfLpf52iUZ8ZRkr29MJaa5/go-libp2p-kad-dht"
ma "gx/ipfs/QmTZBfrPJmjWsCvHEtX5FE6KimVJhsJg5sBbqEFYf4UZtL/go-multiaddr"
config "gx/ipfs/QmUAuYuiafnJRZxDDX7MuruMNsicYNuyub5vUeAcupUBNs/go-ipfs-config"
ipnspb "gx/ipfs/QmUwMnKKjH3JwGKNVZ3TcP37W93xzqNA4ECFFiMo6sXkkc/go-ipns/pb"
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
oniontp "gx/ipfs/QmYv2MbwHn7qcvAPFisZ94w85crQVpwUuv8G7TuUeBnfPb/go-onion-transport"
ipfslogging "gx/ipfs/QmbkT7eMTyXfpeyB3ZMxxcxg7XH8t6uXp49jqzz4HB7BGF/go-log/writer"
manet "gx/ipfs/Qmc85NSvmSG4Frn9Vb2cBc1rMyULH6D3TNVEfCzSKoUpip/go-multiaddr-net"
proto "gx/ipfs/QmddjPSGZb3ieihSseFeCfVRpZzcqczPNsD2DvarSwnjJB/gogo-protobuf/proto"
"github.com/OpenBazaar/openbazaar-go/api"
"github.com/OpenBazaar/openbazaar-go/core"
"github.com/OpenBazaar/openbazaar-go/ipfs"
obnet "github.com/OpenBazaar/openbazaar-go/net"
"github.com/OpenBazaar/openbazaar-go/net/service"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/OpenBazaar/openbazaar-go/repo/db"
"github.com/OpenBazaar/openbazaar-go/repo/migrations"
"github.com/OpenBazaar/openbazaar-go/schema"
sto "github.com/OpenBazaar/openbazaar-go/storage"
"github.com/OpenBazaar/openbazaar-go/storage/dropbox"
"github.com/OpenBazaar/openbazaar-go/storage/selfhosted"
"github.com/OpenBazaar/openbazaar-go/wallet"
lis "github.com/OpenBazaar/openbazaar-go/wallet/listeners"
"github.com/OpenBazaar/openbazaar-go/wallet/resync"
wi "github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil/base58"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/fatih/color"
"github.com/ipfs/go-ipfs/commands"
ipfscore "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/corehttp"
"github.com/ipfs/go-ipfs/namesys"
"github.com/ipfs/go-ipfs/repo/fsrepo"
"github.com/natefinch/lumberjack"
"github.com/op/go-logging"
"github.com/tyler-smith/go-bip39"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/proxy"
)
var stdoutLogFormat = logging.MustStringFormatter(
`%{color:reset}%{color}%{time:15:04:05.000} [%{level}] [%{module}/%{shortfunc}] %{message}`,
)
var fileLogFormat = logging.MustStringFormatter(
`%{time:15:04:05.000} [%{level}] [%{module}/%{shortfunc}] %{message}`,
)
var ErrNoGateways = errors.New("no gateway addresses configured")
type Start struct {
Password string `short:"p" long:"password" description:"the encryption password if the database is encrypted"`
Testnet bool `short:"t" long:"testnet" description:"use the test network"`
Regtest bool `short:"r" long:"regtest" description:"run in regression test mode"`
LogLevel string `short:"l" long:"loglevel" description:"set the logging level [debug, info, notice, warning, error, critical]" default:"debug"`
NoLogFiles bool `short:"f" long:"nologfiles" description:"save logs on disk"`
AllowIP []string `short:"a" long:"allowip" description:"only allow API connections from these IPs"`
STUN bool `short:"s" long:"stun" description:"use stun on µTP IPv4"`
DataDir string `short:"d" long:"datadir" description:"specify the data directory to be used"`
AuthCookie string `short:"c" long:"authcookie" description:"turn on API authentication and use this specific cookie"`
UserAgent string `short:"u" long:"useragent" description:"add a custom user-agent field"`
Verbose bool `short:"v" long:"verbose" description:"print openbazaar logs to stdout"`
TorPassword string `long:"torpassword" description:"Set the tor control password. This will override the tor password in the config."`
Tor bool `long:"tor" description:"Automatically configure the daemon to run as a Tor hidden service and use Tor exclusively. Requires Tor to be running."`
DualStack bool `long:"dualstack" description:"Automatically configure the daemon to run as a Tor hidden service IN ADDITION to using the clear internet. Requires Tor to be running. WARNING: this mode is not private"`
DisableWallet bool `long:"disablewallet" description:"disable the wallet functionality of the node"`
DisableExchangeRates bool `long:"disableexchangerates" description:"disable the exchange rate service to prevent api queries"`
Storage string `long:"storage" description:"set the outgoing message storage option [self-hosted, dropbox] default=self-hosted"`
BitcoinCash bool `long:"bitcoincash" description:"use a Bitcoin Cash wallet in a dedicated data directory"`
ZCash string `long:"zcash" description:"use a ZCash wallet in a dedicated data directory. To use this you must pass in the location of the zcashd binary."`
ForceKeyCachePurge bool `long:"forcekeypurge" description:"repair test for issue OpenBazaar/openbazaar-go#1593; use as instructed only"`
}
func (x *Start) Execute(args []string) error {
printSplashScreen(x.Verbose)
ipfs.UpdateIPFSGlobalProtocolVars(x.Testnet || x.Regtest)
if x.Testnet && x.Regtest {
return errors.New("invalid combination of testnet and regtest modes")
}
if x.Tor && x.DualStack {
return errors.New("invalid combination of tor and dual stack modes")
}
isTestnet := false
if x.Testnet || x.Regtest {
isTestnet = true
}
if x.BitcoinCash && x.ZCash != "" {
return errors.New("bitcoin cash and zcash cannot be used at the same time")
}
// Set repo path
repoPath, err := repo.GetRepoPath(isTestnet)
if err != nil {
return err
}
if x.BitcoinCash {
repoPath += "-bitcoincash"
} else if x.ZCash != "" {
repoPath += "-zcash"
}
if x.DataDir != "" {
repoPath = x.DataDir
}
repoLockFile := filepath.Join(repoPath, fsrepo.LockFile)
os.Remove(repoLockFile)
// Logging
w := &lumberjack.Logger{
Filename: path.Join(repoPath, "logs", "ob.log"),
MaxSize: 10, // Megabytes
MaxBackups: 3,
MaxAge: 30, // Days
}
var backendStdoutFormatter logging.Backend
if x.Verbose {
backendStdout := logging.NewLogBackend(os.Stdout, "", 0)
backendStdoutFormatter = logging.NewBackendFormatter(backendStdout, stdoutLogFormat)
logging.SetBackend(backendStdoutFormatter)
}
if !x.NoLogFiles {
backendFile := logging.NewLogBackend(w, "", 0)
backendFileFormatter := logging.NewBackendFormatter(backendFile, fileLogFormat)
if x.Verbose {
logging.SetBackend(backendFileFormatter, backendStdoutFormatter)
} else {
logging.SetBackend(backendFileFormatter)
}
ipfslogging.LdJSONFormatter()
w2 := &lumberjack.Logger{
Filename: path.Join(repoPath, "logs", "ipfs.log"),
MaxSize: 10, // Megabytes
MaxBackups: 3,
MaxAge: 30, // Days
}
ipfslogging.Output(w2)()
}
var level logging.Level
switch strings.ToLower(x.LogLevel) {
case "debug":
level = logging.DEBUG
case "info":
level = logging.INFO
case "notice":
level = logging.NOTICE
case "warning":
level = logging.WARNING
case "error":
level = logging.ERROR
case "critical":
level = logging.CRITICAL
default:
level = logging.DEBUG
}
logging.SetLevel(level, "")
err = core.CheckAndSetUlimit()
if err != nil {
return err
}
ct := wi.Bitcoin
if x.BitcoinCash || strings.Contains(repoPath, "-bitcoincash") {
ct = wi.BitcoinCash
} else if x.ZCash != "" || strings.Contains(repoPath, "-zcash") {
ct = wi.Zcash
}
migrations.WalletCoinType = ct
sqliteDB, err := InitializeRepo(repoPath, x.Password, "", isTestnet, time.Now(), ct)
if err != nil && err != repo.ErrRepoExists {
log.Error("repo init:", err)
return err
}
// Create user-agent file
userAgentBytes := []byte(core.USERAGENT + x.UserAgent)
err = ioutil.WriteFile(path.Join(repoPath, "root", "user_agent"), userAgentBytes, os.FileMode(0644))
if err != nil {
log.Error("write user_agent:", err)
return err
}
// Get creation date. Ignore the error and use a default timestamp.
creationDate, err := sqliteDB.Config().GetCreationDate()
if err != nil {
log.Error("error loading wallet creation date from database - using unix epoch.")
}
// Load config
configFile, err := ioutil.ReadFile(path.Join(repoPath, "config"))
if err != nil {
log.Error("read config:", err)
return err
}
apiConfig, err := schema.GetAPIConfig(configFile)
if err != nil {
log.Error("scan api config:", err)
return err
}
torConfig, err := schema.GetTorConfig(configFile)
if err != nil {
log.Error("scan tor config:", err)
return err
}
dataSharing, err := schema.GetDataSharing(configFile)
if err != nil {
log.Error("scan data sharing config:", err)
return err
}
dropboxToken, err := schema.GetDropboxApiToken(configFile)
if err != nil {
log.Error("scan dropbox api token:", err)
return err
}
republishInterval, err := schema.GetRepublishInterval(configFile)
if err != nil {
log.Error("scan republish interval config:", err)
return err
}
walletsConfig, err := schema.GetWalletsConfig(configFile)
if err != nil {
log.Error("scan wallets config:", err)
return err
}
ipnsExtraConfig, err := schema.GetIPNSExtraConfig(configFile)
if err != nil {
log.Error("scan ipns extra config:", err)
return err
}
// IPFS node setup
r, err := fsrepo.Open(repoPath)
if err != nil {
log.Error("open repo:", err)
return err
}
cctx, cancel := context.WithCancel(context.Background())
defer cancel()
cfg, err := r.Config()
if err != nil {
log.Error("get repo config:", err)
return err
}
identityKey, err := sqliteDB.Config().GetIdentityKey()
if err != nil {
log.Error("get identity key:", err)
return err
}
identity, err := ipfs.IdentityFromKey(identityKey)
if err != nil {
log.Error("get identity from key:", err)
return err
}
cfg.Identity = identity
// Setup testnet
if x.Testnet || x.Regtest {
// set testnet bootstrap addrs
testnetBootstrapAddrs, err := schema.GetTestnetBootstrapAddrs(configFile)
if err != nil {
log.Error(err)
return err
}
cfg.Bootstrap = testnetBootstrapAddrs
// don't use pushnodes on testnet
dataSharing.PushTo = []string{}
}
onionAddr, err := obnet.MaybeCreateHiddenServiceKey(repoPath)
if err != nil {
log.Error("create onion key:", err)
return err
}
onionAddrString := "/onion/" + onionAddr + ":4003"
if x.Tor {
cfg.Addresses.Swarm = []string{}
cfg.Addresses.Swarm = append(cfg.Addresses.Swarm, onionAddrString)
} else if x.DualStack {
cfg.Addresses.Swarm = []string{}
cfg.Addresses.Swarm = append(cfg.Addresses.Swarm, onionAddrString)
cfg.Addresses.Swarm = append(cfg.Addresses.Swarm, "/ip4/0.0.0.0/tcp/4001")
cfg.Addresses.Swarm = append(cfg.Addresses.Swarm, "/ip6/::/tcp/4001")
cfg.Addresses.Swarm = append(cfg.Addresses.Swarm, "/ip6/::/tcp/9005/ws")
cfg.Addresses.Swarm = append(cfg.Addresses.Swarm, "/ip4/0.0.0.0/tcp/9005/ws")
}
// Iterate over our address and process them as needed
var (
torDialer proxy.Dialer
usingTor, usingClearnet bool
controlPort int
torControl = torConfig.TorControl
torPw = torConfig.Password
)
for i, addr := range cfg.Addresses.Swarm {
m, err := ma.NewMultiaddr(addr)
if err != nil {
log.Error("creating swarm multihash:", err)
return err
}
p := m.Protocols()
// If we are using UTP and the stun option has been select, run stun and replace the port in the address
if x.STUN && p[0].Name == "ip4" && p[1].Name == "udp" && p[2].Name == "utp" {
usingClearnet = true
port, serr := obnet.Stun()
if serr != nil {
log.Error("stun setup:", serr)
return err
}
cfg.Addresses.Swarm = append(cfg.Addresses.Swarm[:i], cfg.Addresses.Swarm[i+1:]...)
cfg.Addresses.Swarm = append(cfg.Addresses.Swarm, "/ip4/0.0.0.0/udp/"+strconv.Itoa(port)+"/utp")
break
} else if p[0].Name == "onion" {
usingTor = true
} else {
usingClearnet = true
}
}
// Create Tor transport
if usingTor {
if torControl == "" {
controlPort, err = obnet.GetTorControlPort()
if err != nil {
log.Error("get tor control port:", err)
return err
}
torControl = "127.0.0.1:" + strconv.Itoa(controlPort)
}
if x.TorPassword != "" {
torPw = x.TorPassword
}
transportOptions := libp2p.ChainOptions(libp2p.Transport(oniontp.NewOnionTransportC("tcp4", torControl, torPw, nil, repoPath, (usingTor && usingClearnet))))
if usingClearnet {
transportOptions = libp2p.ChainOptions(
transportOptions,
libp2p.DefaultTransports,
)
}
libp2p.DefaultTransports = transportOptions
}
if usingTor && !usingClearnet {
log.Notice("Using Tor exclusively")
cfg.Swarm.DisableNatPortMap = true
}
ncfg := ipfs.PrepareIPFSConfig(r, ipnsExtraConfig.APIRouter, x.Testnet, x.Regtest)
nd, err := ipfscore.NewNode(cctx, ncfg)
if err != nil {
log.Error("create new ipfs node:", err)
return err
}
ctx := commands.Context{}
ctx.Online = true
ctx.ConfigRoot = repoPath
ctx.LoadConfig = func(_ string) (*config.Config, error) {
return fsrepo.ConfigAt(repoPath)
}
ctx.ConstructNode = func() (*ipfscore.IpfsNode, error) {
return nd, nil
}
torDialer = oniontp.TorDialer
log.Info("Peer ID: ", nd.Identity.Pretty())
printSwarmAddrs(nd)
// Extract the DHT from the tiered routing so it will be more accessible later
tiered, ok := nd.Routing.(routinghelpers.Tiered)
if !ok {
return errors.New("IPFS routing is not a type routinghelpers.Tiered")
}
var dhtRouting *dht.IpfsDHT
for _, router := range tiered.Routers {
if r, ok := router.(*ipfs.CachingRouter); ok {
r.APIRouter().Start(torDialer)
dhtRouting, err = r.DHT()
if err != nil {
return err
}
} else if x.Regtest {
if r, ok := router.(*dht.IpfsDHT); ok {
dhtRouting = r
}
}
}
if dhtRouting == nil {
return errors.New("IPFS DHT routing is not configured")
}
// Get current directory root hash
ipnskey := namesys.IpnsDsKey(nd.Identity)
ival, hasherr := nd.Repo.Datastore().Get(ipnskey)
if hasherr != nil {
log.Error("get ipns key:", hasherr)
}
ourIpnsRecord := new(ipnspb.IpnsEntry)
err = proto.Unmarshal(ival, ourIpnsRecord)
if err != nil {
log.Error("unmarshal record value", err)
nd.Repo.Datastore().Delete(ipnskey)
}
if x.ForceKeyCachePurge {
log.Infof("forcing key purge from namesys cache...")
nd.Repo.Datastore().Delete(ipnskey)
}
// Wallet
mn, err := sqliteDB.Config().GetMnemonic()
if err != nil {
log.Error("get config mnemonic:", err)
return err
}
var params chaincfg.Params
if x.Testnet {
params = chaincfg.TestNet3Params
} else if x.Regtest {
params = chaincfg.RegressionNetParams
} else {
params = chaincfg.MainNetParams
}
// Multiwallet setup
var walletLogWriter io.Writer
if x.NoLogFiles {
walletLogWriter = &DummyWriter{}
} else {
walletLogWriter = &lumberjack.Logger{
Filename: path.Join(repoPath, "logs", "wallet.log"),
MaxSize: 10, // Megabytes
MaxBackups: 3,
MaxAge: 30, // Days
}
}
walletLogFile := logging.NewLogBackend(walletLogWriter, "", 0)
walletFileFormatter := logging.NewBackendFormatter(walletLogFile, fileLogFormat)
walletLogger := logging.MultiLogger(walletFileFormatter)
multiwalletConfig := &wallet.WalletConfig{
ConfigFile: walletsConfig,
DB: sqliteDB.DB(),
Params: ¶ms,
RepoPath: repoPath,
Logger: walletLogger,
Proxy: torDialer,
WalletCreationDate: creationDate,
Mnemonic: mn,
DisableExchangeRates: x.DisableExchangeRates,
}
mw, err := wallet.NewMultiWallet(multiwalletConfig)
if err != nil {
return err
}
resyncManager := resync.NewResyncManager(sqliteDB.Sales(), sqliteDB.Purchases(), mw)
// Master key setup
seed := bip39.NewSeed(mn, "")
mPrivKey, err := hdkeychain.NewMaster(seed, ¶ms)
if err != nil {
log.Error(err)
return err
}
// Push nodes
var pushNodes []peer.ID
for _, pnd := range dataSharing.PushTo {
p, err := peer.IDB58Decode(pnd)
if err != nil {
log.Error("Invalid peerID in DataSharing config")
return err
}
pushNodes = append(pushNodes, p)
}
// Authenticated gateway
gatewayMaddr, err := ma.NewMultiaddr(cfg.Addresses.Gateway[0])
if err != nil {
log.Error(err)
return err
}
addr, err := gatewayMaddr.ValueForProtocol(ma.P_IP4)
if err != nil {
log.Error(err)
return err
}
// Override config file preference if this is Mainnet, open internet and API enabled
if addr != "127.0.0.1" && params.Name == chaincfg.MainNetParams.Name && apiConfig.Enabled {
apiConfig.Authenticated = true
}
apiConfig.AllowedIPs = append(apiConfig.AllowedIPs, x.AllowIP...)
// Create authentication cookie
var authCookie http.Cookie
authCookie.Name = "OpenBazaar_Auth_Cookie"
if x.AuthCookie != "" {
authCookie.Value = x.AuthCookie
apiConfig.Authenticated = true
} else {
cookiePrefix := authCookie.Name + "="
cookiePath := path.Join(repoPath, ".cookie")
cookie, err := ioutil.ReadFile(cookiePath)
if err != nil {
authBytes := make([]byte, 32)
_, err = rand.Read(authBytes)
if err != nil {
log.Error(err)
return err
}
authCookie.Value = base58.Encode(authBytes)
f, err := os.Create(cookiePath)
if err != nil {
log.Error(err)
return err
}
cookie := cookiePrefix + authCookie.Value
_, werr := f.Write([]byte(cookie))
if werr != nil {
log.Error(werr)
return werr
}
f.Close()
} else {
if string(cookie)[:len(cookiePrefix)] != cookiePrefix {
return errors.New("invalid authentication cookie. Delete it to generate a new one")
}
split := strings.SplitAfter(string(cookie), cookiePrefix)
authCookie.Value = split[1]
}
}
// Set up the ban manager
settings, err := sqliteDB.Settings().Get()
if err != nil && err != db.SettingsNotSetError {
log.Error(err)
return err
}
var blockedNodes []peer.ID
if settings.BlockedNodes != nil {
for _, pid := range *settings.BlockedNodes {
id, err := peer.IDB58Decode(pid)
if err != nil {
continue
}
blockedNodes = append(blockedNodes, id)
}
}
bm := obnet.NewBanManager(blockedNodes)
if x.Testnet {
setTestmodeRecordAgingIntervals()
}
// Build pubsub
publisher := ipfs.NewPubsubPublisher(context.Background(), nd.PeerHost, nd.Routing, nd.Repo.Datastore(), nd.PubSub)
subscriber := ipfs.NewPubsubSubscriber(context.Background(), nd.PeerHost, nd.Routing, nd.Repo.Datastore(), nd.PubSub)
ps := ipfs.Pubsub{Publisher: publisher, Subscriber: subscriber}
// OpenBazaar node setup
core.Node = &core.OpenBazaarNode{
AcceptStoreRequests: dataSharing.AcceptStoreRequests,
BanManager: bm,
Datastore: sqliteDB,
IpfsNode: nd,
DHT: dhtRouting,
MasterPrivateKey: mPrivKey,
Multiwallet: mw,
OfflineMessageFailoverTimeout: 30 * time.Second,
Pubsub: ps,
PushNodes: pushNodes,
RegressionTestEnable: x.Regtest,
RepoPath: repoPath,
RootHash: string(ourIpnsRecord.Value),
TestnetEnable: x.Testnet,
TorDialer: torDialer,
UserAgent: core.USERAGENT,
IPNSQuorumSize: uint(ipnsExtraConfig.DHTQuorumSize),
}
core.Node.PublishLock.Lock()
// Offline messaging storage
var storage sto.OfflineMessagingStorage
if x.Storage == "self-hosted" || x.Storage == "" {
storage = selfhosted.NewSelfHostedStorage(repoPath, core.Node.IpfsNode, pushNodes, core.Node.SendStore)
} else if x.Storage == "dropbox" {
if usingTor && !usingClearnet {
log.Error("dropbox can not be used with tor")
return errors.New("dropbox can not be used with tor")
}
if dropboxToken == "" {
err = errors.New("dropbox token not set in config file")
log.Error(err)
return err
}
storage, err = dropbox.NewDropBoxStorage(dropboxToken)
if err != nil {
log.Error(err)
return err
}
} else {
err = errors.New("invalid storage option")
log.Error(err)
return err
}
core.Node.MessageStorage = storage
if len(cfg.Addresses.Gateway) <= 0 {
return ErrNoGateways
}
if (apiConfig.SSL && apiConfig.SSLCert == "") || (apiConfig.SSL && apiConfig.SSLKey == "") {
return errors.New("SSL cert and key files must be set when SSL is enabled")
}
gateway, err := newHTTPGateway(core.Node, ctx, authCookie, *apiConfig, x.NoLogFiles)
if err != nil {
log.Error(err)
return err
}
if len(cfg.Addresses.API) > 0 && cfg.Addresses.API[0] != "" {
if _, err := serveHTTPApi(&ctx); err != nil {
log.Error(err)
return err
}
}
go func() {
if !x.DisableWallet {
// If the wallet doesn't allow resyncing from a specific height to scan for unpaid orders, wait for all messages to process before continuing.
if resyncManager == nil {
core.Node.WaitForMessageRetrieverCompletion()
}
TL := lis.NewTransactionListener(core.Node.Multiwallet, core.Node.Datastore, core.Node.Broadcast)
for ct, wal := range mw {
WL := lis.NewWalletListener(core.Node.Datastore, core.Node.Broadcast, ct)
wal.AddTransactionListener(WL.OnTransactionReceived)
wal.AddTransactionListener(TL.OnTransactionReceived)
}
log.Info("Starting multiwallet...")
su := wallet.NewStatusUpdater(mw, core.Node.Broadcast, nd.Context())
go su.Start()
go mw.Start()
if resyncManager != nil {
go resyncManager.Start()
go func() {
core.Node.WaitForMessageRetrieverCompletion()
resyncManager.CheckUnfunded()
}()
}
}
core.Node.Service = service.New(core.Node, sqliteDB)
core.Node.Service.WaitForReady()
log.Info("OpenBazaar Service Ready")
core.Node.StartMessageRetriever()
core.Node.StartPointerRepublisher()
core.Node.StartRecordAgingNotifier()
core.Node.PublishLock.Unlock()
err = core.Node.UpdateFollow()
if err != nil {
log.Error(err)
}
if !core.Node.InitalPublishComplete {
err = core.Node.SeedNode()
if err != nil {
log.Error(err)
}
}
core.Node.SetUpRepublisher(republishInterval)
}()
// Start gateway
err = gateway.Serve()
if err != nil {
log.Error(err)
}
return nil
}
func setTestmodeRecordAgingIntervals() {
repo.VendorDisputeTimeout_lastInterval = time.Duration(60) * time.Minute
repo.ModeratorDisputeExpiry_firstInterval = time.Duration(20) * time.Minute
repo.ModeratorDisputeExpiry_secondInterval = time.Duration(40) * time.Minute
repo.ModeratorDisputeExpiry_thirdInterval = time.Duration(59) * time.Minute
repo.ModeratorDisputeExpiry_lastInterval = time.Duration(60) * time.Minute
repo.BuyerDisputeTimeout_firstInterval = time.Duration(20) * time.Minute
repo.BuyerDisputeTimeout_secondInterval = time.Duration(40) * time.Minute
repo.BuyerDisputeTimeout_thirdInterval = time.Duration(59) * time.Minute
repo.BuyerDisputeTimeout_lastInterval = time.Duration(60) * time.Minute
repo.BuyerDisputeTimeout_totalDuration = time.Duration(60) * time.Minute
repo.BuyerDisputeExpiry_firstInterval = time.Duration(20) * time.Minute
repo.BuyerDisputeExpiry_secondInterval = time.Duration(40) * time.Minute
repo.BuyerDisputeExpiry_lastInterval = time.Duration(59) * time.Minute
repo.BuyerDisputeExpiry_totalDuration = time.Duration(60) * time.Minute
}
// Prints the addresses of the host
func printSwarmAddrs(node *ipfscore.IpfsNode) {
var addrs []string
for _, addr := range node.PeerHost.Addrs() {
addrs = append(addrs, addr.String())
}
sort.Strings(addrs)
for _, addr := range addrs {
log.Infof("Swarm listening on %s\n", addr)
}
}
type DummyWriter struct{}
func (d *DummyWriter) Write(p []byte) (n int, err error) {
return 0, nil
}
type DummyListener struct {
addr net.Addr
}
func (d *DummyListener) Addr() net.Addr {
return d.addr
}
func (d *DummyListener) Accept() (net.Conn, error) {
conn, _ := net.FileConn(nil)
return conn, nil
}
func (d *DummyListener) Close() error {
return nil
}
// Collects options, creates listener, prints status message and starts serving requests
func newHTTPGateway(node *core.OpenBazaarNode, ctx commands.Context, authCookie http.Cookie, config schema.APIConfig, noLogFiles bool) (*api.Gateway, error) {
// Get API configuration
cfg, err := ctx.GetConfig()
if err != nil {
return nil, err
}
// Create a network listener
gatewayMaddr, err := ma.NewMultiaddr(cfg.Addresses.Gateway[0])
if err != nil {
return nil, fmt.Errorf("newHTTPGateway: invalid gateway address: %q (err: %s)", cfg.Addresses.Gateway, err)
}
var gwLis manet.Listener
if config.SSL {
netAddr, err := manet.ToNetAddr(gatewayMaddr)
if err != nil {
return nil, err
}
gwLis, err = manet.WrapNetListener(&DummyListener{netAddr})
if err != nil {
return nil, err
}
} else {
gwLis, err = manet.Listen(gatewayMaddr)
if err != nil {
return nil, fmt.Errorf("newHTTPGateway: manet.Listen(%s) failed: %s", gatewayMaddr, err)
}
}
// We might have listened to /tcp/0 - let's see what we are listing on
gatewayMaddr = gwLis.Multiaddr()
log.Infof("Gateway/API server listening on %s\n", gatewayMaddr)
// Setup an options slice
var opts = []corehttp.ServeOption{
corehttp.MetricsCollectionOption("gateway"),
corehttp.CommandsROOption(ctx),
corehttp.VersionOption(),
corehttp.IPNSHostnameOption(),
corehttp.GatewayOption(cfg.Gateway.Writable, "/ipfs", "/ipns"),
}
if len(cfg.Gateway.RootRedirect) > 0 {
opts = append(opts, corehttp.RedirectOption("", cfg.Gateway.RootRedirect))
}
if err != nil {
return nil, fmt.Errorf("newHTTPGateway: ConstructNode() failed: %s", err)
}
// Create and return an API gateway
var w4 io.Writer
if noLogFiles {
w4 = &DummyWriter{}
} else {
w4 = &lumberjack.Logger{
Filename: path.Join(node.RepoPath, "logs", "api.log"),
MaxSize: 10, // Megabytes
MaxBackups: 3,
MaxAge: 30, // Days
}
}
apiFile := logging.NewLogBackend(w4, "", 0)
apiFileFormatter := logging.NewBackendFormatter(apiFile, fileLogFormat)
ml := logging.MultiLogger(apiFileFormatter)
return api.NewGateway(node, authCookie, manet.NetListener(gwLis), config, ml, opts...)
}
// serveHTTPApi collects options, creates listener, prints status message and starts serving requests
func serveHTTPApi(cctx *commands.Context) (<-chan error, error) {
cfg, err := cctx.GetConfig()
if err != nil {
return nil, fmt.Errorf("serveHTTPApi: GetConfig() failed: %s", err)
}
apiAddr := cfg.Addresses.API[0]
apiMaddr, err := ma.NewMultiaddr(apiAddr)
if err != nil {
return nil, fmt.Errorf("serveHTTPApi: invalid API address: %q (err: %s)", apiAddr, err)
}
apiLis, err := manet.Listen(apiMaddr)
if err != nil {
return nil, fmt.Errorf("serveHTTPApi: manet.Listen(%s) failed: %s", apiMaddr, err)
}
// we might have listened to /tcp/0 - lets see what we are listing on
apiMaddr = apiLis.Multiaddr()
fmt.Printf("API server listening on %s\n", apiMaddr)
// by default, we don't let you load arbitrary ipfs objects through the api,
// because this would open up the api to scripting vulnerabilities.
// only the webui objects are allowed.
// if you know what you're doing, go ahead and pass --unrestricted-api.
unrestricted := false
gatewayOpt := corehttp.GatewayOption(false, corehttp.WebUIPaths...)
if unrestricted {
gatewayOpt = corehttp.GatewayOption(true, "/ipfs", "/ipns")
}
var opts = []corehttp.ServeOption{
corehttp.MetricsCollectionOption("api"),
corehttp.CommandsOption(*cctx),
corehttp.WebUIOption,
gatewayOpt,
corehttp.VersionOption(),
corehttp.MetricsScrapingOption("/debug/metrics/prometheus"),
corehttp.LogOption(),
}
if len(cfg.Gateway.RootRedirect) > 0 {
opts = append(opts, corehttp.RedirectOption("", cfg.Gateway.RootRedirect))
}
node, err := cctx.ConstructNode()
if err != nil {
return nil, fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err)
}
if err := node.Repo.SetAPIAddr(apiMaddr); err != nil {
return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %s", err)
}
errc := make(chan error)
go func() {
errc <- corehttp.Serve(node, manet.NetListener(apiLis), opts...)
close(errc)
}()
return errc, nil
}
func InitializeRepo(dataDir, password, mnemonic string, testnet bool, creationDate time.Time, coinType wi.CoinType) (*db.SQLiteDatastore, error) {
// Database
sqliteDB, err := db.Create(dataDir, password, testnet, coinType)
if err != nil {
return sqliteDB, err
}
// Initialize the IPFS repo if it does not already exist
err = repo.DoInit(dataDir, 4096, testnet, password, mnemonic, creationDate, sqliteDB.Config().Init)
if err != nil {
// handle encrypted databases
if sqliteDB.Config().IsEncrypted() {
sqliteDB.Close()
fmt.Printf("Database at %s appears encrypted.\nEnter your password (will not appear while typing): ", dataDir)
// nolint:unconvert
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println("")
pw := string(bytePassword)
sqliteDB, err = db.Create(dataDir, pw, testnet, coinType)
if err != nil {
return sqliteDB, err
}
// try again with the password
err = repo.DoInit(dataDir, 4096, testnet, pw, mnemonic, creationDate, sqliteDB.Config().Init)
if err != nil && err != repo.ErrRepoExists {
log.Errorf("unable to open encrypted database: %s", err.Error())
return sqliteDB, fmt.Errorf("repo init encrypted: %s", err.Error())
}
}
return sqliteDB, err
}
return sqliteDB, nil
}
func printSplashScreen(verbose bool) {
blue := color.New(color.FgBlue)
white := color.New(color.FgWhite)
for i, l := range []string{
"________ ",
" __________",
`\_____ \ ______ ____ ____`,
`\______ \_____ _____________ _____ _______`,
` / | \\____ \_/ __ \ / \`,
`| | _/\__ \ \___ /\__ \ \__ \\_ __ \ `,
`/ | \ |_> > ___/| | \ `,
`| \ / __ \_/ / / __ \_/ __ \| | \/`,
`\_______ / __/ \___ >___| /`,
`______ /(____ /_____ \(____ (____ /__|`,
` \/|__| \/ \/ `,
` \/ \/ \/ \/ \/`,
} {
if i%2 == 0 {
if _, err := white.Printf(l); err != nil {
log.Debug(err)
return
}
continue
}
if _, err := blue.Println(l); err != nil {
log.Debug(err)
return
}
}
blue.DisableColor()
white.DisableColor()
fmt.Println("")
fmt.Println("OpenBazaar Server v" + core.VERSION)
log.Infof("\nOpenBazaar Server v%s", core.VERSION)
if !verbose {
fmt.Println("[Press Ctrl+C to exit]")
}
}