mirror of
https://github.com/OpenFactorioServerManager/factorio-server-manager.git
synced 2025-01-30 05:39:30 +02:00
started on http authentication
This commit is contained in:
parent
4a83c4b9ac
commit
0eb92ea6fe
39
auth.go
Normal file
39
auth.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/apexskier/httpauth"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
backend httpauth.LeveldbAuthBackend
|
||||
aaa httpauth.Authorizer
|
||||
}
|
||||
|
||||
func initAuth() *Auth {
|
||||
return &Auth{}
|
||||
}
|
||||
|
||||
func (auth *Auth) createRoles() {
|
||||
roles := make(map[string]httpauth.Role)
|
||||
|
||||
roles["user"] = 30
|
||||
roles["admin"] = 80
|
||||
auth.aaa = httpauth.NewAuthorizer(auth.backend, "topsecretkey", "user", roles)
|
||||
}
|
||||
|
||||
func (auth *Auth) createUser(username, role, password, email string) error {
|
||||
user := httpauth.UserData{Username: username, Role: role}
|
||||
err = backend.SaveUser(user)
|
||||
if err != nil {
|
||||
log.Printf("Error saving user: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err := auth.aaa.Update(nil, nil, username, password, email)
|
||||
if err != nil {
|
||||
log.Printf("Error saving user: %s", err)
|
||||
return err
|
||||
}
|
||||
}
|
6
conf.json
Normal file
6
conf.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"factorio_dir": "/home/mitch/.factorio",
|
||||
"username": "name",
|
||||
"password": "$111",
|
||||
"database_file": "auth.leveldb"
|
||||
}
|
54
main.go
54
main.go
@ -1,22 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
FactorioDir string
|
||||
FactorioSavesDir string
|
||||
FactorioModsDir string
|
||||
FactorioConfigFile string
|
||||
FactorioLog string
|
||||
FactorioBinary string
|
||||
ServerIP string
|
||||
ServerPort string
|
||||
MaxUploadSize int64
|
||||
FactorioDir string `json:"factorio_dir"`
|
||||
FactorioSavesDir string `json:"saves_dir"`
|
||||
FactorioModsDir string `json:"mods_dir"`
|
||||
FactorioConfigFile string `json:"config_file"`
|
||||
FactorioLog string `json:"logfile"`
|
||||
FactorioBinary string `json:"factorio_binary"`
|
||||
ServerIP string `json:"server_ip"`
|
||||
ServerPort string `json:"server_port"`
|
||||
MaxUploadSize int64 `json:"max_upload_size"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
DatabaseFile string `json:"database_file"`
|
||||
}
|
||||
|
||||
var (
|
||||
@ -24,7 +29,30 @@ var (
|
||||
FactorioServ *FactorioServer
|
||||
)
|
||||
|
||||
func loadFlags() {
|
||||
func failOnError(err error, msg string) {
|
||||
if err != nil {
|
||||
log.Printf("%s: %s", msg, err)
|
||||
panic(fmt.Sprintf("%s: %s", msg, err))
|
||||
}
|
||||
}
|
||||
|
||||
// Loads server configuration files
|
||||
// JSON config file contains default values,
|
||||
// any provided flags to the binary will overwrite values in config file.
|
||||
func loadServerConfig(f string) {
|
||||
file, err := os.Open(f)
|
||||
failOnError(err, "Error loading config file.")
|
||||
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&config)
|
||||
|
||||
auth := initAuth
|
||||
|
||||
fmt.Println(config.Password)
|
||||
parseFlags()
|
||||
}
|
||||
|
||||
func parseFlags() {
|
||||
factorioDir := flag.String("dir", "./", "Specify location of Factorio directory.")
|
||||
factorioIP := flag.String("host", "0.0.0.0", "Specify IP for webserver to listen on.")
|
||||
factorioPort := flag.String("port", "8080", "Specify a port for the server.")
|
||||
@ -35,18 +63,18 @@ func loadFlags() {
|
||||
flag.Parse()
|
||||
|
||||
config.FactorioDir = *factorioDir
|
||||
config.ServerIP = *factorioIP
|
||||
config.ServerPort = *factorioPort
|
||||
config.FactorioSavesDir = config.FactorioDir + "/saves"
|
||||
config.FactorioModsDir = config.FactorioDir + "/mods"
|
||||
config.FactorioConfigFile = config.FactorioDir + "/" + *factorioConfigFile
|
||||
config.FactorioBinary = config.FactorioDir + "/" + *factorioBinary
|
||||
config.ServerIP = *factorioIP
|
||||
config.ServerPort = *factorioPort
|
||||
config.FactorioLog = config.FactorioDir + "/factorio-current.log"
|
||||
config.MaxUploadSize = *factorioMaxUpload
|
||||
}
|
||||
|
||||
func main() {
|
||||
loadFlags()
|
||||
loadServerConfig("./conf.json")
|
||||
|
||||
FactorioServ = initFactorio()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user