mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2026-04-22 22:57:04 +08:00
a74df99adb
Signed-off-by: Steffen Vogel <post@steffenvogel.de>
32 lines
725 B
Go
32 lines
725 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/pion/stun"
|
|
)
|
|
|
|
func main() {
|
|
// Creating a "connection" to STUN server.
|
|
c, err := stun.Dial("udp", "127.0.0.1:12345")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
// Building binding request with random transaction id.
|
|
message := stun.MustBuild(stun.TransactionID, stun.BindingRequest)
|
|
// Sending request to STUN server, waiting for response message.
|
|
if err := c.Do(message, func(res stun.Event) {
|
|
if res.Error != nil {
|
|
panic(res.Error)
|
|
}
|
|
// Decoding XOR-MAPPED-ADDRESS attribute from message.
|
|
var xorAddr stun.XORMappedAddress
|
|
if err := xorAddr.GetFrom(res.Message); err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println("your IP is", xorAddr.IP)
|
|
}); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|