croc/src/tcp/tcp.go

284 lines
5.8 KiB
Go
Raw Normal View History

2018-09-23 21:34:29 +02:00
package tcp
import (
2019-04-30 00:24:22 +02:00
"bytes"
"fmt"
2018-09-23 21:34:29 +02:00
"net"
2019-05-02 01:10:02 +02:00
"strings"
2018-09-23 21:34:29 +02:00
"sync"
"time"
log "github.com/cihub/seelog"
"github.com/pkg/errors"
2019-04-30 01:38:49 +02:00
"github.com/schollz/croc/v6/src/comm"
"github.com/schollz/croc/v6/src/logger"
2019-04-30 15:57:45 +02:00
"github.com/schollz/croc/v6/src/models"
2018-09-23 21:34:29 +02:00
)
2019-04-30 01:29:36 +02:00
type server struct {
2019-04-30 01:38:49 +02:00
port string
debugLevel string
2019-05-01 01:05:19 +02:00
banner string
2019-04-30 01:38:49 +02:00
rooms roomMap
2019-04-30 01:29:36 +02:00
}
2018-09-23 21:34:29 +02:00
type roomInfo struct {
2019-04-29 22:06:18 +02:00
first *comm.Comm
second *comm.Comm
2019-04-27 18:20:03 +02:00
opened time.Time
full bool
2018-09-23 21:34:29 +02:00
}
type roomMap struct {
rooms map[string]roomInfo
sync.Mutex
}
// Run starts a tcp listener, run async
2019-05-01 01:05:19 +02:00
func Run(debugLevel, port string, banner ...string) (err error) {
2019-04-30 01:38:49 +02:00
s := new(server)
2019-04-30 01:29:36 +02:00
s.port = port
s.debugLevel = debugLevel
2019-05-01 01:05:19 +02:00
if len(banner) > 0 {
s.banner = banner[0]
}
2019-04-30 06:25:30 +02:00
return s.start()
2019-04-30 01:29:36 +02:00
}
2019-04-30 06:25:30 +02:00
func (s *server) start() (err error) {
2019-04-30 01:38:49 +02:00
logger.SetLogLevel(s.debugLevel)
2019-04-30 01:29:36 +02:00
s.rooms.Lock()
s.rooms.rooms = make(map[string]roomInfo)
s.rooms.Unlock()
2018-10-13 15:13:50 +02:00
// delete old rooms
go func() {
for {
time.Sleep(10 * time.Minute)
2019-05-03 05:57:55 +02:00
roomsToDelete := []string{}
2019-04-30 01:29:36 +02:00
s.rooms.Lock()
for room := range s.rooms.rooms {
if time.Since(s.rooms.rooms[room].opened) > 3*time.Hour {
roomsToDelete = append(roomsToDelete, room)
2018-10-13 15:13:50 +02:00
}
}
2019-04-30 01:29:36 +02:00
s.rooms.Unlock()
2019-05-03 05:57:55 +02:00
for _, room := range roomsToDelete {
s.deleteRoom(room)
}
2018-10-13 15:13:50 +02:00
}
}()
2019-04-30 06:25:30 +02:00
err = s.run()
2018-09-23 21:34:29 +02:00
if err != nil {
log.Error(err)
2019-05-01 01:05:19 +02:00
}
return
2018-09-23 21:34:29 +02:00
}
2019-04-30 01:29:36 +02:00
func (s *server) run() (err error) {
2019-05-02 21:08:23 +02:00
log.Infof("starting TCP server on " + s.port)
2019-04-30 06:33:13 +02:00
server, err := net.Listen("tcp", ":"+s.port)
2018-09-23 21:34:29 +02:00
if err != nil {
2019-04-30 01:38:49 +02:00
return errors.Wrap(err, "Error listening on :"+s.port)
2018-09-23 21:34:29 +02:00
}
defer server.Close()
// spawn a new goroutine whenever a client connects
for {
2018-09-26 19:43:38 +02:00
connection, err := server.Accept()
2018-09-23 21:34:29 +02:00
if err != nil {
return errors.Wrap(err, "problem accepting connection")
}
log.Debugf("client %s connected", connection.RemoteAddr().String())
2018-09-26 19:43:38 +02:00
go func(port string, connection net.Conn) {
2019-04-30 01:38:49 +02:00
errCommunication := s.clientCommuncation(port, comm.New(connection))
2018-09-23 21:34:29 +02:00
if errCommunication != nil {
log.Warnf("relay-%s: %s", connection.RemoteAddr().String(), errCommunication.Error())
}
2019-04-30 01:38:49 +02:00
}(s.port, connection)
2018-09-23 21:34:29 +02:00
}
}
2019-04-30 01:38:49 +02:00
func (s *server) clientCommuncation(port string, c *comm.Comm) (err error) {
2018-09-23 21:34:29 +02:00
// send ok to tell client they are connected
2019-05-02 01:10:02 +02:00
banner := s.banner
if len(banner) == 0 {
banner = "ok"
2019-05-01 20:20:02 +02:00
}
2019-05-02 01:10:02 +02:00
log.Debugf("sending '%s'", banner)
err = c.Send([]byte(banner + "|||" + c.Connection().RemoteAddr().String()))
2018-09-23 21:34:29 +02:00
if err != nil {
return
}
// wait for client to tell me which room they want
2018-10-13 15:09:55 +02:00
log.Debug("waiting for answer")
2019-04-27 18:20:03 +02:00
roomBytes, err := c.Receive()
2018-09-23 21:34:29 +02:00
if err != nil {
return
}
2019-04-27 18:20:03 +02:00
room := string(roomBytes)
2018-09-23 21:34:29 +02:00
2019-04-30 01:29:36 +02:00
s.rooms.Lock()
2019-04-27 18:20:03 +02:00
// create the room if it is new
2019-04-30 01:29:36 +02:00
if _, ok := s.rooms.rooms[room]; !ok {
s.rooms.rooms[room] = roomInfo{
2019-04-27 18:20:03 +02:00
first: c,
opened: time.Now(),
2018-09-23 21:34:29 +02:00
}
2019-04-30 01:29:36 +02:00
s.rooms.Unlock()
2018-09-23 21:34:29 +02:00
// tell the client that they got the room
2019-04-27 18:20:03 +02:00
err = c.Send([]byte("ok"))
2018-09-23 21:34:29 +02:00
if err != nil {
2018-10-13 15:09:55 +02:00
log.Error(err)
2019-05-01 20:11:20 +02:00
s.deleteRoom(room)
2018-09-23 21:34:29 +02:00
return
}
2019-04-27 18:20:03 +02:00
log.Debugf("room %s has 1", room)
2018-09-23 21:34:29 +02:00
return nil
}
2019-04-30 01:29:36 +02:00
if s.rooms.rooms[room].full {
s.rooms.Unlock()
2019-04-27 18:20:03 +02:00
err = c.Send([]byte("room full"))
if err != nil {
log.Error(err)
2019-05-01 20:11:20 +02:00
s.deleteRoom(room)
2019-04-27 18:20:03 +02:00
return
}
return nil
}
log.Debugf("room %s has 2", room)
2019-04-30 01:29:36 +02:00
s.rooms.rooms[room] = roomInfo{
first: s.rooms.rooms[room].first,
2019-04-27 18:20:03 +02:00
second: c,
2019-04-30 01:29:36 +02:00
opened: s.rooms.rooms[room].opened,
2019-04-27 18:20:03 +02:00
full: true,
}
2019-04-30 01:29:36 +02:00
otherConnection := s.rooms.rooms[room].first
s.rooms.Unlock()
2018-09-23 21:34:29 +02:00
// second connection is the sender, time to staple connections
var wg sync.WaitGroup
wg.Add(1)
// start piping
2019-04-29 22:06:18 +02:00
go func(com1, com2 *comm.Comm, wg *sync.WaitGroup) {
2018-09-23 21:34:29 +02:00
log.Debug("starting pipes")
pipe(com1.Connection(), com2.Connection())
wg.Done()
log.Debug("done piping")
2019-04-27 18:20:03 +02:00
}(otherConnection, c, &wg)
2018-09-23 21:34:29 +02:00
// tell the sender everything is ready
2019-04-27 18:20:03 +02:00
err = c.Send([]byte("ok"))
2018-09-23 21:34:29 +02:00
if err != nil {
2019-05-01 20:11:20 +02:00
s.deleteRoom(room)
2018-09-23 21:34:29 +02:00
return
}
wg.Wait()
// delete room
2019-05-01 20:11:20 +02:00
s.deleteRoom(room)
return nil
}
func (s *server) deleteRoom(room string) {
2019-04-30 01:29:36 +02:00
s.rooms.Lock()
2019-05-01 20:11:20 +02:00
defer s.rooms.Unlock()
if _, ok := s.rooms.rooms[room]; !ok {
return
}
2018-09-23 21:34:29 +02:00
log.Debugf("deleting room: %s", room)
2019-04-30 01:29:36 +02:00
s.rooms.rooms[room].first.Close()
s.rooms.rooms[room].second.Close()
s.rooms.rooms[room] = roomInfo{first: nil, second: nil}
delete(s.rooms.rooms, room)
2019-05-01 20:11:20 +02:00
2018-09-23 21:34:29 +02:00
}
// chanFromConn creates a channel from a Conn object, and sends everything it
// Read()s from the socket to the channel.
2018-09-26 19:43:38 +02:00
func chanFromConn(conn net.Conn) chan []byte {
2019-04-29 23:46:40 +02:00
c := make(chan []byte, 1)
2018-09-23 21:34:29 +02:00
go func() {
2019-04-30 15:57:45 +02:00
b := make([]byte, models.TCP_BUFFER_SIZE)
2018-09-26 23:31:45 +02:00
2018-09-23 21:34:29 +02:00
for {
2018-09-26 18:55:14 +02:00
n, err := conn.Read(b)
2018-09-26 15:36:47 +02:00
if n > 0 {
2018-09-26 18:14:24 +02:00
res := make([]byte, n)
// Copy the buffer so it doesn't get changed while read by the recipient.
copy(res, b[:n])
c <- res
2018-09-26 15:36:47 +02:00
}
2018-09-23 21:34:29 +02:00
if err != nil {
2018-10-13 15:09:55 +02:00
log.Debug(err)
2018-09-23 21:34:29 +02:00
c <- nil
break
}
}
2019-04-29 23:46:40 +02:00
log.Debug("exiting")
2018-09-23 21:34:29 +02:00
}()
return c
}
// pipe creates a full-duplex pipe between the two sockets and
// transfers data from one to the other.
2018-09-26 19:43:38 +02:00
func pipe(conn1 net.Conn, conn2 net.Conn) {
2018-09-23 21:34:29 +02:00
chan1 := chanFromConn(conn1)
2018-09-26 23:31:45 +02:00
chan2 := chanFromConn(conn2)
2018-09-23 21:34:29 +02:00
for {
2018-09-26 23:31:45 +02:00
select {
case b1 := <-chan1:
if b1 == nil {
return
}
conn2.Write(b1)
2018-09-26 18:41:12 +02:00
2018-09-26 23:31:45 +02:00
case b2 := <-chan2:
if b2 == nil {
return
}
conn1.Write(b2)
}
2018-09-23 21:34:29 +02:00
}
}
2019-04-30 04:20:03 +02:00
func ConnectToTCPServer(address, room string, timelimit ...time.Duration) (c *comm.Comm, banner string, ipaddr string, err error) {
if len(timelimit) > 0 {
c, err = comm.NewConnection(address, timelimit[0])
} else {
c, err = comm.NewConnection(address)
}
2019-04-30 00:24:22 +02:00
if err != nil {
return
}
2019-05-01 19:45:13 +02:00
log.Debug("waiting for first ok")
2019-04-30 00:24:22 +02:00
data, err := c.Receive()
if err != nil {
return
}
2019-05-02 01:10:02 +02:00
banner = strings.Split(string(data), "|||")[0]
ipaddr = strings.Split(string(data), "|||")[1]
2019-05-01 19:45:13 +02:00
log.Debug("sending room")
2019-04-30 00:24:22 +02:00
err = c.Send([]byte(room))
if err != nil {
return
}
2019-05-01 19:45:13 +02:00
log.Debug("waiting for room confirmation")
2019-04-30 00:24:22 +02:00
data, err = c.Receive()
if err != nil {
return
}
if !bytes.Equal(data, []byte("ok")) {
err = fmt.Errorf("got bad response: %s", data)
return
}
2019-05-01 19:45:13 +02:00
log.Debug("all set")
2019-04-30 00:24:22 +02:00
return
}