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
package tlsproxy
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/dispatcher"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/hostport"
)
type Config struct {
Hostnames []string
}
type Deps struct {
Upstream func() (string, bool)
DialTLS func(ctx context.Context, network, addr string) (net.Conn, error)
Log *slog.Logger
}
type Proxy struct {
hosts []string
upstream func() (string, bool)
log *slog.Logger
proxy *httputil.ReverseProxy
}
var _ dispatcher.Proxy = (*Proxy)(nil)
type targetKey struct{}
func New(cfg Config, deps Deps) (*Proxy, error) {
if deps.Upstream == nil {
return nil, errors.New("tlsproxy: an upstream resolver is required")
}
log := deps.Log
if log == nil {
log = slog.New(slog.DiscardHandler)
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.Proxy = nil
if deps.DialTLS != nil {
transport.DialTLSContext = deps.DialTLS
}
p := &Proxy{hosts: AllowedHosts(cfg.Hostnames), upstream: deps.Upstream, log: log}
p.proxy = &httputil.ReverseProxy{
Rewrite: func(req *httputil.ProxyRequest) {
target, _ := req.In.Context().Value(targetKey{}).(*url.URL)
if target == nil {
return
}
req.Out.URL.Scheme = target.Scheme
req.Out.URL.Host = target.Host
req.Out.Host = req.In.Host
},
Transport: transport,
ErrorHandler: func(w http.ResponseWriter, req *http.Request, proxyErr error) {
log.Warn("dispatcher_tls_upstream_failed", "host", req.Host, "path", req.URL.Path, "err", proxyErr)
http.Error(w, "upstream unreachable", http.StatusBadGateway)
},
}
return p, nil
}
func (p *Proxy) Hosts() []string { return append([]string(nil), p.hosts...) }
func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
host := hostport.HostnameOnly(req.Host)
if !HostAllowed(p.hosts, host) {
p.log.Info("dispatcher_tls_refused", "host", host, "path", req.URL.Path)
http.Error(w, "not a dispatcher name", http.StatusNotFound)
return
}
raw, ok := p.upstream()
if !ok {
p.log.Warn("dispatcher_tls_upstream_unresolved", "host", req.Host, "path", req.URL.Path)
http.Error(w, "upstream unreachable", http.StatusBadGateway)
return
}
target, err := ParseUpstream(raw)
if err != nil {
p.log.Warn("dispatcher_tls_upstream_invalid", "upstream", raw, "err", err)
http.Error(w, "upstream unreachable", http.StatusBadGateway)
return
}
p.proxy.ServeHTTP(w, req.WithContext(context.WithValue(req.Context(), targetKey{}, target)))
}
func ParseUpstream(raw string) (*url.URL, error) {
target, err := url.Parse(strings.TrimSpace(raw))
if err != nil {
return nil, fmt.Errorf("tlsproxy: parsing the upstream: %w", err)
}
if target.Scheme != "http" && target.Scheme != "https" {
return nil, fmt.Errorf("tlsproxy: the upstream scheme %q is not http or https", target.Scheme)
}
if target.Host == "" {
return nil, errors.New("tlsproxy: the upstream has no host")
}
return target, nil
}
func AllowedHosts(hostnames []string) []string {
seen := map[string]struct{}{}
hosts := []string{}
for _, host := range hostnames {
host = strings.ToLower(hostport.HostnameOnly(strings.TrimSpace(host)))
if host == "" {
continue
}
if _, ok := seen[host]; ok {
continue
}
seen[host] = struct{}{}
hosts = append(hosts, host)
}
if len(hosts) == 0 {
hosts = []string{"localhost"}
}
return hosts
}
func HostAllowed(hosts []string, host string) bool {
host = strings.ToLower(hostport.HostnameOnly(host))
for _, allowed := range hosts {
if allowed == "*" || host == allowed || strings.HasSuffix(host, "."+allowed) {
return true
}
}
return false
}