fix if statement in runHandshake, clean up code

This commit is contained in:
noot 2019-08-25 03:42:29 -04:00
parent 048274e445
commit 7df7e33044
3 changed files with 23 additions and 22 deletions

View File

@ -14,7 +14,7 @@ import (
"io"
mrand "math/rand"
"testing"
"time"
//"time"
)
func generateKey(seed int64) (crypto.PrivKey, error) {
@ -102,18 +102,17 @@ func TestLibp2pIntegration(t *testing.T) {
t.Fatal(err)
}
stream, err := ha.NewStream(ctx, hb.ID(), ID)
if err != nil {
t.Fatal(err)
}
// stream, err := ha.NewStream(ctx, hb.ID(), ID)
// if err != nil {
// t.Fatal(err)
// }
_, err = stream.Write([]byte("hello\n"))
if err != nil {
t.Fatal(err)
}
// _, err = stream.Write([]byte("hello\n"))
// if err != nil {
// t.Fatal(err)
// }
time.Sleep(5 * time.Second)
fmt.Println("fin")
// fmt.Println("fin")
}
func handleStream(stream net.Stream) {

View File

@ -138,7 +138,7 @@ func (s *secureSession) verifyPayload(payload *pb.NoiseHandshakePayload, noiseKe
func (s *secureSession) runHandshake(ctx context.Context) error {
// if we have the peer's noise static key and we support noise pipes, we can try IK
if s.noiseStaticKeyCache[s.remotePeer] != [32]byte{} || s.noisePipesSupport {
if s.noiseStaticKeyCache[s.remotePeer] != [32]byte{} && s.noisePipesSupport {
// known static key for peer, try IK //
buf, err := s.runHandshake_ik(ctx)
@ -192,10 +192,10 @@ func (s *secureSession) Read(buf []byte) (int, error) {
//return s.insecure.Read(buf)
l := len(buf)
log.Debug("length", l)
//log.Debug("length", l)
if l <= len(s.msgBuffer) {
log.Debug("length < msgbuffer")
//log.Debug("length < msgbuffer")
copy(buf, s.msgBuffer)
s.msgBuffer = s.msgBuffer[l:]
@ -204,7 +204,7 @@ func (s *secureSession) Read(buf []byte) (int, error) {
l, err := s.readLength()
if err != nil {
log.Error("read length err", err)
//log.Error("read length err", err)
return 0, err
}
@ -212,13 +212,13 @@ func (s *secureSession) Read(buf []byte) (int, error) {
_, err = s.rw.Read(ciphertext)
if err != nil {
log.Error("read ciphertext err", err)
//log.Error("read ciphertext err", err)
return 0, err
}
plaintext, err := s.Decrypt(ciphertext)
if err != nil {
log.Error("decrypt err", err)
//log.Error("decrypt err", err)
return 0, err
}
@ -228,7 +228,7 @@ func (s *secureSession) Read(buf []byte) (int, error) {
s.msgBuffer = append(s.msgBuffer, plaintext[len(buf):]...)
}
log.Debug("read", "plaintext", plaintext, len(plaintext))
//log.Debug("read", "plaintext", plaintext, len(plaintext))
return c, nil
}

View File

@ -215,7 +215,9 @@ func (s *secureSession) runHandshake_xx(ctx context.Context, fallback bool, init
log.Error("stage 2 initiator verify payload", "err", err)
}
s.noiseStaticKeyCache[s.remotePeer] = s.xx_ns.RemoteKey()
if s.noisePipesSupport {
s.noiseStaticKeyCache[s.remotePeer] = s.xx_ns.RemoteKey()
}
} else {
@ -278,8 +280,6 @@ func (s *secureSession) runHandshake_xx(ctx context.Context, fallback bool, init
log.Debugf("stage 2 responder", "plaintext", plaintext, "remote key", s.xx_ns.RemoteKey())
//copy(s.remote.noiseKey[:], plaintext)
// unmarshal payload
err = proto.Unmarshal(plaintext, nhp)
if err != nil {
@ -308,7 +308,9 @@ func (s *secureSession) runHandshake_xx(ctx context.Context, fallback bool, init
return fmt.Errorf("stage 2 responder fail: %s", err)
}
s.noiseStaticKeyCache[s.remotePeer] = s.remote.noiseKey
if s.noisePipesSupport {
s.noiseStaticKeyCache[s.remotePeer] = s.remote.noiseKey
}
log.Debugf("stage 2 responder", "remote key", s.remote.noiseKey)
}