Skip to content

Commit

Permalink
Remove hardcoded passwords and use an external password file for the …
Browse files Browse the repository at this point in the history
…server part
  • Loading branch information
axelberardino committed Aug 11, 2019
1 parent f495df2 commit 56efbfa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
poe-stash
data/*.html
data/cache/*
pass.txt
1 change: 0 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ go get -u github.com/gin-gonic/gin
TODO
More rigourous item description generation
Shop id for the link after shop generation
Don't hardcode account passwords!
Search bar?
56 changes: 50 additions & 6 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"bufio"
"flag"
"fmt"
"html/template"
"io"
"os"
"strings"

"github.com/gin-gonic/gin"

Expand All @@ -13,7 +17,7 @@ import (
)

// setupRouter setups the http server and all its pages.
func setupRouter() *gin.Engine {
func setupRouter(passwords map[string]string) *gin.Engine {
router := gin.Default()

t := template.Must(generate.LoadAllTemplates())
Expand All @@ -24,21 +28,61 @@ func setupRouter() *gin.Engine {
router.GET("/", page.MainPageHandler)
router.GET("/view/:account", page.ViewAccountHandler)

authorized := router.Group("/", gin.BasicAuth(gin.Accounts{
"***": "***",
"****": "****",
}))
authorized := router.Group("/", gin.BasicAuth(passwords))
authorized.GET("/gen/:account", page.GenAccountHandler)

return router
}

// loadPasswords load passwords from a given file.
// Format is:
// login:pass
// login:pass
// login:pass
// ...
func loadPasswords(filename string) (r map[string]string, mainErr error) {
res := make(map[string]string, 2)
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer func() {
err := file.Close()
if err == nil {
mainErr = err
}
}()

reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
parts := strings.Split(string(line), ":")
// Invalid line is skipped
if len(parts) != 2 {
fmt.Println("Skipped invalid line:", string(line))
} else {
res[parts[0]] = parts[1]
}
}

return res, nil
}

// main is the main routine which launch the http server.
// This server allows to generate and view account characters,
// stash and items for given users.
func main() {
port := flag.Int("port", 2121, "port")
passwordFile := flag.String("passwords", "./pass.txt", "password file (containing login:pass in plain text)")
flag.Parse()
r := setupRouter()

passwords, err := loadPasswords(*passwordFile)
if err != nil {
panic(err)
}
r := setupRouter(passwords)
r.Run(fmt.Sprintf(":%d", *port))
}

0 comments on commit 56efbfa

Please sign in to comment.