mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2025-08-12 06:26:39 +00:00
25 lines
582 B
Go
25 lines
582 B
Go
package util
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
)
|
|
|
|
// get the local ip and port based on our destination ip
|
|
func LocalIPPort(dstip net.IP) (net.IP, int) {
|
|
serverAddr, err := net.ResolveUDPAddr("udp", dstip.String()+":12345")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// We don't actually connect to anything, but we can determine
|
|
// based on our destination ip what source ip we should use.
|
|
if con, err := net.DialUDP("udp", nil, serverAddr); err == nil {
|
|
defer con.Close()
|
|
if udpaddr, ok := con.LocalAddr().(*net.UDPAddr); ok {
|
|
return udpaddr.IP, udpaddr.Port
|
|
}
|
|
}
|
|
return nil, -1
|
|
}
|