From e043d7a4772e08ce158645199c7dc14908589e51 Mon Sep 17 00:00:00 2001 From: Nikolaus Gotsche Date: Sat, 30 Sep 2017 01:36:55 +0200 Subject: redis functions renaming --- hexfool.go | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ log.go | 2 +- redigo.go | 51 +++++++++++++++++++++-- signup.go | 118 ----------------------------------------------------- 4 files changed, 183 insertions(+), 123 deletions(-) create mode 100644 hexfool.go delete mode 100644 signup.go diff --git a/hexfool.go b/hexfool.go new file mode 100644 index 0000000..2da7de7 --- /dev/null +++ b/hexfool.go @@ -0,0 +1,135 @@ +package main + +import "database/sql" +import _ "github.com/go-sql-driver/mysql" + +import "golang.org/x/crypto/bcrypt" + +import "net/http" +//import "fmt" +import "os/exec" + +var db *sql.DB +var err error + +func signupPage(res http.ResponseWriter, req *http.Request) { + if req.Method != "POST" { + http.ServeFile(res, req, "html/signup.html") + return + } + + username := req.FormValue("username") + password := req.FormValue("password") + email := req.FormValue("email") + var user string + + err := db.QueryRow("SELECT username FROM players WHERE username=?",username).Scan(&user) + + switch { + case err == sql.ErrNoRows: + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + http.Error(res, "Server Error creating Password. Unable to create account! You Fool...", 500) + Log("ERROR 500 - Hash generation Failed") + panic(err) + return + } + newuuid, uiderr := exec.Command("uuidgen").Output() + if uiderr != nil{ + http.Error(res, "Server Error creating UUID. Unable to create account! You Fool...", 500) + Log("ERROR 500 - Creating UUID failed") + panic(uiderr) + return + } + + _, err = db.Exec("INSERT INTO players(username, password, email) VALUES(?, ?, ?)", username, hashedPassword, email) + if err != nil { + http.Error(res, "Server Error Inserting User. Unable to create account! You Fool...", 500) + Log("ERROR adding user"+username) + panic(err) + //fmt.Println("Error adding User",username) + return + } + res.Write([]byte("User Created Successfully!")) + Log("Successfully Created User"+username+string(newuuid)) + //fmt.Println("Successfully Created User",username,newuuid) + return + case err != nil: + http.Error(res, "Server Error! Something fucked up", 500) + Log("ERROR Something fucked up") + panic(err) + return + default: + http.Redirect(res, req, "/", 301) + } +} + +func loginPage(res http.ResponseWriter, req *http.Request) { + if req.Method != "POST" { + http.ServeFile(res, req, "html/login.html") + return + } + + username := req.FormValue("username") + password := req.FormValue("password") + + var databaseUsername string + var databasePassword string +// var databaseUUID string + + err := db.QueryRow("SELECT username, password FROM players WHERE username=?", username).Scan(&databaseUsername, &databasePassword) + + if err != nil { + http.Redirect(res, req, "/login", 301) + Log("Failed Login atempt by "+username) + //fmt.Println("Failed Login atempt by",username) + return + } + + err = bcrypt.CompareHashAndPassword([]byte(databasePassword), []byte(password)) + if err != nil { + http.Redirect(res, req, "/login", 301) + Log(username+" used wrong Password "+password) + panic(err) + //fmt.Println(username," used wrong Password ",password) + return + } + tok, uiderr := getToken() + if uiderr != nil{ + http.Error(res, "Server Error creating Token. You Fool...", 500) + Log("ERROR 500 - Creating UUID failed") + panic(uiderr) + return + } + + res.Write([]byte("Hello "+ databaseUsername + "! Your token is "+ string(tok))) + Log("Successful Login by "+username) + //fmt.Println("Successful Login by",username) +} + +func getToken() ([]byte, error) { + newuuid, uiderr := exec.Command("uuidgen").Output() + return newuuid, uiderr +} + +func homePage(res http.ResponseWriter, req *http.Request) { + http.ServeFile(res, req, "html/index.html") +} + +func main() { + db, err = sql.Open("mysql", "hexmaster:waSIdocHneTdUobeRoarscH@tcp(192.168.0.16)/hexmaster") + if err != nil { + panic(err.Error()) + } + defer db.Close() + + err = db.Ping() + if err != nil { + panic(err.Error()) + } + + http.HandleFunc("/signup", signupPage) + http.HandleFunc("/login", loginPage) + http.HandleFunc("/", homePage) + http.ListenAndServe(":8088", nil) +} diff --git a/log.go b/log.go index 365989b..976ce91 100644 --- a/log.go +++ b/log.go @@ -21,7 +21,7 @@ func Log(text string) { panic(err) } if loud { - fmt.Println(full) + fmt.Println("Log",full) } } diff --git a/redigo.go b/redigo.go index ff1bf3f..f83b174 100644 --- a/redigo.go +++ b/redigo.go @@ -3,8 +3,47 @@ package main import ( "github.com/go-redis/redis" "fmt" + "time" ) +var client *redis.Client + +func init() { + client = redis.NewClient(&redis.Options{ + Addr: "192.168.0.16:6379", + Password: "", // no password set + DB: 0, // use default DB + + // DialTimeout: 10 * time.Second, + // ReadTimeout: 30 * time.Second, + // WriteTimeout: 30 * time.Second, + // PoolSize: 10, + // PoolTimeout: 30 * time.Second, + }) + //client.FlushDB() +} + +func getRed(key string) string { + val, err := client.Get(key).Result() + if err == redis.Nil { + return "nil" + } else if err != nil { + panic(err) + return err.Error() + } else { + return val + } +} + +func setRed(key string , val string, dur int) bool { + err := client.Set(key, val,time.Duration(dur)*time.Second).Err() + if err != nil { + panic(err) + return false + } + return true +} + func ExampleNewClient() { client := redis.NewClient(&redis.Options{ Addr: "192.168.0.16:6379", @@ -17,13 +56,14 @@ func ExampleNewClient() { // Output: PONG } + func ExampleClient() { client := redis.NewClient(&redis.Options{ Addr: "192.168.0.16:6379", Password: "", // no password set DB: 0, // use default DB }) - err := client.Set("key", "value", 0).Err() + err := client.Set("zack", "kack", 0).Err() if err != nil { panic(err) } @@ -46,6 +86,9 @@ func ExampleClient() { // key2 does not exists } -func main() { - ExampleNewClient() -} +//func main() { + //ExampleClient() +// if setRed("scheisse","geil",10) { +// fmt.Println("euda") +// } +//} diff --git a/signup.go b/signup.go deleted file mode 100644 index 1285c41..0000000 --- a/signup.go +++ /dev/null @@ -1,118 +0,0 @@ -package main - -import "database/sql" -import _ "github.com/go-sql-driver/mysql" - -import "golang.org/x/crypto/bcrypt" - -import "net/http" -//import "fmt" -import "os/exec" - -var db *sql.DB -var err error - -func signupPage(res http.ResponseWriter, req *http.Request) { - if req.Method != "POST" { - http.ServeFile(res, req, "html/signup.html") - return - } - - username := req.FormValue("username") - password := req.FormValue("password") - - var user string - - err := db.QueryRow("SELECT username FROM players WHERE username=?",username).Scan(&user) - - switch { - case err == sql.ErrNoRows: - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) - if err != nil { - http.Error(res, "Server Error creating Password. Unable to create account! You Fool...", 500) - Log("ERROR 500") - return - } - newuuid, uiderr := exec.Command("uuidgen").Output() - if uiderr != nil{ - http.Error(res, "Server Error creating UUID. Unable to create account! You Fool...", 500) - Log("ERROR 500") - return - } - - _, err = db.Exec("INSERT INTO players(username, password, uuid) VALUES(?, ?, ?)", username, hashedPassword, newuuid) - if err != nil { - http.Error(res, "Server Error Inserting User. Unable to create account! You Fool...", 500) - Log("ERROR adding user"+username) - //fmt.Println("Error adding User",username) - return - } - res.Write([]byte("User Created Successfully!")) - Log("Successfully Created User"+username+string(newuuid)) - //fmt.Println("Successfully Created User",username,newuuid) - return - case err != nil: - http.Error(res, "Server Error! Something fucked up", 500) - Log("ERROR Something fucked up") - return - default: - http.Redirect(res, req, "/", 301) - } -} - -func loginPage(res http.ResponseWriter, req *http.Request) { - if req.Method != "POST" { - http.ServeFile(res, req, "html/login.html") - return - } - - username := req.FormValue("username") - password := req.FormValue("password") - - var databaseUsername string - var databasePassword string - var databaseUUID string - - err := db.QueryRow("SELECT username, password, uuid FROM players WHERE username=?", username).Scan(&databaseUsername, &databasePassword, &databaseUUID) - - if err != nil { - http.Redirect(res, req, "/login", 301) - Log("Failed Login atempt by "+username) - //fmt.Println("Failed Login atempt by",username) - return - } - - err = bcrypt.CompareHashAndPassword([]byte(databasePassword), []byte(password)) - if err != nil { - http.Redirect(res, req, "/login", 301) - Log(username+" used wrong Password "+password) - //fmt.Println(username," used wrong Password ",password) - return - } - - res.Write([]byte("Hello "+ databaseUsername + "! Your UUID is "+ databaseUUID)) - Log("Successful Login by "+username) - //fmt.Println("Successful Login by",username) -} - -func homePage(res http.ResponseWriter, req *http.Request) { - http.ServeFile(res, req, "html/index.html") -} - -func main() { - db, err = sql.Open("mysql", "hexmaster:waSIdocHneTdUobeRoarscH@tcp(192.168.0.16)/hexmaster") - if err != nil { - panic(err.Error()) - } - defer db.Close() - - err = db.Ping() - if err != nil { - panic(err.Error()) - } - - http.HandleFunc("/signup", signupPage) - http.HandleFunc("/login", loginPage) - http.HandleFunc("/", homePage) - http.ListenAndServe(":8088", nil) -} -- cgit v1.2.3