ngrok.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import ngrok
  2. # Connect to ngrok for ingress
  3. def connect(token, port, options):
  4. account = None
  5. if token is None:
  6. token = 'None'
  7. else:
  8. if ':' in token:
  9. # token = authtoken:username:password
  10. token, username, password = token.split(':', 2)
  11. account = f"{username}:{password}"
  12. # For all options see: https://github.com/ngrok/ngrok-py/blob/main/examples/ngrok-connect-full.py
  13. if not options.get('authtoken_from_env'):
  14. options['authtoken'] = token
  15. if account:
  16. options['basic_auth'] = account
  17. if not options.get('session_metadata'):
  18. options['session_metadata'] = 'stable-diffusion-webui'
  19. try:
  20. public_url = ngrok.connect(f"127.0.0.1:{port}", **options).url()
  21. except Exception as e:
  22. print(f'Invalid ngrok authtoken? ngrok connection aborted due to: {e}\n'
  23. f'Your token: {token}, get the right one on https://dashboard.ngrok.com/get-started/your-authtoken')
  24. else:
  25. print(f'ngrok connected to localhost:{port}! URL: {public_url}\n'
  26. 'You can use this link after the launch is complete.')