diff --git a/src/croc/croc.go b/src/croc/croc.go index eacb0d7..49a8208 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -642,9 +642,8 @@ func (c *Client) procesMesssagePake(m message.Message) (err error) { salt := make([]byte, 8) rand.Read(salt) err = message.Send(c.conn[0], c.Key, message.Message{ - Type: "salt", - Bytes: salt, - Message: c.ExternalIP, + Type: "salt", + Bytes: salt, }) if err != nil { return @@ -684,9 +683,8 @@ func (c *Client) processMessageSalt(m message.Message) (done bool, err error) { if !c.Options.IsSender { log.Debug("sending salt back") err = message.Send(c.conn[0], c.Key, message.Message{ - Type: "salt", - Bytes: m.Bytes, - Message: c.ExternalIP, + Type: "salt", + Bytes: m.Bytes, }) } log.Debugf("session key is verified, generating encryption with salt: %x", m.Bytes) @@ -699,6 +697,27 @@ func (c *Client) processMessageSalt(m message.Message) (done bool, err error) { return true, err } log.Debugf("key = %+x", c.Key) + if c.Options.IsSender { + log.Debug("sending external IP") + err = message.Send(c.conn[0], c.Key, message.Message{ + Type: "externalip", + Bytes: m.Bytes, + }) + } + return +} + +func (c *Client) processExternalIP(m message.Message) (done bool, err error) { + log.Debug("received external IP") + if !c.Options.IsSender { + err = message.Send(c.conn[0], c.Key, message.Message{ + Type: "externalip", + Message: c.ExternalIP, + }) + if err != nil { + return true, err + } + } if c.ExternalIPConnected == "" { // it can be preset by the local relay c.ExternalIPConnected = m.Message @@ -711,6 +730,7 @@ func (c *Client) processMessageSalt(m message.Message) (done bool, err error) { func (c *Client) processMessage(payload []byte) (done bool, err error) { m, err := message.Decode(c.Key, payload) if err != nil { + err = fmt.Errorf("problem with decoding: %s", err.Error()) return } @@ -726,6 +746,8 @@ func (c *Client) processMessage(payload []byte) (done bool, err error) { err = c.procesMesssagePake(m) case "salt": done, err = c.processMessageSalt(m) + case "externalip": + done, err = c.processExternalIP(m) case "error": // c.spinner.Stop() fmt.Print("\r") diff --git a/src/croc/croc_test.go b/src/croc/croc_test.go index 15811e0..30464d9 100644 --- a/src/croc/croc_test.go +++ b/src/croc/croc_test.go @@ -12,15 +12,18 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCroc(t *testing.T) { +func init() { log.SetLevel("trace") - defer os.Remove("README.md") go tcp.Run("debug", "8081", "pass123", "8082,8083,8084,8085") go tcp.Run("debug", "8082", "pass123") go tcp.Run("debug", "8083", "pass123") go tcp.Run("debug", "8084", "pass123") go tcp.Run("debug", "8085", "pass123") - time.Sleep(1 * time.Second) + time.Sleep(5 * time.Second) +} + +func TestCrocReadme(t *testing.T) { + defer os.Remove("README.md") log.Debug("setting up sender") sender, err := New(Options{ diff --git a/src/message/message.go b/src/message/message.go index d46f983..2f4b9dc 100644 --- a/src/message/message.go +++ b/src/message/message.go @@ -24,11 +24,11 @@ func (m Message) String() string { // Send will send out func Send(c *comm.Comm, key []byte, m Message) (err error) { + log.Debugf("writing %s message", m.Type) mSend, err := Encode(key, m) if err != nil { return } - log.Debugf("writing %s message (%d bytes)", m.Type, len(mSend)) _, err = c.Write(mSend) return }