-
Notifications
You must be signed in to change notification settings - Fork 27
/
config.go
138 lines (118 loc) · 2.76 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
package socks5
import (
"encoding/json"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
)
const DefaultListenPort = 1080
const DefaultTcpTimeout = 60
const DefaultUdpTimeout = 60
const DefaultLogLevel = "error"
var DefaultServerConfig = ServerCfg{
ListenPort: DefaultListenPort,
UserName: "",
Password: "",
UDPTimout: DefaultTcpTimeout,
TCPTimeout: DefaultUdpTimeout,
UDPAdvertisedIP: "",
LogLevel: DefaultLogLevel,
}
type ServerCfg struct {
ListenPort int //tcp,udp监听端口,仅当TCPListen或UDPListen无值时有效,监听地址为 0.0.0.0:ListenPort
TCPListen string //tcp监听地址
UDPListen string //udp监听地址
UDPAdvertisedIP string //udp的广告IP地址,告诉客户端将UDP数据发往这个ip,默认值为udp监听的本地ip地址
UserName string
Password string
UDPTimout int
TCPTimeout int
LogLevel string
}
func ReadOrCreateServerCfg(path string) (*ServerCfg, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err := CreateServerCfg(path)
if err != nil {
return nil, err
}
logrus.WithField("path", path).Info("create config file")
} else {
return nil, err
}
}
return ReadServerCfg(path)
}
func ReadServerCfg(path string) (*ServerCfg, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
conf := ServerCfg{}
err = json.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return &conf, nil
}
func CheckServerCfgDefault(cfg *ServerCfg) {
if cfg.ListenPort <= 0 {
cfg.ListenPort = DefaultListenPort
}
if cfg.TCPTimeout <= 0 {
cfg.TCPTimeout = DefaultTcpTimeout
}
if cfg.UDPTimout <= 0 {
cfg.UDPTimout = DefaultUdpTimeout
}
if len(cfg.LogLevel) == 0 {
cfg.LogLevel = DefaultLogLevel
}
}
func CreateServerCfg(path string) error {
c, _ := json.MarshalIndent(DefaultServerConfig, "", " ")
return ioutil.WriteFile(path, c, 0644)
}
type ClientCfg struct {
ServerAddr string
UserName string
Password string
UDPTimout int
TCPTimeout int
}
func ReadClientCfg(path string) (*ServerCfg, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err := CreateClientCfg(path)
if err != nil {
return nil, err
}
logrus.WithField("path", path).Info("create config file")
} else {
return nil, err
}
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
conf := ServerCfg{}
err = json.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return &conf, nil
}
func CreateClientCfg(path string) error {
cfg := ClientCfg{
ServerAddr: "127.0.0.1:1080",
UserName: "",
Password: "",
UDPTimout: 60,
TCPTimeout: 60,
}
c, _ := json.MarshalIndent(cfg, "", " ")
return ioutil.WriteFile(path, c, 0644)
}