Added internet check and warning when offline

This commit is contained in:
2026-05-21 10:10:18 -06:00
parent 283ffb66eb
commit 93c4825ff7
4 changed files with 117 additions and 94 deletions

View File

@@ -8,6 +8,9 @@ import (
) )
func startStopServer(cmd string, con UsrConfig) { func startStopServer(cmd string, con UsrConfig) {
if hasInternet() != true {
fmt.Printf("WARNING!\n\tNot connected to internet, LAN access only\n")
}
switch cmd { switch cmd {
case "start": case "start":
x := fmt.Sprintf("%s --port %d --server-settings %s --start-server %s", con.Server.ServExec, con.Server.ServPort, con.Server.ServCfg, con.Server.WorldFile) x := fmt.Sprintf("%s --port %d --server-settings %s --start-server %s", con.Server.ServExec, con.Server.ServPort, con.Server.ServCfg, con.Server.WorldFile)

View File

@@ -9,6 +9,8 @@ import (
) )
func downloadMods(c UsrConfig) { func downloadMods(c UsrConfig) {
if hasInternet() == true {
//var c = readCfg("config.yml") //var c = readCfg("config.yml")
modlist := string(c.Server.ServDir) + "/mods/mod-list.json" modlist := string(c.Server.ServDir) + "/mods/mod-list.json"
@@ -80,5 +82,7 @@ func downloadMods(c UsrConfig) {
} }
} }
} else {
fmt.Println("Unable to access internet, please check connection.")
}
} }

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"archive/tar" "archive/tar"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
@@ -13,7 +14,7 @@ import (
) )
func downloadHeadless(c UsrConfig) { func downloadHeadless(c UsrConfig) {
//var c = readCfg("config.yml") if hasInternet() == true {
headlessQuery := "https://www.factorio.com/get-download/latest/headless/linux64" headlessQuery := "https://www.factorio.com/get-download/latest/headless/linux64"
url := headlessQuery url := headlessQuery
@@ -46,4 +47,7 @@ func downloadHeadless(c UsrConfig) {
} }
// zip does not preserve permissions correctly so i have to do this // zip does not preserve permissions correctly so i have to do this
exec.Command("chmod", "+x", c.Server.ServExec) exec.Command("chmod", "+x", c.Server.ServExec)
} else {
fmt.Println("Unable to access internet, please check connection.")
}
} }

View File

@@ -4,10 +4,12 @@ import (
"archive/zip" "archive/zip"
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
) )
//Utilities //Utilities
@@ -91,3 +93,13 @@ func unzip(src, dest string) ([]string, error) {
return filenames, nil return filenames, nil
} }
func hasInternet() bool {
timeout := 2 * time.Second
conn, err := net.DialTimeout("tcp", "8.8.8.8:53", timeout)
if err != nil {
return false
}
conn.Close()
return true
}