Skip to content

Commit

Permalink
Support binding specified IP
Browse files Browse the repository at this point in the history
  • Loading branch information
xlch88 committed Jan 4, 2024
1 parent 0f417a9 commit 5d69bce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
func main() {
var serverAddr = flag.String("s", stun.DefaultServerAddr, "STUN server address")
var localPort = flag.Int("p", 0, "The port on which to bind requests, set to 0 to pick a random port")
var localIP = flag.String("i", "", "The ip on which to bind requests, set to empty will use default")
var behaviorTestMode = flag.Bool("b", false, "Enable NAT behavior test mode")
var verboseLevel = flag.Int("v", 0, "Verbose level (0: none, 1: verbose, 2: double verbose, 3: triple verbose)")
flag.Parse()
Expand All @@ -39,6 +40,7 @@ func main() {
client := stun.NewClient()
client.SetServerAddr(*serverAddr)
client.SetLocalPort(*localPort)
client.SetLocalIP(*localIP)
client.SetVerbose(*verboseLevel >= 1)
client.SetVVerbose(*verboseLevel >= 2)

Expand Down
24 changes: 20 additions & 4 deletions stun/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
// to discover NAT type.
type Client struct {
serverAddr string
localIP string
localPort int
softwareName string
conn net.PacketConn
Expand Down Expand Up @@ -77,6 +78,11 @@ func (c *Client) SetLocalPort(port int) {
c.localPort = port
}

// SetLocalIP allows user to set the local ip address to send request.
func (c *Client) SetLocalIP(ip string) {
c.localIP = ip
}

// SetSoftwareName allows user to set the name of the software, which is used
// for logging purpose (NOT used in the current implementation).
func (c *Client) SetSoftwareName(name string) {
Expand All @@ -98,12 +104,18 @@ func (c *Client) Discover() (NATType, *Host, error) {
conn := c.conn
if conn == nil {
var laddr *net.UDPAddr
if c.localPort != 0 {
laddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", c.localPort))

if c.localPort != 0 || c.localIP != "" {
var address = fmt.Sprintf("%s:%d", c.localIP, c.localPort)

laddr, err = net.ResolveUDPAddr("udp", address)
if err != nil {
return NATError, nil, err
}

c.logger.Debugln("Local listen address: " + address)
}

conn, err = net.ListenUDP("udp", laddr)
if err != nil {
return NATError, nil, err
Expand All @@ -127,11 +139,15 @@ func (c *Client) BehaviorTest() (*NATBehavior, error) {
conn := c.conn
if conn == nil {
var laddr *net.UDPAddr
if c.localPort != 0 {
laddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", c.localPort))
if c.localPort != 0 || c.localIP != "" {
var address = fmt.Sprintf("%s:%d", c.localIP, c.localPort)

laddr, err = net.ResolveUDPAddr("udp", address)
if err != nil {
return nil, err
}

c.logger.Debugln("Local listen address: " + address)
}
conn, err = net.ListenUDP("udp", laddr)
if err != nil {
Expand Down

0 comments on commit 5d69bce

Please sign in to comment.