-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_default_impl.go
More file actions
130 lines (105 loc) · 2.83 KB
/
Copy pathclient_default_impl.go
File metadata and controls
130 lines (105 loc) · 2.83 KB
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
//go:build !wasm
// client_default_impl.go is the entry point for standalone builds for non-wasm build targets
package main
import (
"log"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"strings"
"github.com/getlantern/broflake/clientcore"
"github.com/getlantern/broflake/common"
)
var (
clientType = "desktop" // Must be "desktop" or "widget"
proxyMode = "socks5" // Must be "socks5" or "http"
)
func main() {
pprof := os.Getenv("PPROF")
freddie := os.Getenv("FREDDIE")
egress := os.Getenv("EGRESS")
netstated := os.Getenv("NETSTATED")
tag := os.Getenv("TAG")
proxyPort := os.Getenv("PORT")
if proxyPort == "" {
proxyPort = "1080"
}
common.Debugf("Welcome to Broflake %v", common.Version)
common.Debugf("clientType: %v", clientType)
common.Debugf("freddie: %v", freddie)
common.Debugf("egress: %v", egress)
common.Debugf("netstated: %v", netstated)
common.Debugf("tag: %v", tag)
common.Debugf("pprof: %v", pprof)
common.Debugf("proxyPort: %v", proxyPort)
bfOpt := clientcore.NewDefaultBroflakeOptions()
bfOpt.ClientType = clientType
bfOpt.Netstated = netstated
if clientType == "widget" {
bfOpt.CTableSize = 5
bfOpt.PTableSize = 5
}
rtcOpt := clientcore.NewDefaultWebRTCOptions()
rtcOpt.Tag = tag
if freddie != "" {
rtcOpt.DiscoverySrv = freddie
}
egOpt := clientcore.NewDefaultEgressOptions()
if egress != "" {
egOpt.Addr = egress
}
// Load or generate persistent peer identity
if id, err := loadOrGenerateIdentity(); err != nil {
common.Debugf("Warning: failed to load/generate peer identity, using UUID: %v", err)
} else {
egOpt.SetIdentity(id)
common.Debugf("PeerID (ed25519 public key): %v", egOpt.PeerID)
}
bfconn, _, err := clientcore.NewBroflake(bfOpt, rtcOpt, egOpt)
if err != nil {
log.Fatal(err)
}
if pprof != "" {
go func() {
common.Debug(http.ListenAndServe("localhost:"+pprof, nil))
}()
}
if clientType == "desktop" {
runLocalProxy(proxyPort, bfconn)
}
select {}
}
func identityFilePath() string {
if p := os.Getenv("IDENTITY_FILE"); p != "" {
return p
}
home, err := os.UserHomeDir()
if err != nil {
return filepath.Join(".", ".unbounded", "identity.key")
}
return filepath.Join(home, ".unbounded", "identity.key")
}
func loadOrGenerateIdentity() (*clientcore.PeerIdentity, error) {
path := identityFilePath()
data, err := os.ReadFile(path)
if err == nil {
hexKey := strings.TrimSpace(string(data))
return clientcore.PeerIdentityFromPrivateKeyHex(hexKey)
}
if !os.IsNotExist(err) {
return nil, err
}
id, err := clientcore.NewPeerIdentity()
if err != nil {
return nil, err
}
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return nil, err
}
if err := os.WriteFile(path, []byte(id.PrivateKeyHex()+"\n"), 0600); err != nil {
return nil, err
}
common.Debugf("Generated new peer identity, saved to %v", path)
return id, nil
}