Skip to content

Commit fbd7050

Browse files
committed
add handlers for received commands, improved initial rcon connection establishment
1 parent 3f9fb6e commit fbd7050

File tree

8 files changed

+70
-36
lines changed

8 files changed

+70
-36
lines changed

app/dist/dist/css/factorio-server-manager.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
div.console-container {
1515
font-size: 0.85em;
1616
font-family: "Menlo", "Consolas", "DejaVu Sans Mono", monospace;
17-
width: 60em;
17+
width: 100%;
1818
max-width: 100%;
1919
box-sizing: border-box;
2020
height: 20em;
@@ -25,4 +25,5 @@ div.console-container {
2525

2626
div.console-prompt-box {
2727
color: #444;
28-
max-width: 100%}
28+
width: 100%;
29+
max-width: 100%;}

src/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (auth *AuthHTTP) CreateOrUpdateUser(username, password, role, email string)
6060
return err
6161
}
6262

63-
log.Printf("Created user: %s", user.Username)
63+
log.Printf("Created/Updated user: %s", user.Username)
6464

6565
return nil
6666
}

src/factorio_server.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) {
165165
log.Printf("Error checking Factorio Server Error: %s", err)
166166
}
167167
}
168+
// If rcon port is opened connect to rcon
169+
rconLog := "Starting RCON interface at port " + strconv.Itoa(config.FactorioRconPort)
170+
// check if slice index is greater than 2 to prevent panic
171+
if len(line) > 2 {
172+
// log line for opened rcon connection
173+
if strings.Join(line[3:], " ") == rconLog {
174+
log.Printf("Rcon running on Factorio Server")
175+
err = connectRC()
176+
if err != nil {
177+
log.Printf("Error: %s", err)
178+
}
179+
}
180+
}
168181
}
169182
if err := stdScanner.Err(); err != nil {
170183
log.Printf("Error reading std buffer: %s", err)
@@ -174,6 +187,7 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) {
174187
}
175188

176189
func (f *FactorioServer) checkLogError(logline []string) error {
190+
// TODO Handle errors generated by running Factorio Server
177191
log.Println(logline)
178192

179193
return nil

src/handlers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ func StartServer(w http.ResponseWriter, r *http.Request) {
615615
} else {
616616
log.Printf("Did not detect running Factorio server attempt: %+v", timeout)
617617
}
618+
618619
timeout++
619620
}
620621
if FactorioServ.Running == false {
@@ -644,6 +645,15 @@ func StopServer(w http.ResponseWriter, r *http.Request) {
644645
}
645646
return
646647
}
648+
err = FactorioServ.Rcon.Close()
649+
if err != nil {
650+
log.Printf("Error closing rcon connection: %s", err)
651+
resp.Data = fmt.Sprintf("Error in stop server handler: %s", err)
652+
if err := json.NewEncoder(w).Encode(resp); err != nil {
653+
log.Printf("Error encoding config file JSON reponse: ", err)
654+
}
655+
return
656+
}
647657
log.Printf("Stopped Factorio server.")
648658
resp.Success = true
649659
resp.Data = fmt.Sprintf("Factorio server stopped")

src/rcon.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
package main
22

33
import (
4-
"fmt"
54
"log"
5+
"strconv"
66

7-
"github.com/james4k/rcon"
7+
"github.com/majormjr/rcon"
88
)
99

10-
func connectRC(addr, pass string) {
11-
rc, err := rcon.Dial(addr, pass)
10+
func connectRC() error {
11+
var err error
12+
rconAddr := config.ServerIP + ":" + strconv.Itoa(config.FactorioRconPort)
13+
FactorioServ.Rcon, err = rcon.Dial(rconAddr, config.FactorioRconPass)
1214
if err != nil {
13-
log.Printf("Error: %s", err)
14-
}
15-
defer rc.Close()
16-
17-
req_id, err := rc.Write("Factorio Server Manager Connected")
18-
if err != nil {
19-
log.Printf("Error: %s", err)
20-
}
21-
22-
log.Printf("Establish RCON connection with request id: %s", req_id)
23-
24-
for {
25-
resp, req_id, err := rc.Read()
26-
if err != nil {
27-
log.Printf("Error: %s", err)
28-
}
29-
30-
fmt.Println(resp, req_id)
15+
log.Printf("Cannot create rcon session: %s", err)
16+
return err
3117
}
18+
log.Printf("rcon session established on %s", rconAddr)
3219

20+
return nil
3321
}

src/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func NewRouter() *mux.Router {
5858
Methods("GET").
5959
Name("Websocket").
6060
Handler(AuthorizeHandler(ws))
61+
ws.Handle("command send", commandSend)
6162
ws.Handle("log subscribe", logSubscribe)
6263

6364
// Serves the frontend application from the app directory

src/wsroutes.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@ import (
77
)
88

99
func logSubscribe(client *Client, data interface{}) {
10-
t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: true})
11-
if err != nil {
12-
log.Printf("Error subscribing to tail log %s", err)
13-
return
14-
}
15-
16-
for line := range t.Lines {
17-
client.send <- Message{"log update", line.Text}
18-
}
10+
go func() {
11+
t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: true})
12+
if err != nil {
13+
log.Printf("Error subscribing to tail log %s", err)
14+
return
15+
}
16+
17+
for line := range t.Lines {
18+
client.send <- Message{"log update", line.Text}
19+
}
20+
}()
21+
}
22+
23+
func commandSend(client *Client, data interface{}) {
24+
go func() {
25+
log.Printf("Received command: %v", data)
26+
27+
req_id, err := FactorioServ.Rcon.Write(data.(string))
28+
if err != nil {
29+
log.Printf("Error sending rcon command: %s", err)
30+
return
31+
}
32+
33+
log.Printf("Command send to Factorio: %s, with rcon request id: %v", data, req_id)
34+
35+
client.send <- Message{"receive command", data}
36+
}()
1937
}

ui/App/components/ConsoleContent.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class ConsoleContent extends React.Component {
5656
if (e.key === "Enter") {
5757
var input_text = this.refs.term.value;
5858

59+
this.socket.emit("command send", input_text);
60+
5961
this.addHistory(this.state.prompt + " " + input_text);
6062

6163
this.clearInput();
@@ -115,8 +117,8 @@ class ConsoleContent extends React.Component {
115117
{output}
116118
</div>
117119
<p>
118-
<span className="console-prompt-box">{this.state.prompt}</span>
119-
<input type="text" onKeyPress={this.handleInput} ref="term" />
120+
<span className="console-prompt-box">{this.state.prompt}
121+
<input type="text" onKeyPress={this.handleInput} ref="term" /></span>
120122
</p>
121123

122124
</section>

0 commit comments

Comments
 (0)