2024-01-20 21:18:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-26 22:38:49 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2024-01-20 21:18:58 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-25 11:10:29 +00:00
|
|
|
address = flag.String("a", "127.0.0.1:4242", "address (host and port)")
|
2024-01-20 21:18:58 +00:00
|
|
|
numClients = flag.Int("c", 10, "number of clients")
|
2024-01-21 00:21:58 +00:00
|
|
|
sleepTime = flag.Duration("s", time.Second, "sleep time for each client")
|
|
|
|
message = []byte{1, 0}
|
2024-01-20 21:18:58 +00:00
|
|
|
)
|
|
|
|
|
2024-02-26 22:38:49 +00:00
|
|
|
func main() {
|
2024-01-20 21:18:58 +00:00
|
|
|
flag.Parse()
|
2024-02-26 22:38:49 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
for i := 0; i < *numClients; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go udpClient(&wg, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2024-01-20 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 22:38:49 +00:00
|
|
|
func udpClient(wg *sync.WaitGroup, id int) {
|
2024-01-20 21:18:58 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2024-01-25 11:10:29 +00:00
|
|
|
clientAddr, err := net.ResolveUDPAddr("udp", *address)
|
2024-01-24 19:57:31 +00:00
|
|
|
|
2024-01-20 21:18:58 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error resolving UDP address:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.DialUDP("udp", nil, clientAddr)
|
2024-01-24 19:57:31 +00:00
|
|
|
|
2024-01-20 21:18:58 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error connecting to UDP server:", err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-24 19:57:31 +00:00
|
|
|
|
2024-01-20 21:18:58 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
2024-02-26 22:38:49 +00:00
|
|
|
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)
|
|
|
|
|
2024-01-20 21:18:58 +00:00
|
|
|
for {
|
|
|
|
_, err := conn.Write(message)
|
2024-01-21 00:21:58 +00:00
|
|
|
|
2024-01-20 21:18:58 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error sending message:", err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-21 00:21:58 +00:00
|
|
|
|
|
|
|
time.Sleep(*sleepTime)
|
2024-01-20 21:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 22:38:49 +00:00
|
|
|
func sha256Text(password string) string {
|
|
|
|
sum := sha256.Sum256([]byte(password))
|
|
|
|
return hex.EncodeToString(sum[:])
|
2024-01-20 21:18:58 +00:00
|
|
|
}
|