scanner fix, minor changes

This commit is contained in:
Mateusz Piątkowski 2022-11-29 21:15:39 +01:00
parent 80830ac363
commit 5eae7b3019
4 changed files with 44 additions and 12 deletions

1
lib/midi Submodule

@ -0,0 +1 @@
Subproject commit f42b663f0d08fc629c7deb26cd32ee06fba76d83

View File

@ -6,8 +6,8 @@ import (
"time" "time"
) )
func scan() string { func scan() []string {
information := "" var information []string
ifaces, _ := net.Interfaces() ifaces, _ := net.Interfaces()
for _, i := range ifaces { for _, i := range ifaces {
addrs, _ := i.Addrs() addrs, _ := i.Addrs()
@ -19,11 +19,15 @@ func scan() string {
ipv4[3]++ ipv4[3]++
for i := 1; i < 256; i++ { for i := 1; i < 256; 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)
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")
} else { } else {
fmt.Println("Response from " + ipv4.String()) fmt.Println("Response from " + ipv4.String())
information = fmt.Sprintf("%s\n%s", information, ipv4) // fmt.Println("Response from " + "192.168.0.100:8081")
information = append(information, ipv4.String())
// information = append(information, "192.168.0.100:8081")
} }
ipv4[3]++ ipv4[3]++
} }

View File

@ -2,11 +2,18 @@ package main
import ( import (
"bufio" "bufio"
"fmt"
"log" "log"
"net" "net"
"os" "os"
) )
func scanError(scanResult []string, conn net.Conn) {
if scanResult == nil {
conn.Write([]byte("Empty scan result, run 'scan' first.\n"))
}
}
func main() { func main() {
// HTTP part // HTTP part
@ -31,18 +38,45 @@ func main() {
go func(c net.Conn) { go func(c net.Conn) {
s := bufio.NewScanner(c) s := bufio.NewScanner(c)
conn.Write([]byte("> ")) conn.Write([]byte("> "))
var scanResult []string
for s.Scan() { for s.Scan() {
resp := s.Text() resp := s.Text()
if resp == "scan" { if resp == "scan" {
conn.Write([]byte("Scanning...\n")) conn.Write([]byte("Scanning...\n"))
conn.Write([]byte(scan() + "\n")) scanResult = scan()
conn.Write([]byte("Scanning done!\n"))
conn.Write([]byte("> "))
fmt.Println(len(scanResult))
continue continue
} }
if resp == "time" { if resp == "time" {
conn.Write([]byte(showTime().String() + "\n")) conn.Write([]byte(showTime().String() + "\n"))
conn.Write([]byte("> "))
continue
}
if resp == "hosts" {
scanError(scanResult, conn)
for _, host := range scanResult {
conn.Write([]byte(host + "\n"))
}
conn.Write([]byte("> "))
continue
} }
if resp == "timesync" { if resp == "timesync" {
conn.Write([]byte(showMonoTime().String() + "\n")) scanError(scanResult, conn)
for _, host := range scanResult {
if host == "" {
fmt.Print("No host")
}
fmt.Println(host)
//conn.Write([]byte(host + "\n"))
_, err := net.Dial("tcp", host)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
} }
if resp == "quit" { if resp == "quit" {
c.Close() c.Close()

View File

@ -8,10 +8,3 @@ func showTime() time.Time {
time := time.Now() time := time.Now()
return time return time
} }
func showMonoTime() time.Duration {
start := time.Now()
t := time.Now()
elapsed := t.Sub(start)
return elapsed
}