forked from skycoin/dmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_common.go
179 lines (151 loc) · 4.27 KB
/
session_common.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
package dmsg
import (
"bufio"
"encoding/binary"
"io"
"net"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/skycoin/yamux"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/noise"
)
// SessionCommon contains the common fields and methods used by a session, whether it be it from the client or server
// perspective.
type SessionCommon struct {
entity *EntityCommon // back reference
rPK cipher.PubKey // remote pk
netConn net.Conn // underlying net.Conn (TCP connection to the dmsg server)
ys *yamux.Session
ns *noise.Noise
nMap noise.NonceMap
rMx sync.Mutex
wMx sync.Mutex
log logrus.FieldLogger
}
// GetConn returns underlying TCP `net.Conn`.
func (sc *SessionCommon) GetConn() net.Conn {
return sc.netConn
}
// GetDecNonce returns value of DecNonce of underlying `*noise.Noise`.
func (sc *SessionCommon) GetDecNonce() uint64 {
sc.rMx.Lock()
defer sc.rMx.Unlock()
return sc.ns.GetDecNonce()
}
// GetEncNonce returns value of EncNonce of underlying `*noise.Noise`.
func (sc *SessionCommon) GetEncNonce() uint64 {
sc.wMx.Lock()
defer sc.wMx.Unlock()
return sc.ns.GetEncNonce()
}
func (sc *SessionCommon) initClient(entity *EntityCommon, conn net.Conn, rPK cipher.PubKey) error {
ns, err := noise.New(noise.HandshakeXK, noise.Config{
LocalPK: entity.pk,
LocalSK: entity.sk,
RemotePK: rPK,
Initiator: true,
})
if err != nil {
return err
}
r := bufio.NewReader(conn)
if err := noise.InitiatorHandshake(ns, r, conn); err != nil {
return err
}
if r.Buffered() > 0 {
return ErrSessionHandshakeExtraBytes
}
ySes, err := yamux.Client(conn, yamux.DefaultConfig())
if err != nil {
return err
}
sc.entity = entity
sc.rPK = rPK
sc.netConn = conn
sc.ys = ySes
sc.ns = ns
sc.nMap = make(noise.NonceMap)
sc.log = entity.log.WithField("session", ns.RemoteStatic())
return nil
}
func (sc *SessionCommon) initServer(entity *EntityCommon, conn net.Conn) error {
ns, err := noise.New(noise.HandshakeXK, noise.Config{
LocalPK: entity.pk,
LocalSK: entity.sk,
Initiator: false,
})
if err != nil {
return err
}
r := bufio.NewReader(conn)
if err := noise.ResponderHandshake(ns, r, conn); err != nil {
return err
}
if r.Buffered() > 0 {
return ErrSessionHandshakeExtraBytes
}
ySes, err := yamux.Server(conn, yamux.DefaultConfig())
if err != nil {
return err
}
sc.entity = entity
sc.rPK = ns.RemoteStatic()
sc.netConn = conn
sc.ys = ySes
sc.ns = ns
sc.nMap = make(noise.NonceMap)
sc.log = entity.log.WithField("session", ns.RemoteStatic())
return nil
}
// writeEncryptedGob encrypts with noise and prefixed with uint16 (2 additional bytes).
func (sc *SessionCommon) writeObject(w io.Writer, obj SignedObject) error {
sc.wMx.Lock()
p := sc.ns.EncryptUnsafe(obj)
sc.wMx.Unlock()
p = append(make([]byte, 2), p...)
binary.BigEndian.PutUint16(p, uint16(len(p)-2))
_, err := w.Write(p)
return err
}
func (sc *SessionCommon) readObject(r io.Reader) (SignedObject, error) {
lb := make([]byte, 2)
if _, err := io.ReadFull(r, lb); err != nil {
return nil, err
}
pb := make([]byte, binary.BigEndian.Uint16(lb))
if _, err := io.ReadFull(r, pb); err != nil {
return nil, err
}
sc.rMx.Lock()
if sc.nMap == nil {
sc.rMx.Unlock()
return nil, ErrSessionClosed
}
obj, err := sc.ns.DecryptWithNonceMap(sc.nMap, pb)
sc.rMx.Unlock()
return obj, err
}
func (sc *SessionCommon) localSK() cipher.SecKey { return sc.entity.sk }
// LocalPK returns the local public key of the session.
func (sc *SessionCommon) LocalPK() cipher.PubKey { return sc.entity.pk }
// RemotePK returns the remote public key of the session.
func (sc *SessionCommon) RemotePK() cipher.PubKey { return sc.rPK }
// LocalTCPAddr returns the local address of the underlying TCP connection.
func (sc *SessionCommon) LocalTCPAddr() net.Addr { return sc.netConn.LocalAddr() }
// RemoteTCPAddr returns the remote address of the underlying TCP connection.
func (sc *SessionCommon) RemoteTCPAddr() net.Addr { return sc.netConn.RemoteAddr() }
// Ping obtains the round trip latency of the session.
func (sc *SessionCommon) Ping() (time.Duration, error) { return sc.ys.Ping() }
// Close closes the session.
func (sc *SessionCommon) Close() error {
if sc == nil {
return nil
}
err := sc.ys.Close()
sc.rMx.Lock()
sc.nMap = nil
sc.rMx.Unlock()
return err
}