-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
101 lines (82 loc) · 1.98 KB
/
http.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
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
"github.com/Alex-Eftimie/netutils"
"github.com/fatih/color"
"golang.org/x/net/http2"
)
var h2cc *http2.ClientConn
// HTTPServer handles http proxy
type HTTPServer struct {
*http.Server
}
func newHTTPServer(addr string) *HTTPServer {
hs := &HTTPServer{}
hs.Server = &http.Server{
Addr: addr,
Handler: hs,
}
return hs
}
func (cs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("[HTTP]", r.RemoteAddr, r.Method, r.URL)
// r.Write(os.Stderr)
var pipW *io.PipeWriter
var pipR *io.PipeReader
if r.Method == "CONNECT" {
pipR, pipW = io.Pipe()
r.Body = pipR
}
resp, err := h2cc.RoundTrip(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, isInternal := w.(Internal)
if r.Method == "CONNECT" {
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Tunneling not supported", http.StatusInternalServerError)
return
}
clientConn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
defer clientConn.Close()
if !isInternal {
downR := http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Close: true,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Body: ioutil.NopCloser(bytes.NewBufferString("")),
Header: make(http.Header),
}
log.Println("We are connected, writing response")
// if we don't close the body, write will just hang
downR.Body.Close()
downR.Write(clientConn)
}
data := netutils.HTTPReadWriteCloser{
Writer: pipW,
ReadCloser: resp.Body,
}
netutils.RunPiper(clientConn, data)
color.Yellow("Piper Finished")
return
}
resp.ProtoMajor = r.ProtoMajor
resp.ProtoMinor = r.ProtoMinor
resp.Proto = r.Proto
netutils.CopyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
resp.Body.Close()
}