-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
79 lines (61 loc) · 1.86 KB
/
conn.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
package shs
import (
"io"
"net"
"time"
b58 "github.com/jbenet/go-base58"
transport "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)
// Conn is an encrypted connection to a remote shs host.
type Conn struct {
io.Reader
io.Writer
lowerConn manet.Conn
remote []byte
t *Transport
}
// LocalMultiaddr returns the local Multiaddr
func (c Conn) LocalMultiaddr() ma.Multiaddr {
return ma.Join(c.lowerConn.LocalMultiaddr(), pubKeyToMA(c.t.keys.Public[:]))
}
// LocalMultiaddr returns the remote end's Multiaddr
func (c Conn) RemoteMultiaddr() ma.Multiaddr {
return ma.Join(c.lowerConn.RemoteMultiaddr(), pubKeyToMA(c.remote))
}
// Close closes the underlying net.Conn
func (c Conn) Close() error {
return c.lowerConn.Close()
}
// LocalAddr returns the local net.Addr with the local public key
func (c Conn) LocalAddr() net.Addr {
return Addr{c.lowerConn.LocalAddr(), c.t.keys.Public[:]}
}
// RemoteAddr returns the remote net.Addr with the remote public key
func (c Conn) RemoteAddr() net.Addr {
return Addr{c.lowerConn.RemoteAddr(), c.remote}
}
// SetDeadline passes the call to the underlying net.Conn
func (c Conn) SetDeadline(t time.Time) error {
return c.lowerConn.SetDeadline(t)
}
// SetReadDeadline passes the call to the underlying net.Conn
func (c Conn) SetReadDeadline(t time.Time) error {
return c.lowerConn.SetReadDeadline(t)
}
// SetWriteDeadline passes the call to the underlying net.Conn
func (c Conn) SetWriteDeadline(t time.Time) error {
return c.lowerConn.SetWriteDeadline(t)
}
func (c Conn) Transport() transport.Transport {
return c.t
}
func pubKeyToMA(pub []byte) ma.Multiaddr {
b58Str := b58.Encode(pub)
a, err := ma.NewMultiaddr(proto.Name + "/" + b58Str)
if err != nil {
panic(err) // TODO find a better way but interface doesn't accept errors
}
return a
}