eusholli commited on
Commit
ccd2aac
·
1 Parent(s): 9a46123

Added STUN connection test code

Browse files
Files changed (1) hide show
  1. utils/turn.py +26 -9
utils/turn.py CHANGED
@@ -1,28 +1,44 @@
1
- import logging
2
  import os
3
-
4
  import streamlit as st
5
  from twilio.base.exceptions import TwilioRestException
6
  from twilio.rest import Client
 
7
 
8
  logger = logging.getLogger(__name__)
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def get_ice_servers():
12
  """Use Twilio's TURN server because Streamlit Community Cloud has changed
13
- its infrastructure and WebRTC connection cannot be established without TURN server now. # noqa: E501
14
- We considered Open Relay Project (https://www.metered.ca/tools/openrelay/) too,
15
- but it is not stable and hardly works as some people reported like https://github.com/aiortc/aiortc/issues/832#issuecomment-1482420656 # noqa: E501
16
- See https://github.com/whitphx/streamlit-webrtc/issues/1213
17
  """
 
18
 
19
- # Ref: https://www.twilio.com/docs/stun-turn/api
20
  try:
21
  account_sid = os.environ["TWILIO_ACCOUNT_SID"]
22
  auth_token = os.environ["TWILIO_AUTH_TOKEN"]
 
23
  except KeyError:
24
  logger.warning(
25
- "Twilio credentials are not set. Fallback to a free STUN server from Google." # noqa: E501
26
  )
27
  return [{"urls": ["stun:stun.l.google.com:19302"]}]
28
 
@@ -31,8 +47,9 @@ def get_ice_servers():
31
  try:
32
  token = client.tokens.create()
33
  except TwilioRestException as e:
 
34
  st.warning(
35
- f"Error occurred while accessing Twilio API. Fallback to a free STUN server from Google. ({e})" # noqa: E501
36
  )
37
  return [{"urls": ["stun:stun.l.google.com:19302"]}]
38
 
 
1
+ import logging
2
  import os
 
3
  import streamlit as st
4
  from twilio.base.exceptions import TwilioRestException
5
  from twilio.rest import Client
6
+ import requests
7
 
8
  logger = logging.getLogger(__name__)
9
 
10
+ def check_connectivity():
11
+ try:
12
+ response = requests.get("https://api.twilio.com")
13
+ if response.status_code == 200:
14
+ logger.info("Twilio API is reachable.")
15
+ else:
16
+ logger.warning(f"Twilio API returned status code {response.status_code}")
17
+ except requests.RequestException as e:
18
+ logger.error(f"Failed to reach Twilio API: {e}")
19
+
20
+ try:
21
+ response = requests.get("https://stun.l.google.com:19302")
22
+ if response.status_code == 200:
23
+ logger.info("Google STUN server is reachable.")
24
+ else:
25
+ logger.warning(f"Google STUN server returned status code {response.status_code}")
26
+ except requests.RequestException as e:
27
+ logger.error(f"Failed to reach Google STUN server: {e}")
28
 
29
  def get_ice_servers():
30
  """Use Twilio's TURN server because Streamlit Community Cloud has changed
31
+ its infrastructure and WebRTC connection cannot be established without TURN server now.
 
 
 
32
  """
33
+ check_connectivity()
34
 
 
35
  try:
36
  account_sid = os.environ["TWILIO_ACCOUNT_SID"]
37
  auth_token = os.environ["TWILIO_AUTH_TOKEN"]
38
+ logger.info(f"Twilio SID: {account_sid}, Auth Token: {auth_token[:4]}...") # Log partial token for security
39
  except KeyError:
40
  logger.warning(
41
+ "Twilio credentials are not set. Fallback to a free STUN server from Google."
42
  )
43
  return [{"urls": ["stun:stun.l.google.com:19302"]}]
44
 
 
47
  try:
48
  token = client.tokens.create()
49
  except TwilioRestException as e:
50
+ logger.error(f"Twilio API error: {e}")
51
  st.warning(
52
+ f"Error occurred while accessing Twilio API. Fallback to a free STUN server from Google. ({e})"
53
  )
54
  return [{"urls": ["stun:stun.l.google.com:19302"]}]
55