Merge pull request #249 from OpenFactorioServerManager/fix-auth-exception

fix redirect after login failed #247
This commit is contained in:
knoxfighter 2021-02-02 20:27:30 +01:00 committed by GitHub
commit 542aa96ec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 62 additions and 42 deletions

View File

@ -36,12 +36,12 @@ func SetupAuth() {
sessionStore = sessions.NewCookieStore(cookieEncryptionKey) sessionStore = sessions.NewCookieStore(cookieEncryptionKey)
sessionStore.Options = &sessions.Options{ sessionStore.Options = &sessions.Options{
Path: "/", Path: "/",
Secure: true, Secure: config.Secure,
} }
auth.db, err = gorm.Open(sqlite.Open(config.SQLiteDatabaseFile), nil) auth.db, err = gorm.Open(sqlite.Open(config.SQLiteDatabaseFile), nil)
if err != nil { if err != nil {
log.Printf("Error opening sqlite or goem database: %s", err) log.Printf("Error opening sqlite or gorm database: %s", err)
panic(err) panic(err)
} }

View File

@ -311,12 +311,15 @@ func LoadModsFromSaveHandler(w http.ResponseWriter, r *http.Request) {
var saveFileStruct struct { var saveFileStruct struct {
Name string `json:"saveFile"` Name string `json:"saveFile"`
} }
resp, err = ReadFromRequestBody(w, r, &saveFileStruct) resp, err = ReadFromRequestBody(w, r, &saveFileStruct)
if err != nil { if err != nil {
return return
} }
config := bootstrap.GetConfig() config := bootstrap.GetConfig()
path := filepath.Join(config.FactorioSavesDir, saveFileStruct.Name) path := filepath.Join(config.FactorioSavesDir, saveFileStruct.Name)
f, err := factorio.OpenArchiveFile(path, "level.dat") f, err := factorio.OpenArchiveFile(path, "level.dat")
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@ -210,11 +210,6 @@ func (room *wsRoom) run() {
LogCache = append(LogCache, message.Message.(string)) LogCache = append(LogCache, message.Message.(string))
config := bootstrap.GetConfig() config := bootstrap.GetConfig()
// Set ConsoleCacheSize to 25 if not set!
if config.ConsoleCacheSize == 0 {
config.ConsoleCacheSize = 25
}
// When cache is bigger than max size, delete one line // When cache is bigger than max size, delete one line
if len(LogCache) > config.ConsoleCacheSize { if len(LogCache) > config.ConsoleCacheSize {
LogCache = LogCache[1:] LogCache = LogCache[1:]

View File

@ -59,9 +59,14 @@ type Config struct {
GlibcLibLoc string `json:"-"` GlibcLibLoc string `json:"-"`
Autostart string `json:"-"` Autostart string `json:"-"`
ConsoleCacheSize int `json:"console_cache_size,omitempty"` // the amount of cached lines, inside the factorio output cache ConsoleCacheSize int `json:"console_cache_size,omitempty"` // the amount of cached lines, inside the factorio output cache
Secure bool `json:"secure"` // set to `false` to use this tool without SSL/TLS (Default: `true`)
} }
var instantiated Config // set Configs default values. JSON unmarshal will replace when it found something different
var instantiated = Config{
ConsoleCacheSize: 25,
Secure: true,
}
func NewConfig(args []string) Config { func NewConfig(args []string) Config {
var opts Flags var opts Flags
@ -69,7 +74,7 @@ func NewConfig(args []string) Config {
if err != nil { if err != nil {
failOnError(err, "Failed to parse arguments") failOnError(err, "Failed to parse arguments")
} }
instantiated = mapFlags(opts) instantiated.mapFlags(opts)
instantiated.loadServerConfig() instantiated.loadServerConfig()
abs, err := filepath.Abs(instantiated.FactorioModPackDir) abs, err := filepath.Abs(instantiated.FactorioModPackDir)
@ -179,26 +184,24 @@ func randomPort() int {
return rand.Intn(5000) + 40000 return rand.Intn(5000) + 40000
} }
func mapFlags(flags Flags) Config { func (config *Config) mapFlags(flags Flags) {
var config = Config{ config.Autostart = flags.Autostart
Autostart: flags.Autostart, config.GlibcCustom = flags.GlibcCustom
GlibcCustom: flags.GlibcCustom, config.GlibcLocation = flags.GlibcLocation
GlibcLocation: flags.GlibcLocation, config.GlibcLibLoc = flags.GlibcLibLoc
GlibcLibLoc: flags.GlibcLibLoc, config.ConfFile = flags.ConfFile
ConfFile: flags.ConfFile, config.FactorioDir = flags.FactorioDir
FactorioDir: flags.FactorioDir, config.ServerIP = flags.ServerIP
ServerIP: flags.ServerIP, config.ServerPort = flags.FactorioPort
ServerPort: flags.FactorioPort, config.FactorioIP = flags.FactorioIP
FactorioIP: flags.FactorioIP, config.FactorioSavesDir = filepath.Join(flags.FactorioDir, "saves")
FactorioSavesDir: filepath.Join(flags.FactorioDir, "saves"), config.FactorioModsDir = filepath.Join(flags.FactorioDir, "mods")
FactorioModsDir: filepath.Join(flags.FactorioDir, "mods"), config.FactorioModPackDir = flags.ModPackDir
FactorioModPackDir: flags.ModPackDir, config.FactorioConfigDir = filepath.Join(flags.FactorioDir, "config")
FactorioConfigDir: filepath.Join(flags.FactorioDir, "config"), config.FactorioConfigFile = filepath.Join(flags.FactorioDir, flags.FactorioConfigFile)
FactorioConfigFile: filepath.Join(flags.FactorioDir, flags.FactorioConfigFile), config.FactorioCredentialsFile = "./factorio.auth"
FactorioCredentialsFile: "./factorio.auth", config.FactorioAdminFile = "server-adminlist.json"
FactorioAdminFile: "server-adminlist.json", config.MaxUploadSize = flags.FactorioMaxUpload
MaxUploadSize: flags.FactorioMaxUpload,
}
if filepath.IsAbs(flags.FactorioBinary) { if filepath.IsAbs(flags.FactorioBinary) {
config.FactorioBinary = flags.FactorioBinary config.FactorioBinary = flags.FactorioBinary
@ -212,8 +215,6 @@ func mapFlags(flags Flags) Config {
} else { } else {
config.FactorioLog = filepath.Join(config.FactorioDir, "factorio-current.log") config.FactorioLog = filepath.Join(config.FactorioDir, "factorio-current.log")
} }
return config
} }
func failOnError(err error, msg string) { func failOnError(err error, msg string) {

View File

@ -8,7 +8,7 @@ require (
github.com/golang/protobuf v1.3.1 // indirect github.com/golang/protobuf v1.3.1 // indirect
github.com/gorilla/mux v1.7.3 github.com/gorilla/mux v1.7.3
github.com/gorilla/securecookie v1.1.1 github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.0 github.com/gorilla/sessions v1.2.1
github.com/gorilla/websocket v1.4.1 github.com/gorilla/websocket v1.4.1
github.com/hpcloud/tail v1.0.0 github.com/hpcloud/tail v1.0.0
github.com/jessevdk/go-flags v1.4.0 github.com/jessevdk/go-flags v1.4.0

View File

@ -18,8 +18,8 @@ github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ= github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=

View File

@ -22,7 +22,9 @@ const Login = ({handleLogin}) => {
history.push('/'); history.push('/');
} }
} catch (e) { } catch (e) {
console.log(e);
window.flash("Login failed. Username or Password wrong.", "red"); window.flash("Login failed. Username or Password wrong.", "red");
throw e;
} }
}; };

View File

@ -7,7 +7,7 @@ const ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
function connect() { function connect() {
const socket = new WebSocket(ws_scheme + "://" + window.location.host + "/ws"); const socket = new WebSocket(ws_scheme + "://" + window.location.host + "/ws");
bus.on('log subscribe', () => { function logSubscribeEvent() {
socket.send( socket.send(
JSON.stringify( JSON.stringify(
{ {
@ -19,9 +19,9 @@ function connect() {
} }
) )
); );
}); }
bus.on('log unsubscribe', () => { function logUnsubscribeEvent() {
socket.send( socket.send(
JSON.stringify( JSON.stringify(
{ {
@ -33,9 +33,9 @@ function connect() {
} }
) )
); );
}) }
bus.on('server status subscribe', () => { function serverStatusSubscribeEvent() {
socket.send( socket.send(
JSON.stringify( JSON.stringify(
{ {
@ -47,9 +47,9 @@ function connect() {
} }
) )
); );
}); }
bus.on('command send', command => { function commandSendEvent(command) {
socket.send( socket.send(
JSON.stringify( JSON.stringify(
{ {
@ -61,7 +61,21 @@ function connect() {
} }
) )
); );
}); }
function registerEventEmitter() {
bus.on('log subscribe', logSubscribeEvent);
bus.on('log unsubscribe', logUnsubscribeEvent);
bus.on('server status subscribe', serverStatusSubscribeEvent);
bus.on('command send', commandSendEvent);
}
function unregisterEventEmitter() {
bus.off('log subscribe', logSubscribeEvent);
bus.off('log unsubscribe', logUnsubscribeEvent);
bus.off('server status subscribe', serverStatusSubscribeEvent);
bus.off('command send', commandSendEvent);
}
socket.onmessage = e => { socket.onmessage = e => {
const {room_name, message} = JSON.parse(e.data); const {room_name, message} = JSON.parse(e.data);
@ -73,9 +87,14 @@ function connect() {
} }
socket.onclose = e => { socket.onclose = e => {
unregisterEventEmitter()
// reconnect after 5 seconds // reconnect after 5 seconds
setTimeout(connect, 5000); setTimeout(connect, 5000);
} }
socket.onopen = e => {
registerEventEmitter(socket)
}
} }
connect(); connect();