Skip to content

Commit

Permalink
Support SRV
Browse files Browse the repository at this point in the history
Fix some security bugs
  • Loading branch information
hzyitc committed Oct 18, 2021
1 parent ebf98f3 commit 10128dc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
15 changes: 11 additions & 4 deletions TCPProtocol/mnhv1.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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{
Expand Down
13 changes: 10 additions & 3 deletions UDPProtocol/mnhv1.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 10128dc

Please sign in to comment.