Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def connect(port, protocol="http") -> str:
"""
Create a new ngrok tunnel
:param port: the tunnel local port
:param protocol: the protocol to use
:return: the ngrok url
"""
return ngrok.connect(port, protocol)
# Calling mock_dynamodb2() above hijacked the requests library, so using the responses
# library (that moto also used), add passthrus for HTTP requests that should still
# succeed in dev
responses.add_passthru("http://127.0.0.1")
responses.add_passthru("https://api.twilio.com")
responses.add_passthru("http://www.airnowapi.org")
responses.add_passthru("https://airnow.gov")
if USE_NGROK:
# Get the dev server port (defaults to 5000 for Flask, can be overridden with `--port`
# when starting the server
port = sys.argv[sys.argv.index("--port") + 1] if "--port" in sys.argv else 5000
# Open a ngrok tunnel to the dev server
public_url = ngrok.connect(port)
print(" * ngrok tunnel \"{}\" -> \"http://127.0.0.1:{}/\"".format(public_url, port))
TWILIO_ACCOUNT_SID = os.environ.get("AIR_QUALITY_DEV_TWILIO_ACCOUNT_SID", None)
TWILIO_AUTH_TOKEN = os.environ.get("AIR_QUALITY_DEV_TWILIO_AUTH_TOKEN", None)
TWILIO_SMS_NUMBER = os.environ.get("AIR_QUALITY_DEV_TWILIO_SMS_NUMBER", None)
# Update any base URLs or webhooks to use the public ngrok URL
if TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN and TWILIO_SMS_NUMBER:
callback_url = "{}/inbound".format(public_url)
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
phone_number_sid = client.incoming_phone_numbers.list(phone_number=TWILIO_SMS_NUMBER)[0].sid
client.incoming_phone_numbers(phone_number_sid).update(sms_url=callback_url)
###################################################################################
def get_endpoint(self) -> None:
return ngrok
def stop(self):
if self.connected:
ngrok.kill()
if self.webhook_server:
self.webhook_server.stop()
def _start_server(self):
try:
self._prepare_webhook_server()
self._load_webhook_routes()
self.ngrok_public_url = self.connect(self.webhook_port, protocol="http")
self.webhook_public_url = f"{self.ngrok_public_url}/webhook"
if self.webhook_server:
self.connected = True
self.webhook_server.serve_forever()
except PyngrokNgrokError as e:
self.logger.error(f"Error when starting webhook service: Your ngrock.com token might be invalid. ({e})")
except Exception as e:
self.logger.exception(e, True, f"Error when running webhook service: ({e})")
self.connected = False
async def prepare(self) -> None:
set_logging_level(self.LOGGERS, logging.WARNING)
ngrok.set_auth_token(self.config[CONFIG_CATEGORY_SERVICES][CONFIG_WEBHOOK][CONFIG_NGROK_TOKEN])
try:
self.webhook_host = os.getenv(ENV_WEBHOOK_ADDRESS, self.config[CONFIG_CATEGORY_SERVICES]
[CONFIG_WEBHOOK][CONFIG_WEB_IP])
except KeyError:
self.webhook_host = os.getenv(ENV_WEBHOOK_ADDRESS, DEFAULT_WEBHOOK_SERVER_IP)
try:
self.webhook_port = int(os.getenv(ENV_WEBHOOK_PORT, self.config[CONFIG_CATEGORY_SERVICES]
[CONFIG_WEBHOOK][CONFIG_WEB_PORT]))
except KeyError:
self.webhook_port = int(os.getenv(ENV_WEBHOOK_PORT, DEFAULT_WEBHOOK_SERVER_PORT))