-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
113 lines (87 loc) · 2.82 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
package prekeyserver
import (
"errors"
"io"
"time"
"github.com/otrv4/gotrx"
)
// GenericServer represents the main entry point for the prekey server functionality.
type GenericServer struct {
// The identity, for example prekey.example.org
identity string
// The fingerprint of the long term key for the server
fingerprint gotrx.Fingerprint
key *gotrx.Keypair
// Should be minimum 48, since the max envelope size is 47
fragLen int
fragmentations *gotrx.Fragmentor
messageHandler messageHandler
rand io.Reader
sessions *sessionManager
storageImpl storage
sessionTimeout time.Duration
fragmentationTimeout time.Duration
rest Restrictor
}
func (g *GenericServer) storage() storage {
return g.storageImpl
}
func (g *GenericServer) handleMessage(from string, message []byte) ([]byte, error) {
if g.messageHandler != nil {
return g.messageHandler.handleMessage(from, message)
}
panic("programmer error, missing message handler")
}
// Handle should receive the message in its original form
// Thus, a real server would give the received message as is to this function,
// excluding surrounding whitespace.
// It will return an error if something went wrong, and a list of messages that should be returned
// Each message to return should be sent in a separate network package, back to the original sender
// The Handle function should be called from its own goroutine to ensure asynchronous behavior of the server
func (g *GenericServer) Handle(from, message string) (returns []string, err error) {
if message == "" {
return nil, errors.New("empty message")
}
if g.fragmentations.IsFragment(message) {
m, c, e := g.fragmentations.NewFragmentReceived(from, message)
if e != nil {
return nil, e
}
if !c {
return nil, nil
}
message = m
}
if message[len(message)-1] != '.' {
return nil, errors.New("invalid message format - missing ending punctuation")
}
decoded, ok := decodeMessage(message[:len(message)-1])
if !ok {
return nil, errors.New("invalid message format - corrupted base64 encoding")
}
msg, e := g.handleMessage(from, decoded)
if e != nil {
return nil, e
}
encoded := encodeMessage(msg) + "."
msgs := g.fragmentations.PotentiallyFragment(encoded, g.fragLen, 0xDEAD, 0xBEEF, g)
g.cleanupAfter()
return msgs, nil
}
func (g *GenericServer) cleanupAfter() {
g.sessions.cleanup(g.sessionTimeout)
g.fragmentations.Cleanup(g.fragmentationTimeout)
g.storageImpl.cleanup()
}
func (g *GenericServer) compositeIdentity() []byte {
return append(gotrx.AppendData(nil, []byte(g.identity)), g.key.Pub.Serialize()...)
}
func (g *GenericServer) session(from string) session {
return g.sessions.get(from)
}
func (g *GenericServer) sessionComplete(from string) {
g.sessions.complete(from)
}
func (g *GenericServer) hasSession(from string) bool {
return g.sessions.has(from)
}