diff --git a/TCPProtocol/mnhv1.go b/TCPProtocol/mnhv1.go index 613898c..3703909 100644 --- a/TCPProtocol/mnhv1.go +++ b/TCPProtocol/mnhv1.go @@ -1,12 +1,14 @@ package TCPProtocol import ( + "errors" "net" "sync" "time" "github.com/hzyitc/mnh/TCPMode" "github.com/hzyitc/mnh/log" + "github.com/hzyitc/netutils" ) type mnhv1 struct { @@ -61,7 +63,12 @@ func (s *mnhv1) keepalive(duration time.Duration, timeout time.Duration) { } func NewMnhv1(m TCPMode.Interface, server string, id string) (Interface, error) { - conn, err := m.Dial(server) + addr, err := netutils.ResolveAddr("tcp", server, "mnhv1", 6641) + if err != nil { + return nil, err + } + + conn, err := m.Dial(addr.String()) if err != nil { return nil, err } @@ -80,10 +87,10 @@ func NewMnhv1(m TCPMode.Interface, server string, id string) (Interface, error) } msg := string(buf[:n]) - holeAddr, err := net.ResolveTCPAddr("tcp", msg) - if err != nil { + holeAddr := netutils.ParseAddr("", msg) + if holeAddr == nil { conn.Close() - return nil, err + return nil, errors.New("fail to parse \"" + msg + "\"") } s := &mnhv1{ diff --git a/UDPProtocol/mnhv1.go b/UDPProtocol/mnhv1.go index 021b0d8..2eefaf2 100644 --- a/UDPProtocol/mnhv1.go +++ b/UDPProtocol/mnhv1.go @@ -1,12 +1,14 @@ package UDPProtocol import ( + "errors" "net" "sync" "time" "github.com/hzyitc/mnh/UDPMode" "github.com/hzyitc/mnh/log" + "github.com/hzyitc/netutils" ) type mnhv1 struct { @@ -61,7 +63,12 @@ func (s *mnhv1) keepalive(duration time.Duration, timeout time.Duration) { } func NewMnhv1(m UDPMode.Interface, server string, id string) (Interface, error) { - conn, err := m.Dial(server) + addr, err := netutils.ResolveAddr("udp", server, "mnhv1", 6641) + if err != nil { + return nil, err + } + + conn, err := m.Dial(addr.String()) if err != nil { return nil, err } @@ -81,10 +88,10 @@ func NewMnhv1(m UDPMode.Interface, server string, id string) (Interface, error) } msg := string(buf[:n]) - holeAddr, err := net.ResolveUDPAddr("udp", msg) + holeAddr := netutils.ParseAddr("", msg) if err != nil { conn.Close() - return nil, err + return nil, errors.New("fail to parse \"" + msg + "\"") } s := &mnhv1{ diff --git a/go.mod b/go.mod index 38650ae..b254d4e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/hzyitc/mnh go 1.16 require ( + github.com/hzyitc/netutils v0.1.0 github.com/libp2p/go-reuseport v0.0.2 github.com/spf13/cobra v1.1.3 gitlab.com/NebulousLabs/go-upnp v0.0.0-20210414172302-67b91c9a5c03 diff --git a/go.sum b/go.sum index 041e6cd..8eff63c 100644 --- a/go.sum +++ b/go.sum @@ -89,6 +89,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/hzyitc/netutils v0.1.0 h1:jnOu0GOWUlGjGbz8P6bt5uFpFyB0v904r08Ke8vhLAI= +github.com/hzyitc/netutils v0.1.0/go.mod h1:DUOpn7k1pRfmS6bXThgrWVTyXHM8R7GCVfKnYQcq1U4= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=