-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
207 lines (180 loc) · 5.89 KB
/
session.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
// Copyright 2014 The zephyr-go authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zephyr
import (
"log"
"net"
"time"
"github.com/zephyr-im/krb5-go"
)
const dedupLifetime = 900 * time.Second
const fragmentLifetime = 30 * time.Second
// A MessageReaderResult is an output of a Session's incoming Message
// channel. It either contains a Message with accompanying AuthStatus
// or an error.
type MessageReaderResult struct {
Message *Message
AuthStatus AuthStatus
}
// A Session is a high-level connection to the zephyr servers. It
// handles reassembly of sharded messages, ACKs, and deduplicating
// notices by UID. This API should be used by most zephyr clients.
type Session struct {
conn *Connection
clock Clock
messages chan MessageReaderResult
}
// NewSession creates a new Session attached to a given connection
// with server configuration and credential.
func NewSession(
conn net.PacketConn,
server ServerConfig,
cred *krb5.Credential,
) (*Session, error) {
return NewSessionFull(conn, server, cred, SystemClock)
}
// NewSessionFull creates a new Session attached to a given connection
// with server configuration and credential. This variant allows
// passing a custom clock for testing.
func NewSessionFull(
conn net.PacketConn,
server ServerConfig,
cred *krb5.Credential,
clock Clock,
) (*Session, error) {
zconn, err := NewConnectionFull(conn, server, cred, clock)
if err != nil {
return nil, err
}
s := &Session{zconn, clock, make(chan MessageReaderResult)}
go s.noticeLoop()
return s, nil
}
func (s *Session) noticeLoop() {
dedup := NewWindowedMapFull(dedupLifetime, s.clock)
reassemble := NewWindowedMapFull(fragmentLifetime, s.clock)
for r := range s.conn.Notices() {
// ACK if appropriate.
if r.Notice.Kind.ExpectsClientACK() {
s.conn.SendNoticeUnackedTo(
r.Notice.MakeACK(CLIENTACK, ""), r.Addr)
}
// Deduplicate.
if _, ok := dedup.Lookup(r.Notice.UID); ok {
continue
}
dedup.Put(r.Notice.UID, nil)
// Reassemble.
var msg *Reassembler
if t, ok := reassemble.Lookup(r.Notice.MultiUID); ok {
msg = t.(*Reassembler)
} else {
var err error
msg, err = NewReassemblerFromMultipartField(r.Notice)
if err != nil {
log.Printf("Error parsing multipart: %v", err)
continue
}
reassemble.Put(r.Notice.MultiUID, msg)
}
if err := msg.AddNotice(r.Notice, r.AuthStatus); err != nil {
log.Printf("Error reassembling notice: %v", err)
continue
}
if msg.Done() {
reassemble.Remove(r.Notice.MultiUID)
m, a := msg.Message()
s.messages <- MessageReaderResult{m, a}
}
}
close(s.messages)
}
// Messages returns the incoming messages from the session.
func (s *Session) Messages() <-chan MessageReaderResult {
return s.messages
}
// LocalAddr returns the local UDP address for the client when
// communicating with the Zephyr servers.
func (s *Session) LocalAddr() *net.UDPAddr {
return s.conn.LocalAddr()
}
// Port returns the local UDP port for the client.
func (s *Session) Port() uint16 {
return uint16(s.conn.LocalAddr().Port)
}
// Credential returns the credential for this session.
func (s *Session) Credential() *krb5.Credential {
return s.conn.Credential()
}
// Sender returns the user the session is authenticated as.
func (s *Session) Sender() string {
return s.Credential().Client.String()
}
// Realm returns the realm of the server being connected to.
func (s *Session) Realm() string {
// TODO(davidben): This really should come from the server
// config or something.
return s.Credential().Server.Realm
}
// Close closes the session.
func (s *Session) Close() error {
return s.conn.Close()
}
// MakeUID creates a new UID for a given time and this sessions local
// IP address.
func (s *Session) MakeUID(t time.Time) UID {
return MakeUID(s.LocalAddr().IP, t)
}
// SendSubscribe sends a subscription notice for a list of triples,
// along with any defaults configured on the server.
func (s *Session) SendSubscribe(
ctx *krb5.Context,
subs []Subscription,
) (*Notice, error) {
return SendSubscribe(ctx, s.conn, 0, subs)
}
// SendSubscribeNoDefaults sends a subscription notice for a list of
// triples. It returns the ACK from the server.
func (s *Session) SendSubscribeNoDefaults(
ctx *krb5.Context,
subs []Subscription,
) (*Notice, error) {
return SendSubscribeNoDefaults(ctx, s.conn, 0, subs)
}
// SendUnsubscribe unsubscribes from a list of triples. It returns the
// ACK from the server.
func (s *Session) SendUnsubscribe(
ctx *krb5.Context,
subs []Subscription,
) (*Notice, error) {
return SendUnsubscribe(ctx, s.conn, 0, subs)
}
// SendCancelSubscriptions closes the session. This should be called
// before exit to release the port on the zephyrd. It returns the ACK
// from the server.
func (s *Session) SendCancelSubscriptions(ctx *krb5.Context) (*Notice, error) {
return SendCancelSubscriptions(ctx, s.conn, 0)
}
// SendMessage sends an authenticated message over the session,
// sharding into multiple notices as needed. It returns the ACK from
// the server if the message is ACKED.
func (s *Session) SendMessage(ctx *krb5.Context, msg *Message) (*Notice, error) {
return SendMessage(ctx, s.conn, msg)
}
// SendMessageUnauth sends an unauthenticated message over the
// session, sharding into multiple notices as needed. It returns the
// ACK from the server if the message is ACKED.
func (s *Session) SendMessageUnauth(msg *Message) (*Notice, error) {
return SendMessageUnauth(s.conn, msg)
}