Listen `0.0.0.0` only on IPv4

This commit is contained in:
YX Hao 2023-12-15 19:59:07 +08:00 committed by lifenjoiner
parent ad80d81d43
commit ac5087315c
4 changed files with 42 additions and 13 deletions

View File

@ -122,11 +122,15 @@ func addColdStartListener(
listenAddrStr string, listenAddrStr string,
captivePortalHandler *CaptivePortalHandler, captivePortalHandler *CaptivePortalHandler,
) error { ) error {
listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr) network := "udp"
if isDigit(listenAddrStr[0]) {
network = "udp4"
}
listenUDPAddr, err := net.ResolveUDPAddr(network, listenAddrStr)
if err != nil { if err != nil {
return err return err
} }
clientPc, err := net.ListenUDP("udp", listenUDPAddr) clientPc, err := net.ListenUDP(network, listenUDPAddr)
if err != nil { if err != nil {
return err return err
} }

View File

@ -166,6 +166,8 @@ func ReadTextFile(filename string) (string, error) {
return string(bin), nil return string(bin), nil
} }
func isDigit(b byte) bool { return b >= '0' && b <= '9' }
func maybeWritableByOtherUsers(p string) (bool, string, error) { func maybeWritableByOtherUsers(p string) (bool, string, error) {
p = path.Clean(p) p = path.Clean(p)
for p != "/" && p != "." { for p != "/" && p != "." {

View File

@ -274,8 +274,6 @@ func removeEDNS0Options(msg *dns.Msg) bool {
return true return true
} }
func isDigit(b byte) bool { return b >= '0' && b <= '9' }
func dddToByte(s []byte) byte { func dddToByte(s []byte) byte {
return byte((s[0]-'0')*100 + (s[1]-'0')*10 + (s[2] - '0')) return byte((s[0]-'0')*100 + (s[1]-'0')*10 + (s[2] - '0'))
} }

View File

@ -118,11 +118,17 @@ func (proxy *Proxy) registerLocalDoHListener(listener *net.TCPListener) {
} }
func (proxy *Proxy) addDNSListener(listenAddrStr string) { func (proxy *Proxy) addDNSListener(listenAddrStr string) {
listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr) udp := "udp"
tcp := "tcp"
if isDigit(listenAddrStr[0]) {
udp = "udp4"
tcp = "tcp4"
}
listenUDPAddr, err := net.ResolveUDPAddr(udp, listenAddrStr)
if err != nil { if err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
listenTCPAddr, err := net.ResolveTCPAddr("tcp", listenAddrStr) listenTCPAddr, err := net.ResolveTCPAddr(tcp, listenAddrStr)
if err != nil { if err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
@ -141,11 +147,11 @@ func (proxy *Proxy) addDNSListener(listenAddrStr string) {
// if 'userName' is set and we are the parent process // if 'userName' is set and we are the parent process
if !proxy.child { if !proxy.child {
// parent // parent
listenerUDP, err := net.ListenUDP("udp", listenUDPAddr) listenerUDP, err := net.ListenUDP(udp, listenUDPAddr)
if err != nil { if err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
listenerTCP, err := net.ListenTCP("tcp", listenTCPAddr) listenerTCP, err := net.ListenTCP(tcp, listenTCPAddr)
if err != nil { if err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
@ -186,7 +192,11 @@ func (proxy *Proxy) addDNSListener(listenAddrStr string) {
} }
func (proxy *Proxy) addLocalDoHListener(listenAddrStr string) { func (proxy *Proxy) addLocalDoHListener(listenAddrStr string) {
listenTCPAddr, err := net.ResolveTCPAddr("tcp", listenAddrStr) network := "tcp"
if isDigit(listenAddrStr[0]) {
network = "tcp4"
}
listenTCPAddr, err := net.ResolveTCPAddr(network, listenAddrStr)
if err != nil { if err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
@ -202,7 +212,7 @@ func (proxy *Proxy) addLocalDoHListener(listenAddrStr string) {
// if 'userName' is set and we are the parent process // if 'userName' is set and we are the parent process
if !proxy.child { if !proxy.child {
// parent // parent
listenerTCP, err := net.ListenTCP("tcp", listenTCPAddr) listenerTCP, err := net.ListenTCP(network, listenTCPAddr)
if err != nil { if err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
@ -442,7 +452,12 @@ func (proxy *Proxy) udpListenerFromAddr(listenAddr *net.UDPAddr) error {
if err != nil { if err != nil {
return err return err
} }
clientPc, err := listenConfig.ListenPacket(context.Background(), "udp", listenAddr.String()) listenAddrStr := listenAddr.String()
network := "udp"
if isDigit(listenAddrStr[0]) {
network = "udp4"
}
clientPc, err := listenConfig.ListenPacket(context.Background(), network, listenAddrStr)
if err != nil { if err != nil {
return err return err
} }
@ -456,7 +471,12 @@ func (proxy *Proxy) tcpListenerFromAddr(listenAddr *net.TCPAddr) error {
if err != nil { if err != nil {
return err return err
} }
acceptPc, err := listenConfig.Listen(context.Background(), "tcp", listenAddr.String()) listenAddrStr := listenAddr.String()
network := "tcp"
if isDigit(listenAddrStr[0]) {
network = "tcp4"
}
acceptPc, err := listenConfig.Listen(context.Background(), network, listenAddrStr)
if err != nil { if err != nil {
return err return err
} }
@ -470,7 +490,12 @@ func (proxy *Proxy) localDoHListenerFromAddr(listenAddr *net.TCPAddr) error {
if err != nil { if err != nil {
return err return err
} }
acceptPc, err := listenConfig.Listen(context.Background(), "tcp", listenAddr.String()) listenAddrStr := listenAddr.String()
network := "tcp"
if isDigit(listenAddrStr[0]) {
network = "tcp4"
}
acceptPc, err := listenConfig.Listen(context.Background(), network, listenAddrStr)
if err != nil { if err != nil {
return err return err
} }