From 3f0014bb2cacf8d9e71981973f3ac4fb87ec24fd Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Tue, 6 Dec 2022 10:20:10 +0100 Subject: [PATCH] fix timesync; concurrent scan --- server/src/main.go | 24 +++++++++++++---- server/src/musique-bridge.go | 2 +- server/src/scaner.go | 51 +++++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/server/src/main.go b/server/src/main.go index 3b6d917..e5cfa1c 100644 --- a/server/src/main.go +++ b/server/src/main.go @@ -86,21 +86,35 @@ func main() { } defer outconn.Close() recvBuf := make([]byte, 1024) - _, err2 := outconn.Write([]byte("showtime")) + msg := []byte("showtime\n") + n, err2 := outconn.Write(msg) + if n != len(msg) { + fmt.Println("didn't send all the bytes") + os.Exit(1) + } if err2 != nil { fmt.Println(err) os.Exit(1) } - _, err3 := outconn.Read(recvBuf) - if err3 != nil { - fmt.Println(err) - os.Exit(1) + // Temporary fix, fixme pls + var read int + for { + var err3 error + read, err3 = outconn.Read(recvBuf) + if read > 2 { + break + } + if err3 != nil { + fmt.Println(err) + os.Exit(1) + } } _, err4 := conn.Write(recvBuf) if err4 != nil { fmt.Println(err) os.Exit(1) } + outconn.Close() } } if resp == "quit" { diff --git a/server/src/musique-bridge.go b/server/src/musique-bridge.go index 2b0e14c..2ab760b 100644 --- a/server/src/musique-bridge.go +++ b/server/src/musique-bridge.go @@ -13,7 +13,7 @@ func ServerInit() { //export ServerBeginProtocol func ServerBeginProtocol() { - protocol := []string { + protocol := []string{ "Make the plan", "Execute the plan", "Expect the plan to go off the rails", diff --git a/server/src/scaner.go b/server/src/scaner.go index 1a4dbef..2ff2268 100644 --- a/server/src/scaner.go +++ b/server/src/scaner.go @@ -3,37 +3,52 @@ package main import ( "fmt" "net" + "sync" "time" ) func scan() []string { var information []string + + var wg sync.WaitGroup + ips := make(chan string, 256) + ifaces, _ := net.Interfaces() - for _, i := range ifaces { - addrs, _ := i.Addrs() - for _, j := range addrs { - ipv4, _, _ := net.ParseCIDR(j.String()) + for _, iface := range ifaces { + addrs, _ := iface.Addrs() + for _, addr := range addrs { + ipv4, _, _ := net.ParseCIDR(addr.String()) + if ipv4.IsGlobalUnicast() && ipv4.To4() != nil { ipv4 = ipv4.To4() ipv4 = ipv4.Mask(ipv4.DefaultMask()) - ipv4[3]++ - for i := 1; i < 2; i++ { - _, dialErr := net.DialTimeout("tcp", ipv4.String()+":8081", time.Duration(1)*time.Second) - // _, dialErr := net.DialTimeout("tcp", "192.168.0.100:8081", time.Duration(1)*time.Second) - if dialErr != nil { - fmt.Println("Cannot connect to " + ipv4.String()) - // fmt.Println("Cannot connect to " + "192.168.0.100:8081") - } else { - fmt.Println("Response from " + ipv4.String()) - // fmt.Println("Response from " + "192.168.0.100:8081") - information = append(information, ipv4.String()) - // information = append(information, "192.168.0.100:8081") - } + + for i := 1; i < 255; i++ { + localIP := make([]byte, 4) + copy(localIP, ipv4) + wg.Add(1) + go func(ip net.IP) { + _, dialErr := net.DialTimeout("tcp", ip.String()+":8081", time.Duration(1)*time.Second) + if dialErr == nil { + ips <- ip.String() + ":8081" + } + wg.Done() + }(localIP) ipv4[3]++ } } } } - return information + go func() { + wg.Wait() + close(ips) + }() + + for ip := range ips { + fmt.Println("Response from " + ip) + information = append(information, ip) + } + + return information }