How does WebRTC know WHO to connect to?
WebRTC signaling uses STUN to exchange ICE candidates and SDPs with peers, but how does it know who to exchange that information with?
Obviously, it's not with just anyone and just by virtue of using the same STUN server doesn't mean you'll get paired with some rando. However, I'd like to know how that selection is made. Probably the URI plays a role, but how big of a role? And is it possible to influence that?
It would seem like a problem if it were URI alone. Then someone could just bombard STUN servers with offers or requests to know who's currently on that webpage with WebRTC active.
WebRTC doesn't use STUN to exchange ICE candidates and SessionDescriptions. STUN is used to create a NAT Mapping and getting information about it. You can establish a WebRTC session without using STUN at all.
A session is established because two WebRTC peers exchange Session Descriptions (Offers and Answers). These offers and answers can be exchanged with any protocol you like. Most commonly you see HTTP/Websockets used. This is known as a signaling server.
It is the signaling servers job to make sure that the Offer/Answer is routed properly. You can read more about what the values actually do here WebRTC for the Curious#Signaling