You've already forked factorio-server-manager
mirror of
https://github.com/OpenFactorioServerManager/factorio-server-manager.git
synced 2025-07-15 01:14:28 +02:00
add handlers for received commands, improved initial rcon connection establishment
This commit is contained in:
@ -14,7 +14,7 @@
|
|||||||
div.console-container {
|
div.console-container {
|
||||||
font-size: 0.85em;
|
font-size: 0.85em;
|
||||||
font-family: "Menlo", "Consolas", "DejaVu Sans Mono", monospace;
|
font-family: "Menlo", "Consolas", "DejaVu Sans Mono", monospace;
|
||||||
width: 60em;
|
width: 100%;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
height: 20em;
|
height: 20em;
|
||||||
@ -25,4 +25,5 @@ div.console-container {
|
|||||||
|
|
||||||
div.console-prompt-box {
|
div.console-prompt-box {
|
||||||
color: #444;
|
color: #444;
|
||||||
max-width: 100%}
|
width: 100%;
|
||||||
|
max-width: 100%;}
|
||||||
|
@ -60,7 +60,7 @@ func (auth *AuthHTTP) CreateOrUpdateUser(username, password, role, email string)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Created user: %s", user.Username)
|
log.Printf("Created/Updated user: %s", user.Username)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -165,6 +165,19 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) {
|
|||||||
log.Printf("Error checking Factorio Server Error: %s", err)
|
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 {
|
if err := stdScanner.Err(); err != nil {
|
||||||
log.Printf("Error reading std buffer: %s", err)
|
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 {
|
func (f *FactorioServer) checkLogError(logline []string) error {
|
||||||
|
// TODO Handle errors generated by running Factorio Server
|
||||||
log.Println(logline)
|
log.Println(logline)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -615,6 +615,7 @@ func StartServer(w http.ResponseWriter, r *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
log.Printf("Did not detect running Factorio server attempt: %+v", timeout)
|
log.Printf("Did not detect running Factorio server attempt: %+v", timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout++
|
timeout++
|
||||||
}
|
}
|
||||||
if FactorioServ.Running == false {
|
if FactorioServ.Running == false {
|
||||||
@ -644,6 +645,15 @@ func StopServer(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
return
|
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.")
|
log.Printf("Stopped Factorio server.")
|
||||||
resp.Success = true
|
resp.Success = true
|
||||||
resp.Data = fmt.Sprintf("Factorio server stopped")
|
resp.Data = fmt.Sprintf("Factorio server stopped")
|
||||||
|
32
src/rcon.go
32
src/rcon.go
@ -1,33 +1,21 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/james4k/rcon"
|
"github.com/majormjr/rcon"
|
||||||
)
|
)
|
||||||
|
|
||||||
func connectRC(addr, pass string) {
|
func connectRC() error {
|
||||||
rc, err := rcon.Dial(addr, pass)
|
var err error
|
||||||
|
rconAddr := config.ServerIP + ":" + strconv.Itoa(config.FactorioRconPort)
|
||||||
|
FactorioServ.Rcon, err = rcon.Dial(rconAddr, config.FactorioRconPass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error: %s", err)
|
log.Printf("Cannot create rcon session: %s", err)
|
||||||
}
|
return 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("rcon session established on %s", rconAddr)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,7 @@ func NewRouter() *mux.Router {
|
|||||||
Methods("GET").
|
Methods("GET").
|
||||||
Name("Websocket").
|
Name("Websocket").
|
||||||
Handler(AuthorizeHandler(ws))
|
Handler(AuthorizeHandler(ws))
|
||||||
|
ws.Handle("command send", commandSend)
|
||||||
ws.Handle("log subscribe", logSubscribe)
|
ws.Handle("log subscribe", logSubscribe)
|
||||||
|
|
||||||
// Serves the frontend application from the app directory
|
// Serves the frontend application from the app directory
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func logSubscribe(client *Client, data interface{}) {
|
func logSubscribe(client *Client, data interface{}) {
|
||||||
|
go func() {
|
||||||
t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: true})
|
t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error subscribing to tail log %s", err)
|
log.Printf("Error subscribing to tail log %s", err)
|
||||||
@ -16,4 +17,21 @@ func logSubscribe(client *Client, data interface{}) {
|
|||||||
for line := range t.Lines {
|
for line := range t.Lines {
|
||||||
client.send <- Message{"log update", line.Text}
|
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}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,8 @@ class ConsoleContent extends React.Component {
|
|||||||
if (e.key === "Enter") {
|
if (e.key === "Enter") {
|
||||||
var input_text = this.refs.term.value;
|
var input_text = this.refs.term.value;
|
||||||
|
|
||||||
|
this.socket.emit("command send", input_text);
|
||||||
|
|
||||||
this.addHistory(this.state.prompt + " " + input_text);
|
this.addHistory(this.state.prompt + " " + input_text);
|
||||||
|
|
||||||
this.clearInput();
|
this.clearInput();
|
||||||
@ -115,8 +117,8 @@ class ConsoleContent extends React.Component {
|
|||||||
{output}
|
{output}
|
||||||
</div>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
<span className="console-prompt-box">{this.state.prompt}</span>
|
<span className="console-prompt-box">{this.state.prompt}
|
||||||
<input type="text" onKeyPress={this.handleInput} ref="term" />
|
<input type="text" onKeyPress={this.handleInput} ref="term" /></span>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
Reference in New Issue
Block a user