forked from openp2p-cn/openp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
86 lines (78 loc) · 1.63 KB
/
config.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
package main
import (
"encoding/json"
"io/ioutil"
"time"
)
var gConf Config
type AppConfig struct {
// required
Protocol string
SrcPort int
PeerNode string
DstPort int
DstHost string
PeerUser string
PeerPassword string
// runtime info
peerToken uint64
peerNatType int
peerIP string
peerConeNatPort int
retryNum int
retryTime time.Time
shareBandwidth int
}
// TODO: add loglevel, maxlogfilesize
type Config struct {
Network NetworkConfig `json:"network"`
Apps []AppConfig `json:"apps"`
daemonMode bool
}
func (c *Config) add(app AppConfig) {
if app.SrcPort == 0 || app.DstPort == 0 {
return
}
for i := 0; i < len(c.Apps); i++ {
if c.Apps[i].Protocol == app.Protocol && c.Apps[i].SrcPort == app.SrcPort {
return
}
}
c.Apps = append(c.Apps, app)
}
func (c *Config) save() {
data, _ := json.MarshalIndent(c, "", "")
ioutil.WriteFile("config.json", data, 0644)
}
func (c *Config) load() error {
data, err := ioutil.ReadFile("config.json")
if err != nil {
gLog.Println(LevelERROR, "read config.json error:", err)
return err
}
err = json.Unmarshal(data, &c)
if err != nil {
gLog.Println(LevelERROR, "parse config.json error:", err)
}
return err
}
type NetworkConfig struct {
// local info
Node string
User string
Password string
NoShare bool
localIP string
ipv6 string
hostName string
mac string
os string
publicIP string
natType int
shareBandwidth int
// server info
ServerHost string
ServerPort int
UDPPort1 int
UDPPort2 int
}