forked from hidu/pproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
257 lines (222 loc) · 6.34 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package serve
import (
"crypto/tls"
"fmt"
"github.com/Unknwon/goconfig"
"github.com/hidu/goutils"
"log"
"net/http"
"net/url"
"strings"
)
// Config pproxy's config
type Config struct {
Port int
AdminPort int
Title string
Notice string
AuthType int
DataDir string
FileDir string
ResponseSave int
SessionView int
DataStoreDay int
ParentProxy *url.URL
SslOn bool
SslCert tls.Certificate
}
const (
authTypeNO = 0
authTypeBasic = 1
authTypeBasicWithAny = 2
authTypeBasicTry = 3
responseSaveAll = 0
responseSaveHasBroad = 1 //has show
sessionViewALL = 0
sessionViewIPOrUser = 1
)
// User user struct
type User struct {
Name string
Psw string
PswMd5 string
IsAdmin bool
SkipCheckPsw bool
}
// String string format
func (u *User) String() string {
return fmt.Sprintf("Name:%s,Psw:%s,isAdmin:%v,SkipCheckPsw:%v", u.Name, u.Psw, u.IsAdmin, u.SkipCheckPsw)
}
// ConfigString one line in file
func (u *User) ConfigString() string {
return fmt.Sprintf("name:%s\tpsw:%s\tis_admin:%v\tpsw_md5:%s", u.Name, u.Psw, u.IsAdmin, u.PswMd5)
}
const (
contentEncoding = "Content-Encoding"
)
//"0:no auth | 1:basic auth | 2:basic auth with any name"
// GetVersion get current version
func GetVersion() string {
return Assest.GetContent("res/version")
}
// GetDemoConf get the demo config
func GetDemoConf() string {
return strings.TrimSpace(Assest.GetContent("res/conf/demo.conf"))
}
func (u *User) isPswEq(psw string) bool {
return u.PswMd5 == utils.StrMd5(psw)
}
// LoadConfig load the pproxy's config
func LoadConfig(confPath string) (*Config, error) {
gconf, err := goconfig.LoadConfigFile(confPath)
if err != nil {
log.Println("load config", confPath, "failed,err:", err)
return nil, err
}
config := new(Config)
config.Port = gconf.MustInt(goconfig.DEFAULT_SECTION, "port", 8080)
config.AdminPort = gconf.MustInt(goconfig.DEFAULT_SECTION, "adminPort", 0)
if config.AdminPort == 0 {
config.AdminPort = config.Port
}
config.DataStoreDay = gconf.MustInt(goconfig.DEFAULT_SECTION, "dataStoreDay", 0)
if config.DataStoreDay < 0 {
log.Println("wrong DataStoreDay,skip")
config.DataStoreDay = 0
}
config.Title = gconf.MustValue(goconfig.DEFAULT_SECTION, "title")
config.Notice = gconf.MustValue(goconfig.DEFAULT_SECTION, "notice")
config.DataDir = gconf.MustValue(goconfig.DEFAULT_SECTION, "dataDir", "../data/")
config.FileDir = gconf.MustValue(goconfig.DEFAULT_SECTION, "fileDir", "../file/")
_authType := strings.ToLower(gconf.MustValue(goconfig.DEFAULT_SECTION, "authType", "none"))
authTypes := map[string]int{"none": 0, "basic": 1, "basic_any": 2, "basic_try": 3, "try_basic": 3}
hasError := false
if authType, has := authTypes[_authType]; has {
config.AuthType = authType
} else {
hasError = true
log.Println("conf error,unknow value authType:", _authType)
}
_responseSave := strings.ToLower(gconf.MustValue(goconfig.DEFAULT_SECTION, "responseSave", "all"))
responseSaveMap := map[string]int{"all": 0, "only_broadcast": 1}
if responseSave, has := responseSaveMap[_responseSave]; has {
config.ResponseSave = responseSave
} else {
hasError = true
log.Println("conf error,unknow value responseSave:", _authType)
}
_sessionView := strings.ToLower(gconf.MustValue(goconfig.DEFAULT_SECTION, "sessionView", "all"))
sessionViewMap := map[string]int{"all": 0, "ip_or_user": 1}
if sessionView, has := sessionViewMap[_sessionView]; has {
config.SessionView = sessionView
} else {
hasError = true
log.Println("conf error,unknow value responseSave:", _authType)
}
parentProxy := gconf.MustValue(goconfig.DEFAULT_SECTION, "parentProxy", "")
if parentProxy != "" {
_urlObj, err := url.Parse(parentProxy)
if err != nil || _urlObj.Scheme != "http" {
hasError = true
log.Println("parentProxy wrong,must http proxy")
} else {
config.ParentProxy = _urlObj
}
}
config.SslOn = gconf.MustValue(goconfig.DEFAULT_SECTION, "ssl", "off") == "on"
if config.SslOn {
_sslClientCert := gconf.MustValue(goconfig.DEFAULT_SECTION, "ssl_client_cert", "")
_sslServerKey := gconf.MustValue(goconfig.DEFAULT_SECTION, "ssl_server_key", "")
cert, err := getSslCert(_sslClientCert, _sslServerKey)
if err != nil {
hasError = true
log.Println("ssl ca config error:", err)
} else {
config.SslCert = cert
}
}
if hasError {
return config, fmt.Errorf("config error")
}
return config, nil
}
type configHosts map[string]string
// loadHosts 读取host配置文件
func loadHosts(confPath string) (hosts configHosts, err error) {
hosts = make(configHosts)
if !utils.File_exists(confPath) {
return
}
hostsByte, err := utils.File_get_contents(confPath)
if err != nil {
log.Println("load hosts_file failed:", confPath, err)
return nil, err
}
hostsArr := utils.LoadText2Slice(string(hostsByte))
for _, v := range hostsArr {
if len(v) != 2 {
log.Println("hosts file line wrong,ignore,", v)
continue
}
hosts[v[0]] = v[1]
}
return
}
func loadUsers(confPath string) (users map[string]*User, err error) {
users = make(map[string]*User)
if !utils.File_exists(confPath) {
return
}
userInfoByte, err := utils.File_get_contents(confPath)
if err != nil {
log.Println("load user file failed:", confPath, err)
return
}
lines := utils.LoadText2SliceMap(string(userInfoByte))
for _, line := range lines {
name, has := line["name"]
if !has || name == "" {
continue
}
if _, has := users[name]; has {
log.Println("dup name in users:", name, line)
continue
}
user := new(User)
user.Name = name
if val, has := line["is_admin"]; has && (val == "admin" || val == "true") {
user.IsAdmin = true
}
if val, has := line["psw_md5"]; has {
user.PswMd5 = val
}
if user.PswMd5 == "" {
if val, has := line["psw"]; has {
user.Psw = val
user.PswMd5 = utils.StrMd5(val)
}
}
users[user.Name] = user
}
return
}
func (config *Config) getTransport() *http.Transport {
if config.ParentProxy == nil {
return nil
}
tr := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
if config.ParentProxy.User.Username() == "pass" {
user := getAuthorInfo(req)
urlTmp, err := url.Parse(config.ParentProxy.String())
if err != nil {
return nil, err
}
urlTmp.User = url.UserPassword(user.Name, user.Psw)
return urlTmp, nil
}
return config.ParentProxy, nil
},
}
return tr
}