Skip to content

Instantly share code, notes, and snippets.

@pjperez

pjperez/puncher.go

Last active Jun 23, 2018
Embed
What would you like to do?
UDP hole puncher PoC
package main
import (
"fmt"
"net"
"strconv"
"time"
)
//ServerIP defines the IP and port of the other peer
const ServerIP string = "1.2.3.4:10001"
func main() {
ServerAddr, err := net.ResolveUDPAddr("udp", ServerIP)
if err != nil {
fmt.Println(err)
}
LocalAddr, err := net.ResolveUDPAddr("udp", "0.0.0.0:10001")
if err != nil {
fmt.Println(err)
}
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
if err != nil {
fmt.Println(err)
}
defer Conn.Close()
i := 0
var recvbuf [1024]byte
for {
// Send data
msg := strconv.Itoa(i)
i++
sendbuf := []byte(msg)
_, err := Conn.Write(sendbuf)
if err != nil {
fmt.Println(err)
}
// Wait 1 second
time.Sleep(time.Second * 1)
// Receive data and print it on screen
rlen, _, err := Conn.ReadFromUDP(recvbuf[:])
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(recvbuf[:rlen]))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.