-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (140 loc) · 3.57 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
"sync"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{}
type client struct {
Name string
Valid bool
Connection *websocket.Conn
}
var clients []client
var lock sync.Mutex // protects adding/removing client and writing to clients
func addClient(name string, connection *websocket.Conn) int {
lock.Lock()
defer lock.Unlock()
for i := 0; i < len(clients); i++ {
if !clients[i].Valid {
clients[i].Valid = true
clients[i].Name = name
clients[i].Connection = connection
return i
}
}
clients = append(clients, client{Name: name, Valid: true, Connection: connection})
return len(clients) - 1
}
func removeClient(id int) {
lock.Lock()
clients[id].Valid = false
lock.Unlock()
}
func forwardMessage(clientID int, clientName string, content string) {
var forward forwardingMessage
forward.Construct(clientName, content)
msg, err := json.Marshal(forward)
if err != nil {
log.Println("error when encoding forwarding message:", err)
return
}
lock.Lock()
defer lock.Unlock()
for i := 0; i < len(clients); i++ {
if i == clientID {
continue
}
if clients[i].Valid {
err := clients[i].Connection.WriteMessage(websocket.TextMessage, msg)
if err != nil {
log.Println("error when forwarding message:", err)
}
}
}
}
func sendRegisterAck(connection *websocket.Conn) error {
var ack registerAck
ack.Construct()
msg, err := json.Marshal(ack)
if err != nil {
return err
}
return connection.WriteMessage(websocket.TextMessage, msg)
}
func serve(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("websocket upgrade:", err)
return
}
defer c.Close()
log.Println("accept new ws connection")
mt, message, err := recvPacket(c)
if err != nil {
log.Println("invalid received packet:", err)
return
}
if (mt != clientRegister{}.GetID()) {
log.Println("the first packet should be client register")
return
}
var regInfo clientRegister
err = regInfo.Decode(message)
if err != nil {
log.Println(err)
return
}
if regInfo.SecretKey != globalConfig.SecretKey {
log.Println("client register with wrong secret key: refused")
return
}
log.Println("client " + regInfo.ClientName + " authentication passed")
err = sendRegisterAck(c)
if err != nil {
log.Println("error when sending register ack:", err)
return
}
log.Println("sent register ack to " + regInfo.ClientName)
curID := addClient(regInfo.ClientName, c)
log.Println("client ID:", curID)
for {
mt, message, err := recvPacket(c)
if mt < 0 {
log.Println("recvPacket fatal error:", err, ", close connection")
break
}
if err != nil {
log.Println("recvPacket:", err)
continue
}
if (mt != clientMessage{}.GetID()) {
log.Println("unsupported action type:", mt)
continue
}
var msgInfo clientMessage
err = msgInfo.Decode(message)
if err != nil {
log.Println(err)
continue
}
log.Println("receive message from " + regInfo.ClientName)
forwardMessage(curID, regInfo.ClientName, msgInfo.Content)
}
removeClient(curID)
}
func main() {
err := loadConfig()
if err != nil {
log.Fatal("fail to load configuration:", err)
return
}
http.HandleFunc(globalConfig.WSpath, serve)
if globalConfig.Wss != nil {
log.Fatal(http.ListenAndServeTLS(globalConfig.Host, globalConfig.Wss.Cert, globalConfig.Wss.Key, nil))
} else {
log.Fatal(http.ListenAndServe(globalConfig.Host, nil))
}
}