Skip to content

Commit

Permalink
feat: expose stun servers config
Browse files Browse the repository at this point in the history
Merge pull request #36 from muka/feature/expose_config
  • Loading branch information
Ivelin Ivanov committed Nov 3, 2020
2 parents 9fdf0b9 + 1f9fbf0 commit 22f69b3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Enables [Progressive Web Apps](https://developer.mozilla.org/en-US/docs/Web/Prog

See Ambianic UI [PNP module](https://github.com/ambianic/ambianic-ui/blob/master/src/store/pnp.js) for a real-world example how PeerJS Python is used with PnP and HTTP Proxy.

- *Setting additional STUN servers* is possible using the `STUN_SERVERS` env variable, separated by semicolon (`;`). Eg. `STUN_SERVERS=stun:example1.org:19302;stun:example2.org:19302;`

## Dependencies

Uses [aiortc](https://github.com/aiortc/aiortc) as Python WebRTC provider. This requires installing a few native dependencies for audio/video media processing.
Expand Down
43 changes: 36 additions & 7 deletions src/peerjs/ext/http-proxy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Register with PNP server and wait for remote peers to connect."""
# import argparse
import os
import asyncio
import logging
import sys
Expand All @@ -12,7 +13,7 @@
# from aiortc import RTCIceCandidate, RTCSessionDescription
from peerjs.peer import Peer, PeerOptions
from peerjs.peerroom import PeerRoom
from peerjs.util import util
from peerjs.util import util, default_stun_servers
from peerjs.enums import ConnectionEventType, PeerEventType

print(sys.version)
Expand All @@ -24,11 +25,29 @@
peer = None
savedPeerId = None
# persisted config dict
config = {}
CONFIG_FILE = '.peerjsrc'

AMBIANIC_PNP_HOST = 'ambianic-pnp.herokuapp.com' # 'localhost'
AMBIANIC_PNP_PORT = 443 # 9779
AMBIANIC_PNP_SECURE = True # False

server_list_str = os.environ.get("STUN_SERVERS")
if server_list_str:
server_list = server_list_str.split(";")
for el in server_list:
if el not in default_stun_servers:
default_stun_servers.append(el)

config = {
'host': AMBIANIC_PNP_HOST,
'port': AMBIANIC_PNP_PORT,
'secure': AMBIANIC_PNP_SECURE,
'stun_servers': default_stun_servers,
}

CONFIG_FILE = '.peerjsrc'
if os.environ.get("PEERJS_CONFIG_FILE"):
CONFIG_FILE = os.environ.get("PEERJS_CONFIG_FILE")

time_start = None
peerConnectionStatus = None
discoveryLoop = None
Expand Down Expand Up @@ -81,6 +100,14 @@ def _loadConfig():
with conf_file.open() as infile:
config = json.load(infile)
savedPeerId = config.get('peerId', None)
if "host" not in config.keys():
config["host"] = AMBIANIC_PNP_HOST
if "port" not in config.keys():
config["port"] = AMBIANIC_PNP_PORT
if "secure" not in config.keys():
config["secure"] = AMBIANIC_PNP_SECURE
if "stun_servers" not in config.keys():
config["stun_servers"] = default_stun_servers


def _setPnPServiceConnectionHandlers(peer=None):
Expand Down Expand Up @@ -241,14 +268,16 @@ async def pnp_service_connect() -> Peer:
# We expect that peerId is crypto secure. No need to replace.
# Unless the user explicitly requests a refresh.
global savedPeerId
global config
log.info('last saved savedPeerId %s', savedPeerId)
new_token = util.randomToken()
log.info('Peer session token %s', new_token)
options = PeerOptions(
host=AMBIANIC_PNP_HOST,
port=AMBIANIC_PNP_PORT,
secure=AMBIANIC_PNP_SECURE,
token=new_token
host=config['host'],
port=config['port'],
secure=config['secure'],
token=new_token,
config=config['stun_servers']
)
peer = Peer(id=savedPeerId, peer_options=options)
log.info('pnpService: peer created with id %s , options: %r',
Expand Down
17 changes: 9 additions & 8 deletions src/peerjs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
from dataclasses import dataclass
from uuid import uuid4
import os
# import msgpack

from aiortc.rtcconfiguration import RTCConfiguration, RTCIceServer
Expand All @@ -13,16 +14,16 @@

log = logging.getLogger(__name__)


default_stun_servers = [
"stun:stun.l.google.com:19302"
]

DEFAULT_CONFIG = RTCConfiguration(
iceServers=[
RTCIceServer(
urls=[
"stun:stun.l.google.com:19302"
]
)
]
)

RTCIceServer(urls=default_stun_servers)
]
)

@dataclass
class UtilSupports:
Expand Down

0 comments on commit 22f69b3

Please sign in to comment.