This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathhttp_proxy.go
72 lines (61 loc) · 1.85 KB
/
http_proxy.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
package main
import (
"flag"
"net"
"os"
"time"
"github.com/getlantern/golog"
"github.com/getlantern/http-proxy/listeners"
"github.com/getlantern/http-proxy/logging"
"github.com/getlantern/http-proxy/proxyfilters"
"github.com/getlantern/http-proxy/server"
)
var (
log = golog.LoggerFor("http-proxy")
help = flag.Bool("help", false, "Get usage help")
keyfile = flag.String("key", "", "Private key file name")
certfile = flag.String("cert", "", "Certificate file name")
https = flag.Bool("https", false, "Use TLS for client to proxy communication")
addr = flag.String("addr", ":8080", "Address to listen")
maxConns = flag.Uint64("maxconns", 0, "Max number of simultaneous connections allowed connections")
idleClose = flag.Uint64("idleclose", 30, "Time in seconds that an idle connection will be allowed before closing it")
)
func main() {
var err error
_ = flag.CommandLine.Parse(os.Args[1:])
if *help {
flag.Usage()
return
}
// Logging
// TODO: use real parameters
err = logging.Init("instanceid", "version", "releasedate")
if err != nil {
log.Error(err)
}
// Create server
srv := server.New(&server.Opts{
IdleTimeout: time.Duration(*idleClose),
Filter: proxyfilters.BlockLocal([]string{}),
})
// Add net.Listener wrappers for inbound connections
srv.AddListenerWrappers(
// Limit max number of simultaneous connections
func(ls net.Listener) net.Listener {
return listeners.NewLimitedListener(ls, *maxConns)
},
// Close connections after 30 seconds of no activity
func(ls net.Listener) net.Listener {
return listeners.NewIdleConnListener(ls, time.Duration(*idleClose)*time.Second)
},
)
// Serve HTTP/S
if *https {
err = srv.ListenAndServeHTTPS(*addr, *keyfile, *certfile, nil)
} else {
err = srv.ListenAndServeHTTP(*addr, nil)
}
if err != nil {
log.Errorf("Error serving: %v", err)
}
}