Skip to content

Commit

Permalink
allow both sender and receiver to reserve a room
Browse files Browse the repository at this point in the history
  • Loading branch information
RCL98 committed Nov 11, 2023
1 parent 20fb6ce commit cb5da0c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 122 deletions.
11 changes: 9 additions & 2 deletions src/croc/croc.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,12 @@ func (c *Client) transferOverLocalRelay(errchan chan error) {
log.Debugf("[%+v] had error: %s", conn, errConn.Error())
break
}
if bytes.Equal(data, handshakeRequest) {
if bytes.Equal(data, ipRequest) {
log.Debug("Got ip request, sending nil since we are local")
if err = conn.Send(nil); err != nil {
log.Errorf("error sending: %v", err)
}
} else if bytes.Equal(data, handshakeRequest) {
wgTransfer.Add(1)
go c.makeLocalTransfer(conn, ipaddr, banner, errchan)
wgTransfer.Wait()
Expand Down Expand Up @@ -989,7 +994,7 @@ func (c *Client) Receive() (err error) {
}

serverTry := net.JoinHostPort(ip, port)
conn, banner2, externalIP, errConn := tcp.ConnectToTCPServer(serverTry, c.Options.RelayPassword, c.Options.SharedSecret[:3], c.Options.IsSender, true, 1, 500*time.Millisecond)
conn, banner2, externalIP, errConn := tcp.ConnectToTCPServer(serverTry, c.Options.RelayPassword, c.Options.SharedSecret[:3], c.Options.IsSender, false, 1, 500*time.Millisecond)
if errConn != nil {
log.Debug(errConn)
log.Debugf("could not connect to " + serverTry)
Expand All @@ -1009,6 +1014,7 @@ func (c *Client) Receive() (err error) {
}
}

log.Debug("sending handshake message")
if err = c.conn[0].Send(handshakeRequest); err != nil {
log.Errorf("handshake send error: %v", err)
}
Expand Down Expand Up @@ -1037,6 +1043,7 @@ func (c *Client) transfer() (err error) {
// if recipient, initialize with sending pake information
log.Debug("ready")
if !c.Options.IsSender && !c.Step1ChannelSecured {
log.Debug("sending pake information")
err = message.Send(c.conn[0], c.Key, message.Message{
Type: message.TypePAKE,
Bytes: c.Pake.Bytes(),
Expand Down
178 changes: 89 additions & 89 deletions src/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type roomInfo struct {
maxTransfers int
doneTransfers int
opened time.Time
transfering bool
}

type roomMap struct {
Expand Down Expand Up @@ -293,39 +292,36 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
}
room = string(roomBytes)

s.rooms.Lock()
if isSender {
if _, ok := s.rooms.rooms[room]; !ok {
// create the room if it is new
err = s.createRoom(c, room, strongKeyForEncryption)
if err != nil {
log.Error(err)
}
// sender is done
return
} else {
// if the room already exists, then tell the client that the room is full
err = s.sendRoomIsFull(c, strongKeyForEncryption)
return
}
log.Debug("Check if this is a main room")
enc, err = c.Receive()
if err != nil {
return
}
data, err = crypt.Decrypt(enc, strongKeyForEncryption)
if err != nil {
return
}
if !bytes.Equal(data, []byte("main")) && !bytes.Equal(data, []byte("secondary")) {
err = fmt.Errorf("got bad response: %s", data)
return
}
isMainRoom := bytes.Equal(data, []byte("main"))
log.Debugf("isMainRoom: %v", isMainRoom)

if _, ok := s.rooms.rooms[room]; !ok {
// if the room does not exist and the client is a receiver, then tell them
// that the room does not exist
s.rooms.Unlock()
bSend, err = crypt.Encrypt([]byte(noRoom), strongKeyForEncryption)
if err != nil {
return
}
err = c.Send(bSend)
s.rooms.Lock()
_, roomExists := s.rooms.rooms[room]
// create the room if it is new
if !roomExists || isSender {
err = s.createOrUpdateRoom(c, room, strongKeyForEncryption, isMainRoom, isSender, roomExists)
if err != nil {
log.Error(err)
return
}

return "", fmt.Errorf("reciever tried to connect to room that does not exist")
} else if s.rooms.rooms[room].transfering {
// if the room is new then return
if !roomExists {
return
}
} else if s.rooms.rooms[room].receiver != nil {
// if the room has a transfer going on
if s.rooms.rooms[room].maxTransfers > 1 {
// if the room is a multi-transfer room then add to queue
Expand All @@ -349,7 +345,6 @@ func (s *server) clientCommunication(port string, c *comm.Comm) (room string, er
maxTransfers: s.rooms.rooms[room].maxTransfers,
doneTransfers: s.rooms.rooms[room].doneTransfers,
opened: s.rooms.rooms[room].opened,
transfering: true,
}
s.rooms.roomLocks[room].Lock()
}
Expand All @@ -376,26 +371,11 @@ func (s *server) sendRoomIsFull(c *comm.Comm, strongKeyForEncryption []byte) (er
return
}

func (s *server) createRoom(c *comm.Comm, room string, strongKeyForEncryption []byte) (err error) {
func (s *server) createOrUpdateRoom(c *comm.Comm, room string, strongKeyForEncryption []byte, isMainRoom, isSender, updateRoom bool) (err error) {
var enc, data, bSend []byte
log.Debug("Check if this is a main room")
enc, err = c.Receive()
if err != nil {
return
}
data, err = crypt.Decrypt(enc, strongKeyForEncryption)
if err != nil {
return
}
if !bytes.Equal(data, []byte("main")) && !bytes.Equal(data, []byte("secondary")) {
err = fmt.Errorf("got bad response: %s", data)
return
}
isMainRoom := bytes.Equal(data, []byte("main"))
log.Debugf("isMainRoom: %v", isMainRoom)

maxTransfers := 1
if isMainRoom {
if isMainRoom && isSender {
log.Debug("Wait for maxTransfers")
enc, err = c.Receive()
if err != nil {
Expand All @@ -413,29 +393,53 @@ func (s *server) createRoom(c *comm.Comm, room string, strongKeyForEncryption []
log.Debugf("maxTransfers: %v", maxTransfers)
}

var sender, receiver *comm.Comm
var queue *list.List
opened := time.Now()
if isSender {
sender = c
if updateRoom {
receiver = s.rooms.rooms[room].receiver
queue = s.rooms.rooms[room].queue
opened = s.rooms.rooms[room].opened
}
} else {
receiver = c
if updateRoom {
sender = s.rooms.rooms[room].sender
queue = s.rooms.rooms[room].queue
opened = s.rooms.rooms[room].opened
}
}

s.rooms.rooms[room] = roomInfo{
sender: c,
receiver: nil,
sender: sender,
receiver: receiver,
queue: queue,
isMainRoom: isMainRoom,
maxTransfers: maxTransfers,
doneTransfers: 0,
opened: time.Now(),
opened: opened,
}
s.rooms.roomLocks[room] = &sync.Mutex{}
s.rooms.Unlock()

// tell the client that they got the room
bSend, err = crypt.Encrypt([]byte("ok"), strongKeyForEncryption)
if err != nil {
return
}
err = c.Send(bSend)
if err != nil {
log.Error(err)
s.deleteRoom(room)
return
if !updateRoom {
log.Debugf("Client crated main room %s, %v", room, isSender)
s.rooms.roomLocks[room] = &sync.Mutex{}
// tell the client that they got the room
bSend, err = crypt.Encrypt([]byte("ok"), strongKeyForEncryption)
if err != nil {
return
}
err = c.Send(bSend)
if err != nil {
log.Error(err)
s.deleteRoom(room)
return
}
log.Debugf("room %s has 1", room)
s.rooms.Unlock()
}
log.Debugf("room %s has 1", room)

return
}

Expand All @@ -455,7 +459,6 @@ func (s *server) handleWaitingRoomForReceivers(c *comm.Comm, room string, strong
maxTransfers: s.rooms.rooms[room].maxTransfers,
doneTransfers: s.rooms.rooms[room].doneTransfers,
queue: queue,
transfering: true,
}
s.rooms.Unlock()

Expand Down Expand Up @@ -500,7 +503,6 @@ func (s *server) handleWaitingRoomForReceivers(c *comm.Comm, room string, strong
maxTransfers: s.rooms.rooms[room].maxTransfers,
doneTransfers: s.rooms.rooms[room].doneTransfers,
opened: s.rooms.rooms[room].opened,
transfering: true,
}
break
}
Expand All @@ -509,7 +511,6 @@ func (s *server) handleWaitingRoomForReceivers(c *comm.Comm, room string, strong
}

func (s *server) beginTransfer(c *comm.Comm, room string, strongKeyForEncryption []byte) (err error) {
otherConnection := s.rooms.rooms[room].sender
s.rooms.Unlock()

// second connection is the sender, time to staple connections
Expand All @@ -522,9 +523,10 @@ func (s *server) beginTransfer(c *comm.Comm, room string, strongKeyForEncryption
pipe(com1.Connection(), com2.Connection())
wg.Done()
log.Debug("done piping")
}(otherConnection, c, &wg)
}(s.rooms.rooms[room].sender, s.rooms.rooms[room].receiver, &wg)

// tell the receiver everything is ready
// tell the client everything is ready
log.Debug("sending ok to client")
bSend, err := crypt.Encrypt([]byte("ok"), strongKeyForEncryption)
if err != nil {
return
Expand All @@ -544,6 +546,7 @@ func (s *server) beginTransfer(c *comm.Comm, room string, strongKeyForEncryption
if s.rooms.rooms[room].queue != nil {
lengthOfQueue = s.rooms.rooms[room].queue.Len()
}
log.Debugf("room %s has %d left in queue", room, lengthOfQueue)
s.rooms.rooms[room] = roomInfo{
sender: s.rooms.rooms[room].sender,
receiver: nil,
Expand All @@ -552,7 +555,6 @@ func (s *server) beginTransfer(c *comm.Comm, room string, strongKeyForEncryption
maxTransfers: s.rooms.rooms[room].maxTransfers,
doneTransfers: newDoneTransfers,
opened: s.rooms.rooms[room].opened,
transfering: lengthOfQueue > 0 && newDoneTransfers < s.rooms.rooms[room].maxTransfers,
}
s.rooms.Unlock()

Expand Down Expand Up @@ -799,13 +801,25 @@ func ConnectToTCPServer(address, password, room string, isSender, isMainRoom boo
return
}

if isSender {
log.Debug("tell server if this is a main room")
roomType := "secondary"
if isMainRoom {
roomType = "main"
}
bSend, err = crypt.Encrypt([]byte(roomType), strongKeyForEncryption)
log.Debug("tell server if this is a main room")
roomType := "secondary"
if isMainRoom {
roomType = "main"
}
bSend, err = crypt.Encrypt([]byte(roomType), strongKeyForEncryption)
if err != nil {
log.Debug(err)
return
}
err = c.Send(bSend)
if err != nil {
log.Debug(err)
return
}

if isMainRoom && isSender {
log.Debug("tell server maxTransfers")
bSend, err = crypt.Encrypt([]byte(strconv.Itoa(maxTransfers)), strongKeyForEncryption)
if err != nil {
log.Debug(err)
return
Expand All @@ -815,20 +829,6 @@ func ConnectToTCPServer(address, password, room string, isSender, isMainRoom boo
log.Debug(err)
return
}

if isMainRoom {
log.Debug("tell server maxTransfers")
bSend, err = crypt.Encrypt([]byte(strconv.Itoa(maxTransfers)), strongKeyForEncryption)
if err != nil {
log.Debug(err)
return
}
err = c.Send(bSend)
if err != nil {
log.Debug(err)
return
}
}
}

log.Debug("waiting for room confirmation")
Expand Down
Loading

0 comments on commit cb5da0c

Please sign in to comment.