-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-plain.go
More file actions
152 lines (136 loc) · 3.84 KB
/
http-plain.go
File metadata and controls
152 lines (136 loc) · 3.84 KB
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
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"strings"
)
func transparentHTTPProxy(upstream http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isIPBanned(r.RemoteAddr) {
w.WriteHeader(403)
w.Write([]byte("Banned"))
}
if !*disableWebUI && (r.URL.Host == lpHost1 || r.URL.Host == lpHost2) {
serveWebUIPlain(w, r)
return
}
isExcluded := authExcludedDomains[r.Host]
if !(isExcluded || isIPAllowed(r.RemoteAddr) || (*proxyPassword == "" && !*enforceCert) || !strings.HasSuffix(r.Host, "apple.com")) {
pauth := r.Header.Get("Proxy-Authorization")
if pauth == "" {
send407(w)
return
} else {
ppassenc := encodeBase64(fmt.Sprintf("lp:%s", *proxyPassword))
if pauth != ppassenc {
log.Printf("[%s] Got invalid password on %s", r.RemoteAddr, r.Host)
addFailedIP(r.RemoteAddr)
send407(w)
return
}
log.Printf("[%s] Got correct password (transparentHTTPProxy)", fmt.Sprintf("%p", r))
allowIP(r.RemoteAddr)
}
}
reqID := fmt.Sprintf("%p", r)
if *logURLs {
log.Printf("[%s] HTTP URL: %s %s", reqID, r.Method, r.URL.String())
}
rw := &responseTracker{
ResponseWriter: w,
reqID: reqID,
url: r.URL.String(),
}
log.Printf("reach x6969")
upstream.ServeHTTP(rw, r)
})
}
func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("oh hi!")
if *blockRemoteConnections {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, "Invalid remote address", http.StatusBadRequest)
return
}
ip := net.ParseIP(host)
if ip == nil || !ip.IsLoopback() {
http.Error(w, "Remote connections not allowed", http.StatusForbidden)
return
}
}
if r.Method == "CONNECT" {
p.serveConnect(w, r)
return
}
// Create a custom director that handles redirects transparently
director := func(req *http.Request) {
httpDirector(req)
// Check for redirects and modify the request to go to the redirect target
if targetURL, shouldRedirect := checkRedirect(req.URL); shouldRedirect {
log.Printf("Redirecting %s → %s", req.URL.String(), targetURL.String())
req.URL = targetURL
req.Host = targetURL.Host
}
}
noerror := fmt.Errorf("TSPMOGNG")
// Create a custom transport that handles connection errors
transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
// First try to connect to the requested address
conn, err := net.Dial(network, addr)
if err != nil {
// If connection fails, check if this is a redirect domain
host, _, _ := net.SplitHostPort(addr)
rules, hasRedirects := redirectRules[host]
if hasRedirects && len(rules) > 0 {
// Try the first redirect target
targetHost := rules[0].toURL.Host
targetPort := rules[0].toURL.Port()
if targetPort == "" {
if rules[0].toURL.Scheme == "https" {
targetPort = "443"
} else {
targetPort = "80"
}
}
targetAddr := net.JoinHostPort(targetHost, targetPort)
conn, err = net.Dial(network, targetAddr)
}
}
if err != nil {
if !noDialErrorDomains[network] {
log.Printf("%s", err)
}
err = noerror
}
return conn, err
},
TLSClientConfig: p.TLSClientConfig,
}
rp := &httputil.ReverseProxy{
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
if err != nil && err != noerror {
log.Printf("ReverseProxy error: %s", err)
}
if w != nil {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("Upstream unavailable"))
}
},
Director: director,
Transport: transport,
FlushInterval: p.FlushInterval,
}
log.Printf("im starting?? im starting????")
h := p.Wrap(rp)
log.Printf("it starts now????")
if !luaProc(w, r) {
log.Printf("fail")
h.ServeHTTP(w, r)
}
}