-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
61 lines (53 loc) · 985 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"net"
"runtime"
)
func run(delay int, port int) {
addr := &net.UDPAddr{
IP: net.ParseIP("0.0.0.0"),
Port: port,
}
c, err := net.ListenUDP("udp4", addr)
if err != nil {
fmt.Print(err)
return
}
defer c.Close()
fmt.Printf("[*] Listening on port %d\n", port)
errs := make(chan error, 1)
for i := 0; i < runtime.GOMAXPROCS(0); i++ {
go listen(c, errs)
}
err = <-errs
fmt.Print(err)
}
func listen(c *net.UDPConn, errs chan<- error) {
buf := make([]byte, 1024)
err := error(nil)
for {
_, addr, err := c.ReadFromUDP(buf)
if err != nil {
break
}
fmt.Printf("[*] Connection from %s\n", addr.String())
data, err := getResponse(buf)
if err != nil {
break
}
_, err = c.WriteToUDP(data, addr)
if err != nil {
break
}
}
errs <- err
}
func getResponse(packet []byte) ([]byte, error) {
h, err := parseHeader(packet)
if err != nil {
return nil, err
}
r := h.getResponse()
return r.toBinary(), nil
}