cmd/derper: listen on host of flag server addr for port 80 and 3478 (#2768)

cmd/derper: listen on host of flag server addr for port 80 and 3478

When using custom derp on the server with multiple IP addresses,
we would like to bind derp 80, 443 and stun 3478 to a certain IP.

derp command provides flag `-a` to customize which address to bind
for port 443. But port :80 and :3478 were hard-coded.

Fixes #2767

Signed-off-by: Li Chuangbo <im@chuangbo.li>
This commit is contained in:
Chuangbo Li 2021-09-03 01:42:27 +08:00 committed by GitHub
parent 4e18cca62e
commit e4e4d336d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -117,6 +117,11 @@ func main() {
tsweb.DevMode = true tsweb.DevMode = true
} }
listenHost, _, err := net.SplitHostPort(*addr)
if err != nil {
log.Fatalf("invalid server address: %v", err)
}
var logPol *logpolicy.Policy var logPol *logpolicy.Policy
if *logCollection != "" { if *logCollection != "" {
logPol = logpolicy.New(*logCollection) logPol = logpolicy.New(*logCollection)
@ -181,7 +186,7 @@ func main() {
debug.Handle("traffic", "Traffic check", http.HandlerFunc(s.ServeDebugTraffic)) debug.Handle("traffic", "Traffic check", http.HandlerFunc(s.ServeDebugTraffic))
if *runSTUN { if *runSTUN {
go serveSTUN() go serveSTUN(listenHost)
} }
httpsrv := &http.Server{ httpsrv := &http.Server{
@ -199,7 +204,6 @@ func main() {
WriteTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second,
} }
var err error
if letsEncrypt { if letsEncrypt {
if *certDir == "" { if *certDir == "" {
log.Fatalf("missing required --certdir flag") log.Fatalf("missing required --certdir flag")
@ -226,7 +230,7 @@ func main() {
} }
go func() { go func() {
port80srv := &http.Server{ port80srv := &http.Server{
Addr: ":80", // the default, but to be explicit Addr: net.JoinHostPort(listenHost, "80"),
Handler: certManager.HTTPHandler(tsweb.Port80Handler{Main: mux}), Handler: certManager.HTTPHandler(tsweb.Port80Handler{Main: mux}),
ReadTimeout: 30 * time.Second, ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second,
@ -248,8 +252,9 @@ func main() {
} }
} }
func serveSTUN() { func serveSTUN(host string) {
pc, err := net.ListenPacket("udp", ":3478")
pc, err := net.ListenPacket("udp", net.JoinHostPort(host, "3478"))
if err != nil { if err != nil {
log.Fatalf("failed to open STUN listener: %v", err) log.Fatalf("failed to open STUN listener: %v", err)
} }