Skip to content

Commit d3c8b5e

Browse files
committed
Add factorio game server bind IP config, refactor config page
1 parent 7933d4f commit d3c8b5e

File tree

5 files changed

+116
-69
lines changed

5 files changed

+116
-69
lines changed

src/factorio_server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type FactorioServer struct {
2323
Cmd *exec.Cmd `json:"-"`
2424
Savefile string `json:"savefile"`
2525
Latency int `json:"latency"`
26+
BindIP string `json:"bindip"`
2627
Port int `json:"port"`
2728
Running bool `json:"running"`
2829
StdOut io.ReadCloser `json:"-"`
@@ -105,6 +106,7 @@ func (f *FactorioServer) Run() error {
105106
}
106107

107108
args := []string{
109+
"--bind", (f.BindIP),
108110
"--port", strconv.Itoa(f.Port),
109111
"--server-settings", filepath.Join(config.FactorioConfigDir, "server-settings.json"),
110112
"--rcon-port", strconv.Itoa(config.FactorioRconPort),
@@ -307,5 +309,3 @@ func (f *FactorioServer) Kill() error {
307309

308310
return nil
309311
}
310-
311-

src/handlers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ func StartServer(w http.ResponseWriter, r *http.Request) {
593593
resp.Success = false
594594
resp.Data = fmt.Sprintf("Error starting Factorio server: %s", "No save file provided")
595595
if err := json.NewEncoder(w).Encode(resp); err != nil {
596-
log.Printf("Error encoding config file JSON reponse: ", err)
596+
log.Printf("Error encoding config file JSON reponse: %s", err)
597597
}
598598
return
599599
}
@@ -667,7 +667,6 @@ func StopServer(w http.ResponseWriter, r *http.Request) {
667667
}
668668
}
669669

670-
671670
func KillServer(w http.ResponseWriter, r *http.Request) {
672671
resp := JSONResponse{
673672
Success: false,
@@ -715,6 +714,7 @@ func CheckServer(w http.ResponseWriter, r *http.Request) {
715714
status["status"] = "running"
716715
status["port"] = strconv.Itoa(FactorioServ.Port)
717716
status["savefile"] = FactorioServ.Savefile
717+
status["address"] = FactorioServ.BindIP
718718
resp.Data = status
719719
if err := json.NewEncoder(w).Encode(resp); err != nil {
720720
log.Printf("Error encoding config file JSON reponse: ", err)

src/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Config struct {
2121
FactorioBinary string `json:"factorio_binary"`
2222
FactorioRconPort int `json:"rcon_port"`
2323
FactorioRconPass string `json:"rcon_pass"`
24+
FactorioIP string `json:"factorio_ip"`
2425
ServerIP string `json:"server_ip"`
2526
ServerPort string `json:"server_port"`
2627
MaxUploadSize int64 `json:"max_upload_size"`
@@ -63,7 +64,8 @@ func loadServerConfig(f string) {
6364
func parseFlags() {
6465
confFile := flag.String("conf", "./conf.json", "Specify location of Factorio Server Manager config file.")
6566
factorioDir := flag.String("dir", "./", "Specify location of Factorio directory.")
66-
factorioIP := flag.String("host", "0.0.0.0", "Specify IP for webserver to listen on.")
67+
serverIP := flag.String("host", "0.0.0.0", "Specify IP for webserver to listen on.")
68+
factorioIP := flag.String("game-bind-address", "0.0.0.0", "Specify IP for Fcatorio gamer server to listen on.")
6769
factorioPort := flag.String("port", "8080", "Specify a port for the server.")
6870
factorioConfigFile := flag.String("config", "config/config.ini", "Specify location of Factorio config.ini file")
6971
factorioMaxUpload := flag.Int64("max-upload", 1024*1024*20, "Maximum filesize for uploaded files (default 20MB).")
@@ -73,7 +75,8 @@ func parseFlags() {
7375

7476
config.ConfFile = *confFile
7577
config.FactorioDir = *factorioDir
76-
config.ServerIP = *factorioIP
78+
config.ServerIP = *serverIP
79+
config.FactorioIP = *factorioIP
7780
config.ServerPort = *factorioPort
7881
config.FactorioSavesDir = filepath.Join(config.FactorioDir, "saves")
7982
config.FactorioModsDir = filepath.Join(config.FactorioDir, "mods")

ui/App/components/ConfigContent.jsx

Lines changed: 87 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@ class ConfigContent extends React.Component {
2121
this.getConfig();
2222
this.getServerSettings();
2323
}
24-
24+
2525
capitalizeFirstLetter(string) {
2626
return string.charAt(0).toUpperCase() + string.slice(1);
2727
}
2828

2929
handleServerSettingsChange(name, e) {
30+
let fieldValue
3031
var change = this.state.serverSettings;
31-
change[name] = e.target.value;
32+
33+
if (e.target.value === "true" || e.target.value === "false") {
34+
fieldValue = Boolean(e.target.value)
35+
} else {
36+
fieldValue = e.target.value
37+
}
38+
console.log(name, fieldValue)
39+
change[name] = fieldValue;
40+
3241
this.setState({serverSettings: change});
33-
console.log(this.state);
3442
}
35-
43+
3644
getConfig() {
3745
$.ajax({
3846
url: "/api/config",
@@ -63,7 +71,7 @@ class ConfigContent extends React.Component {
6371
}
6472
});
6573
}
66-
74+
6775
updateServerSettings(e) {
6876
e.preventDefault();
6977
var serverSettingsJSON = JSON.stringify(this.state.serverSettings)
@@ -75,7 +83,7 @@ class ConfigContent extends React.Component {
7583
success: (data) => {
7684
console.log(data);
7785
if (data.success === true) {
78-
console.log("settings updated")
86+
console.log("settings updated")
7987
}
8088
}
8189
})
@@ -84,7 +92,7 @@ class ConfigContent extends React.Component {
8492
formTypeField(key, setting) {
8593
if (key.startsWith("_comment_")) {
8694
return (
87-
<input
95+
<input
8896
key={key}
8997
ref={key}
9098
id={key}
@@ -93,58 +101,81 @@ class ConfigContent extends React.Component {
93101
/>
94102
)
95103
}
96-
if (typeof setting === "number") {
104+
105+
switch(typeof setting) {
106+
case "number":
97107
return (
98108
<input
99109
key={key}
100-
ref={key}
101-
id={key}
102-
className="form-control"
103-
defaultValue={setting}
104-
type="number"
105-
onChange={this.handleServerSettingsChange.bind(this, key)}
106-
/>
107-
)
108-
} else if (typeof setting === "string") {
109-
return (
110-
<input
111-
key={key}
112-
ref={key}
113-
id={key}
114-
className="form-control"
115-
defaultValue={setting}
116-
type="text"
110+
ref={key}
111+
id={key}
112+
className="form-control"
113+
defaultValue={setting}
114+
type="number"
117115
onChange={this.handleServerSettingsChange.bind(this, key)}
118116
/>
119117
)
120-
} else if (typeof setting === "boolean") {
118+
case "string":
119+
if (key.includes("password")) {
120+
return (
121+
<input
122+
key={key}
123+
ref={key}
124+
id={key}
125+
className="form-control"
126+
defaultValue={setting}
127+
type="password"
128+
onChange={this.handleServerSettingsChange.bind(this, key)}
129+
/>
130+
)
131+
} else {
132+
return (
133+
<input
134+
key={key}
135+
ref={key}
136+
id={key}
137+
className="form-control"
138+
defaultValue={setting}
139+
type="text"
140+
onChange={this.handleServerSettingsChange.bind(this, key)}
141+
/>
142+
)
143+
}
144+
case "boolean":
145+
console.log(key)
121146
return (
122147
<select key={key} ref={key} id={key} className="form-control" onChange={this.handleServerSettingsChange.bind(this, key)}>
123148
<option value={true}>True</option>
124149
<option value={false}>False</option>
125150
</select>
126151
)
127-
} else if (Array.isArray(setting)) {
128-
return (
129-
<input
130-
key={key}
131-
ref={key}
132-
id={key}
133-
className="form-control"
134-
defaultValue={setting}
135-
type="password"
136-
onChange={this.handleServerSettingsChange.bind(this, key)}
137-
/>
138-
)
139-
} else {
152+
case "object":
153+
if (Array.isArray(setting)) {
154+
return (
155+
<input
156+
key={key}
157+
ref={key}
158+
id={key}
159+
className="form-control"
160+
defaultValue={setting}
161+
type="text"
162+
onChange={this.handleServerSettingsChange.bind(this, key)}
163+
/>
164+
)
165+
} else {
166+
if (Object.keys(setting).length > 0) {
167+
//this.formTypeField(key, setting);
168+
}
169+
}
170+
default:
140171
return (
141-
<input
172+
<input
142173
key={key}
143-
ref={key}
144-
id={key}
145-
className="form-control"
146-
defaultValue={setting}
147-
type="text"
174+
ref={key}
175+
id={key}
176+
className="form-control"
177+
defaultValue={setting}
178+
type="text"
148179
onChange={this.handleServerSettingsChange.bind(this, key)}
149180
/>
150181
)
@@ -157,15 +188,15 @@ class ConfigContent extends React.Component {
157188
<div className="content-wrapper">
158189
<section className="content-header">
159190
<h1>
160-
Config
191+
Config
161192
<small>Manage game configuration</small>
162193
</h1>
163194
<ol className="breadcrumb">
164195
<li><IndexLink to="/"><i className="fa fa-dashboard"></i>Server Control</IndexLink></li>
165196
<li className="active">Here</li>
166197
</ol>
167198
</section>
168-
199+
169200
<section className="content">
170201
<div className="box">
171202
<div className="box-header">
@@ -185,13 +216,13 @@ class ConfigContent extends React.Component {
185216
var setting_key = this.capitalizeFirstLetter(key.replace(/_/g, " "))
186217
var comment = this.state.serverSettings["_comment_" + key]
187218
return(
188-
<div className="form-group">
189-
<label htmlFor={key} className="control-label col-md-3">{setting_key}</label>
190-
<div className="col-md-6">
191-
{this.formTypeField(key, setting)}
192-
<p className="help-block">{comment}</p>
193-
</div>
194-
</div>
219+
<div className="form-group">
220+
<label htmlFor={key} className="control-label col-md-3">{setting_key}</label>
221+
<div className="col-md-6">
222+
{this.formTypeField(key, setting)}
223+
<p className="help-block">{comment}</p>
224+
</div>
225+
</div>
195226
)
196227
}, this)}
197228
<div className="col-xs-6">
@@ -213,7 +244,7 @@ class ConfigContent extends React.Component {
213244
<div className="box-header">
214245
<h3 className="box-title">Game Configuration</h3>
215246
</div>
216-
247+
217248
<div className="box-body">
218249
<div className="row">
219250
<div className="col-md-10">
@@ -232,7 +263,7 @@ class ConfigContent extends React.Component {
232263
</thead>
233264
<Settings
234265
section={key}
235-
config={conf}
266+
config={conf}
236267
/>
237268
</table>
238269
</div>

ui/App/components/ServerCtl/ServerCtl.jsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ServerCtl extends React.Component {
1111
this.decrementPort = this.decrementPort.bind(this);
1212

1313
this.state = {
14+
gameBindIP: "0.0.0.0",
1415
savefile: "",
1516
port: 34197,
1617
}
@@ -19,6 +20,7 @@ class ServerCtl extends React.Component {
1920
startServer(e) {
2021
e.preventDefault();
2122
let serverSettings = {
23+
bindip: this.refs.gameBindIP.value,
2224
savefile: this.refs.savefile.value,
2325
port: Number(this.refs.port.value),
2426
}
@@ -83,14 +85,14 @@ class ServerCtl extends React.Component {
8385
let port = this.state.port - 1;
8486
this.setState({port: port})
8587
}
86-
88+
8789
render() {
8890
return(
8991
<div className="box">
9092
<div className="box-header">
9193
<h3 className="box-title">Server Control</h3>
9294
</div>
93-
95+
9496
<div className="box-body">
9597

9698
<form action="" onSubmit={this.startServer}>
@@ -99,7 +101,7 @@ class ServerCtl extends React.Component {
99101
<div className="col-md-4">
100102
<button className="btn btn-block btn-success" type="submit"><i className="fa fa-play fa-fw"></i>Start Factorio Server</button>
101103
</div>
102-
104+
103105
<div className="col-md-4">
104106
<button className="btn btn-block btn-warning" type="button" onClick={this.stopServer}><i className="fa fa-stop fa-fw"></i>Stop &amp; Save Factorio Server</button>
105107
</div>
@@ -115,8 +117,8 @@ class ServerCtl extends React.Component {
115117
{this.props.saves.map( (save, i) => {
116118
return(
117119
<option key={save.name} value={save.name}>{save.name}</option>
118-
)
119-
120+
)
121+
120122
})}
121123
</select>
122124
</div>
@@ -128,12 +130,23 @@ class ServerCtl extends React.Component {
128130
</div>
129131
</button>
130132
<div className="box-body" style={{display: "none"}}>
133+
<label htmlFor="port">Factorio Server IP</label>
134+
<div id="port" className="input-group">
135+
<input ref="gameBindIP"
136+
name="gameBindIP"
137+
id="gameBindIP"
138+
type="text"
139+
className="form-control"
140+
onChange={this.state.gameBindIP}
141+
value={this.state.gameBindIP}
142+
placeholder={this.state.gameBindIP} />
143+
</div>
131144
<label htmlFor="port">Factorio Server Port</label>
132145
<div id="port" className="input-group">
133146
<input ref="port" name="port" id="port" type="text" className="form-control" onChange={this.state.port} value={this.state.port} placeholder={this.state.port} />
134147
<div className="input-group-btn">
135-
<button type="button" className="btn btn-primary" onClick={this.incrementPort}><i className="fa fa-arrow-up"></i></button>
136-
<button type="button" className="btn btn-primary" onClick={this.decrementPort}><i className="fa fa-arrow-down"></i></button>
148+
<button type="button" className="btn btn-primary" onClick={this.incrementPort}><i className="fa fa-arrow-up"></i></button>
149+
<button type="button" className="btn btn-primary" onClick={this.decrementPort}><i className="fa fa-arrow-down"></i></button>
137150
</div>
138151
</div>
139152
</div>

0 commit comments

Comments
 (0)