Skip to content

Commit

Permalink
Fix credentials not being returned along with the request ID (#78)
Browse files Browse the repository at this point in the history
Client expects credentials to be returned as a promise, but it can't
match the response to the request due to response ID not being set:

```typescript
if (packet.rid !== undefined) { // this is always false
        const request = this.requests.get(packet.rid)
        if (request != null) {
          this.requests.delete(packet.rid)
          if (packet.type === 'error') {
            request.reject(new SignalingError('server-error', packet.message))
          } else {
            request.resolve(packet)
          }
        }
      }
```

This causes TURN to be completely broken since no credentials are
available.

Tested locally by hardcoding some creds:

```go
func (c *CredentialsClient) GetCredentials(ctx context.Context) (*Credentials, error) {
	return &Credentials{
		URL:        "someURL",
		Username:   "bsusername",
		Credential: "bspassword",
		Lifetime:   123,
	}, nil
}
```

Happy to make adjustments as I have no experience in Go :)
  • Loading branch information
remvst authored Apr 12, 2024
1 parent b524870 commit e92409c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 9 additions & 5 deletions internal/signaling/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,20 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
util.ErrorAndDisconnect(ctx, conn, err)
}

typeOnly := struct{ Type string }{}
if err := json.Unmarshal(raw, &typeOnly); err != nil {
base := struct {
Type string `json:"type"`
RequestID string `json:"rid"`
}{}
if err := json.Unmarshal(raw, &base); err != nil {
util.ErrorAndDisconnect(ctx, conn, err)
}

if peer.closedPacketReceived {
logger.Warn("received packet after close", zap.String("peer", peer.ID), zap.String("type", typeOnly.Type))
logger.Warn("received packet after close", zap.String("peer", peer.ID), zap.String("type", base.Type))
continue
}

switch typeOnly.Type {
switch base.Type {
case "credentials":
credentials, err := cloudflare.GetCredentials(ctx)
if err != nil {
Expand All @@ -123,6 +126,7 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
packet := CredentialsPacket{
Type: "credentials",
Credentials: *credentials,
RequestID: base.RequestID,
}
if err := peer.Send(ctx, packet); err != nil {
util.ErrorAndDisconnect(ctx, conn, err)
Expand All @@ -140,7 +144,7 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
// ignore, ping/pong is just for the tcp keepalive.

default:
if err := peer.HandlePacket(ctx, typeOnly.Type, raw); err != nil {
if err := peer.HandlePacket(ctx, base.Type, raw); err != nil {
util.ErrorAndDisconnect(ctx, conn, err)
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/signaling/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ type ForwardablePacket struct {

type CredentialsPacket struct {
cloudflare.Credentials
Type string `json:"type"`
Type string `json:"type"`
RequestID string `json:"rid,omitempty"`
}

type EventPacket struct {
Expand Down

0 comments on commit e92409c

Please sign in to comment.