|
1 | 1 | """Register with PNP server and wait for remote peers to connect.""" |
2 | 2 | # import argparse |
| 3 | +import os |
3 | 4 | import asyncio |
4 | 5 | import logging |
5 | 6 | import sys |
|
12 | 13 | # from aiortc import RTCIceCandidate, RTCSessionDescription |
13 | 14 | from peerjs.peer import Peer, PeerOptions |
14 | 15 | from peerjs.peerroom import PeerRoom |
15 | | -from peerjs.util import util |
| 16 | +from peerjs.util import util, default_stun_servers |
16 | 17 | from peerjs.enums import ConnectionEventType, PeerEventType |
17 | 18 |
|
18 | 19 | print(sys.version) |
|
24 | 25 | peer = None |
25 | 26 | savedPeerId = None |
26 | 27 | # persisted config dict |
27 | | -config = {} |
28 | | -CONFIG_FILE = '.peerjsrc' |
| 28 | + |
29 | 29 | AMBIANIC_PNP_HOST = 'ambianic-pnp.herokuapp.com' # 'localhost' |
30 | 30 | AMBIANIC_PNP_PORT = 443 # 9779 |
31 | 31 | 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 | + |
32 | 51 | time_start = None |
33 | 52 | peerConnectionStatus = None |
34 | 53 | discoveryLoop = None |
@@ -81,6 +100,14 @@ def _loadConfig(): |
81 | 100 | with conf_file.open() as infile: |
82 | 101 | config = json.load(infile) |
83 | 102 | 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 |
84 | 111 |
|
85 | 112 |
|
86 | 113 | def _setPnPServiceConnectionHandlers(peer=None): |
@@ -241,14 +268,16 @@ async def pnp_service_connect() -> Peer: |
241 | 268 | # We expect that peerId is crypto secure. No need to replace. |
242 | 269 | # Unless the user explicitly requests a refresh. |
243 | 270 | global savedPeerId |
| 271 | + global config |
244 | 272 | log.info('last saved savedPeerId %s', savedPeerId) |
245 | 273 | new_token = util.randomToken() |
246 | 274 | log.info('Peer session token %s', new_token) |
247 | 275 | 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'] |
252 | 281 | ) |
253 | 282 | peer = Peer(id=savedPeerId, peer_options=options) |
254 | 283 | log.info('pnpService: peer created with id %s , options: %r', |
|
0 commit comments