Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default port option for Factorio game server #356

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion conf.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"sq_lite_database_file": "sqlite.db",
"cookie_encryption_key": "",
"settings_file": "server-settings.json",
"log_file": "factorio-server-manager.log"
"log_file": "factorio-server-manager.log",
"factorio_port": 34197,
"factorio_port_lock": false,
}
7 changes: 6 additions & 1 deletion src/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,15 @@ func KillServer(w http.ResponseWriter, r *http.Request) {
}

func CheckServer(w http.ResponseWriter, r *http.Request) {
var serverInfo = factorio.GetFactorioServer()
defer func() {
WriteResponse(w, factorio.GetFactorioServer())
WriteResponse(w, serverInfo)
}()

config := bootstrap.GetConfig()
serverInfo.DefaultPort = config.FactorioPort
serverInfo.PortLock = config.FactorioPortLock

w.Header().Set("Content-Type", "application/json;charset=UTF-8")
}

Expand Down
8 changes: 6 additions & 2 deletions src/bootstrap/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ type Flags struct {
ConfFile string `long:"conf" default:"./conf.json" description:"Specify location of Factorio Server Manager config file." env:"FSM_CONF"`
FactorioDir string `long:"dir" default:"./" description:"Specify location of Factorio directory." env:"FSM_DIR"`
ServerIP string `long:"host" default:"0.0.0.0" description:"Specify IP for webserver to listen on." env:"FSM_SERVER_IP"`
ServerPort string `long:"port" default:"80" description:"Specify a port for the server." env:"FSM_PORT"`
FactorioIP string `long:"game-bind-address" default:"0.0.0.0" description:"Specify IP for Factorio game server to listen on." env:"FSM_FACTORIO_IP"`
FactorioPort string `long:"port" default:"80" description:"Specify a port for the server." env:"FSM_PORT"`
FactorioPort string `long:"game-port" default:"34197" description:"Default port for the Factorio game server." env:"FSM_FACTORIO_PORT"`
FactorioConfigFile string `long:"config" default:"config/config.ini" description:"Specify location of Factorio config.ini file." env:"FSM_FACTORIO_CONFIG_FILE"`
FactorioMaxUpload int64 `long:"max-upload" default:"20" description:"Maximum filesize for uploaded files in MB." env:"FSM_MAX_UPLOAD"`
FactorioBinary string `long:"bin" default:"bin/x64/factorio" description:"Location of Factorio Server binary file." env:"FSM_BINARY"`
Expand All @@ -46,6 +47,8 @@ type Config struct {
FactorioRconPass string `json:"rcon_pass,omitempty"`
FactorioCredentialsFile string `json:"factorio_credentials_file,omitempty"`
FactorioIP string `json:"factorio_ip,omitempty"`
FactorioPort string `json:"factorio_port,omitempty"`
FactorioPortLock bool `json:"factorio_port_lock,omitempty"`
FactorioAdminFile string `json:"factorio_admin_file,omitempty"`
ServerIP string `json:"server_ip,omitempty"`
ServerPort string `json:"server_port,omitempty"`
Expand Down Expand Up @@ -212,8 +215,9 @@ func (config *Config) mapFlags(flags Flags) {
config.ConfFile = flags.ConfFile
config.FactorioDir = flags.FactorioDir
config.ServerIP = flags.ServerIP
config.ServerPort = flags.FactorioPort
config.ServerPort = flags.ServerPort
config.FactorioIP = flags.FactorioIP
config.FactorioPort = flags.FactorioPort
config.FactorioSavesDir = filepath.Join(flags.FactorioDir, "saves")
config.FactorioModsDir = filepath.Join(flags.FactorioDir, "mods")
config.FactorioModPackDir = flags.ModPackDir
Expand Down
14 changes: 11 additions & 3 deletions src/factorio/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Server struct {
Latency int `json:"latency"`
BindIP string `json:"bindip"`
Port int `json:"port"`
DefaultPort string `json:"default_port"`
PortLock bool `json:"port_lock"`
Running bool `json:"running"`
Version Version `json:"fac_version"`
BaseModVersion string `json:"base_mod_version"`
Expand Down Expand Up @@ -249,9 +251,15 @@ func (server *Server) Run() error {
args = append(args, "--library-path", config.GlibcLibLoc, config.FactorioBinary, "--executable-path", config.FactorioBinary)
}

game_port := strconv.Itoa(server.Port)
if (config.FactorioPortLock) {
// Force the game to run with --game-port when "factorio_port_lock" is set to true in conf.json.
game_port = config.FactorioPort
}

args = append(args,
"--bind", server.BindIP,
"--port", strconv.Itoa(server.Port),
"--port", game_port,
"--server-settings", config.SettingsFile,
"--rcon-port", strconv.Itoa(config.FactorioRconPort),
"--rcon-password", config.FactorioRconPass)
Expand All @@ -265,7 +273,7 @@ func (server *Server) Run() error {
} else {
args = append(args, "--start-server", filepath.Join(config.FactorioSavesDir, server.Savefile))
}

// Write chat log to a different file if requested (if not it will be mixed-in with the default logfile)
if config.ChatLogFile != "" {
args = append(args, "--console-log", config.ChatLogFile)
Expand All @@ -278,7 +286,7 @@ func (server *Server) Run() error {
log.Println("Starting server with command: ", config.FactorioBinary, args)
server.Cmd = exec.Command(config.FactorioBinary, args...)
}

server.StdOut, err = server.Cmd.StdoutPipe()
if err != nil {
log.Printf("Error opening stdout pipe: %s", err)
Expand Down
2 changes: 2 additions & 0 deletions ui/App/components/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Input = ({
onKeyDown = () => null,
min = null,
value = undefined,
readOnly = false,
disabled = false
}) => {
return (
Expand All @@ -22,6 +23,7 @@ const Input = ({
defaultValue={defaultValue}
min={min}
value={value}
readonly={readOnly}
disabled={disabled}
/>
)
Expand Down
3 changes: 2 additions & 1 deletion ui/App/views/Controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ const Controls = ({serverStatus}) => {
<Input
type="number"
min={1}
defaultValue={"34197"}
defaultValue={serverStatus.default_port}
readOnly={serverStatus.port_freeze}
register={register('port',{required: true})}
/>
<Error error={errors.port} message="Port is required"/>
Expand Down