80 lines
1.4 KiB
Go
80 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
address = flag.String("a", "127.0.0.1:4242", "address (host and port)")
|
|
numClients = flag.Int("c", 10, "number of clients")
|
|
sleepTime = flag.Duration("s", time.Second, "sleep time for each client")
|
|
message = []byte{1, 0}
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i := 0; i < *numClients; i++ {
|
|
wg.Add(1)
|
|
go udpClient(&wg, i)
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func udpClient(wg *sync.WaitGroup, id int) {
|
|
defer wg.Done()
|
|
|
|
clientAddr, err := net.ResolveUDPAddr("udp", *address)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error resolving UDP address:", err)
|
|
return
|
|
}
|
|
|
|
conn, err := net.DialUDP("udp", nil, clientAddr)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error connecting to UDP server:", err)
|
|
return
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
loginRequest := [2]string{fmt.Sprintf("user%d", id+1), sha256Text("password")}
|
|
data, err := json.Marshal(loginRequest)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error creating JSON:", err)
|
|
return
|
|
}
|
|
|
|
request := append([]byte{2}, data...)
|
|
fmt.Println(string(request))
|
|
conn.Write(request)
|
|
|
|
for {
|
|
_, err := conn.Write(message)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error sending message:", err)
|
|
return
|
|
}
|
|
|
|
time.Sleep(*sleepTime)
|
|
}
|
|
}
|
|
|
|
func sha256Text(password string) string {
|
|
sum := sha256.Sum256([]byte(password))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|