-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.go
223 lines (189 loc) · 5.95 KB
/
server.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
package main
import (
"aim-oscar/config"
"aim-oscar/models"
"aim-oscar/oscar"
"aim-oscar/services"
"aim-oscar/util"
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"net"
"strings"
"time"
"github.com/google/uuid"
"github.com/uptrace/bun"
"golang.org/x/exp/slog"
)
type HandlerFunc func(context.Context, *oscar.FLAP) context.Context
type HandleCloseFn func(context.Context, *oscar.Session)
type Handler struct {
conf *config.AppConfig
db *bun.DB
logger *slog.Logger
sessionManager *SessionManager
serviceManager *ServiceManager
onlineCh chan *models.User
}
func NewHandler(conf *config.AppConfig, db *bun.DB, logger *slog.Logger, sm *SessionManager, svm *ServiceManager, onlineCh chan *models.User) *Handler {
return &Handler{
conf, db, logger, sm, svm, onlineCh,
}
}
func (h *Handler) Handle(conn net.Conn, logger *slog.Logger) {
connLogger := logger.With("session_id", uuid.New(), "ip", conn.RemoteAddr().String())
connLogger.Info("New Connection")
ctx := oscar.NewContextWithSession(context.Background(), conn, connLogger)
session, err := oscar.SessionFromContext(ctx)
if err != nil {
connLogger.Error("could not create session for context", "err", err)
}
var buf bytes.Buffer
for {
if !session.GreetedClient {
// send a hello
hello := oscar.NewFLAP(1)
hello.Data.Write([]byte{0, 0, 0, 1})
session.Send(hello)
session.GreetedClient = true
}
// Wait for some data to read
conn.SetReadDeadline(time.Now().Add(30 * time.Second))
incoming := make([]byte, 512)
n, err := conn.Read(incoming)
if err != nil && err != io.EOF {
if strings.Contains(err.Error(), "use of closed network connection") {
session.Disconnect()
h.handleCloseFn(ctx, session)
return
}
// If the read timed out, just try reading again
if err, ok := err.(net.Error); ok && err.Timeout() {
continue
}
connLogger.Error("OSCAR Read Error", "err", err.Error())
return
}
if n == 0 {
return
}
buf.Write(incoming[:n])
// Try to parse all of the FLAPs in the buffer if we have enough bytes to
// fill a FLAP header
for buf.Len() >= 6 && buf.Bytes()[0] == 0x2a {
bufBytes := buf.Bytes()
dataLength := binary.BigEndian.Uint16(bufBytes[4:6])
flapLength := int(dataLength) + 6
if len(bufBytes) < flapLength {
connLogger.Error(fmt.Sprintf("not enough data, expected %d bytes but have %d bytes", flapLength, len(bufBytes)))
fmt.Printf("%s\n", util.PrettyBytes(bufBytes))
break
}
flap := &oscar.FLAP{}
flapBuf := make([]byte, flapLength)
buf.Read(flapBuf)
if err := flap.UnmarshalBinary(flapBuf); err != nil {
connLogger.Error("could not unmarshal FLAP", "err", err)
// Toss out everything
buf.Reset()
break
}
ctx = h.handleFn(ctx, flap)
}
}
}
func (h *Handler) handleFn(ctx context.Context, flap *oscar.FLAP) context.Context {
session, err := oscar.SessionFromContext(ctx)
if err != nil {
// TODO
h.logger.Error("no session in context", "err", err, "flap", flap)
return ctx
}
if user := models.UserFromContext(ctx); user != nil {
if h.conf.LogLevel == slog.LevelDebug.String() {
session.Logger.Debug("RECV",
slog.String("screen_name", user.ScreenName),
slog.String("ip", session.RemoteAddr().String()),
"flap", flap,
)
}
user.LastActivityAt = time.Now()
ctx = models.NewContextWithUser(ctx, user)
session.ScreenName = user.ScreenName
h.sessionManager.SetSession(user.ScreenName, session)
} else {
if h.conf.LogLevel == slog.LevelDebug.String() {
session.Logger.Debug("RECV",
slog.String("ip", session.RemoteAddr().String()),
"flap", flap,
)
}
}
if flap.Header.Channel == 1 {
// Is this a hello?
if bytes.Equal(flap.Data.Bytes(), []byte{0, 0, 0, 1}) {
return ctx
}
user, screenName, err := services.AuthenticateFLAPCookie(ctx, h.db, flap)
if err != nil {
session.Logger.Error("Could not authenticate user cookie", "screen_name", screenName, slog.String("err", err.Error()))
return ctx
}
session.Logger.Info("Authenticated user", "screen_name", user.ScreenName)
session.ScreenName = user.ScreenName
ctx = models.NewContextWithUser(ctx, user)
// Send available services
servicesSnac := oscar.NewSNAC(0x1, 0x3)
for _, service := range services.ServiceVersions {
servicesSnac.Data.WriteUint16(service.Family)
}
servicesFlap := oscar.NewFLAP(2)
servicesFlap.Data.WriteBinary(servicesSnac)
session.Send(servicesFlap)
return ctx
} else if flap.Header.Channel == 2 {
snac := &oscar.SNAC{}
if err := snac.UnmarshalBinary(flap.Data.Bytes()); err != nil {
session.Logger.Error("could not unmarshal FLAP data", "err", err)
session.Disconnect()
h.handleCloseFn(ctx, session)
return ctx
}
if service, ok := h.serviceManager.GetService(snac.Header.Family); ok {
newCtx, err := service.HandleSNAC(ctx, h.db, snac)
if err != nil {
session.Logger.Error("error handling SNAC", slog.String("err", err.Error()))
session.Disconnect()
h.handleCloseFn(ctx, session)
}
return newCtx
}
} else if flap.Header.Channel == 4 {
h.handleCloseFn(ctx, session)
} else if flap.Header.Channel == 5 {
// User is still connected
// TODO: handle when user stops sending these messages?
return ctx
// session.Logger.Debug(fmt.Sprintf("%s is still connected", session.ScreenName))
} else {
session.Logger.Info("unhandled channel message", "channel", flap.Header.Channel, "flap", flap)
}
return ctx
}
func (h *Handler) handleCloseFn(ctx context.Context, session *oscar.Session) {
session.Logger.Info("Disconnected")
user := models.UserFromContext(ctx)
if user != nil {
if err := user.SetAway(ctx, h.db); err != nil {
h.logger.Error("Could not set user as away", slog.String("err", err.Error()))
}
h.logger.Info("Disconnecting user", slog.String("screen_name", user.ScreenName))
h.onlineCh <- user
if session, err := oscar.SessionFromContext(ctx); err == nil {
session.Disconnect()
h.sessionManager.RemoveSession(user.ScreenName)
}
}
}