-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
98 lines (79 loc) · 2.18 KB
/
connect.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
package main
import (
"crypto/tls"
"log"
"net"
"net/http"
)
type getCertFn func(hostname string) (*tls.Config, error)
type interceptHandler struct {
listener channelListener
server *http.Server
getCert getCertFn
}
func newInterceptHandler(getCert getCertFn, innerHandler http.HandlerFunc) *interceptHandler {
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Scheme = "https"
r.URL.Host = r.Host
innerHandler(w, r)
}),
}
listener := channelListener(make(chan net.Conn))
go func() {
// returns always a non-nil error if the server is not closed/shtudown
_ = server.Serve(listener)
}()
return &interceptHandler{
listener: listener,
server: server,
getCert: getCert,
}
}
func (i *interceptHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
log.Printf("split host port failed '%s': %s", r.Host, err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
tlsConfig, err := i.getCert(host)
if err != nil {
log.Println("failed to obtain tls config:", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
hj, ok := w.(http.Hijacker)
if !ok {
log.Print("hijack of connection failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
clientConn, _, err := hj.Hijack()
if err != nil {
log.Println("hijack failed:", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
tlsConn := tls.Server(clientConn, tlsConfig)
i.handleConnection(tlsConn)
}
func (i *interceptHandler) handleConnection(c net.Conn) {
i.listener <- c
}
func (i *interceptHandler) close() {
i.server.Close()
}
// channelListener allows to send connection into a listener through a channel
type channelListener chan net.Conn
func (cl channelListener) Accept() (net.Conn, error) {
return <-cl, nil
}
func (cl channelListener) Addr() net.Addr {
return nil
}
func (cl channelListener) Close() error {
close(cl)
return nil
}