forked from mosajjal/sniproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpproxy.go
165 lines (143 loc) · 4.04 KB
/
httpproxy.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
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
slog "golang.org/x/exp/slog"
)
var httplog = slog.New(log.Handler().WithAttrs([]slog.Attr{{Key: "service", Value: slog.StringValue("http")}}))
var passthruRequestHeaderKeys = [...]string{
"Accept",
"Accept-Encoding",
"Accept-Language",
"Cache-Control",
"Cookie",
"Referer",
"User-Agent",
}
var passthruResponseHeaderKeys = [...]string{
"Content-Encoding",
"Content-Language",
"Content-Type",
"Cache-Control", // TODO: Is this valid in a response?
"Date",
"Etag",
"Expires",
"Last-Modified",
"Location",
"Server",
"Vary",
}
func runHTTP() {
handler := http.DefaultServeMux
handler.HandleFunc("/", handle80)
s := &http.Server{
Addr: fmt.Sprintf(":%d", c.HTTPPort),
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if err := s.ListenAndServe(); err != nil {
httplog.Error("", err)
panic(-1)
}
}
func handle80(w http.ResponseWriter, r *http.Request) {
c.recievedHTTP.Inc(1)
if !checkGeoIPSkip(r.RemoteAddr) {
http.Error(w, "Could not reach origin server", 403)
return
}
httplog.Info("rejected request", "ip", r.RemoteAddr)
// if the URL starts with the public IP, it needs to be skipped to avoid loops
if strings.HasPrefix(r.Host, c.PublicIP) {
httplog.Warn("someone is requesting HTTP to sniproxy itself, ignoring...")
http.Error(w, "Could not reach origin server", 404)
return
}
httplog.Info("REQ", "method", r.Method, "host", r.Host, "url", r.URL)
// Construct filtered header to send to origin server
hh := http.Header{}
for _, hk := range passthruRequestHeaderKeys {
if hv, ok := r.Header[hk]; ok {
hh[hk] = hv
}
}
// Construct request to send to origin server
rr := http.Request{
Method: r.Method,
URL: r.URL,
Header: hh,
Body: r.Body,
// TODO: Is this correct for a 0 value?
// Perhaps a 0 may need to be reinterpreted as -1?
ContentLength: r.ContentLength,
Close: r.Close,
}
rr.URL.Scheme = "http"
rr.URL.Host = r.Host
// check to see if this host is listed to be processed, otherwise RESET
if !c.AllDomains && inDomainList(r.Host+".") {
http.Error(w, "Could not reach origin server", 403)
httplog.Warn("a client requested connection to " + r.Host + ", but it's not allowed as per configuration.. sending 403")
return
}
// if host is the reverse proxy, this request needs to be handled by the upstream address
if r.Host == c.reverseProxySNI {
reverseProxyURI, err := url.Parse(c.reverseProxyAddr)
if err != nil {
httplog.Error("failed to parse reverseproxy url", err)
}
// TODO: maybe this won't work and I need to be more specific
// rr.URL = reverseProxyURI
hostPort := ""
if reverseProxyURI.Port() != "80" {
hostPort = fmt.Sprintf("%s:%s", reverseProxyURI.Host, reverseProxyURI.Port())
} else {
hostPort = reverseProxyURI.Host
}
rr.URL.Host = reverseProxyURI.Host
// add the port to the host header
rr.Header.Set("Host", hostPort)
}
transport := http.Transport{
Dial: c.dialer.Dial,
}
// Forward request to origin server
resp, err := transport.RoundTrip(&rr)
if err != nil {
// TODO: Passthru more error information
http.Error(w, "Could not reach origin server", 500)
httplog.Error("", err)
return
}
defer resp.Body.Close()
httplog.Info("http response", "status_code", resp.Status)
// Transfer filtered header from origin server -> client
respH := w.Header()
for _, hk := range passthruResponseHeaderKeys {
if hv, ok := resp.Header[hk]; ok {
respH[hk] = hv
}
}
c.proxiedHTTP.Inc(1)
w.WriteHeader(resp.StatusCode)
// Transfer response from origin server -> client
//TODO: error handling
io.Copy(w, resp.Body)
// if resp.ContentLength > 0 {
// // (Ignore I/O errors, since there's nothing we can do)
// io.CopyN(w, resp.Body, resp.ContentLength)
// } else if resp.Close { // TODO: Is this condition right?
// // Copy until EOF or some other error occurs
// for {
// if _, err := io.Copy(w, resp.Body); err != nil {
// break
// }
// }
// }
}