timesync wip

This commit is contained in:
Mateusz Piątkowski 2022-12-05 21:50:30 +01:00
parent 5eae7b3019
commit 14cddb8051
3 changed files with 29 additions and 20 deletions

View File

@ -38,7 +38,3 @@ func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello!") fmt.Fprintf(w, "Hello!")
} }
func scanHandler(w http.ResponseWriter, r *http.Request) {
}

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"net" "net"
"os" "os"
"time"
) )
func scanError(scanResult []string, conn net.Conn) { func scanError(scanResult []string, conn net.Conn) {
@ -58,24 +59,36 @@ func main() {
scanError(scanResult, conn) scanError(scanResult, conn)
for _, host := range scanResult { for _, host := range scanResult {
conn.Write([]byte(host + "\n")) conn.Write([]byte(host + "\n"))
fmt.Println("CONNECTED")
} }
conn.Write([]byte("> ")) conn.Write([]byte("> "))
continue continue
} }
if resp == "showtime" {
cTime := showTime()
conn.Write([]byte(cTime.String() + "\n"))
}
if resp == "timesync" { if resp == "timesync" {
time.Sleep(1 * time.Second)
conn.Write([]byte("Server time: " + time.Now().String() + "\n"))
scanError(scanResult, conn) scanError(scanResult, conn)
for _, host := range scanResult { for _, host := range scanResult {
if host == "" { if host == "" {
fmt.Print("No host") fmt.Print("No host")
} }
fmt.Println(host) fmt.Println(host)
//conn.Write([]byte(host + "\n")) conn.Write([]byte("Waiting for time from " + host + "\n"))
_, err := net.Dial("tcp", host) outconn, err := net.Dial("tcp", host)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
defer outconn.Close()
recvBuf := make([]byte, 1024)
outconn.Write([]byte("showtime"))
outconn.Read(recvBuf)
conn.Write(recvBuf)
} }
} }
if resp == "quit" { if resp == "quit" {

View File

@ -17,20 +17,20 @@ func scan() []string {
ipv4 = ipv4.To4() ipv4 = ipv4.To4()
ipv4 = ipv4.Mask(ipv4.DefaultMask()) ipv4 = ipv4.Mask(ipv4.DefaultMask())
ipv4[3]++ ipv4[3]++
for i := 1; i < 256; i++ { // for i := 1; i < 2; i++ {
_, dialErr := net.DialTimeout("tcp", ipv4.String()+":8081", time.Duration(1)*time.Second) // _, 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) _, dialErr := net.DialTimeout("tcp", "192.168.0.100:8081", time.Duration(1)*time.Second)
if dialErr != nil { if dialErr != nil {
fmt.Println("Cannot connect to " + ipv4.String()) // fmt.Println("Cannot connect to " + ipv4.String())
// fmt.Println("Cannot connect to " + "192.168.0.100:8081") fmt.Println("Cannot connect to " + "192.168.0.100:8081")
} else { } else {
fmt.Println("Response from " + ipv4.String()) // fmt.Println("Response from " + ipv4.String())
// fmt.Println("Response from " + "192.168.0.100:8081") fmt.Println("Response from " + "192.168.0.100:8081")
information = append(information, ipv4.String()) // information = append(information, ipv4.String())
// information = append(information, "192.168.0.100:8081") information = append(information, "192.168.0.100:8081")
} }
ipv4[3]++ ipv4[3]++
} // }
} }
} }
} }