Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support binding specified IP #51

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading