2024-06-01 02:34:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2024-06-01 03:01:48 +00:00
|
|
|
"os"
|
2024-06-01 02:34:53 +00:00
|
|
|
"slices"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxPacksize = 1400
|
|
|
|
msgCtrlToken = 0x04
|
|
|
|
)
|
|
|
|
|
|
|
|
func ctrlToken(myToken []byte) []byte {
|
|
|
|
header := []byte{0x04, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff}
|
|
|
|
ctrlToken := append([]byte{0x05}, myToken...)
|
|
|
|
zeros := []byte{512: 0}
|
|
|
|
data := slices.Concat(header, ctrlToken, zeros)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2024-06-01 03:01:48 +00:00
|
|
|
func getConnection() (net.Conn, error) {
|
2024-06-01 02:34:53 +00:00
|
|
|
conn, err := net.Dial("udp", "127.0.0.1:8303")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Some error %v", err)
|
|
|
|
}
|
2024-06-01 03:01:48 +00:00
|
|
|
return conn, err
|
|
|
|
}
|
2024-06-01 02:34:53 +00:00
|
|
|
|
2024-06-01 03:01:48 +00:00
|
|
|
func readNetwork(ch chan []byte, conn net.Conn) {
|
|
|
|
packet := make([]byte, maxPacksize)
|
2024-06-01 02:34:53 +00:00
|
|
|
|
|
|
|
for {
|
2024-06-01 03:01:48 +00:00
|
|
|
_, err := bufio.NewReader(conn).Read(packet)
|
2024-06-01 02:34:53 +00:00
|
|
|
if err == nil {
|
|
|
|
ch <- packet
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Some error %v\n", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
2024-06-01 03:01:48 +00:00
|
|
|
func onMessage(data []byte, conn net.Conn) {
|
2024-06-01 02:34:53 +00:00
|
|
|
if data[0] == msgCtrlToken {
|
|
|
|
serverToken := data[8:12]
|
|
|
|
fmt.Printf("got token %v\n", serverToken)
|
2024-06-01 03:01:48 +00:00
|
|
|
conn.Write([]byte{0xff, 0xff, 0xff})
|
2024-06-01 02:34:53 +00:00
|
|
|
} else {
|
2024-06-01 03:01:48 +00:00
|
|
|
fmt.Printf("unknown message: %v\n", data)
|
2024-06-01 02:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ch := make(chan []byte, maxPacksize)
|
|
|
|
|
2024-06-01 03:01:48 +00:00
|
|
|
conn, err := getConnection()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error connecting %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
go readNetwork(ch, conn)
|
|
|
|
|
|
|
|
myToken := []byte{0x01, 0x02, 0x03, 0x04}
|
|
|
|
conn.Write(ctrlToken(myToken))
|
2024-06-01 02:34:53 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(10_000_000)
|
|
|
|
select {
|
|
|
|
case msg := <-ch:
|
2024-06-01 03:01:48 +00:00
|
|
|
onMessage(msg, conn)
|
2024-06-01 02:34:53 +00:00
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|