-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathhttps.go
109 lines (89 loc) · 3 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
package netemx
import (
"io"
"net"
"net/http"
"sync"
"github.com/ooni/netem"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// HTTPSecureServerFactory implements [NetStackServerFactory] for HTTP-over-TLS (i.e., HTTPS).
//
// Use this factory along with [QAEnvOptionNetStack] to create HTTPS servers.
type HTTPSecureServerFactory struct {
// Factory is the MANDATORY factory for creating the [http.Handler].
Factory HTTPHandlerFactory
// Ports is the MANDATORY list of ports where to listen.
Ports []int
// ServerNameMain is the MANDATORY server name we should configure.
ServerNameMain string
// ServerNameExtras contains OPTIONAL extra server names we should configure.
ServerNameExtras []string
}
var _ NetStackServerFactory = &HTTPSecureServerFactory{}
// MustNewServer implements NetStackServerFactory.
func (f *HTTPSecureServerFactory) MustNewServer(env NetStackServerFactoryEnv, stack *netem.UNetStack) NetStackServer {
return &httpSecureServer{
closers: []io.Closer{},
env: env,
factory: f.Factory,
mu: sync.Mutex{},
ports: f.Ports,
serverNameMain: f.ServerNameMain,
serverNameExtras: f.ServerNameExtras,
unet: stack,
}
}
type httpSecureServer struct {
closers []io.Closer
env NetStackServerFactoryEnv
factory HTTPHandlerFactory
mu sync.Mutex
ports []int
serverNameMain string
serverNameExtras []string
unet *netem.UNetStack
}
// Close implements NetStackServer.
func (srv *httpSecureServer) Close() error {
// make the method locked as requested by the documentation
defer srv.mu.Unlock()
srv.mu.Lock()
// close each of the closers
for _, closer := range srv.closers {
_ = closer.Close()
}
// be idempotent
srv.closers = []io.Closer{}
return nil
}
// MustStart implements NetStackServer.
func (srv *httpSecureServer) MustStart() {
// make the method locked as requested by the documentation
defer srv.mu.Unlock()
srv.mu.Lock()
// create the handler
handler := srv.factory.NewHandler(srv.env, srv.unet)
// create the listening address
ipAddr := net.ParseIP(srv.unet.IPAddress())
runtimex.Assert(ipAddr != nil, "expected valid IP address")
for _, port := range srv.ports {
srv.mustListenPortLocked(handler, ipAddr, port)
}
}
func (srv *httpSecureServer) mustListenPortLocked(handler http.Handler, ipAddr net.IP, port int) {
// create the listening socket
addr := &net.TCPAddr{IP: ipAddr, Port: port}
listener := runtimex.Try1(srv.unet.ListenTCP("tcp", addr))
// create TLS config for the server name
tlsConfig := srv.unet.MustNewServerTLSConfig(srv.serverNameMain, srv.serverNameExtras...)
// serve requests in a background goroutine
srvr := &http.Server{ // #nosec G112 - just a testing server
Handler: handler,
TLSConfig: tlsConfig,
}
go srvr.ServeTLS(listener, "", "")
// make sure we track the server (the .Serve method will close the
// listener once we close the server itself)
srv.closers = append(srv.closers, srvr)
}