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 session
import (
"context"
"crypto/ed25519"
"crypto/rand"
"net"
"sync"
"time"
"github.com/kyren223/eko/internal/packet"
"github.com/kyren223/eko/pkg/assert"
"github.com/kyren223/eko/pkg/snowflake"
)
const (
WriteQueueSize = 10
NonceSize = 32
)
type SessionManager interface {
AddSession(session *Session, userId snowflake.ID, pubKey ed25519.PublicKey)
RemoveSession(id snowflake.ID)
Session(id snowflake.ID) *Session
UseSessions(f func(map[snowflake.ID]*Session))
Node() *snowflake.Node
}
type Session struct {
manager SessionManager
addr *net.TCPAddr
cancel context.CancelFunc
writeQueue chan packet.Packet
writerWg *sync.WaitGroup
writeMu sync.RWMutex
issuedTime time.Time
challenge []byte
isTosAccepted bool
pubKey ed25519.PublicKey
id snowflake.ID
mu sync.Mutex
}
func NewSession(
manager SessionManager,
addr *net.TCPAddr, cancel context.CancelFunc,
writerWg *sync.WaitGroup,
) *Session {
assert.NotNil(addr, "tcp address should be valid")
assert.NotNil(manager, "session manager should be valid")
session := &Session{
manager: manager,
addr: addr,
cancel: cancel,
writeQueue: make(chan packet.Packet, WriteQueueSize),
writerWg: writerWg,
writeMu: sync.RWMutex{},
issuedTime: time.Time{},
challenge: make([]byte, NonceSize),
pubKey: ed25519.PublicKey{},
id: snowflake.InvalidID,
mu: sync.Mutex{},
isTosAccepted: false,
}
return session
}
func (s *Session) Addr() *net.TCPAddr {
assert.NotNil(s.addr, "tcp address should be valid")
return s.addr
}
func (s *Session) IsTosAccepted() bool {
return s.isTosAccepted
}
func (s *Session) ReceivedTosAcceptance() {
s.isTosAccepted = true
}
func (s *Session) IsAuthenticated() bool {
return s.id != snowflake.InvalidID
}
func (s *Session) ID() snowflake.ID {
assert.Assert(s.IsAuthenticated(), "use of ID in an unauthenticated session", "addr", s.addr)
return s.id
}
func (s *Session) PubKey() ed25519.PublicKey {
assert.Assert(s.IsAuthenticated(), "use of PubKey in an unauthenticated session", "addr", s.addr)
return s.pubKey
}
func (s *Session) Promote(userId snowflake.ID, pubKey ed25519.PublicKey) {
s.id = userId
s.pubKey = pubKey
}
func (s *Session) Manager() SessionManager {
return s.manager
}
func (s *Session) Challenge() []byte {
s.mu.Lock()
defer s.mu.Unlock()
if time.Since(s.issuedTime) > time.Minute {
s.issuedTime = time.Now()
_, err := rand.Read(s.challenge)
assert.NoError(err, "random should always produce a value")
}
return s.challenge
}
func (s *Session) Write(ctx context.Context, pkt packet.Packet) bool {
s.writerWg.Add(1)
defer s.writerWg.Done()
s.writeMu.RLock()
defer s.writeMu.RUnlock()
select {
case s.writeQueue <- pkt:
return true
case <-ctx.Done():
return false
}
}
func (s *Session) Read() <-chan packet.Packet {
s.writeMu.RLock()
defer s.writeMu.RUnlock()
return s.writeQueue
}
func (s *Session) CloseWriteQueue() {
s.writeMu.Lock()
defer s.writeMu.Unlock()
close(s.writeQueue)
s.writeQueue = nil
}
func (s *Session) Close() {
s.cancel()
}
|