-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
98 lines (90 loc) · 1.95 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
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"encoding/json"
"io/ioutil"
"log"
"reflect"
"github.com/ccsexyz/utils"
)
const (
defaultExpires = 30
defaultPassword = "123456"
)
type config struct {
Type string `json:"type"`
Localaddr string `json:"localaddr"`
Remoteaddr string `json:"remoteaddr"`
NoHTTP bool `json:"nohttp"`
Host string `json:"host"`
Expires int `json:"expires"`
DataShard int `json:"datashard"`
ParityShard int `json:"parityshard"`
Method string `json:"method"`
Password string `json:"password"`
Mtu int `json:"mtu"`
UDP bool `json:"udp"`
Dummy bool `json:"dummy"`
UseMul bool `json:"usemul"`
MulConn int `json:"mulconn"`
Ivlen int
}
func (c *config) valid() bool {
if len(c.Localaddr) == 0 {
return false
}
if len(c.Remoteaddr) == 0 && c.Type != "server" {
return false
}
if c.DataShard < 0 || c.ParityShard < 0 || c.Mtu < 0 || c.Expires < 0 {
return false
}
return true
}
func readConfig(path string) (configs []*config, err error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return
}
err = json.Unmarshal(bytes, &configs)
if err != nil {
var c config
err = json.Unmarshal(bytes, &c)
if err == nil {
configs = append(configs, &c)
}
}
for _, c := range configs {
checkConfig(c)
}
return
}
func checkConfig(c *config) {
c.Ivlen = utils.GetIvLen(c.Method)
if c.Expires == 0 {
c.Expires = defaultExpires
}
if len(c.Method) != 0 && len(c.Password) == 0 {
c.Password = defaultPassword
}
if len(c.Localaddr) == 0 {
log.Fatal("no localaddr")
}
if len(c.Remoteaddr) == 0 {
log.Fatal("no remoteaddr")
}
if c.Mtu <= 0 {
c.Mtu = 1420
}
}
func (c *config) print() {
val := reflect.ValueOf(c)
typ := reflect.Indirect(val).Type()
nfield := typ.NumField()
for i := 0; i < nfield; i++ {
jv := typ.Field(i).Tag.Get("json")
if len(jv) != 0 {
log.Println(jv+":", val.Elem().Field(i))
}
}
log.Println("")
}