forked from cta08403/goagent-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
237 lines (204 loc) · 5.81 KB
/
main.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
package main
import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/golang/glog"
"github.com/golang/groupcache"
"github.com/phuslu/http2"
"./httpproxy"
"./httpproxy/filters"
"./storage"
_ "./httpproxy/filters/auth"
_ "./httpproxy/filters/autoproxy"
_ "./httpproxy/filters/direct"
_ "./httpproxy/filters/gae"
_ "./httpproxy/filters/iplist"
_ "./httpproxy/filters/php"
_ "./httpproxy/filters/ratelimit"
_ "./httpproxy/filters/stripssl"
_ "./httpproxy/filters/vps"
)
var version = "r9999"
func init() {
rand.Seed(time.Now().UnixNano())
}
type Config struct {
LogToStderr bool
Addr string
Http struct {
Ssl bool
KeepAlivePeriod int
ReadTimeout int
WriteTimeout int
Certificate string
PrivateKey string
}
GroupCache struct {
Addr string
Peers []string
}
Filters struct {
Request []string
RoundTrip []string
Response []string
}
}
func main() {
filename := "main.json"
configUri := filters.LookupConfigStoreURI(filename)
config := new(Config)
err := storage.ReadJsonConfig(configUri, filename, config)
if err != nil {
fmt.Printf("storage.ReadJsonConfig(%#v) failed: %s", filename, err)
return
}
pidfile := ""
flag.StringVar(&pidfile, "pidfile", "", "goproxy pidfile")
flag.StringVar(&config.Addr, "addr", config.Addr, "goproxy listen address")
flag.StringVar(&config.GroupCache.Addr, "groupcache-addr", config.GroupCache.Addr, "groupcache listen address")
if config.LogToStderr || runtime.GOOS == "windows" {
logToStderr := true
for i := 1; i < len(os.Args); i++ {
if strings.HasPrefix(os.Args[i], "-logtostderr=") {
logToStderr = false
break
}
}
if logToStderr {
flag.Set("logtostderr", "true")
}
}
flag.Parse()
if runtime.GOOS != "windows" && pidfile != "" {
if err = ioutil.WriteFile(pidfile, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
glog.Fatalf("Write pidfile(%s) error: %s", pidfile, err)
}
}
var ln0 net.Listener
if config.GroupCache.Addr != "" {
peers := groupcache.NewHTTPPool("http://" + config.GroupCache.Addr)
peers.Set(config.GroupCache.Peers...)
ln0, err = net.Listen("tcp", config.GroupCache.Addr)
if err != nil {
glog.Fatalf("ListenTCP(%s) error: %s", config.GroupCache.Addr, err)
}
go http.Serve(ln0, peers)
}
fmt.Fprintf(os.Stderr, `------------------------------------------------------
GoProxy Version : %s (go/%s %s/%s)
Listen Address : %s
RoundTrip Filters : %v
Pac Server : http://%s/proxy.pac
------------------------------------------------------
`, version, runtime.Version(), runtime.GOOS, runtime.GOARCH,
config.Addr,
fmt.Sprintf("%s|%s|%s", strings.Join(config.Filters.Request, ","), strings.Join(config.Filters.RoundTrip, ","), strings.Join(config.Filters.Response, ",")),
config.Addr)
requestFilters, roundtripFilters, responseFilters := getFilters(config)
var tlsConfig *tls.Config
if config.Http.Ssl {
readPem := func(object string) []byte {
store, err := storage.OpenURI(configUri)
if err != nil {
glog.Fatalf("store.OpenURI(%v) error: %s", configUri, err)
}
o, err := store.GetObject(object, -1, -1)
if err != nil {
glog.Fatalf("store.GetObject(%v) error: %s", object, err)
}
rc := o.Body()
defer rc.Close()
b, err := ioutil.ReadAll(rc)
if err != nil {
glog.Fatalf("ioutil.ReadAll error: %s", err)
}
return b
}
certPem := readPem(config.Http.Certificate)
keyPem := readPem(config.Http.Certificate)
tlsCert, err := tls.X509KeyPair(certPem, keyPem)
if err != nil {
glog.Fatalf("tls.X509KeyPair error: %s", err)
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{tlsCert},
}
}
listenOpts := &httpproxy.ListenOptions{TLSConfig: tlsConfig}
ln, err := httpproxy.ListenTCP("tcp", config.Addr, listenOpts)
if err != nil {
glog.Fatalf("ListenTCP(%s, %#v) error: %s", config.Addr, listenOpts, err)
}
h := httpproxy.Handler{
Listener: ln,
RequestFilters: requestFilters,
RoundTripFilters: roundtripFilters,
ResponseFilters: responseFilters,
}
s := &http.Server{
Handler: h,
ReadTimeout: time.Duration(config.Http.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(config.Http.WriteTimeout) * time.Second,
MaxHeaderBytes: 1 << 20,
}
if config.Http.Ssl {
s.TLSConfig = tlsConfig
http2.ConfigureServer(s, &http2.Server{})
}
glog.Infof("ListenAndServe on %s\n", h.Listener.Addr().String())
s.Serve(h.Listener)
}
func getFilters(config *Config) ([]filters.RequestFilter, []filters.RoundTripFilter, []filters.ResponseFilter) {
fs := make(map[string]filters.Filter)
for _, names := range [][]string{config.Filters.Request,
config.Filters.RoundTrip,
config.Filters.Response} {
for _, name := range names {
if _, ok := fs[name]; !ok {
f, err := filters.GetFilter(name)
if err != nil {
glog.Fatalf("filters.GetFilter(%#v) failed: %#v", name, err)
}
fs[name] = f
}
}
}
requestFilters := make([]filters.RequestFilter, 0)
for _, name := range config.Filters.Request {
f := fs[name]
f1, ok := f.(filters.RequestFilter)
if !ok {
glog.Fatalf("%#v is not a RequestFilter", f)
}
requestFilters = append(requestFilters, f1)
}
roundtripFilters := make([]filters.RoundTripFilter, 0)
for _, name := range config.Filters.RoundTrip {
f := fs[name]
f1, ok := f.(filters.RoundTripFilter)
if !ok {
glog.Fatalf("%#v is not a RoundTripFilter", f)
}
roundtripFilters = append(roundtripFilters, f1)
}
responseFilters := make([]filters.ResponseFilter, 0)
for _, name := range config.Filters.Response {
f := fs[name]
f1, ok := f.(filters.ResponseFilter)
if !ok {
glog.Fatalf("%#v is not a ResponseFilter", f)
}
responseFilters = append(responseFilters, f1)
}
return requestFilters, roundtripFilters, responseFilters
}