2022-11-23 22:29:10 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-11-29 21:15:39 +01:00
|
|
|
"fmt"
|
2022-11-23 22:29:10 +01:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2022-12-05 21:50:30 +01:00
|
|
|
"time"
|
2022-11-23 22:29:10 +01:00
|
|
|
)
|
|
|
|
|
2022-11-29 21:15:39 +01:00
|
|
|
func scanError(scanResult []string, conn net.Conn) {
|
|
|
|
if scanResult == nil {
|
|
|
|
conn.Write([]byte("Empty scan result, run 'scan' first.\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 22:29:10 +01:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
// HTTP part
|
|
|
|
// fileServer := http.FileServer(http.Dir("./static"))
|
|
|
|
// http.Handle("/", fileServer)
|
|
|
|
// http.HandleFunc("/cmd", cmdHandler)
|
|
|
|
// http.HandleFunc("/scan", scanHandler)
|
|
|
|
|
|
|
|
// http.HandleFunc("/hello", helloHandler)
|
|
|
|
|
|
|
|
// TCP part
|
|
|
|
l, err := net.Listen("tcp", ":8081")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
go func(c net.Conn) {
|
|
|
|
s := bufio.NewScanner(c)
|
|
|
|
conn.Write([]byte("> "))
|
2022-11-29 21:15:39 +01:00
|
|
|
var scanResult []string
|
2022-11-23 22:29:10 +01:00
|
|
|
for s.Scan() {
|
|
|
|
resp := s.Text()
|
|
|
|
if resp == "scan" {
|
|
|
|
conn.Write([]byte("Scanning...\n"))
|
2022-11-29 21:15:39 +01:00
|
|
|
scanResult = scan()
|
|
|
|
conn.Write([]byte("Scanning done!\n"))
|
|
|
|
conn.Write([]byte("> "))
|
|
|
|
fmt.Println(len(scanResult))
|
2022-11-23 22:29:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp == "time" {
|
|
|
|
conn.Write([]byte(showTime().String() + "\n"))
|
2022-11-29 21:15:39 +01:00
|
|
|
conn.Write([]byte("> "))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp == "hosts" {
|
|
|
|
scanError(scanResult, conn)
|
|
|
|
for _, host := range scanResult {
|
|
|
|
conn.Write([]byte(host + "\n"))
|
2022-12-05 21:50:30 +01:00
|
|
|
fmt.Println("CONNECTED")
|
2022-11-29 21:15:39 +01:00
|
|
|
}
|
|
|
|
conn.Write([]byte("> "))
|
|
|
|
continue
|
2022-11-23 22:29:10 +01:00
|
|
|
}
|
2022-12-05 21:50:30 +01:00
|
|
|
if resp == "showtime" {
|
|
|
|
cTime := showTime()
|
|
|
|
conn.Write([]byte(cTime.String() + "\n"))
|
|
|
|
}
|
|
|
|
|
2022-11-23 22:29:10 +01:00
|
|
|
if resp == "timesync" {
|
2022-12-05 21:50:30 +01:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
conn.Write([]byte("Server time: " + time.Now().String() + "\n"))
|
2022-11-29 21:15:39 +01:00
|
|
|
scanError(scanResult, conn)
|
|
|
|
for _, host := range scanResult {
|
|
|
|
if host == "" {
|
|
|
|
fmt.Print("No host")
|
|
|
|
}
|
|
|
|
fmt.Println(host)
|
2022-12-05 21:50:30 +01:00
|
|
|
conn.Write([]byte("Waiting for time from " + host + "\n"))
|
|
|
|
outconn, err := net.Dial("tcp", host)
|
2022-11-29 21:15:39 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-12-05 21:50:30 +01:00
|
|
|
defer outconn.Close()
|
|
|
|
recvBuf := make([]byte, 1024)
|
2022-12-06 10:20:10 +01:00
|
|
|
msg := []byte("showtime\n")
|
|
|
|
n, err2 := outconn.Write(msg)
|
|
|
|
if n != len(msg) {
|
|
|
|
fmt.Println("didn't send all the bytes")
|
2022-12-05 22:54:15 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-12-06 10:20:10 +01:00
|
|
|
if err2 != nil {
|
2022-12-05 22:54:15 +01:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-12-06 10:20:10 +01:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 22:54:15 +01:00
|
|
|
_, err4 := conn.Write(recvBuf)
|
|
|
|
if err4 != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-12-06 10:20:10 +01:00
|
|
|
outconn.Close()
|
2022-11-29 21:15:39 +01:00
|
|
|
}
|
2022-11-23 22:29:10 +01:00
|
|
|
}
|
|
|
|
if resp == "quit" {
|
|
|
|
c.Close()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
conn.Write([]byte("> "))
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}(conn)
|
|
|
|
}
|
|
|
|
}
|