musique/server/src/server.go

91 lines
1.7 KiB
Go
Raw Normal View History

package main
import (
"bufio"
2022-11-29 21:15:39 +01:00
"fmt"
"log"
"net"
"os"
)
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"))
}
}
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
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))
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"))
}
conn.Write([]byte("> "))
continue
}
if resp == "timesync" {
2022-11-29 21:15:39 +01:00
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" {
c.Close()
os.Exit(0)
}
conn.Write([]byte("> "))
}
c.Close()
}(conn)
}
}