-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
103 lines (81 loc) · 2.27 KB
/
listener.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
package shs
import (
"net"
b58 "github.com/jbenet/go-base58"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
bs "github.com/keks/secretstream/boxstream"
shs "github.com/keks/secretstream/secrethandshake"
transport "github.com/libp2p/go-libp2p-transport"
)
// Listener implements the go-libp2p-transport.Listener interface
type Listener struct {
l manet.Listener
t *Transport
}
// NetListener returns a net.Listener that is equivalent to this manet.Listener.
func (l *Listener) NetListener() net.Listener {
return &netListener{l}
}
// Accept waits for an incoming connection and returns it. Else it returns an error.
func (l *Listener) Accept() (transport.Conn, error) {
c, err := l.l.Accept()
if err != nil {
return nil, err
}
state, err := shs.NewServerState(l.t.appKey, l.t.keys)
if err != nil {
return nil, err
}
err = shs.Server(state, c)
if err != nil {
return nil, err
}
enKey, enNonce := state.GetBoxstreamEncKeys()
deKey, deNonce := state.GetBoxstreamDecKeys()
remote := state.Remote()
boxed := Conn{
Reader: bs.NewUnboxer(c, &deNonce, &deKey),
Writer: bs.NewBoxer(c, &enNonce, &enKey),
lowerConn: c,
remote: remote[:],
t: l.t,
}
return boxed, nil
}
// Close closes the listener.
func (l *Listener) Close() error {
// TODO maybe overwrite keys?
return l.l.Close()
}
// Addr returns the net.Addr the Listener bound to.
func (l *Listener) Addr() net.Addr {
return Addr{l.l.Addr(), l.t.keys.Public[:]}
}
// Multiaddr returns the Multiaddr the Listener bound to.
func (l *Listener) Multiaddr() ma.Multiaddr {
return l.l.Multiaddr().Encapsulate(pubKeyToMA(l.t.keys.Public[:]))
}
// Addr implements net.Addr
type Addr struct {
lower net.Addr
pubKey []byte
}
// Network returns the network we are on. Most likely "tcp/shs".
func (a Addr) Network() string {
return a.lower.Network() + "/" + proto.Name
}
// PubKey returns the public key of the node at this address
func (a Addr) PubKey() []byte {
return a.pubKey
}
// String is the string representation of this address.
func (a Addr) String() string {
return a.lower.String() + "/" + b58.Encode(a.pubKey)
}
func maHead(m ma.Multiaddr) (head, tail ma.Multiaddr) {
ms := ma.Split(m)
head = ms[len(ms)-1]
tail = ma.Join(ms[:len(ms)-1]...)
return
}