From 479197f7d0124959b6190c793e7fd4f81fb551d8 Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Fri, 8 Nov 2019 18:39:10 +0000 Subject: [PATCH] Add register functionality Add gitignore --- .gitignore | 1 + config.gcfg | 1 + nethack-launcher.go | 86 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/config.gcfg b/config.gcfg index 5ec487c..d0cf04c 100644 --- a/config.gcfg +++ b/config.gcfg @@ -1,5 +1,6 @@ [nethacklauncher] loglevel = "debug" + serverdisplay = "unixvoid.com underground nethack server" recordlocation = "record" reclistlocation = "reclist" bootstrapdelay = 1 diff --git a/nethack-launcher.go b/nethack-launcher.go index c0a1723..39568e5 100644 --- a/nethack-launcher.go +++ b/nethack-launcher.go @@ -7,9 +7,11 @@ import ( "net/http" "os" "os/exec" + "strings" "time" "github.com/unixvoid/glogger" + "golang.org/x/crypto/sha3" "gopkg.in/gcfg.v1" "gopkg.in/redis.v5" ) @@ -17,6 +19,7 @@ import ( type Config struct { NethackLauncher struct { Loglevel string + ServerDisplay string RecordLocation string ReclistLocation string BootstrapDelay time.Duration @@ -111,7 +114,7 @@ func clearScreen() { func printWelcomeScreen(redisClient *redis.Client) string { clearScreen() - println("unixvoid.com underground nethack server") + fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) println("") println(" Not logged in.") println("") @@ -133,12 +136,14 @@ func printWelcomeScreen(redisClient *redis.Client) string { switch string(b) { case "l": // restart display - exec.Command("stty", "-F", "/dev/tty", "echo").Run() + exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() clearScreen() printLoginScreen(redisClient) case "r": + // restart display + exec.Command("stty", "-F", "/dev/tty", "echo", "-cbreak").Run() clearScreen() - return ("r") + printRegisterScreen(redisClient) case "w": clearScreen() return ("w") @@ -151,7 +156,7 @@ func printWelcomeScreen(redisClient *redis.Client) string { } func printLoginScreen(redisClient *redis.Client) { - println("unixvoid.com underground nethack server") + fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) println("") println(" Please enter your username. (blank entry aborts)") println("") @@ -165,6 +170,79 @@ func printLoginScreen(redisClient *redis.Client) { fmt.Printf("you entered '%s'\n", scanner.Text()) // check redis for user + usernameHash := sha3.Sum512([]byte(scanner.Text())) + _, err := redisClient.Get(fmt.Sprintf("user:%x", usernameHash)).Result() + if err != redis.Nil { + // user already exists + + } else { + // set up user + } + } + if err := scanner.Err(); err != nil { + fmt.Printf("%s\n", err) + } +} + +func printRegisterScreen(redisClient *redis.Client) { + // TODO : configure password restriction checking + fmt.Printf(" %s\n", config.NethackLauncher.ServerDisplay) + println("") + println(" Welcome new user. Please enter a username") + println(" Only characters and numbers are allowed, with no spaces.") + println(" 20 characters max.") + println("") + fmt.Printf(">> ") + + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + if scanner.Text() == "" { + printWelcomeScreen(redisClient) + } + + // check redis for user + usernameHash := sha3.Sum512([]byte(scanner.Text())) + _, err := redisClient.Get(fmt.Sprintf("user:%x", usernameHash)).Result() + if err != redis.Nil { + // user already exists + fmt.Printf("There was a problem with your last entry.\n>> ") + } else { + // set up user + + // turn off echo display + exec.Command("stty", "-F", "/dev/tty", "-echo").Run() + reader := bufio.NewReader(os.Stdin) + + noPass := true + sec := "" + for noPass { + // pull pass the first time + fmt.Printf(" Please enter your password (blank entry aborts).\n>> ") + sec0, _ := reader.ReadString('\n') + sec0 = strings.Replace(sec0, "\n", "", -1) + if sec0 == "" { + printWelcomeScreen(redisClient) + } + + // pull pass the second time + fmt.Printf("\n Please enter your password again.\n>> ") + sec1, _ := reader.ReadString('\n') + sec1 = strings.Replace(sec1, "\n", "", -1) + + // make sure passwords match + if sec0 == sec1 { + sec = sec0 + noPass = false + } else { + fmt.Println("Lets try that again.") + } + } + // set user in redis + secHash := sha3.Sum512([]byte(sec)) + redisClient.Set(fmt.Sprintf("user:%x", usernameHash), fmt.Sprintf("%x", secHash), 0).Err() + fmt.Println("Success") + os.Exit(0) + } } if err := scanner.Err(); err != nil { fmt.Printf("%s\n", err)