Skip to content

Commit

Permalink
fix: added stun checker&provider to webrtc
Browse files Browse the repository at this point in the history
  • Loading branch information
lchenut committed Nov 15, 2024
1 parent 10d2389 commit 302aae5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion webrtc/stun/stun_attributes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ proc encode*(T: typedesc[UsernameAttribute], username: string): RawStunAttribute
return UsernameAttribute.encode(username.toBytes())

proc decode*(T: typedesc[UsernameAttribute], rawAttr: RawStunAttribute): T =
return Binary.decode(rawAttr.value, T)
result.username = rawAttr.value

# Error Code
# https://datatracker.ietf.org/doc/html/rfc5389#section-15.6
Expand Down
6 changes: 3 additions & 3 deletions webrtc/stun/stun_transport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ proc stop*(self: Stun) {.async: (raises: []).} =
await self.readingLoop.cancelAndWait()
untrackCounter(StunTransportTracker)

proc defaultUsernameProvider(): string = ""
proc defaultUsernameChecker(username: seq[byte]): bool = true
proc defaultPasswordProvider(username: seq[byte]): seq[byte] = @[]
proc defaultUsernameProvider*(): string = ""
proc defaultUsernameChecker*(username: seq[byte]): bool = true
proc defaultPasswordProvider*(username: seq[byte]): seq[byte] = @[]

proc new*(
T: type Stun,
Expand Down
13 changes: 10 additions & 3 deletions webrtc/webrtc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# This file may not be copied, modified, or distributed except according to
# those terms.

import chronos, chronicles
import bearssl, chronos, chronicles

import udp_transport
import stun/stun_transport
Expand All @@ -16,6 +16,8 @@ import sctp/sctp_transport
import datachannel
import errors

from stun/stun_connection import StunUsernameProvider, StunUsernameChecker, StunPasswordProvider

logScope:
topics = "webrtc"

Expand All @@ -26,10 +28,15 @@ type WebRTC* = ref object
sctp: Sctp
port: int

proc new*(T: typedesc[WebRTC], address: TransportAddress): T =
proc new*(T: typedesc[WebRTC], address: TransportAddress,
usernameProvider: StunUsernameProvider = defaultUsernameProvider,
usernameChecker: StunUsernameChecker = defaultUsernameChecker,
passwordProvider: StunPasswordProvider = defaultPasswordProvider,
rng: ref HmacDrbgContext,
): T =
result = T()
result.udp = UdpTransport.new(address)
result.stun = Stun.new(result.udp)
result.stun = Stun.new(result.udp, usernameProvider, usernameChecker, passwordProvider, rng)
result.dtls = Dtls.new(result.stun)
result.sctp = Sctp.new(result.dtls)

Expand Down

0 comments on commit 302aae5

Please sign in to comment.