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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/crypto/bcrypt"
"net/http"
"strings"
"os/exec"
"encoding/json"
_ "strconv"
"fmt"
"io"
"time"
)
var db *sql.DB
var err error
type UserIdent struct {
Name string
Token string
Action string
level int
experience int
//Token []byte
//lastact int
}
type Area struct {
coord_x int
coord_y int
coord_z int
created string
player string
terrtype int
wealth int
}
type Map []Area
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,level,experience) VALUES(?, ?, ?, ?, ?)", username, hashedPassword, email, 1, 0)
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+" email:"+email)
//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 databaseLevel int
var databaseExp int
// var databaseUUID string
err := db.QueryRow("SELECT username, password, level, experience FROM players WHERE username=?", username).Scan(&databaseUsername, &databasePassword, &databaseLevel, &databaseExp)
if err != nil {
http.Redirect(res, req, "/login", 301)
Log("Failed Login atempt by "+username+" ;"+err.Error())
//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
}
type userAction struct {
Name string
Token string
Action string
}
useract := userAction{
Name: username,
Token: token,
Action: "nil",
}
jout, err := json.Marshal(useract)
if err != nil {
Log("Error:"+ err.Error())
}
//jout := map[string]string {
// "Name":username,
// "Token":token,
//}
//fmt.Println(databaseLevel,databaseExp)
keynam := username + "_token"
setRed(keynam,token,600)
keynam = username + "_lvl"
setRed(keynam,databaseLevel,0)
keynam = username + "_xp"
setRed(keynam,databaseExp,0)
res.Write([]byte(string(jout)+" \n\n Have a RESTful party at /game"))
//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 gamePage(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
res.Write([]byte("Access Denied"))
Log("Attempted Access to Game")
//http.ServeFile(res, req, "html/login.html")
return
}
//make byte array
out := make([]byte,1024)
//
bodyLen, err := req.Body.Read(out)
if err != io.EOF {
fmt.Println(err.Error())
res.Write([]byte("{error:" + err.Error() + "}"))
return
}
var k UserIdent
err = json.Unmarshal(out[:bodyLen],&k)
if err != nil {
res.Write([]byte("{error:" + err.Error() + "}"))
return
}
//fmt.Println(k.Name, k.Token)
if !checkLegit(k) {
Log(k.Name +" token Missing")
return
} //else{
// fmt.Println("Nice")
//}
//somemap := false
var mymap Map
for {
mymap = getSQLMap(k.Name)
if mymap == nil {
//so := getNewArea(0,0,k.Name)
//fmt.Println("New:",so)
if !addSQLMap(getNewArea(0,0,0,k.Name)) {
return
}
}
break
}
fmt.Println("MyMap:",mymap)
testEngine()
}
func getSQLMap(nam string) Map {
var rmap Map
results,err := db.Query("SELECT coord_x, coord_y, coord_z, created, player, terrtype, wealth FROM maps WHERE player=?",nam)
if err != nil {
panic(err.Error())
}
i := 0
for results.Next(){
i++
var tf Area
err = results.Scan(&tf.coord_x, &tf.coord_y, &tf.coord_z, &tf.created, &tf.player, &tf.terrtype, &tf.wealth)
if err != nil {
panic(err.Error())
}
rmap=append(rmap,tf)
}
if i==0 {
Log(nam + " Has Empty Map.")
return nil
}
return rmap
}
func addSQLMap(newar Area) bool {
_, err = db.Exec("INSERT INTO maps(coord_x, coord_y, player, terrtype, wealth) VALUES(?, ?, ?, ?, ?)", newar.coord_x, newar.coord_y, newar.player, newar.terrtype, newar.wealth)
if err != nil {
Log("ERROR adding area for user "+newar.player)
panic(err)
return false
}
return true
}
func checkLegit(usr UserIdent) bool{
namet := usr.Name+"_token"
if usr.Token == getRed(namet) {
return true
}
return false
//fmt.Println(getRed(namet))
}
func main() {
//var tstart time
tstart := time.Now()
fmt.Println("Hexfool startet at", tstart)
defer fmt.Println("Goodbye! Uptime", (time.Now().Second()-tstart.Second()))
//defer(tend := time.Now()-tstart)
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("/game", gamePage)
http.HandleFunc("/", homePage)
http.ListenAndServe(":8088", nil)
}
|