forked from mosajjal/sniproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttps.go
157 lines (148 loc) · 3.96 KB
/
https.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
package main
import (
"crypto/rand"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
slog "golang.org/x/exp/slog"
"golang.org/x/net/proxy"
)
var httpslog = slog.New(log.Handler().WithAttrs([]slog.Attr{{Key: "service", Value: slog.StringValue("https")}}))
// handle HTTPS connections coming to the reverse proxy. this will get a connction from the handle443 function
// need to grab the HTTP request from this, and pass it on to the HTTP handler.
func handleReverse(conn net.Conn) error {
httpslog.Info("connecting to HTTP")
// send the reverse conn to local HTTP listner
srcAddr := net.TCPAddr{
IP: c.sourceAddr,
Port: 0,
}
target, err := net.DialTCP("tcp", &srcAddr, &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: int(c.HTTPPort)})
if err != nil {
return err
}
pipe(conn, target)
return nil
}
func handle443(conn net.Conn) error {
c.recievedHTTPS.Inc(1)
if !checkGeoIPSkip(conn.RemoteAddr().String()) {
httpslog.Warn("Rejected request due to GEOIP restriction", "ip", conn.RemoteAddr().String())
conn.Close()
return nil
}
defer conn.Close()
incoming := make([]byte, 2048)
n, err := conn.Read(incoming)
if err != nil {
httpslog.Error("", err)
return err
}
sni, err := GetHostname(incoming)
if err != nil {
httpslog.Error("", err)
return err
}
// check SNI against domainlist for an extra layer of security
if !c.AllDomains && inDomainList(sni+".") {
httpslog.Warn("a client requested connection to " + sni + ", but it's not allowed as per configuration.. resetting TCP")
conn.Close()
return nil
}
rAddr, err := c.dnsClient.lookupDomain4(sni)
rPort := 443
if err != nil || rAddr == nil {
httpslog.Warn(err.Error())
return err
}
// TODO: handle timeout and context here
if rAddr.IsLoopback() || rAddr.IsPrivate() || rAddr.Equal(net.IPv4(0, 0, 0, 0)) {
httpslog.Info("connection to private IP ignored")
return nil
}
// if SNI is the reverse proxy, this request needs to be handled by a HTTPS handler
if sni == c.reverseProxySNI {
rAddr = net.IPv4(127, 0, 0, 1)
// TODO: maybe 65000 as a static port is not a good idea and this needs to be random OR unix socket
rPort = 65000
}
httpslog.Info("establishing connection",
"remote_ip", rAddr,
"source_ip", conn.RemoteAddr().String(),
"host", sni,
)
var target *net.TCPConn
if c.dialer == proxy.Direct {
// with the manipulation of the soruce address, we can set the outbound interface
srcAddr := net.TCPAddr{
IP: c.sourceAddr,
Port: 0,
}
target, err = net.DialTCP("tcp", &srcAddr, &net.TCPAddr{IP: rAddr, Port: rPort})
if err != nil {
httpslog.Error("could not connect to target", err)
conn.Close()
return err
}
} else {
tmp, err := c.dialer.Dial("tcp", fmt.Sprintf("%s:%d", rAddr, rPort))
if err != nil {
httpslog.Error("could not connect to target", err)
conn.Close()
return err
}
target = tmp.(*net.TCPConn)
}
defer target.Close()
c.proxiedHTTPS.Inc(1)
target.Write(incoming[:n])
pipe(conn, target)
return nil
}
func runReverse() {
// reverse https can't run on 443. we'll pick a random port and pipe the 443 traffic back to it.
cert, err := tls.LoadX509KeyPair(c.ReverseProxyCert, c.ReverseProxyKey)
if err != nil {
httpslog.Error("", err)
}
config := tls.Config{Certificates: []tls.Certificate{cert}}
config.Rand = rand.Reader
listener, err := tls.Listen("tcp", ":65000", &config)
if err != nil {
httpslog.Error("", err)
}
for {
conn, err := listener.Accept()
if err != nil {
httpslog.Error("", err)
break
}
defer conn.Close()
tlscon, ok := conn.(*tls.Conn)
if ok {
state := tlscon.ConnectionState()
for _, v := range state.PeerCertificates {
fmt.Println(x509.MarshalPKIXPublicKey(v.PublicKey))
}
}
go handleReverse(conn)
}
}
func runHTTPS() {
l, err := net.Listen("tcp", c.BindIP+fmt.Sprintf(":%d", c.HTTPSPort))
if err != nil {
httpslog.Error("", err)
panic(-1)
}
defer l.Close()
for {
c, err := l.Accept()
if err != nil {
httpslog.Error("", err)
}
go func() {
go handle443(c)
}()
}
}