/
options.go
228 lines (188 loc) · 5.25 KB
/
options.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
package mobile
import (
"fmt"
"log"
"reflect"
"strings"
"github.com/FavorLabs/favorX/pkg/node"
"github.com/FavorLabs/favorX/pkg/resolver/multiresolver"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/util/gconv"
)
// Options represents the collection of configuration values to fine tune the
// node embedded into a mobile process. The available values are a subset of the
// entire API provided by node to reduce the maintenance surface and dev
// complexity.
type Options struct {
// api setting
EnableTLS bool
ApiPort int
DebugAPIPort int
EnableDebugAPI bool
// vpn setting
VpnEnable bool
VpnGroupName string
VpnPort int
// proxy setting
ProxyEnable bool
ProxyGroupName string
ProxyPort int
// group setting json
Group string
// rpc setting
WebsocketPort int
// p2p setup
NetworkID int64 // default type uint64
P2PPort int
WelcomeMessage string
// kademlia
BinMaxPeers int
LightMaxPeers int
// cache size
CacheCapacity int64 // default type uint64
// node bootstrap
BootNodes string // default type []string
EnableDevNode bool
EnableFullNode bool
// chain setting
ChainEndpoint string
OracleContract string
// traffic stat
EnableFlowStat bool
FlowContract string
// domain resolver
ResolverOptions string // default type []string
// security
Password string
DataPath string
// leveldb opts
BlockCacheCapacity int64 // default type uint64
OpenFilesLimit int64 // default type uint64
WriteBufferSize int64 // default type uint64
DisableSeeksCompaction bool
// misc
Verbosity string
}
// defaultOptions contains the default node configuration values to use if all
// or some fields are missing from the user's specified list.
var defaultOptions = &Options{
EnableTLS: true,
ApiPort: 1633,
DebugAPIPort: 1635,
WebsocketPort: 1637,
P2PPort: 1634,
VpnPort: 1638,
CacheCapacity: 4000,
EnableFullNode: false,
BinMaxPeers: 20,
LightMaxPeers: 100,
BlockCacheCapacity: 8 * 1024 * 1024,
OpenFilesLimit: 1000,
WriteBufferSize: 4 * 1024 * 1024,
Verbosity: "info",
}
const listenAddress = "localhost"
func (o Options) DataDir(c *node.Options) {
c.DataDir = o.DataPath
}
func (o Options) APIAddr(c *node.Options) {
c.APIAddr = fmt.Sprintf("%s:%d", listenAddress, o.ApiPort)
}
func (o Options) VpnGroup(c *node.Options) {
if !o.VpnEnable {
return
}
c.VpnGroup = o.VpnGroupName
}
func (o Options) VpnListen(c *node.Options) {
if !o.VpnEnable {
return
}
c.VpnListen = fmt.Sprintf("%s:%d", listenAddress, o.VpnPort)
}
func (o Options) ProxyGroup(c *node.Options) {
if !o.ProxyEnable {
return
}
c.ProxyGroup = o.ProxyGroupName
}
func (o Options) ProxyAddr(c *node.Options) {
if !o.ProxyEnable {
return
}
c.ProxyAddr = fmt.Sprintf("%s:%d", listenAddress, o.ProxyPort)
}
func (o Options) Groups(c *node.Options) {
json, err := gjson.LoadContent(o.Group)
if err != nil {
log.Println(err)
}
// var tmp []*model.ConfigNodeGroup
err = gconv.Structs(json.Interfaces(), &c.Groups)
if err != nil {
log.Println(err)
}
}
func (o Options) EnableApiTLS(c *node.Options) {
c.EnableApiTLS = o.EnableTLS
}
func (o Options) DebugAPIAddr(c *node.Options) {
if o.EnableDebugAPI {
c.DebugAPIAddr = fmt.Sprintf("%s:%d", listenAddress, o.DebugAPIPort)
}
}
func (o Options) WSAddr(c *node.Options) {
c.WSAddr = fmt.Sprintf("%s:%d", listenAddress, o.WebsocketPort)
c.CORSAllowedOrigins = []string{"*"}
}
func (o Options) Bootnodes(c *node.Options) {
bootNodes := strings.Split(o.BootNodes, ",")
c.Bootnodes = append(c.Bootnodes, bootNodes...)
}
func (o Options) ResolverConnectionCfgs(c *node.Options) {
resolverOptions := strings.Split(o.ResolverOptions, ",")
resolverCfgs, err := multiresolver.ParseConnectionStrings(resolverOptions)
if err == nil {
c.ResolverConnectionCfgs = resolverCfgs
}
}
func (o Options) IsDev(c *node.Options) {
c.IsDev = o.EnableDevNode
}
func (o Options) KadBinMaxPeers(c *node.Options) {
c.KadBinMaxPeers = o.BinMaxPeers
}
func (o Options) LightNodeMaxPeers(c *node.Options) {
c.LightNodeMaxPeers = o.LightMaxPeers
}
func (o Options) OracleContractAddress(c *node.Options) {
c.OracleContractAddress = o.OracleContract
}
func (o Options) TrafficEnable(c *node.Options) {
c.TrafficEnable = o.EnableFlowStat
}
func (o Options) TrafficContractAddr(c *node.Options) {
c.TrafficContractAddr = o.FlowContract
}
// Export exports Options to node.Options, skipping all other extra fields
func (o *Options) export() (c node.Options) {
localVal := reflect.ValueOf(o).Elem()
remotePtr := reflect.ValueOf(&c)
remoteVal := reflect.ValueOf(&c).Elem()
remoteType := reflect.TypeOf(&c).Elem()
for i := 0; i < remoteVal.NumField(); i++ {
remoteFieldVal := remoteVal.Field(i)
localFieldVal := localVal.FieldByName(remoteType.Field(i).Name)
if reflect.ValueOf(localFieldVal).IsZero() {
localMethod := localVal.MethodByName(remoteType.Field(i).Name)
if localMethod.IsValid() {
localMethod.Call([]reflect.Value{remotePtr})
}
} else if localFieldVal.IsValid() {
if remoteFieldVal.IsValid() && remoteFieldVal.Type() == localFieldVal.Type() {
remoteFieldVal.Set(localFieldVal)
}
}
}
return remoteVal.Interface().(node.Options)
}