This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
/
econetwork.go
162 lines (143 loc) · 3.88 KB
/
econetwork.go
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
package econetwork
import (
"crypto/rand"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"os"
_ "github.com/mattn/go-sqlite3"
"github.com/gorilla/websocket"
"github.com/sony/sonyflake"
)
const version = "Econode v0.1.0-beta"
type Econetwork struct {
Address string
sessions map[string]Client
conn *websocket.Conn
db *sql.DB
sf *sonyflake.Sonyflake
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func New() (*Econetwork, error) {
if _, err := os.Stat("econetwork.db"); os.IsNotExist(err) {
os.Create("econetwork.db")
}
db, err := sql.Open("sqlite3", "econetwork.db")
if err != nil {
return nil, err
}
// make our tables
// sqlite doesnt have a boolean type, so `op` is an INTEGER which'll be either 0 (false) or 1 (true)
db.Exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, password TEXT, node INTEGER, op INTEGER);")
db.Exec("CREATE TABLE IF NOT EXISTS nodes (id INTEGER PRIMARY KEY, name TEXT, owner INTEGER, members TEXT, inventory TEXT, balance INTEGER, multi REAL);" /* REAL is a float */)
var st sonyflake.Settings
st.MachineID = func() (uint16, error) {
return 1, nil
}
return &Econetwork{
Address: ":7768",
sessions: map[string]Client{},
conn: nil,
db: db,
sf: sonyflake.NewSonyflake(st),
}, nil
}
func (e *Econetwork) Stop() {
e.db.Close()
}
func (e *Econetwork) Start() {
http.HandleFunc("/econetwork", func(w http.ResponseWriter, r *http.Request) {
conn, _ := upgrader.Upgrade(w, r, nil) // error ignored for sake of simplicity
c := Client{Conn: conn}
go func() {
for {
// Read message from browser
resp := ClientResponse{}
err := conn.ReadJSON(&resp)
if err != nil {
if websocket.IsCloseError(err) || websocket.IsUnexpectedCloseError(err) {
name := "client"
if c.Account != nil {
name = c.Account.Username
}
fmt.Printf("%s disconnected\n", name)
if c.SessionID != "" {
delete(e.sessions, c.SessionID)
}
return
}
fmt.Println(err)
}
// Print the message to the console
fmt.Printf("%+v\n", resp)
jsondata, _ := json.Marshal(resp.Data)
switch resp.Method {
case "register":
if c.Account != nil {
c.SendError("register", "client already authorized")
continue
}
registerInfo := AuthPayload{}
if err := json.Unmarshal(jsondata, ®isterInfo); err != nil {
c.SendMalformed("register")
continue
}
err = e.register(registerInfo)
if err == nil {
c.Account = &Account{Username: registerInfo.Username}
sessionid := SessionID()
e.sessions[sessionid] = c
c.SessionID = sessionid
c.SendSuccess("register", sessionid)
} else {
fmt.Println("Error in register method occurred\n", err)
c.SendError("register", nil)
}
case "login":
if c.Account != nil {
c.SendError("login", "client already authorized")
continue
}
loginInfo := AuthPayload{}
if err := json.Unmarshal(jsondata, &loginInfo); err != nil {
c.SendMalformed("login")
continue
}
passMatch, err := e.login(loginInfo)
if err == nil {
if !passMatch {
c.SendError("login", "password is incorrect")
continue
}
loginAcc, _ := e.getAccount(loginInfo.Username)
sessionid := SessionID()
c.Account = loginAcc
e.sessions[sessionid] = c
c.SessionID = sessionid
c.SendSuccess("login", sessionid)
} else {
fmt.Println("Error in login method occurred\n", err)
c.SendError("login", nil)
}
case "stats":
stats := StatsPayload{
NetworkVersion: version,
}
c.SendSuccess("stats", stats)
}
}
}()
})
http.ListenAndServe(e.Address, nil)
}
// Generates a session id for a user
func SessionID() string {
idRaw := make([]byte, 24)
rand.Read(idRaw)
return hex.EncodeToString(idRaw)
}