package main import "database/sql" import _ "github.com/go-sql-driver/mysql" import "golang.org/x/crypto/bcrypt" import "net/http" import "strings" 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() token := strings.TrimRight(string(tok), "\n") if uiderr != nil{ http.Error(res, "Server Error creating Token. You Fool...", 500) Log("ERROR 500 - Creating UUID failed") panic(uiderr) return } keynam := username + "_token" setRed(keynam,token,300) 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) }