alexdlaird / pyngrok

A Python wrapper for ngrok
https://pyngrok.readthedocs.io
MIT License
425 stars 59 forks source link

How to set region while connecting nrok tunnel #26

Closed sumitdubey closed 4 years ago

sumitdubey commented 4 years ago

Describe the Bug I have a paid version of ngrok and thus a reserved hostname for region in. When I try to connect ngrok tunnel following

public_url = ngrok.connect(options={'hostname':'example.com'}) I get following region error as default region is us:

Traceback (most recent call last):
  File "main.py", line 25, in <module>
    public_url = ngrok.connect(options={'region': 'in','hostname':'example.com'})
  File "/usr/local/lib/python3.6/dist-packages/pyngrok/ngrok.py", line 178, in connect
    tunnel = NgrokTunnel(api_request("{}/api/{}".format(api_url, "tunnels"), "POST", data=options, timeout=timeout))
  File "/usr/local/lib/python3.6/dist-packages/pyngrok/ngrok.py", line 314, in api_request
    status_code, e.msg, e.hdrs, response_data)
pyngrok.exception.PyngrokNgrokHTTPError: ngrok client exception, API returned 502: {"error_code":103,"status_code":502,"msg":"failed to start tunnel","details":{"err":"This name is reserved in a different region.\nFailed to bind the domain 'example.com' in the region 'us' because it is reserved\nin the 'in' region.\n\nTry connecting to a different region: https://ngrok.com/docs#global-usage\r\n\r\nERR_NGROK_322\r\n"}}

Steps to Reproduce public_url = ngrok.connect(options={'hostname':'example.com'}) Pass options with reserved hostname with region other than us image

Expected Behavior There should be a function like ngrok.set_region("<REGION>") like we have ngrok.set_auth_token("<NGROK_AUTH_TOKEN>")

alexdlaird commented 4 years ago

That's for bringing this to my attention. I will have a look at this this week and will try to push a new version.

What version of pyngrok are you currently running? Will need to decide if this can just be patched in 2.0.x or if we'll need to cut a hotfix for 1.4.x as well.

sumitdubey commented 4 years ago

@alexdlaird I was just giving it try with the latest version pyngrok 2.0.1. This is similar to def set_auth_token(token, ngrok_path=None, config_path=None)

So, a small patch/hotfix will be helpful for people.

alexdlaird commented 4 years ago

Circling back to this, I don't believe this should need a hotfix. The region param should be passed as part of options, the same way hostname is (https://ngrok.com/docs#http-custom-domains). Does this not work for you?

public_url = ngrok.connect(options={'hostname':'example.com', 'region': 'in'})
alexdlaird commented 4 years ago

Apologies, I now see from the stacktrace that I believe this is what you were actually trying. I don't see the same error, but I also don't have a reserved domain. Let me play around with this to try to figure out why ngrok is throwing this error, but I believe whatever the fix is, you'll still want to pass the region in the options param as you were doing, not in a set_region method. Will get back to you when I've investigated further.

alexdlaird commented 4 years ago

Unfortunately, I'm unable to reproduce this. Wether I use public_url = ngrok.connect(options={'hostname':'example.com', 'region': 'in'}) or ngrok http 8080 -hostname=example.com -region=in (from the command line using pyngrok), both work and properly start ngrok using the in region.

This appears to be an issue with ngrok and not pyngrok, as the parameter is being passed and the error you're getting is a response from ngrok.

However, a workaround should be to simply edit your ngrok config file and specify the region there (https://ngrok.com/docs#config-examples). Edit ~/.ngrok2/ngrok.yml and specify region: in there, then try again (this time without specifying region in your options and just give the hostname) and it should work for you.

alexdlaird commented 4 years ago

This has now been resolved in 2.1.0 with the introduction of the region paramter. Documented here, and a test example is here.

Thanks for raising this, and sorry for the prior confusion on where the issue was!