fix timesync; concurrent scan

This commit is contained in:
Robert Bendun 2022-12-06 10:20:10 +01:00
parent 3f82212d27
commit 3f0014bb2c
3 changed files with 53 additions and 24 deletions

View File

@ -86,21 +86,35 @@ func main() {
} }
defer outconn.Close() defer outconn.Close()
recvBuf := make([]byte, 1024) 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 { if err2 != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
_, err3 := outconn.Read(recvBuf) // Temporary fix, fixme pls
var read int
for {
var err3 error
read, err3 = outconn.Read(recvBuf)
if read > 2 {
break
}
if err3 != nil { if err3 != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
}
_, err4 := conn.Write(recvBuf) _, err4 := conn.Write(recvBuf)
if err4 != nil { if err4 != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
outconn.Close()
} }
} }
if resp == "quit" { if resp == "quit" {

View File

@ -13,7 +13,7 @@ func ServerInit() {
//export ServerBeginProtocol //export ServerBeginProtocol
func ServerBeginProtocol() { func ServerBeginProtocol() {
protocol := []string { protocol := []string{
"Make the plan", "Make the plan",
"Execute the plan", "Execute the plan",
"Expect the plan to go off the rails", "Expect the plan to go off the rails",

View File

@ -3,37 +3,52 @@ package main
import ( import (
"fmt" "fmt"
"net" "net"
"sync"
"time" "time"
) )
func scan() []string { func scan() []string {
var information []string var information []string
var wg sync.WaitGroup
ips := make(chan string, 256)
ifaces, _ := net.Interfaces() ifaces, _ := net.Interfaces()
for _, i := range ifaces { for _, iface := range ifaces {
addrs, _ := i.Addrs() addrs, _ := iface.Addrs()
for _, j := range addrs { for _, addr := range addrs {
ipv4, _, _ := net.ParseCIDR(j.String()) ipv4, _, _ := net.ParseCIDR(addr.String())
if ipv4.IsGlobalUnicast() && ipv4.To4() != nil { if ipv4.IsGlobalUnicast() && ipv4.To4() != nil {
ipv4 = ipv4.To4() ipv4 = ipv4.To4()
ipv4 = ipv4.Mask(ipv4.DefaultMask()) 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")
}
ipv4[3]++
}
}
}
}
return information
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]++
}
}
}
}
go func() {
wg.Wait()
close(ips)
}()
for ip := range ips {
fmt.Println("Response from " + ip)
information = append(information, ip)
}
return information
} }