forked from koalabearguo/php-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
191 lines (183 loc) · 6.43 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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"runtime"
)
//CaCert/CA.crt should be trusted by local OS
//Php-Proxy CA
var CaCert = []byte(`-----BEGIN CERTIFICATE-----
MIIDvTCCAqWgAwIBAgIJAJ6lzaYHcaNcMA0GCSqGSIb3DQEBCwUAMHUxCzAJBgNV
BAYTAkNOMREwDwYDVQQIDAhJbnRlcm5ldDEPMA0GA1UEBwwGQ2VybmV0MRIwEAYD
VQQKDAlQaHAtUHJveHkxFzAVBgNVBAsMDlBocC1Qcm94eSBSb290MRUwEwYDVQQD
DAxQaHAtUHJveHkgQ0EwHhcNMjEwNzE2MDEyNDU2WhcNMzEwNzE0MDEyNDU2WjB1
MQswCQYDVQQGEwJDTjERMA8GA1UECAwISW50ZXJuZXQxDzANBgNVBAcMBkNlcm5l
dDESMBAGA1UECgwJUGhwLVByb3h5MRcwFQYDVQQLDA5QaHAtUHJveHkgUm9vdDEV
MBMGA1UEAwwMUGhwLVByb3h5IENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAyoA89JBn6tpag1JFNO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Rn8N5jwbnn9tAoGAcVeZ
7fxEKOhRxSXKGEaM0lBKnblXRZacmSdmXHAposkG+Sqcrv3iI6gCw0UAA7qr7ldo
oX/tk3KTPplHBCNLioC47ne3m/5oudGsSTzWHJEtQwnN9KpmBD3H78uGbBh6C5lP
02mm7+GrMVf+N3jYDaTe1inxtAS4XcTfcS1XvEcCgYEAiohDWwiDO/GQloKPuZWc
gVEJoAty/LbU/TyW+i3bM94rcJLKY8ySPBmTQb+ifeqvSRo4W2zv3tQ5lBag77y3
pRYFQzk5UG6yo4V0/oo2UUSDY0UoxX9lVNcJvYVwPcNwKHnX9a9fRrlf3o+c5rrE
jIxm1tgZheqRxqpv1LwQ4hQ=
-----END PRIVATE KEY-----`)
//
const version string = "2.1.5"
//
type config struct {
//php fetchserver path
Fetchserver string `json:"fetchserver"`
//password
Password string `json:"password"`
//when connect https php server,TLS sni extension
Sni string `json:"sni"`
//local listen address
Listen string `json:"listen"`
//enable http3
Http3 bool `json:"http3"`
//debug enable
Debug bool `json:"debug"`
//insecure connect to php server
Insecure bool `json:"insecure"`
//auto proxy
Autoproxy bool `json:"autoproxy"`
//user agent
User_agent string `json:"user-agent"`
//parent proxy
Proxy string `json:"proxy"`
//ca
Ca string `json:"ca"`
//key
Key string `json:"key"`
}
func (c *config) init_config() {
//
confpath := ""
//
log.Printf("Php-Proxy version:v%s\n", version)
//
log.Printf("Go version:%s\n", runtime.Version())
//
flag.CommandLine.SetOutput(os.Stdout)
//
flag.StringVar(&confpath, "c", "", "config file path")
flag.StringVar(&c.Listen, "l", "127.0.0.1:8081", "Local listen address(HTTP Proxy address)")
flag.StringVar(&c.Password, "p", "123456", "php server password")
flag.StringVar(&c.Sni, "sni", "", "HTTPS sni extension ServerName(default fetchserver hostname)")
flag.StringVar(&c.Fetchserver, "s", "https://a.bc.com/php-proxy/index.php", "php fetchserver path(http/https)")
flag.StringVar(&c.User_agent, "ua", "", "customize User-Agent to php server(default use brower User-Agent)")
flag.StringVar(&c.Proxy, "fp", "", "connect php server via Forward Proxy(https://user:[email protected],http://user:[email protected]:8080,socks5://user:[email protected]:1080)")
flag.StringVar(&c.Ca, "ca", "", "ca file path")
flag.StringVar(&c.Key, "key", "", "key file path")
flag.BoolVar(&c.Debug, "d", false, "enable debug mode for debug")
flag.BoolVar(&c.Http3, "h3", false, "enable http3 client mode")
flag.BoolVar(&c.Autoproxy, "a", false, "enable auto proxy")
flag.BoolVar(&c.Insecure, "k", false, "insecure connect to php server(ignore certs verify/middle attack)")
flag.Parse()
//
//c.writeconfig()
if len(os.Args) < 2 || confpath != "" {
c.loadconfig(confpath)
} else {
c.writeconfig()
}
//
if c.Autoproxy {
log.Printf("Autoproxy enabled")
} else {
log.Printf("Autoproxy not enabled")
}
if c.Http3 {
log.Printf("Http3 enabled")
} else {
log.Printf("Http3 not enabled")
}
//
log.Printf("php Fetch server:%s\n", c.Fetchserver)
//
if c.Proxy == "" {
log.Printf("Forward Proxy not enabled")
} else {
log.Printf("Forward Proxy enabled:" + c.Proxy)
}
}
func (c *config) loadconfig(confpath string) {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
fpath := ""
if confpath != "" {
fpath = confpath
} else {
fpath = dir + "/php-proxy.json"
}
raw, err1 := ioutil.ReadFile(fpath)
if err1 != nil {
//log.Print(err1)
return
}
log.Print("Load config from " + fpath)
err = json.Unmarshal(raw, c)
if err != nil {
log.Print(err)
}
}
func (c *config) writeconfig() {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
raw, err1 := json.MarshalIndent(c, "", "")
//raw, err1 := json.Marshal(c)
//log.Print(string(raw))
if err1 != nil {
log.Print(err1)
return
}
err = ioutil.WriteFile(dir+"/php-proxy.json", raw, 0644)
if err != nil {
log.Print(err)
return
}
log.Print("Write config to " + dir + "/php-proxy.json")
}