-
Notifications
You must be signed in to change notification settings - Fork 127
/
config.go
177 lines (149 loc) · 4.07 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
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
//
// date : 2023-12-12
// author: xjdrew
//
package kone
import (
"os"
"strings"
"unicode"
"github.com/xjdrew/dnsconfig"
"gopkg.in/ini.v1"
)
func init() {
ini.PrettyFormat = true
}
const (
HTTP_PROXY = "http_proxy"
HTTPS_PROXY = "https_proxy"
SOCKS_PROXY = "socks_proxy"
)
type GeneralConfig struct {
ManagerAddr string `ini:"manager-addr"`
LogLevel string `ini:"log-level"`
}
type CoreConfig struct {
Tun string `ini:"tun"` // tun name
Network string `ini:"network"` // tun network
TcpListenPort uint16 `ini:"tcp-listen-port"`
TcpNatPortStart uint16 `ini:"tcp-nat-port-start"`
TcpNatPortEnd uint16 `ini:"tcp-nat-port-end"`
UdpListenPort uint16 `ini:"udp-listen-port"`
UdpNatPortStart uint16 `ini:"udp-nat-port-start"`
UdpNatPortEnd uint16 `ini:"udp-nat-port-end"`
DnsListenPort uint16 `ini:"dns-listen-port"`
DnsTtl uint `ini:"dns-ttl"`
DnsPacketSize uint16 `ini:"dns-packet-size"`
DnsReadTimeout uint `ini:"dns-read-timeout"`
DnsWriteTimeout uint `ini:"dns-write-timeout"`
DnsServer []string `ini:"dns-server" delim:","`
}
type RuleConfig struct {
Schema string
Pattern string
Proxy string
}
type KoneConfig struct {
source interface{} // config source: file name or raw ini data
inif *ini.File // parsed ini file
General GeneralConfig
Core CoreConfig
Proxy map[string]string
Rule []RuleConfig
}
func (cfg *KoneConfig) parseRule(sec *ini.Section) (err error) {
keys := sec.KeyStrings()
var ops []string
for _, key := range keys {
ops = strings.FieldsFunc(key, func(c rune) bool {
if c == ',' || unicode.IsSpace(c) {
return true
}
return false
})
logger.Debugf("%s %v", key, ops)
if len(ops) == 3 { // ignore invalid format
cfg.Rule = append(cfg.Rule, RuleConfig{
Schema: ops[0],
Pattern: ops[1],
Proxy: ops[2],
})
}
}
if len(ops) == 2 { //final rule
cfg.Rule = append(cfg.Rule, RuleConfig{
Schema: ops[0],
Proxy: ops[1],
})
}
return nil
}
func (cfg *KoneConfig) check() (err error) {
return nil
}
func (cfg *KoneConfig) GetSystemDnsservers() (servers []string) {
config := dnsconfig.ReadDnsConfig()
if config.Err != nil {
logger.Warningf("read dns config failed: %v", config.Err)
return []string{"114.114.114.114", "8.8.8.8"} // default
}
return config.Servers
}
func ParseConfig(source interface{}) (*KoneConfig, error) {
cfg := new(KoneConfig)
cfg.source = source
// set default value
cfg.Core.Network = "10.192.0.1/16"
cfg.Core.TcpListenPort = 82
cfg.Core.TcpNatPortStart = 10000
cfg.Core.TcpNatPortEnd = 60000
cfg.Core.UdpListenPort = 82
cfg.Core.UdpNatPortStart = 10000
cfg.Core.UdpNatPortEnd = 60000
cfg.Core.DnsListenPort = DnsDefaultPort
cfg.Core.DnsTtl = DnsDefaultTtl
cfg.Core.DnsPacketSize = DnsDefaultPacketSize
cfg.Core.DnsReadTimeout = DnsDefaultReadTimeout
cfg.Core.DnsWriteTimeout = DnsDefaultWriteTimeout
// decode config value
f, err := ini.LoadSources(ini.LoadOptions{AllowBooleanKeys: true, KeyValueDelimiters: "="}, source)
if err != nil {
logger.Errorf("%v", err)
return nil, err
}
cfg.inif = f
err = f.MapTo(cfg)
if err != nil {
return nil, err
}
// init proxy
if proxySection, err := f.GetSection("Proxy"); err == nil {
cfg.Proxy = proxySection.KeysHash()
}
// read proxy from env
if os.Getenv(HTTP_PROXY) != "" {
cfg.Proxy[HTTP_PROXY] = os.Getenv(HTTP_PROXY)
logger.Debugf("[env]set proxy %s=%s", HTTP_PROXY, cfg.Proxy[HTTP_PROXY])
}
if os.Getenv(HTTPS_PROXY) != "" {
cfg.Proxy[HTTPS_PROXY] = os.Getenv(HTTPS_PROXY)
logger.Debugf("[env]set proxy %s=%s", HTTPS_PROXY, cfg.Proxy[HTTPS_PROXY])
}
if os.Getenv(SOCKS_PROXY) != "" {
cfg.Proxy[SOCKS_PROXY] = os.Getenv(SOCKS_PROXY)
logger.Debugf("[env]set proxy %s=%s", SOCKS_PROXY, cfg.Proxy[SOCKS_PROXY])
}
// set backend dns default value
if len(cfg.Core.DnsServer) == 0 {
cfg.Core.DnsServer = cfg.GetSystemDnsservers()
}
// init rule
if err := cfg.parseRule(f.Section("Rule")); err != nil {
return nil, err
}
err = cfg.check()
if err != nil {
return nil, err
}
return cfg, nil
}