forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpListener.go
45 lines (41 loc) · 1.06 KB
/
httpListener.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
package main
import (
"crypto/tls"
"errors"
"net"
"net/http"
"tailscale.com/client/tailscale"
)
func (a *goBlog) getTCPListener(serverAddr string) (net.Listener, error) {
if a.tailscaleEnabled() {
// Tailscale listener
return a.getTailscaleListener(serverAddr)
} else if serverAddr == ":443" && a.cfg.Server.PublicHTTPS {
m := a.getAutocertManager()
if m == nil {
return nil, errors.New("autocert not initialized")
}
return a.getAutocertManager().Listener(), nil
} else if serverAddr == ":443" && a.cfg.Server.TailscaleHTTPS {
// Listener with Tailscale TLS config
ln, err := net.Listen("tcp", serverAddr)
if err != nil {
return nil, err
}
tailscaleLC := &tailscale.LocalClient{}
return tls.NewListener(ln, &tls.Config{
GetCertificate: tailscaleLC.GetCertificate,
MinVersion: tls.VersionTLS12,
}), nil
} else {
// Default
return net.Listen("tcp", serverAddr)
}
}
func (a *goBlog) listenAndServe(s *http.Server) error {
listener, err := a.getTCPListener(s.Addr)
if err != nil {
return err
}
return s.Serve(listener)
}