1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
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)
}
|