forked from honeynet/ochi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
368 lines (309 loc) · 8.77 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"context"
"embed"
"encoding/json"
"errors"
"io"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/signal"
"sync"
"time"
"github.com/honeynet/ochi/entities"
"github.com/honeynet/ochi/repos"
"github.com/jmoiron/sqlx"
"golang.org/x/time/rate"
"google.golang.org/api/idtoken"
"nhooyr.io/websocket"
)
type server struct {
// subscriberMessageBuffer controls the max number
// of messages that can be queued for a subscriber
// before it is kicked.
//
// Defaults to 16.
subscriberMessageBuffer int
// publishLimiter controls the rate limit applied to the publish endpoint.
//
// Defaults to one publish every 100ms with a burst of 8.
publishLimiter *rate.Limiter
// serveMux routes the various endpoints to the appropriate handler.
serveMux http.ServeMux
subscribersMu sync.Mutex
subscribers map[*subscriber]struct{}
// the repositories
uRepo *repos.UserRepo
// http client
httpClient *http.Client
}
//go:embed public
var public embed.FS
// newServer constructs a server with the defaults.
func newServer() *server {
cs := &server{
subscriberMessageBuffer: 16,
subscribers: make(map[*subscriber]struct{}),
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
httpClient: &http.Client{
Timeout: time.Second,
Transport: &http.Transport{
TLSHandshakeTimeout: time.Second,
},
},
}
content, err := fs.Sub(public, "public")
if err != nil {
log.Fatal(err)
}
db, err := sqlx.Connect("sqlite3", "./data.db")
if err != nil {
log.Fatal(err)
}
cs.uRepo, err = repos.NewUserRepo(db)
if err != nil {
log.Fatal(err)
}
cs.serveMux.Handle("/", http.FileServer(http.FS(content)))
cs.serveMux.HandleFunc("/subscribe", cs.subscribeHandler)
cs.serveMux.HandleFunc("/publish", cs.publishHandler)
cs.serveMux.HandleFunc("/login", cs.loginHandler)
cs.serveMux.HandleFunc("/session", cs.sessionHandler)
return cs
}
// subscriber represents a subscriber
type subscriber struct {
msgs chan []byte
closeSlow func()
}
func (cs *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cs.serveMux.ServeHTTP(w, r)
}
// subscribeHandler accepts the WebSocket connection and then subscribes
// it to all future messages.
func (cs *server) subscribeHandler(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
return
}
defer c.Close(websocket.StatusInternalError, "")
err = cs.subscribe(r.Context(), c)
if errors.Is(err, context.Canceled) {
return
}
if websocket.CloseStatus(err) == websocket.StatusNormalClosure ||
websocket.CloseStatus(err) == websocket.StatusGoingAway {
return
}
if err != nil {
return
}
}
// publishHandler reads the request body with a limit of 8192 bytes and then publishes
// the received message.
func (cs *server) publishHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
token, ok := r.URL.Query()["token"]
if !ok || len(token) == 0 || token[0] != os.Args[2] {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
body := http.MaxBytesReader(w, r.Body, 8192)
msg, err := io.ReadAll(body)
if err != nil {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
cs.publish(msg)
w.WriteHeader(http.StatusAccepted)
}
type response struct {
User entities.User `json:"user,omitempty"`
Token string `json:"token,omitempty"`
}
// sessionHandler ...
func (cs *server) sessionHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
body := http.MaxBytesReader(w, r.Body, 1024)
data, err := io.ReadAll(body)
if err != nil {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
claims, valid, err := entities.ValidateToken(string(data), os.Args[3])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !valid {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
user, err := cs.uRepo.Get(claims.UserID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
token, err := entities.NewToken(os.Args[3], user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
if err = json.NewEncoder(w).Encode(response{user, token}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// loginHandler ...
func (cs *server) loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
body := http.MaxBytesReader(w, r.Body, 8192)
data, err := io.ReadAll(body)
if err != nil {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
return
}
ctx := context.Background()
val, err := idtoken.NewValidator(ctx, idtoken.WithHTTPClient(cs.httpClient))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
payload, err := val.Validate(ctx, string(data), "610036027764-0lveoeejd62j594aqab5e24o2o82r8uf.apps.googleusercontent.com")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var user entities.User
if emailInt, ok := payload.Claims["email"]; ok {
if email, ok := emailInt.(string); ok {
user, err = cs.uRepo.Find(email)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
}
token, err := entities.NewToken(os.Args[3], user)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
if err = json.NewEncoder(w).Encode(response{user, token}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// subscribe subscribes the given WebSocket to all broadcast messages.
// It creates a subscriber with a buffered msgs chan to give some room to slower
// connections and then registers the subscriber. It then listens for all messages
// and writes them to the WebSocket. If the context is cancelled or
// an error occurs, it returns and deletes the subscription.
//
// It uses CloseRead to keep reading from the connection to process control
// messages and cancel the context if the connection drops.
func (cs *server) subscribe(ctx context.Context, c *websocket.Conn) error {
ctx = c.CloseRead(ctx)
s := &subscriber{
msgs: make(chan []byte, cs.subscriberMessageBuffer),
closeSlow: func() {
c.Close(websocket.StatusPolicyViolation, "connection too slow to keep up with messages")
},
}
cs.addSubscriber(s)
defer cs.deleteSubscriber(s)
for {
select {
case msg := <-s.msgs:
err := writeTimeout(ctx, time.Second*5, c, msg)
if err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// publish publishes the msg to all subscribers.
func (cs *server) publish(msg []byte) {
cs.subscribersMu.Lock()
defer cs.subscribersMu.Unlock()
cs.publishLimiter.Wait(context.Background())
for s := range cs.subscribers {
select {
case s.msgs <- msg:
default:
go s.closeSlow()
}
}
}
// addSubscriber registers a subscriber.
func (cs *server) addSubscriber(s *subscriber) {
cs.subscribersMu.Lock()
cs.subscribers[s] = struct{}{}
cs.subscribersMu.Unlock()
}
// deleteSubscriber deletes the given subscriber.
func (cs *server) deleteSubscriber(s *subscriber) {
cs.subscribersMu.Lock()
delete(cs.subscribers, s)
cs.subscribersMu.Unlock()
}
func writeTimeout(ctx context.Context, timeout time.Duration, c *websocket.Conn, msg []byte) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return c.Write(ctx, websocket.MessageText, msg)
}
// run initializes the server
func run() error {
if len(os.Args) < 4 {
return errors.New("please provide an address to listen on as the first argument, token second, secret third")
}
l, err := net.Listen("tcp", os.Args[1])
if err != nil {
return err
}
log.Printf("listening on http://%v", l.Addr())
cs := newServer()
s := &http.Server{
Handler: cs,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}
defer cs.uRepo.Close()
errc := make(chan error, 1)
go func() {
errc <- s.Serve(l)
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
select {
case err := <-errc:
log.Printf("failed to serve: %v", err)
case sig := <-sigs:
log.Printf("terminating: %v", sig)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
return s.Shutdown(ctx)
}
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}