mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-12-22 15:47:59 +08:00
fix: don't panic when listen on localhost
Some checks failed
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 14s
Some checks failed
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 14s
https://github.com/MetaCubeX/mihomo/issues/1655
This commit is contained in:
parent
fbead56ec9
commit
9de9f1ef51
@ -2,7 +2,9 @@ package inbound
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"sync"
|
||||
|
||||
"github.com/metacubex/mihomo/component/keepalive"
|
||||
@ -42,6 +44,27 @@ func MPTCP() bool {
|
||||
}
|
||||
|
||||
func ListenContext(ctx context.Context, network, address string) (net.Listener, error) {
|
||||
switch network { // like net.Resolver.internetAddrList but filter domain to avoid call net.Resolver.lookupIPAddr
|
||||
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6":
|
||||
if host, port, err := net.SplitHostPort(address); err == nil {
|
||||
switch host {
|
||||
case "localhost":
|
||||
switch network {
|
||||
case "tcp6", "udp6", "ip6":
|
||||
address = net.JoinHostPort("::1", port)
|
||||
default:
|
||||
address = net.JoinHostPort("127.0.0.1", port)
|
||||
}
|
||||
case "": // internetAddrList can handle this special case
|
||||
break
|
||||
default:
|
||||
if _, err := netip.ParseAddr(host); err != nil { // not ip
|
||||
return nil, fmt.Errorf("invalid network address: %s", address)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
return lc.Listen(ctx, network, address)
|
||||
|
Loading…
Reference in New Issue
Block a user