2022-07-11 17:29:01 -04:00
|
|
|
package noise
|
2022-07-11 23:41:58 -04:00
|
|
|
|
|
|
|
import (
|
2022-10-17 02:03:59 -04:00
|
|
|
"bytes"
|
2022-09-19 14:35:49 -04:00
|
|
|
"fmt"
|
2022-07-18 12:32:16 -04:00
|
|
|
"net"
|
2022-10-17 02:03:59 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2022-07-18 12:32:16 -04:00
|
|
|
|
2022-07-11 23:41:58 -04:00
|
|
|
cb "github.com/emirpasic/gods/queues/circularbuffer"
|
2022-10-17 02:03:59 -04:00
|
|
|
"github.com/flynn/noise"
|
2022-07-18 12:32:16 -04:00
|
|
|
|
|
|
|
"github.com/go-i2p/go-i2p/lib/common/router_info"
|
2022-07-11 23:41:58 -04:00
|
|
|
"github.com/go-i2p/go-i2p/lib/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NoiseSession struct {
|
2022-07-18 12:32:16 -04:00
|
|
|
router_info.RouterInfo
|
2022-10-17 02:03:59 -04:00
|
|
|
*noise.CipherState
|
|
|
|
sync.Mutex
|
|
|
|
*sync.Cond
|
2022-12-15 23:52:05 +00:00
|
|
|
*NoiseTransport // The parent transport, which "Dialed" the connection to the peer whith whom we established the session
|
2022-11-14 00:10:58 -05:00
|
|
|
RecvQueue *cb.Queue
|
|
|
|
SendQueue *cb.Queue
|
2022-12-15 23:52:05 +00:00
|
|
|
SendKey noise.DHKey
|
|
|
|
RecvKey noise.DHKey
|
|
|
|
HandKey noise.DHKey
|
2022-11-13 22:24:31 -05:00
|
|
|
VerifyCallback VerifyCallbackFunc
|
2022-10-17 02:03:59 -04:00
|
|
|
handshakeBuffer bytes.Buffer
|
|
|
|
activeCall int32
|
|
|
|
handshakeComplete bool
|
|
|
|
Conn net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr implements net.Conn
|
2022-12-12 17:44:43 +00:00
|
|
|
func (noise_session *NoiseSession) RemoteAddr() net.Addr {
|
|
|
|
return &noise_session.RouterInfo
|
2022-10-17 02:03:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDeadline implements net.Conn
|
2022-12-12 17:44:43 +00:00
|
|
|
func (noise_session *NoiseSession) SetDeadline(t time.Time) error {
|
|
|
|
return noise_session.Conn.SetDeadline(t)
|
2022-10-17 02:03:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadDeadline implements net.Conn
|
2022-12-12 17:44:43 +00:00
|
|
|
func (noise_session *NoiseSession) SetReadDeadline(t time.Time) error {
|
|
|
|
return noise_session.Conn.SetReadDeadline(t)
|
2022-10-17 02:03:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteDeadline implements net.Conn
|
2022-12-12 17:44:43 +00:00
|
|
|
func (noise_session *NoiseSession) SetWriteDeadline(t time.Time) error {
|
|
|
|
return noise_session.Conn.SetWriteDeadline(t)
|
2022-07-11 23:41:58 -04:00
|
|
|
}
|
|
|
|
|
2024-10-03 22:36:23 -04:00
|
|
|
var exampleNoiseSession transport.TransportSession = &NoiseSession{}
|
|
|
|
var ExampleNoiseSession net.Conn = exampleNoiseSession.(*NoiseSession)
|
2022-10-17 02:03:59 -04:00
|
|
|
|
|
|
|
func (s *NoiseSession) LocalAddr() net.Addr {
|
|
|
|
return s.Conn.LocalAddr()
|
|
|
|
}
|
2022-07-11 23:41:58 -04:00
|
|
|
|
|
|
|
func (s *NoiseSession) Close() error {
|
2022-11-14 00:10:58 -05:00
|
|
|
s.SendQueue.Clear()
|
|
|
|
s.RecvQueue.Clear()
|
2022-07-11 23:41:58 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:24:31 -05:00
|
|
|
func (c *NoiseSession) processCallback(publicKey []byte, payload []byte) error {
|
|
|
|
if c.VerifyCallback == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := c.VerifyCallback(publicKey, payload)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:46:57 -05:00
|
|
|
// newBlock allocates a new packet, from hc's free list if possible.
|
2022-11-14 00:10:58 -05:00
|
|
|
func newBlock() []byte {
|
|
|
|
return make([]byte, MaxPayloadSize)
|
2022-11-13 23:46:57 -05:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:24:31 -05:00
|
|
|
type VerifyCallbackFunc func(publicKey []byte, data []byte) error
|
|
|
|
|
2022-09-19 14:35:49 -04:00
|
|
|
func NewNoiseTransportSession(ri router_info.RouterInfo) (transport.TransportSession, error) {
|
2024-10-03 22:36:23 -04:00
|
|
|
//socket, err := DialNoise("noise", ri)
|
2022-12-15 23:52:05 +00:00
|
|
|
for _, addr := range ri.RouterAddresses() {
|
|
|
|
socket, err := net.Dial("tcp", string(addr.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &NoiseSession{
|
|
|
|
SendQueue: cb.New(1024),
|
|
|
|
RecvQueue: cb.New(1024),
|
|
|
|
RouterInfo: ri,
|
|
|
|
Conn: socket,
|
|
|
|
}, nil
|
2022-09-19 14:35:49 -04:00
|
|
|
}
|
2022-12-15 23:52:05 +00:00
|
|
|
return nil, fmt.Errorf("Transport constructor error")
|
2022-09-19 14:35:49 -04:00
|
|
|
}
|