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
|
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"
)
type SessionManager interface {
AddSession(session *Session)
RemoveSession(id snowflake.ID)
Session(id snowflake.ID) (session *Session, ok bool)
Node() *snowflake.Node
}
type Session struct {
// Channel to directly write packets to the cient.
// Can be nil in cases where the connection is not available.
WriteQueue chan packet.Packet
PubKey ed25519.PublicKey
manager SessionManager
addr *net.TCPAddr
id snowflake.ID
mu sync.Mutex
challenge []byte
issuedTime time.Time
}
func NewSession(manager SessionManager, addr *net.TCPAddr, id snowflake.ID, pubKey ed25519.PublicKey) *Session {
session := &Session{
WriteQueue: make(chan packet.Packet, 10),
PubKey: pubKey,
manager: manager,
addr: addr,
id: id,
challenge: make([]byte, 32),
}
session.Challenge() // Make sure an initial nonce is generated
return session
}
func (s *Session) Addr() *net.TCPAddr {
return s.addr
}
func (s *Session) ID() snowflake.ID {
return s.id
}
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
}
type key struct{}
var sessKey key
func NewContext(ctx context.Context, sess *Session) context.Context {
return context.WithValue(ctx, sessKey, sess)
}
func FromContext(ctx context.Context) (*Session, bool) {
sess, ok := ctx.Value(sessKey).(*Session)
return sess, ok
}
|