fix: ipv{4,6}-only listen on wildcard address

fix: #797

when listening on a wildcard address like "0.0.0.0" or "[::]", hysteria
actually listened on both IPv4 and IPv6. this is a well-known bug of the
golang net package.

this commit introduces a fix for that, the intended behavior will be:

0.0.0.0:443 => listen on IPv4 only
[::]:443    => listen on IPv6 only
:443        => listen on both IPv4 and IPv6
This commit is contained in:
Haruue Icymoon
2023-11-26 16:09:01 +08:00
parent f48a5edd39
commit e70838cd98
4 changed files with 108 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ import (
"crypto/x509"
"encoding/hex"
"errors"
"github.com/apernet/hysteria/extras/correctnet"
"net"
"os"
"strconv"
@@ -504,7 +505,7 @@ func clientSOCKS5(config socks5Config, c client.Client) error {
if config.Listen == "" {
return configError{Field: "listen", Err: errors.New("listen address is empty")}
}
l, err := net.Listen("tcp", config.Listen)
l, err := correctnet.Listen("tcp", config.Listen)
if err != nil {
return configError{Field: "listen", Err: err}
}
@@ -529,7 +530,7 @@ func clientHTTP(config httpConfig, c client.Client) error {
if config.Listen == "" {
return configError{Field: "listen", Err: errors.New("listen address is empty")}
}
l, err := net.Listen("tcp", config.Listen)
l, err := correctnet.Listen("tcp", config.Listen)
if err != nil {
return configError{Field: "listen", Err: err}
}
@@ -562,7 +563,7 @@ func clientTCPForwarding(entries []tcpForwardingEntry, c client.Client) error {
if e.Remote == "" {
return configError{Field: "remote", Err: errors.New("remote address is empty")}
}
l, err := net.Listen("tcp", e.Listen)
l, err := correctnet.Listen("tcp", e.Listen)
if err != nil {
return configError{Field: "listen", Err: err}
}
@@ -589,7 +590,7 @@ func clientUDPForwarding(entries []udpForwardingEntry, c client.Client) error {
if e.Remote == "" {
return configError{Field: "remote", Err: errors.New("remote address is empty")}
}
l, err := net.ListenPacket("udp", e.Listen)
l, err := correctnet.ListenPacket("udp", e.Listen)
if err != nil {
return configError{Field: "listen", Err: err}
}