-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy.go
100 lines (94 loc) · 2.63 KB
/
proxy.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
package main
import (
_ "embed"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"sync"
)
func handleConnect(w http.ResponseWriter, destAddr string, r *http.Request) {
r.Body.Close()
hijacker, ok := w.(http.Hijacker)
if !ok {
log.Printf("can't hijack response writer %v", w)
// Probably tried over HTTP2, dumbass browsers...
http.Error(w, "can't hijack response writer", http.StatusBadRequest)
return
}
conn, err := net.Dial("tcp", destAddr)
if err != nil {
log.Printf("error dialling %q: %v", destAddr, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close()
rConn, buf, err := hijacker.Hijack()
if err != nil {
log.Printf("error hijacking connect response: %v", err)
http.Error(w, "error hijacking response writer: %v", http.StatusServiceUnavailable)
return
}
defer rConn.Close()
if buf.Reader.Buffered() != 0 || buf.Writer.Buffered() != 0 {
log.Printf("hijacked connection has %v unread and %v unwritten", buf.Reader.Buffered(), buf.Writer.Buffered())
}
rConn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
var wg sync.WaitGroup
wg.Add(2)
copyErrs := make(chan error, 2)
go func() {
defer wg.Done()
_, err := io.Copy(rConn, conn)
if err != nil {
log.Printf("error copying from origin to proxy client: %v", err)
}
copyErrs <- err
}()
go func() {
defer wg.Done()
_, err := io.Copy(conn, rConn)
if err != nil {
log.Printf("error copying from proxy client to origin: %v", err)
}
copyErrs <- err
}()
//<-copyErrs
wg.Wait()
}
func reverseProxy(w http.ResponseWriter, r *http.Request) {
(&httputil.ReverseProxy{
Director: func(r *http.Request) {
log.Printf("directing request for %v", r.URL)
r.URL.Scheme = "https"
r.URL.Host = r.Host
},
}).ServeHTTP(w, r)
}
var proxyPacTmpl = htmlTemplates.Lookup("templates/pac.tmpl")
type pacData struct {
HttpProxy string
HttpsProxy string
RootDomain string
}
func serveDynamicPac(w http.ResponseWriter, r *http.Request, httpProxyPort string, httpsProxyPort string) error {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}
// Chrome seems to ignore the inline, but does pay attention to the filename. It's just nice for
// end users to be able to view what they're getting remotely.
w.Header().Set("Content-Disposition", "inline; filename=btlink.pac")
w.Header().Set("Content-Type", `application/x-ns-proxy-autoconfig`)
err = proxyPacTmpl.Execute(w, pacData{
HttpProxy: net.JoinHostPort(host, httpProxyPort),
HttpsProxy: net.JoinHostPort(host, httpsProxyPort),
RootDomain: "." + rootDomain,
})
if err != nil {
err = fmt.Errorf("executing template: %w", err)
}
return err
}