summaryrefslogtreecommitdiff
path: root/hexfool.go
diff options
context:
space:
mode:
Diffstat (limited to 'hexfool.go')
-rw-r--r--hexfool.go135
1 files changed, 135 insertions, 0 deletions
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)
+}