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.Options = &sessions.Options{
Path: "/",
Secure: true,
Secure: config.Secure,
}
auth.db, err = gorm.Open(sqlite.Open(config.SQLiteDatabaseFile), 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)
}

View File

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

View File

@ -210,11 +210,6 @@ func (room *wsRoom) run() {
LogCache = append(LogCache, message.Message.(string))
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
if len(LogCache) > config.ConsoleCacheSize {
LogCache = LogCache[1:]

View File

@ -59,9 +59,14 @@ type Config struct {
GlibcLibLoc string `json:"-"`
Autostart string `json:"-"`
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 {
var opts Flags
@ -69,7 +74,7 @@ func NewConfig(args []string) Config {
if err != nil {
failOnError(err, "Failed to parse arguments")
}
instantiated = mapFlags(opts)
instantiated.mapFlags(opts)
instantiated.loadServerConfig()
abs, err := filepath.Abs(instantiated.FactorioModPackDir)
@ -179,26 +184,24 @@ func randomPort() int {
return rand.Intn(5000) + 40000
}
func mapFlags(flags Flags) Config {
var config = Config{
Autostart: flags.Autostart,
GlibcCustom: flags.GlibcCustom,
GlibcLocation: flags.GlibcLocation,
GlibcLibLoc: flags.GlibcLibLoc,
ConfFile: flags.ConfFile,
FactorioDir: flags.FactorioDir,
ServerIP: flags.ServerIP,
ServerPort: flags.FactorioPort,
FactorioIP: flags.FactorioIP,
FactorioSavesDir: filepath.Join(flags.FactorioDir, "saves"),
FactorioModsDir: filepath.Join(flags.FactorioDir, "mods"),
FactorioModPackDir: flags.ModPackDir,
FactorioConfigDir: filepath.Join(flags.FactorioDir, "config"),
FactorioConfigFile: filepath.Join(flags.FactorioDir, flags.FactorioConfigFile),
FactorioCredentialsFile: "./factorio.auth",
FactorioAdminFile: "server-adminlist.json",
MaxUploadSize: flags.FactorioMaxUpload,
}
func (config *Config) mapFlags(flags Flags) {
config.Autostart = flags.Autostart
config.GlibcCustom = flags.GlibcCustom
config.GlibcLocation = flags.GlibcLocation
config.GlibcLibLoc = flags.GlibcLibLoc
config.ConfFile = flags.ConfFile
config.FactorioDir = flags.FactorioDir
config.ServerIP = flags.ServerIP
config.ServerPort = flags.FactorioPort
config.FactorioIP = flags.FactorioIP
config.FactorioSavesDir = filepath.Join(flags.FactorioDir, "saves")
config.FactorioModsDir = filepath.Join(flags.FactorioDir, "mods")
config.FactorioModPackDir = flags.ModPackDir
config.FactorioConfigDir = filepath.Join(flags.FactorioDir, "config")
config.FactorioConfigFile = filepath.Join(flags.FactorioDir, flags.FactorioConfigFile)
config.FactorioCredentialsFile = "./factorio.auth"
config.FactorioAdminFile = "server-adminlist.json"
config.MaxUploadSize = flags.FactorioMaxUpload
if filepath.IsAbs(flags.FactorioBinary) {
config.FactorioBinary = flags.FactorioBinary
@ -212,8 +215,6 @@ func mapFlags(flags Flags) Config {
} else {
config.FactorioLog = filepath.Join(config.FactorioDir, "factorio-current.log")
}
return config
}
func failOnError(err error, msg string) {

View File

@ -8,7 +8,7 @@ require (
github.com/golang/protobuf v1.3.1 // indirect
github.com/gorilla/mux v1.7.3
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/hpcloud/tail v1.0.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/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/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ=
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
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/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=

View File

@ -22,7 +22,9 @@ const Login = ({handleLogin}) => {
history.push('/');
}
} catch (e) {
console.log(e);
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() {
const socket = new WebSocket(ws_scheme + "://" + window.location.host + "/ws");
bus.on('log subscribe', () => {
function logSubscribeEvent() {
socket.send(
JSON.stringify(
{
@ -19,9 +19,9 @@ function connect() {
}
)
);
});
}
bus.on('log unsubscribe', () => {
function logUnsubscribeEvent() {
socket.send(
JSON.stringify(
{
@ -33,9 +33,9 @@ function connect() {
}
)
);
})
}
bus.on('server status subscribe', () => {
function serverStatusSubscribeEvent() {
socket.send(
JSON.stringify(
{
@ -47,9 +47,9 @@ function connect() {
}
)
);
});
}
bus.on('command send', command => {
function commandSendEvent(command) {
socket.send(
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 => {
const {room_name, message} = JSON.parse(e.data);
@ -73,9 +87,14 @@ function connect() {
}
socket.onclose = e => {
unregisterEventEmitter()
// reconnect after 5 seconds
setTimeout(connect, 5000);
}
socket.onopen = e => {
registerEventEmitter(socket)
}
}
connect();