refactor: make 'inner' encoder/decoder explicit

This commit is contained in:
Matthias Radestock committed 2015-07-20 23:50:10 +01:00
1 parent 4c9b0be117
commit ad4e4ce6b8
2 files changed
+22 -28

No files matched your search

+10 -14
View File
@@ -337,9 +337,9 @@ type SimpleTCPSender struct {
}
type EncryptedTCPSender struct {
SimpleTCPSender
sync.RWMutex
state *TCPCryptoState
sender TCPSender
state *TCPCryptoState
}
func NewSimpleTCPSender(encoder *gob.Encoder) *SimpleTCPSender {
@@ -350,10 +350,8 @@ func (sender *SimpleTCPSender) Send(msg []byte) error {
return sender.encoder.Encode(msg)
}
func NewEncryptedTCPSender(encoder *gob.Encoder, sessionKey *[32]byte, outbound bool) *EncryptedTCPSender {
return &EncryptedTCPSender{
SimpleTCPSender: *NewSimpleTCPSender(encoder),
state: NewTCPCryptoState(sessionKey, outbound)}
func NewEncryptedTCPSender(sender TCPSender, sessionKey *[32]byte, outbound bool) *EncryptedTCPSender {
return &EncryptedTCPSender{sender: sender, state: NewTCPCryptoState(sessionKey, outbound)}
}
func (sender *EncryptedTCPSender) Send(msg []byte) error {
@@ -361,7 +359,7 @@ func (sender *EncryptedTCPSender) Send(msg []byte) error {
defer sender.Unlock()
encodedMsg := secretbox.Seal(nil, msg, &sender.state.nonce, sender.state.sessionKey)
sender.state.advance()
return sender.SimpleTCPSender.Send(encodedMsg)
return sender.sender.Send(encodedMsg)
}
type TCPReceiver interface {
@@ -373,8 +371,8 @@ type SimpleTCPReceiver struct {
}
type EncryptedTCPReceiver struct {
SimpleTCPReceiver
state *TCPCryptoState
receiver TCPReceiver
state *TCPCryptoState
}
func NewSimpleTCPReceiver(decoder *gob.Decoder) *SimpleTCPReceiver {
@@ -387,14 +385,12 @@ func (receiver *SimpleTCPReceiver) Receive() ([]byte, error) {
return msg, err
}
func NewEncryptedTCPReceiver(decoder *gob.Decoder, sessionKey *[32]byte, outbound bool) *EncryptedTCPReceiver {
return &EncryptedTCPReceiver{
SimpleTCPReceiver: *NewSimpleTCPReceiver(decoder),
state: NewTCPCryptoState(sessionKey, !outbound)}
func NewEncryptedTCPReceiver(receiver TCPReceiver, sessionKey *[32]byte, outbound bool) *EncryptedTCPReceiver {
return &EncryptedTCPReceiver{receiver: receiver, state: NewTCPCryptoState(sessionKey, !outbound)}
}
func (receiver *EncryptedTCPReceiver) Receive() ([]byte, error) {
msg, err := receiver.SimpleTCPReceiver.Receive()
msg, err := receiver.receiver.Receive()
if err != nil {
return nil, err
}
+12 -14
View File
@@ -180,12 +180,13 @@ func (res *ProtocolIntroResults) doIntroV1(params ProtocolIntroParams, pubKey, p
return err
}
res.Sender = NewSimpleTCPSender(enc)
res.Receiver = NewSimpleTCPReceiver(dec)
if pubKey == nil {
if _, present := res.Features["PublicKey"]; present {
return ErrExpectedNoCrypto
}
res.setupNoCrypto(enc, dec)
} else {
remotePubKeyStr, ok := res.Features["PublicKey"]
if !ok {
@@ -197,7 +198,7 @@ func (res *ProtocolIntroResults) doIntroV1(params ProtocolIntroParams, pubKey, p
return err
}
res.setupCrypto(params, enc, dec, remotePubKey, privKey)
res.setupCrypto(params, remotePubKey, privKey)
}
res.Features = filterV1Features(res.Features)
@@ -265,7 +266,8 @@ func (res *ProtocolIntroResults) doIntroV2(params ProtocolIntroParams, pubKey, p
return ErrExpectedCrypto
}
res.setupNoCrypto(gob.NewEncoder(params.Conn), gob.NewDecoder(params.Conn))
res.Sender = NewSimpleTCPSender(gob.NewEncoder(params.Conn))
res.Receiver = NewSimpleTCPReceiver(gob.NewDecoder(params.Conn))
case 1:
if pubKey == nil {
@@ -277,7 +279,9 @@ func (res *ProtocolIntroResults) doIntroV2(params ProtocolIntroParams, pubKey, p
return err
}
res.setupCrypto(params, gob.NewEncoder(params.Conn), gob.NewDecoder(params.Conn), rbuf, privKey)
res.Sender = NewSimpleTCPSender(gob.NewEncoder(params.Conn))
res.Receiver = NewSimpleTCPReceiver(gob.NewDecoder(params.Conn))
res.setupCrypto(params, rbuf, privKey)
default:
return fmt.Errorf("Bad encryption flag %d", rbuf[0])
@@ -314,18 +318,12 @@ func (res *ProtocolIntroResults) doIntroV2(params ProtocolIntroParams, pubKey, p
return nil
}
func (res *ProtocolIntroResults) setupNoCrypto(enc *gob.Encoder, dec *gob.Decoder) {
res.Sender = NewSimpleTCPSender(enc)
res.Receiver = NewSimpleTCPReceiver(dec)
}
func (res *ProtocolIntroResults) setupCrypto(params ProtocolIntroParams,
enc *gob.Encoder, dec *gob.Decoder, remotePubKey []byte, privKey *[32]byte) {
func (res *ProtocolIntroResults) setupCrypto(params ProtocolIntroParams, remotePubKey []byte, privKey *[32]byte) {
var remotePubKeyArr [32]byte
copy(remotePubKeyArr[:], remotePubKey)
res.SessionKey = FormSessionKey(&remotePubKeyArr, privKey, params.Password)
res.Sender = NewEncryptedTCPSender(enc, res.SessionKey, params.Outbound)
res.Receiver = NewEncryptedTCPReceiver(dec, res.SessionKey, params.Outbound)
res.Sender = NewEncryptedTCPSender(res.Sender, res.SessionKey, params.Outbound)
res.Receiver = NewEncryptedTCPReceiver(res.Receiver, res.SessionKey, params.Outbound)
}
type ProtocolTag byte