-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathexplicit.go
172 lines (145 loc) · 3.94 KB
/
explicit.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
package transproxy
import (
"encoding/base64"
"io"
"log"
"net"
"net/http"
"net/url"
"strings"
"time"
)
type ExplicitProxy struct {
ExplicitProxyConfig
user string
category string
// For HTTP
proxyTransport *http.Transport
proxyAuthTransport *http.Transport
// For HTTPS
proxyHost string
proxyAuthorization string
}
type ExplicitProxyConfig struct {
ListenAddress string
UseProxyAuthorization bool
}
func NewExplicitProxy(c ExplicitProxyConfig) *ExplicitProxy {
return &ExplicitProxy{
ExplicitProxyConfig: c,
}
}
func (s ExplicitProxy) Start() error {
u, err := url.Parse(GetProxyEnv("http_proxy"))
if err != nil {
return err
}
if s.UseProxyAuthorization {
s.category = "Explicit-Proxy(Auth)"
if u.User == nil {
log.Printf("info: Not Started because of no proxy user category='%s'", s.category)
return nil
}
// For HTTPS
s.proxyAuthorization = "Basic " + base64.StdEncoding.EncodeToString([]byte(u.User.String()))
s.proxyHost = u.Host
// For HTTP
s.proxyAuthTransport = &http.Transport{Proxy: http.ProxyURL(u)}
} else {
s.category = "Explicit-Proxy(NoAuth)"
// For HTTPS
s.proxyHost = u.Host
// For HTTP
u, _ = url.Parse(u.String())
u.User = nil
s.proxyTransport = &http.Transport{Proxy: http.ProxyURL(u)}
}
handler := http.HandlerFunc(s.handleRequest)
log.Printf("info: Start listener on %s category='%s'", s.ListenAddress, s.category)
go func() {
http.ListenAndServe(s.ListenAddress, handler)
}()
return nil
}
func (s ExplicitProxy) handleRequest(w http.ResponseWriter, r *http.Request) {
// access logging
s.accessLog(r)
if r.Method == "CONNECT" {
s.handleHttps(w, r)
} else {
s.handleHttp(w, r)
}
}
func (s ExplicitProxy) accessLog(r *http.Request) {
host, _, _ := net.SplitHostPort(r.RemoteAddr)
if s.UseProxyAuthorization {
log.Printf("info: category='%s' remoteAddr='%s' method='%s' uri='%s'", s.category, host, r.Method, r.RequestURI)
} else {
var decodedAuth string
values := r.Header["Proxy-Authorization"]
if len(values) > 0 {
auth := strings.Split(values[0], " ")
if len(auth) > 0 {
b, _ := base64.StdEncoding.DecodeString(auth[1])
decodedAuth = strings.Split(string(b[:]), ":")[0]
}
}
log.Printf("info: category='%s' user='%s' remoteAddr='%s' method='%s' uri='%s'", s.category, decodedAuth, host, r.Method, r.RequestURI)
}
}
func (s ExplicitProxy) handleHttps(w http.ResponseWriter, r *http.Request) {
destConn, err := net.DialTimeout("tcp", s.proxyHost, 10*time.Second)
if err != nil {
log.Printf("error: %s category='%s'", err, s.category)
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
hj, ok := w.(http.Hijacker)
if !ok {
log.Printf("error: Hijacking not supported category='%s'", s.category)
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
destConn.Close()
return
}
clientConn, _, err := hj.Hijack()
if err != nil {
log.Printf("error: %s category='%s'", err, s.category)
http.Error(w, err.Error(), http.StatusServiceUnavailable)
destConn.Close()
return
}
if s.UseProxyAuthorization {
r.Header.Set("Proxy-Authorization", s.proxyAuthorization)
}
r.Write(destConn)
go transfer(clientConn, destConn)
go transfer(destConn, clientConn)
log.Printf("debug: End proxy category='%s'", s.category)
}
func transfer(destination io.WriteCloser, source io.ReadCloser) {
defer destination.Close()
defer source.Close()
io.Copy(destination, source)
}
func (s ExplicitProxy) handleHttp(w http.ResponseWriter, r *http.Request) {
hj, _ := w.(http.Hijacker)
var client *http.Client
if s.UseProxyAuthorization {
client = &http.Client{
Transport: s.proxyAuthTransport,
}
} else {
client = &http.Client{
Transport: s.proxyTransport,
}
}
r.RequestURI = ""
if resp, err := client.Do(r); err != nil {
log.Printf("error: %s", err)
} else if conn, _, err := hj.Hijack(); err != nil {
log.Printf("error: %s", err)
} else {
defer conn.Close()
resp.Write(conn)
}
}