-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
179 lines (166 loc) · 6.74 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
178
179
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"gopkg.in/yaml.v3"
)
type HTTPConfig struct {
Listen []string `json:"listen" yaml:"listen"`
ServerName []string `json:"server_name" yaml:"server_name"`
Keyfile string `json:"keyfile" yaml:"keyfile"`
Certfile string `json:"certfile" yaml:"certfile"`
ServerConfig map[string]struct {
Keyfile string `json:"keyfile" yaml:"keyfile"`
Certfile string `json:"certfile" yaml:"certfile"`
DisableHttp2 bool `json:"disable_http2" yaml:"disable_http2"`
DisableHttp3 bool `json:"disable_http3" yaml:"disable_http3"`
DisableTls11 bool `json:"disable_tls11" yaml:"disable_tls11"`
PreferChacha20 bool `json:"perfer_chacha20" yaml:"perfer_chacha20"`
} `json:"server_config" yaml:"server_config"`
Sniproxy []struct {
ServerName string `json:"server_name" yaml:"server_name"`
ProxyPass string `json:"proxy_pass" yaml:"proxy_pass"`
DialTimeout int `json:"dial_timeout" yaml:"dial_timeout"`
} `json:"sniproxy" yaml:"sniproxy"`
Forward struct {
Policy string `json:"policy" yaml:"policy"`
AuthTable string `json:"auth_table" yaml:"auth_table"`
Dialer string `json:"dialer" yaml:"dialer"`
DenyDomainsTable string `json:"deny_domains_table" yaml:"deny_domains_table"`
SpeedLimit int64 `json:"speed_limit" yaml:"speed_limit"`
BindInterface string `json:"bind_interface" yaml:"bind_interface"`
PreferIpv6 bool `json:"prefer_ipv6" yaml:"prefer_ipv6"`
Websocket string `json:"websocket" yaml:"websocket"`
Log bool `json:"log" yaml:"log"`
LogInterval int64 `json:"log_interval" yaml:"log_interval"`
} `json:"forward" yaml:"forward"`
Web []struct {
Location string `json:"location" yaml:"location"`
Cgi struct {
Enabled bool `json:"enabled" yaml:"enabled"`
Root string `json:"root" yaml:"root"`
DefaultAPP string `json:"default_app" yaml:"default_app"`
} `json:"cgi" yaml:"cgi"`
Dav struct {
Enabled bool `json:"enabled" yaml:"enabled"`
Root string `json:"root" yaml:"root"`
AuthBasicUserFile string `json:"auth_basic_user_file" yaml:"auth_basic_user_file"`
} `json:"dav" yaml:"dav"`
Index struct {
Root string `json:"root" yaml:"root"`
Headers string `json:"headers" yaml:"headers"`
Body string `json:"body" yaml:"body"`
File string `json:"file" yaml:"file"`
} `json:"index" yaml:"index"`
Proxy struct {
Pass string `json:"pass" yaml:"pass"`
AuthBasicUserFile string `json:"auth_basic_user_file" yaml:"auth_basic_user_file"`
SetHeaders string `json:"set_headers" yaml:"set_headers"`
DumpFailure bool `json:"dump_failure" yaml:"dump_failure"`
} `json:"proxy" yaml:"proxy"`
} `json:"web" yaml:"web"`
}
type SocksConfig struct {
Listen []string `json:"listen" yaml:"listen"`
Forward struct {
Policy string `json:"policy" yaml:"policy"`
AuthTable string `json:"auth_table" yaml:"auth_table"`
Dialer string `json:"dialer" yaml:"dialer"`
DenyDomainsTable string `json:"deny_domains_table" yaml:"deny_domains_table"`
SpeedLimit int64 `json:"speed_limit" yaml:"speed_limit"`
BindInterface string `json:"bind_interface" yaml:"bind_interface"`
PreferIpv6 bool `json:"prefer_ipv6" yaml:"prefer_ipv6"`
Log bool `json:"log" yaml:"log"`
} `json:"forward" yaml:"forward"`
}
type StreamConfig struct {
Listen []string `json:"listen" yaml:"listen"`
Keyfile string `json:"keyfile" yaml:"keyfile"`
Certfile string `json:"certfile" yaml:"certfile"`
ProxyPass string `json:"proxy_pass" yaml:"proxy_pass"`
DialTimeout int `json:"dial_timeout" yaml:"dial_timeout"`
Dialer string `json:"dialer" yaml:"dialer"`
SpeedLimit int64 `json:"speed_limit" yaml:"speed_limit"`
Log bool `json:"log" yaml:"log"`
}
type TunnelConfig struct {
Server struct {
Listen string `json:"listen" yaml:"listen"`
Key string `json:"key" yaml:"key"`
} `json:"server" yaml:"server"`
Client struct {
RemoteAddr string `json:"remote_addr" yaml:"remote_addr"`
LocalAddr string `json:"local_addr" yaml:"local_addr"`
Key string `json:"key" yaml:"key"`
} `json:"client" yaml:"client"`
}
type Config struct {
Global struct {
LogLevel string `json:"log_level" yaml:"log_level"`
LogBackups int `json:"log_backups" yaml:"log_backups"`
LogMaxsize int64 `json:"log_maxsize" yaml:"log_maxsize"`
LogLocaltime bool `json:"log_localtime" yaml:"log_localtime"`
ForbidLocalAddr bool `json:"forbid_local_addr" yaml:"forbid_local_addr"`
DialTimeout int `json:"dial_timeout" yaml:"dial_timeout"`
DialReadBuffer int `json:"dial_read_buffer" yaml:"dial_read_buffer"`
DialWriteBuffer int `json:"dial_write_buffer" yaml:"dial_write_buffer"`
DnsServer string `json:"dns_server" yaml:"dns_server"`
DnsCacheDuration string `json:"dns_cache_duration" yaml:"dns_cache_duration"`
IdleConnTimeout int `json:"idle_conn_timeout" yaml:"idle_conn_timeout"`
MaxIdleConns int `json:"max_idle_conns" yaml:"max_idle_conns"`
TcpBrutalRate uint64 `json:"tcp_brutal_rate" yaml:"tcp_brutal_rate"`
} `json:"global" yaml:"global"`
Cron []struct {
Spec string `json:"spec" yaml:"spec"`
Command string `json:"command" yaml:"command"`
} `json:"cron" yaml:"cron"`
Dialer map[string]string `json:"dialer" yaml:"dialer"`
Https []HTTPConfig `json:"https" yaml:"https"`
Http []HTTPConfig `json:"http" yaml:"http"`
Socks []SocksConfig `json:"socks" yaml:"socks"`
Stream []StreamConfig `json:"stream" yaml:"stream"`
Tunnel []TunnelConfig `json:"tunnel" yaml:"tunnel"`
}
func NewConfig(filename string) (*Config, error) {
if filename == "" {
var env = "development"
// perfer GOLANG_ENV
for _, name := range []string{"GOLANG_ENV", "ENV"} {
if s := os.Getenv(name); s != "" {
env = s
break
}
}
// perfer .json
for _, ext := range []string{".json", ".yaml"} {
filename = env + ext
if _, err := os.Stat(filename); err == nil {
break
}
}
}
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
data = regexp.MustCompilePOSIX(`^( *)upstream:`).ReplaceAll(data, []byte("${1}dialer:"))
c := new(Config)
switch filepath.Ext(filename) {
case ".json":
err = json.Unmarshal(data, c)
case ".yaml":
err = yaml.Unmarshal(data, c)
default:
err = fmt.Errorf("format of %s not supportted", filename)
}
if err != nil {
return nil, fmt.Errorf("yaml.Decode(%#v) error: %w", filename, err)
}
if filename == "development.yaml" {
fmt.Fprintf(os.Stderr, "%s WAN 1 config.go:122 > liner is running in the development mode.\n", timeNow().Format("15:04:05"))
}
return c, nil
}