5
"net"
6
"time"
7
8
- msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
8
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10
15
16
// secureConn wraps another Conn object with an encrypted channel.
17
type secureConn struct {
19
-
20
- // the wrapped conn
21
- insecure Conn
22
-
23
- // secure io (wrapping insecure)
24
- secure msgio.ReadWriteCloser
25
-
26
- // secure Session
27
- session secio.Session
18
+ insecure Conn // the wrapped conn
19
+ secure secio.Session // secure Session
20
}
21
22
// newConn constructs a new connection
29
return nil, errors.New("insecure.LocalPeer() is nil")
30
}
31
if sk == nil {
40
- panic("way")
32
return nil, errors.New("private key is nil")
33
}
34
35
// NewSession performs the secure handshake, which takes multiple RTT
36
sessgen := secio.SessionGenerator{LocalID: insecure.LocalPeer(), PrivateKey: sk}
46
- session, err := sessgen.NewSession(ctx, insecure)
37
+ secure, err := sessgen.NewSession(ctx, insecure)
38
if err != nil {
39
return nil, err
40
}
41
42
conn := &secureConn{
43
insecure: insecure,
53
- session: session,
54
- secure: session.ReadWriter(),
44
+ secure: secure,
45
}
56
- log.Debugf("newSecureConn: %v to %v handshake success!", conn.LocalPeer(), conn.RemotePeer())
46
return conn, nil
47
}
48
91
92
// LocalPeer is the Peer on this side
93
func (c *secureConn) LocalPeer() peer.ID {
105
- return c.session.LocalPeer()
94
+ return c.secure.LocalPeer()
95
}
96
97
// RemotePeer is the Peer on the remote side
98
func (c *secureConn) RemotePeer() peer.ID {
110
- return c.session.RemotePeer()
99
+ return c.secure.RemotePeer()
100
}
101
102
// LocalPrivateKey is the public key of the peer on this side
103
func (c *secureConn) LocalPrivateKey() ic.PrivKey {
115
- return c.session.LocalPrivateKey()
104
+ return c.secure.LocalPrivateKey()
105
}
106
107
// RemotePubKey is the public key of the peer on the remote side
108
func (c *secureConn) RemotePublicKey() ic.PubKey {
120
- return c.session.RemotePublicKey()
109
+ return c.secure.RemotePublicKey()
110
}
111
112
// Read reads data, net.Conn style
113
func (c *secureConn) Read(buf []byte) (int, error) {
125
- return c.secure.Read(buf)
114
+ return c.secure.ReadWriter().Read(buf)
115
}
116
117
// Write writes data, net.Conn style
118
func (c *secureConn) Write(buf []byte) (int, error) {
130
- return c.secure.Write(buf)
119
+ return c.secure.ReadWriter().Write(buf)
120
}
121
122
func (c *secureConn) NextMsgLen() (int, error) {
134
- return c.secure.NextMsgLen()
123
+ return c.secure.ReadWriter().NextMsgLen()
124
}
125
126
// ReadMsg reads data, net.Conn style
127
func (c *secureConn) ReadMsg() ([]byte, error) {
139
- return c.secure.ReadMsg()
128
+ return c.secure.ReadWriter().ReadMsg()
129
}
130
131
// WriteMsg writes data, net.Conn style
132
func (c *secureConn) WriteMsg(buf []byte) error {
144
- return c.secure.WriteMsg(buf)
133
+ return c.secure.ReadWriter().WriteMsg(buf)
134
}
135
136
// ReleaseMsg releases a buffer
137
func (c *secureConn) ReleaseMsg(m []byte) {
149
- c.secure.ReleaseMsg(m)
138
+ c.secure.ReadWriter().ReleaseMsg(m)
139
}