-
Notifications
You must be signed in to change notification settings - Fork 10
/
gontlm-proxy.go
141 lines (123 loc) · 3.69 KB
/
gontlm-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
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
package ntlm_proxy
import (
"context"
"flag"
"net"
"net/http"
"net/url"
"os"
// "regexp"
"time"
log "github.com/sirupsen/logrus"
"github.com/aus/proxyplease"
"github.com/elazarl/goproxy"
// "github.com/bhendo/concord"
// "github.com/bhendo/concord/handshakers"
)
var ProxyBind string
var ProxyServer string
var ProxyVerbose bool
func init() {
flag.StringVar(&ProxyBind, "bind", getEnv("GONTLM_BIND", "http://0.0.0.0:3128"), "IP & Port to bind to")
flag.StringVar(&ProxyServer, "proxy", getEnv("GONTLM_PROXY", ""), "Forwarding proxy server")
flag.BoolVar(&ProxyVerbose, "verbose", false, "Enable verbose logging")
}
func Run() {
proxy := goproxy.NewProxyHttpServer()
//
// Log Configuration
//
if _, verbose := os.LookupEnv("GONTLM_PROXY_VERBOSE"); log.IsLevelEnabled(log.DebugLevel) || ProxyVerbose || verbose {
if !log.IsLevelEnabled(log.DebugLevel) {
log.SetLevel(log.DebugLevel)
}
proxy.Verbose = true
}
// Override ProxyPlease Logger
proxyplease.SetDebugf(func(section string, msgs ...interface{}) {
log.Debugf("proxyplease."+section, msgs...)
})
if ProxyServer == "" {
ProxyServer = getProxyServer()
}
bind, err := url.Parse(ProxyBind)
if err != nil {
log.Fatal(err)
}
log.Infof("Listening on: %s", bind.Host)
var proxyUrl *url.URL
if ProxyServer != "" {
proxyUrl, err = url.Parse(ProxyServer)
if err != nil {
log.Fatal(err)
}
if isLocalHost(proxyUrl.Hostname()) {
if bind.Port() == proxyUrl.Port() {
log.WithFields(log.Fields{
"Bind": bind.Host,
"Proxy": proxyUrl.Host,
}).Fatal("Loop condition detected!")
}
}
log.Infof("Forwarding Proxy is: %s", proxyUrl.String())
}
//
// Dial Context
//
dialContext := proxyplease.NewDialContext(proxyplease.Proxy{URL: proxyUrl})
proxy.ConnectDial = func(network, addr string) (net.Conn, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
return dialContext(ctx, network, addr)
}
proxy.Tr.Proxy = nil
proxy.Tr.DialContext = dialContext
//
// HTTP Handler
//
// var HttpConnect goproxy.FuncHttpsHandler = func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
// HTTPConnect := &goproxy.ConnectAction{
// Action: goproxy.ConnectAccept,
// TLSConfig: goproxy.TLSConfigFromCA(&goproxy.GoproxyCa),
// }
//
// return HTTPConnect, host
// }
// proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile(".*:80$|.*:8080$"))).HandleConnect(HttpConnect)
//
// Connect Handler
//
var AlwaysMitm goproxy.FuncHttpsHandler = func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
// HTTPSConnect := &goproxy.ConnectAction{
// // ConnectMitm enables SSL Interception, required for request filtering over HTTPS.
// // Action: goproxy.ConnectMitm,
// // ConnectAccept preserves upstream SSL Certificates, etc. TCP tunneling basically.
// Action: goproxy.ConnectAccept,
// TLSConfig: goproxy.TLSConfigFromCA(&goproxy.GoproxyCa),
// }
// return HTTPSConnect, host
return goproxy.OkConnect, host
}
proxy.OnRequest().HandleConnect(AlwaysMitm)
//
// Request Handling
//
// MITM Action is required for HTTPS Requests (e.g. goproxy.ConnectMitm instead of goproxy.ConnectAccept)
//
// proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
// log.Fatal(req.URL.String())
// return req, nil
// })
srv := &http.Server{
Addr: bind.Host,
Handler: proxy,
IdleTimeout: time.Second * 60,
}
log.Fatal(srv.ListenAndServe())
}
// Check if it is a WebSocketUpgrade
// func IsWebSocketUpgrade() goproxy.ReqConditionFunc {
// return func(req *http.Request, ctx *goproxy.ProxyCtx) bool {
// return websocket.IsWebSocketUpgrade(req)
// }
// }