Skip to content

Commit 22f69b3

Browse files
author
Ivelin Ivanov
authored
feat: expose stun servers config
Merge pull request #36 from muka/feature/expose_config
2 parents 9fdf0b9 + 1f9fbf0 commit 22f69b3

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Enables [Progressive Web Apps](https://developer.mozilla.org/en-US/docs/Web/Prog
1212

1313
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.
1414

15+
- *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;`
16+
1517
## Dependencies
1618

1719
Uses [aiortc](https://github.com/aiortc/aiortc) as Python WebRTC provider. This requires installing a few native dependencies for audio/video media processing.

src/peerjs/ext/http-proxy.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Register with PNP server and wait for remote peers to connect."""
22
# import argparse
3+
import os
34
import asyncio
45
import logging
56
import sys
@@ -12,7 +13,7 @@
1213
# from aiortc import RTCIceCandidate, RTCSessionDescription
1314
from peerjs.peer import Peer, PeerOptions
1415
from peerjs.peerroom import PeerRoom
15-
from peerjs.util import util
16+
from peerjs.util import util, default_stun_servers
1617
from peerjs.enums import ConnectionEventType, PeerEventType
1718

1819
print(sys.version)
@@ -24,11 +25,29 @@
2425
peer = None
2526
savedPeerId = None
2627
# persisted config dict
27-
config = {}
28-
CONFIG_FILE = '.peerjsrc'
28+
2929
AMBIANIC_PNP_HOST = 'ambianic-pnp.herokuapp.com' # 'localhost'
3030
AMBIANIC_PNP_PORT = 443 # 9779
3131
AMBIANIC_PNP_SECURE = True # False
32+
33+
server_list_str = os.environ.get("STUN_SERVERS")
34+
if server_list_str:
35+
server_list = server_list_str.split(";")
36+
for el in server_list:
37+
if el not in default_stun_servers:
38+
default_stun_servers.append(el)
39+
40+
config = {
41+
'host': AMBIANIC_PNP_HOST,
42+
'port': AMBIANIC_PNP_PORT,
43+
'secure': AMBIANIC_PNP_SECURE,
44+
'stun_servers': default_stun_servers,
45+
}
46+
47+
CONFIG_FILE = '.peerjsrc'
48+
if os.environ.get("PEERJS_CONFIG_FILE"):
49+
CONFIG_FILE = os.environ.get("PEERJS_CONFIG_FILE")
50+
3251
time_start = None
3352
peerConnectionStatus = None
3453
discoveryLoop = None
@@ -81,6 +100,14 @@ def _loadConfig():
81100
with conf_file.open() as infile:
82101
config = json.load(infile)
83102
savedPeerId = config.get('peerId', None)
103+
if "host" not in config.keys():
104+
config["host"] = AMBIANIC_PNP_HOST
105+
if "port" not in config.keys():
106+
config["port"] = AMBIANIC_PNP_PORT
107+
if "secure" not in config.keys():
108+
config["secure"] = AMBIANIC_PNP_SECURE
109+
if "stun_servers" not in config.keys():
110+
config["stun_servers"] = default_stun_servers
84111

85112

86113
def _setPnPServiceConnectionHandlers(peer=None):
@@ -241,14 +268,16 @@ async def pnp_service_connect() -> Peer:
241268
# We expect that peerId is crypto secure. No need to replace.
242269
# Unless the user explicitly requests a refresh.
243270
global savedPeerId
271+
global config
244272
log.info('last saved savedPeerId %s', savedPeerId)
245273
new_token = util.randomToken()
246274
log.info('Peer session token %s', new_token)
247275
options = PeerOptions(
248-
host=AMBIANIC_PNP_HOST,
249-
port=AMBIANIC_PNP_PORT,
250-
secure=AMBIANIC_PNP_SECURE,
251-
token=new_token
276+
host=config['host'],
277+
port=config['port'],
278+
secure=config['secure'],
279+
token=new_token,
280+
config=config['stun_servers']
252281
)
253282
peer = Peer(id=savedPeerId, peer_options=options)
254283
log.info('pnpService: peer created with id %s , options: %r',

src/peerjs/util.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
from dataclasses import dataclass
66
from uuid import uuid4
7+
import os
78
# import msgpack
89

910
from aiortc.rtcconfiguration import RTCConfiguration, RTCIceServer
@@ -13,16 +14,16 @@
1314

1415
log = logging.getLogger(__name__)
1516

17+
18+
default_stun_servers = [
19+
"stun:stun.l.google.com:19302"
20+
]
21+
1622
DEFAULT_CONFIG = RTCConfiguration(
1723
iceServers=[
18-
RTCIceServer(
19-
urls=[
20-
"stun:stun.l.google.com:19302"
21-
]
22-
)
23-
]
24-
)
25-
24+
RTCIceServer(urls=default_stun_servers)
25+
]
26+
)
2627

2728
@dataclass
2829
class UtilSupports:

0 commit comments

Comments
 (0)