mirror of https://github.com/schollz/croc.git
collect ipaddr from connecting to tcp
This commit is contained in:
parent
494b224db0
commit
7162e5e45b
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -96,13 +97,12 @@ func (s *server) run() (err error) {
|
||||||
|
|
||||||
func (s *server) clientCommuncation(port string, c *comm.Comm) (err error) {
|
func (s *server) clientCommuncation(port string, c *comm.Comm) (err error) {
|
||||||
// send ok to tell client they are connected
|
// send ok to tell client they are connected
|
||||||
log.Debugf("sending '%s'", s.banner)
|
banner := s.banner
|
||||||
if len(s.banner) > 0 {
|
if len(banner) == 0 {
|
||||||
err = c.Send([]byte(s.banner))
|
banner = "ok"
|
||||||
} else {
|
|
||||||
err = c.Send([]byte("ok"))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
log.Debugf("sending '%s'", banner)
|
||||||
|
err = c.Send([]byte(banner + "|||" + c.Connection().RemoteAddr().String()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -243,7 +243,7 @@ func pipe(conn1 net.Conn, conn2 net.Conn) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConnectToTCPServer(address, room string) (c *comm.Comm, banner string, err error) {
|
func ConnectToTCPServer(address, room string) (c *comm.Comm, banner string, ipaddr string, err error) {
|
||||||
c, err = comm.NewConnection(address)
|
c, err = comm.NewConnection(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -253,7 +253,8 @@ func ConnectToTCPServer(address, room string) (c *comm.Comm, banner string, err
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
banner = string(data)
|
banner = strings.Split(string(data), "|||")[0]
|
||||||
|
ipaddr = strings.Split(string(data), "|||")[1]
|
||||||
log.Debug("sending room")
|
log.Debug("sending room")
|
||||||
err = c.Send([]byte(room))
|
err = c.Send([]byte(room))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package tcp
|
package tcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -8,13 +9,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTCP(t *testing.T) {
|
func TestTCP(t *testing.T) {
|
||||||
go Run("debug", "8081")
|
go Run("debug", "8081", "8082")
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
c1, _, err := ConnectToTCPServer("localhost:8081", "testRoom")
|
c1, banner, ipaddr, err := ConnectToTCPServer("localhost:8081", "testRoom")
|
||||||
|
assert.Equal(t, banner, "8082")
|
||||||
|
assert.True(t, strings.HasPrefix(ipaddr, "127.0.0.1"))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
c2, _, err := ConnectToTCPServer("localhost:8081", "testRoom")
|
c2, _, _, err := ConnectToTCPServer("localhost:8081", "testRoom")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
_, _, err = ConnectToTCPServer("localhost:8081", "testRoom")
|
_, _, _, err = ConnectToTCPServer("localhost:8081", "testRoom")
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
// try sending data
|
// try sending data
|
||||||
|
|
Loading…
Reference in New Issue