-
Notifications
You must be signed in to change notification settings - Fork 51
/
tlsproxy.go
87 lines (73 loc) · 2.13 KB
/
tlsproxy.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
package netemx
import (
"io"
"net"
"sync"
"github.com/ooni/netem"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/testingx"
)
// NewTLSProxyServerFactory is a [NetStackServerFactory] for the TCP echo service.
func NewTLSProxyServerFactory(logger model.Logger, ports ...uint16) NetStackServerFactory {
return &tlsProxyServerFactory{
logger: logger,
ports: ports,
}
}
type tlsProxyServerFactory struct {
logger model.Logger
ports []uint16
}
// MustNewServer implements NetStackServerFactory.
func (f *tlsProxyServerFactory) MustNewServer(_ NetStackServerFactoryEnv, stack *netem.UNetStack) NetStackServer {
return &tlsProxyServer{
closers: []io.Closer{},
logger: f.logger,
mu: sync.Mutex{},
ports: f.ports,
unet: stack,
}
}
type tlsProxyServer struct {
closers []io.Closer
logger model.Logger
mu sync.Mutex
ports []uint16
unet *netem.UNetStack
}
// Close implements NetStackServer.
func (srv *tlsProxyServer) Close() error {
// "this method MUST be CONCURRENCY SAFE"
defer srv.mu.Unlock()
srv.mu.Lock()
// make sure we close all the child listeners
for _, closer := range srv.closers {
_ = closer.Close()
}
// "this method MUST be IDEMPOTENT"
srv.closers = []io.Closer{}
return nil
}
// MustStart implements NetStackServer.
func (srv *tlsProxyServer) MustStart() {
// "this method MUST be CONCURRENCY SAFE"
defer srv.mu.Unlock()
srv.mu.Lock()
// for each port of interest - note that here we panic liberally because we are
// allowed to do so by the [NetStackServer] documentation.
for _, port := range srv.ports {
// create the endpoint address
ipAddr := net.ParseIP(srv.unet.IPAddress())
runtimex.Assert(ipAddr != nil, "invalid IP address")
epnt := &net.TCPAddr{IP: ipAddr, Port: int(port)}
server := testingx.MustNewTLSSNIProxyEx(
srv.logger,
&netxlite.Netx{Underlying: &netxlite.NetemUnderlyingNetworkAdapter{UNet: srv.unet}},
epnt,
)
// track this server as something to close later
srv.closers = append(srv.closers, server)
}
}