add handlers for received commands, improved initial rcon connection establishment

This commit is contained in:
majormjr 2016-12-21 18:10:14 -05:00 committed by Mitch Roote
parent 3f9fb6e2a1
commit fbd70507a0
8 changed files with 69 additions and 35 deletions

View File

@ -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%;}

View File

@ -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
}

View File

@ -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

View File

@ -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")

View File

@ -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
}

View File

@ -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

View File

@ -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}
}()
}

View File

@ -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}
</div>
<p>
<span className="console-prompt-box">{this.state.prompt}</span>
<input type="text" onKeyPress={this.handleInput} ref="term" />
<span className="console-prompt-box">{this.state.prompt}
<input type="text" onKeyPress={this.handleInput} ref="term" /></span>
</p>
</section>