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"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -96,13 +97,12 @@ func (s *server) run() (err error) {
|
|||
|
||||
func (s *server) clientCommuncation(port string, c *comm.Comm) (err error) {
|
||||
// send ok to tell client they are connected
|
||||
log.Debugf("sending '%s'", s.banner)
|
||||
if len(s.banner) > 0 {
|
||||
err = c.Send([]byte(s.banner))
|
||||
} else {
|
||||
err = c.Send([]byte("ok"))
|
||||
|
||||
banner := s.banner
|
||||
if len(banner) == 0 {
|
||||
banner = "ok"
|
||||
}
|
||||
log.Debugf("sending '%s'", banner)
|
||||
err = c.Send([]byte(banner + "|||" + c.Connection().RemoteAddr().String()))
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -253,7 +253,8 @@ func ConnectToTCPServer(address, room string) (c *comm.Comm, banner string, err
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
banner = string(data)
|
||||
banner = strings.Split(string(data), "|||")[0]
|
||||
ipaddr = strings.Split(string(data), "|||")[1]
|
||||
log.Debug("sending room")
|
||||
err = c.Send([]byte(room))
|
||||
if err != nil {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package tcp
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -8,13 +9,15 @@ import (
|
|||
)
|
||||
|
||||
func TestTCP(t *testing.T) {
|
||||
go Run("debug", "8081")
|
||||
go Run("debug", "8081", "8082")
|
||||
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)
|
||||
c2, _, err := ConnectToTCPServer("localhost:8081", "testRoom")
|
||||
c2, _, _, err := ConnectToTCPServer("localhost:8081", "testRoom")
|
||||
assert.Nil(t, err)
|
||||
_, _, err = ConnectToTCPServer("localhost:8081", "testRoom")
|
||||
_, _, _, err = ConnectToTCPServer("localhost:8081", "testRoom")
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// try sending data
|
||||
|
|
Loading…
Reference in New Issue