62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
)
|
|
|
|
func main() {
|
|
var addr, dest string
|
|
flag.StringVar(&addr, "addr", ":9999", "listen address")
|
|
flag.StringVar(&dest, "dest", "", "destination")
|
|
|
|
flag.Parse()
|
|
|
|
if dest == "" {
|
|
fmt.Println("Error: missing -dest")
|
|
fmt.Println("run with -h to see options.")
|
|
|
|
return
|
|
}
|
|
|
|
log.Printf("Listening to %s - Relaying to %s", addr, dest)
|
|
raddr, err := net.ResolveUDPAddr("udp", dest)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
udpConn, err := net.DialUDP("udp", nil, raddr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer udpConn.Close()
|
|
|
|
laddr, err := net.ResolveUDPAddr("udp", addr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
l, err := net.ListenUDP("udp", laddr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer l.Close()
|
|
|
|
buf := make([]byte, 1024)
|
|
for {
|
|
n, raddr, err := l.ReadFrom(buf)
|
|
log.Printf("received packet %d\n", n)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if _, err := udpConn.Write(buf[0:n]); err != nil {
|
|
log.Printf("error relaying data: %s\n", err)
|
|
}
|
|
|
|
log.Printf("%d bytes relayed from %s to destination.\n", n, raddr.String())
|
|
}
|
|
}
|