-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpproxy_capture.go
191 lines (156 loc) · 4.61 KB
/
httpproxy_capture.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 http_proxy
import (
"context"
"io"
"log"
"net"
"net/http"
"strings"
"github.com/costinm/ssh-mesh/nio"
"github.com/costinm/ugate"
)
// Used for HTTP_PROXY=localhost:port, to intercept outbound traffic using http
// proxy protocol.
//
// It also handles CONNECT and 'transparent' proxy.
//
//
// Android allows using HTTP_PROXY, and is used by browser - more efficient than TUN.
// HttpProxy handles HTTP PROXY and plain text HTTP requests (primarily on port 80)
// for egress side.
//
// A DNAT or explicit proxy are sufficient - no need for TPROXY or REDIRECT, host
// is extracted from request and sessions should use cookies.
type HttpProxy struct {
gw *ugate.UGate
}
func NewHTTPProxy(gw *ugate.UGate) *HttpProxy {
return &HttpProxy{
gw: gw,
}
}
func ForwardHTTP(c *ugate.MeshCluster, w http.ResponseWriter, r *http.Request, pathH string) error {
r.Host = pathH
r1 := nio.CreateUpstreamRequest(w, r)
r1.URL.Scheme = "http"
// will be used by RoundTrip.
r1.URL.Host = pathH
res, err := c.RoundTrip(r1)
if err != nil {
return err
}
nio.SendBackResponse(w, r, res, err)
return nil
}
// RoundTripStart listening on the addr, as a HTTP_PROXY
// Handles CONNECT and PROXY requests using the gateway
// for streams.
func (gw *HttpProxy) HttpProxyCapture(addr string) error {
// For http proxy we need a dedicated plain HTTP port
nl, err := net.Listen("tcp", addr)
if err != nil {
log.Println("Failed to listen", err)
return err
}
go http.Serve(nl, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "CONNECT" {
gw.handleConnect(w, r)
return
}
// This is a real HTTP proxy
if r.URL.IsAbs() {
log.Println("HTTPPRX", r.Method, r.Host, r.RemoteAddr, r.URL)
gw.captureHttpProxyAbsURL(w, r)
return
}
gw.captureHttpProxyAbsURL(w, r)
}))
return nil
}
// Http proxy to a configured HTTP host. Hostname to HTTP address explicitly
// configured. Also hostnmae to file serving.
func (gw *HttpProxy) proxy(w http.ResponseWriter, r *http.Request) bool {
// TODO: if host is XXXX.m.SUFFIX -> forward to node.
host, found := gw.gw.Clusters[r.Host]
if !found {
return false
}
if len(host.Addr) > 0 {
log.Println("FWDHTTP: ", r.Method, r.Host, r.RemoteAddr, r.URL)
ForwardHTTP(host, w, r, host.Addr)
}
return true
}
// WIP: HTTP proxy with absolute address, to a QUIC server (or sidecar)`
func (gw *HttpProxy) captureHttpProxyAbsURL(w http.ResponseWriter, r *http.Request) {
// HTTP proxy mode - uses the QUIC client to connect to the node
// TODO: redirect via VPN, only root VPN can do plaintext requests
// parse r.URL, follow the same steps as TCP - if mesh use Client/mtls, if VPN set forward to VPN, else use H2 client
// r.Host is populated from the absolute URL.
// Typical headers (curl):
// User-Agent, Acept, Proxy-Connection:Keep-Alive
if gw.proxy(w, r) {
// found the host in clusters - it is an internal/mesh request
return
}
ht := &http.Transport{
DialContext: gw.gw.DialContext,
}
hc := &http.Client{Transport: ht}
// TODO: use VPN to RoundTripStart !!!
//
resp, err := hc.Transport.RoundTrip(r)
if err != nil {
log.Println("XXX ", err)
http.Error(w, err.Error(), 500)
return
}
origBody := resp.Body
defer origBody.Close()
nio.CopyResponseHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
log.Println("PHTTP: ", r.URL)
}
// WIP: If method is CONNECT - operate in TCP proxy mode. This can be used to proxy
// a TCP UdpNat to a mesh node, from localhost or from a net node.
// Only used to capture local traffic - should be bound to localhost only, like socks.
// It speaks HTTP/1.1, no QUIC
func (gw *HttpProxy) handleConnect(w http.ResponseWriter, r *http.Request) {
hij, ok := w.(http.Hijacker)
if !ok {
w.WriteHeader(503)
w.Write([]byte("Error - no hijack support"))
return
}
host := r.URL.Host
if !strings.Contains(host, ":") {
host = host + ":443"
}
// TODO: second param may contain unprocessed data.
proxyClient, clientBuffer, e := hij.Hijack()
if e != nil {
w.WriteHeader(503)
w.Write([]byte("Error - no hijack support"))
return
}
//ra := proxyClient.RemoteAddr().(*net.TCPAddr)
str := nio.GetStream(proxyClient, proxyClient)
if clientBuffer.Reader.Size() > 0 {
}
gw.gw.OnStream(str)
defer gw.gw.OnStreamDone(str)
str.Dest = host
str.Direction = nio.StreamTypeOut
nc, err := gw.gw.DialContext(context.Background(), "tcp", str.Dest)
if err != nil {
w.WriteHeader(503)
w.Write([]byte("RoundTripStart error" + err.Error()))
return
}
proxyClient.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
nio.Proxy(nc, str, str, str.Dest)
//defer nc.Close()
//
//str.ProxyTo(nc)
}