/
destination.go
98 lines (85 loc) · 2.05 KB
/
destination.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 (
"bufio"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"strings"
)
type Destination interface {
fmt.Stringer
GetAddr(conn net.Conn) (destinationAddr string, err error)
}
type StaticDestination struct {
Dest string
}
func (dest *StaticDestination) GetAddr(conn net.Conn) (destinationAddr string, err error) {
return dest.Dest, nil
}
func (dest *StaticDestination) String() string {
return dest.Dest
}
type HostHeaderDestination struct {
ListenPort string
}
func (dest *HostHeaderDestination) GetAddr(conn net.Conn) (destinationAddr string, err error) {
rq, err := http.ReadRequest(bufio.NewReader(conn))
host := strings.TrimSpace(rq.Host)
switch {
case err != nil:
err = fmt.Errorf("failed to read host header: %w", err)
case host == "":
err = fmt.Errorf("empty host header")
default:
destinationAddr = setPort(host, dest.ListenPort)
if DEBUG {
log.Printf("HTTP host destination: %s", destinationAddr)
}
}
return
}
func (dest *HostHeaderDestination) String() string {
return fmt.Sprintf("HTTP_HOST:[%s]", dest.ListenPort)
}
type SNIDestination struct {
ListenPort string
}
func (dest *SNIDestination) GetAddr(conn net.Conn) (destinationAddr string, err error) {
var host string
hostRead := false
_ = tls.Server(
conn,
&tls.Config{
GetConfigForClient: func(helloInfo *tls.ClientHelloInfo) (*tls.Config, error) {
host = strings.TrimSpace(helloInfo.ServerName)
hostRead = true
return nil, nil
},
},
).Handshake()
switch {
case !hostRead:
err = fmt.Errorf("failed to read TLS SNI")
case host == "":
err = fmt.Errorf("empty TLS SNI host name")
default:
destinationAddr = setPort(host, dest.ListenPort)
if DEBUG {
log.Printf("SNI destination: %s", destinationAddr)
}
}
return
}
func (dest *SNIDestination) String() string {
return fmt.Sprintf("TLS_SNI:[%s]", dest.ListenPort)
}
func setPort(hostWithOptPort, defaultPort string) string {
host, port, err := net.SplitHostPort(hostWithOptPort)
if err != nil {
host = hostWithOptPort
port = defaultPort
}
return net.JoinHostPort(host, port)
}