This commit is contained in:
Zack Scholl 2019-04-29 17:38:49 -06:00
parent 06541ee0d3
commit 7d9b199ca3
1 changed files with 14 additions and 15 deletions

View File

@ -9,15 +9,16 @@ import (
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/schollz/croc/src/comm" "github.com/schollz/croc/v6/src/comm"
"github.com/schollz/croc/src/logger" "github.com/schollz/croc/v6/src/logger"
) )
const TCP_BUFFER_SIZE = 1024 * 64 const TCP_BUFFER_SIZE = 1024 * 64
type server struct { type server struct {
port string port string
rooms roomMap debugLevel string
rooms roomMap
} }
type roomInfo struct { type roomInfo struct {
@ -32,18 +33,16 @@ type roomMap struct {
sync.Mutex sync.Mutex
} }
var rooms roomMap
// Run starts a tcp listener, run async // Run starts a tcp listener, run async
func Run(debugLevel, port string) { func Run(debugLevel, port string) {
s = new(server) s := new(server)
s.port = port s.port = port
s.debugLevel = debugLevel s.debugLevel = debugLevel
s.start() s.start()
} }
func (s *server) start() { func (s *server) start() {
logger.SetLogLevel(debugLevel) logger.SetLogLevel(s.debugLevel)
s.rooms.Lock() s.rooms.Lock()
s.rooms.rooms = make(map[string]roomInfo) s.rooms.rooms = make(map[string]roomInfo)
s.rooms.Unlock() s.rooms.Unlock()
@ -62,17 +61,17 @@ func (s *server) start() {
} }
}() }()
err := run(port) err := s.run()
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
} }
func (s *server) run() (err error) { func (s *server) run() (err error) {
log.Debugf("starting TCP server on " + port) log.Debugf("starting TCP server on " + s.port)
server, err := net.Listen("tcp", "0.0.0.0:"+port) server, err := net.Listen("tcp", "0.0.0.0:"+s.port)
if err != nil { if err != nil {
return errors.Wrap(err, "Error listening on :"+port) return errors.Wrap(err, "Error listening on :"+s.port)
} }
defer server.Close() defer server.Close()
// spawn a new goroutine whenever a client connects // spawn a new goroutine whenever a client connects
@ -83,15 +82,15 @@ func (s *server) run() (err error) {
} }
log.Debugf("client %s connected", connection.RemoteAddr().String()) log.Debugf("client %s connected", connection.RemoteAddr().String())
go func(port string, connection net.Conn) { go func(port string, connection net.Conn) {
errCommunication := clientCommuncation(port, comm.New(connection)) errCommunication := s.clientCommuncation(port, comm.New(connection))
if errCommunication != nil { if errCommunication != nil {
log.Warnf("relay-%s: %s", connection.RemoteAddr().String(), errCommunication.Error()) log.Warnf("relay-%s: %s", connection.RemoteAddr().String(), errCommunication.Error())
} }
}(port, connection) }(s.port, connection)
} }
} }
func 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.Debug("sending ok") log.Debug("sending ok")
err = c.Send([]byte("ok")) err = c.Send([]byte("ok"))