diff --git a/app/dist/dist/css/factorio-server-manager.css b/app/dist/dist/css/factorio-server-manager.css index 062efa7..5cfd3fb 100755 --- a/app/dist/dist/css/factorio-server-manager.css +++ b/app/dist/dist/css/factorio-server-manager.css @@ -14,7 +14,7 @@ div.console-container { font-size: 0.85em; font-family: "Menlo", "Consolas", "DejaVu Sans Mono", monospace; - width: 60em; + width: 100%; max-width: 100%; box-sizing: border-box; height: 20em; @@ -25,4 +25,5 @@ div.console-container { div.console-prompt-box { color: #444; - max-width: 100%} + width: 100%; + max-width: 100%;} diff --git a/src/auth.go b/src/auth.go index 43441eb..34551f9 100644 --- a/src/auth.go +++ b/src/auth.go @@ -60,7 +60,7 @@ func (auth *AuthHTTP) CreateOrUpdateUser(username, password, role, email string) return err } - log.Printf("Created user: %s", user.Username) + log.Printf("Created/Updated user: %s", user.Username) return nil } diff --git a/src/factorio_server.go b/src/factorio_server.go index 9fbf593..ee8a03f 100644 --- a/src/factorio_server.go +++ b/src/factorio_server.go @@ -165,6 +165,19 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) { log.Printf("Error checking Factorio Server Error: %s", err) } } + // If rcon port is opened connect to rcon + rconLog := "Starting RCON interface at port " + strconv.Itoa(config.FactorioRconPort) + // check if slice index is greater than 2 to prevent panic + if len(line) > 2 { + // log line for opened rcon connection + if strings.Join(line[3:], " ") == rconLog { + log.Printf("Rcon running on Factorio Server") + err = connectRC() + if err != nil { + log.Printf("Error: %s", err) + } + } + } } if err := stdScanner.Err(); err != nil { log.Printf("Error reading std buffer: %s", err) @@ -174,6 +187,7 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) { } func (f *FactorioServer) checkLogError(logline []string) error { + // TODO Handle errors generated by running Factorio Server log.Println(logline) return nil diff --git a/src/handlers.go b/src/handlers.go index 190b464..17d69b7 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -615,6 +615,7 @@ func StartServer(w http.ResponseWriter, r *http.Request) { } else { log.Printf("Did not detect running Factorio server attempt: %+v", timeout) } + timeout++ } if FactorioServ.Running == false { @@ -644,6 +645,15 @@ func StopServer(w http.ResponseWriter, r *http.Request) { } return } + err = FactorioServ.Rcon.Close() + if err != nil { + log.Printf("Error closing rcon connection: %s", err) + resp.Data = fmt.Sprintf("Error in stop server handler: %s", err) + if err := json.NewEncoder(w).Encode(resp); err != nil { + log.Printf("Error encoding config file JSON reponse: ", err) + } + return + } log.Printf("Stopped Factorio server.") resp.Success = true resp.Data = fmt.Sprintf("Factorio server stopped") diff --git a/src/rcon.go b/src/rcon.go index 0d0b92d..6c0b1d2 100644 --- a/src/rcon.go +++ b/src/rcon.go @@ -1,33 +1,21 @@ package main import ( - "fmt" "log" + "strconv" - "github.com/james4k/rcon" + "github.com/majormjr/rcon" ) -func connectRC(addr, pass string) { - rc, err := rcon.Dial(addr, pass) +func connectRC() error { + var err error + rconAddr := config.ServerIP + ":" + strconv.Itoa(config.FactorioRconPort) + FactorioServ.Rcon, err = rcon.Dial(rconAddr, config.FactorioRconPass) if err != nil { - log.Printf("Error: %s", err) - } - defer rc.Close() - - req_id, err := rc.Write("Factorio Server Manager Connected") - if err != nil { - log.Printf("Error: %s", err) - } - - log.Printf("Establish RCON connection with request id: %s", req_id) - - for { - resp, req_id, err := rc.Read() - if err != nil { - log.Printf("Error: %s", err) - } - - fmt.Println(resp, req_id) + log.Printf("Cannot create rcon session: %s", err) + return err } + log.Printf("rcon session established on %s", rconAddr) + return nil } diff --git a/src/routes.go b/src/routes.go index a0daaa6..03779ef 100644 --- a/src/routes.go +++ b/src/routes.go @@ -58,6 +58,7 @@ func NewRouter() *mux.Router { Methods("GET"). Name("Websocket"). Handler(AuthorizeHandler(ws)) + ws.Handle("command send", commandSend) ws.Handle("log subscribe", logSubscribe) // Serves the frontend application from the app directory diff --git a/src/wsroutes.go b/src/wsroutes.go index ce28b1f..a3d44dd 100644 --- a/src/wsroutes.go +++ b/src/wsroutes.go @@ -7,13 +7,31 @@ import ( ) func logSubscribe(client *Client, data interface{}) { - t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: true}) - if err != nil { - log.Printf("Error subscribing to tail log %s", err) - return - } + go func() { + t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: true}) + if err != nil { + log.Printf("Error subscribing to tail log %s", err) + return + } - for line := range t.Lines { - client.send <- Message{"log update", line.Text} - } + for line := range t.Lines { + client.send <- Message{"log update", line.Text} + } + }() +} + +func commandSend(client *Client, data interface{}) { + go func() { + log.Printf("Received command: %v", data) + + req_id, err := FactorioServ.Rcon.Write(data.(string)) + if err != nil { + log.Printf("Error sending rcon command: %s", err) + return + } + + log.Printf("Command send to Factorio: %s, with rcon request id: %v", data, req_id) + + client.send <- Message{"receive command", data} + }() } diff --git a/ui/App/components/ConsoleContent.jsx b/ui/App/components/ConsoleContent.jsx index 89d71bd..9e53a6a 100644 --- a/ui/App/components/ConsoleContent.jsx +++ b/ui/App/components/ConsoleContent.jsx @@ -56,6 +56,8 @@ class ConsoleContent extends React.Component { if (e.key === "Enter") { var input_text = this.refs.term.value; + this.socket.emit("command send", input_text); + this.addHistory(this.state.prompt + " " + input_text); this.clearInput(); @@ -115,8 +117,8 @@ class ConsoleContent extends React.Component { {output}
- {this.state.prompt} - + {this.state.prompt} +