Skip to content

Instantly share code, notes, and snippets.

@Rihcus
Forked from nagleaidan/ngrok-plex.py
Last active May 11, 2024 08:17
Show Gist options
  • Save Rihcus/4a217e0092e1fa34299e395222759165 to your computer and use it in GitHub Desktop.
Save Rihcus/4a217e0092e1fa34299e395222759165 to your computer and use it in GitHub Desktop.
Run plex through ngrok with SSL via duckdns
#!/usr/bin/python3
from plexapi.myplex import MyPlexAccount
import sys
import json
import requests
import time
import socket
# please make sure to install PlexAPI via pip, "pip install PlexAPI"
# makesure to update the duckdns url with token and domainname
# don't forget to add ur username and pasword
# need time for ngrok to start up before requesting data
print("Waiting 5 seconds for ngrok to start up")
time.sleep(5)
def get_ngrok_port():
url = "http://localhost:4040/api/tunnels"
res = requests.get(url)
res_unicode = res.content.decode("utf-8")
res_json = json.loads(res_unicode)
for i in res_json["tunnels"]:
if i['proto'] == 'tcp':
return i['public_url'].split(':')[-1]
def get_ngrok_url():
url = "http://localhost:4040/api/tunnels"
res = requests.get(url)
res_unicode = res.content.decode("utf-8")
res_json = json.loads(res_unicode)
for i in res_json["tunnels"]:
if i['proto'] == 'tcp':
return i['public_url'].split(':')[-2]
# get and clean up ngrok tcp url for later (this code is messy) :(
ngrokurl=get_ngrok_url()
ngrokurlclean= ngrokurl.split('//')[-1]
print("ngrok base url = " + ngrokurlclean)
# get ngrok url ip
ngrokip = socket.gethostbyname(ngrokurlclean)
print("ngrok url and ip no port = " + ngrokip)
#update duckdns with ngrok ip make sure to change [] or copy url from duckdns install guide
duckdnsupdateurl="https://www.duckdns.org/update?domains=[yourname]&token=[yourapitoken]&ip=" + ngrokip
res = requests.get(duckdnsupdateurl)
print(res.status_code)
def build_url_list():
NGROK_PORT = get_ngrok_port()
NGROK_BASES = ["https://[yourname].duckdns.org:"]
NGROK_URLS = map(lambda base: base + NGROK_PORT, NGROK_BASES)
CUSTOM_URL = ""
for url in NGROK_URLS:
CUSTOM_URL += url + ", "
CUSTOM_URL = CUSTOM_URL[:-2]
return CUSTOM_URL
# Load user defined config"
PLEX_USER = "email"
PLEX_PWORD = "password"
PLEX_SERVER = "plex server name"
# stores plex user login info into a variable
account = MyPlexAccount(PLEX_USER, PLEX_PWORD)
plex = account.resource(PLEX_SERVER).connect() # returns a PlexServer instance
# displays current plex custom url settings. Not needed but nice to see
print("Old settings: " + plex.settings.customConnections)
# sets plex's "Custom server access URLs" with one from Ngrok
customUrl = plex.settings.get('customConnections')
customUrl.set(build_url_list())
plex.settings.save()
# displays new custom plex url from Ngrok. Not needed but nice to see
account = MyPlexAccount(PLEX_USER, PLEX_PWORD)
plex = account.resource(PLEX_SERVER).connect() # returns a PlexServer instance
print("New settings: " + plex.settings.customConnections)
@Rihcus
Copy link
Author

Rihcus commented Dec 9, 2020

Also just to note to use ssl if you use duckdns's txt validation via certbot:
https://www.reddit.com/r/letsencrypt/comments/65ravi/duckdnsorg_now_supports_txt_records/

Covert the public.pem and private.pem to . p12 (this guide shows how to generate pfx rename the output to p12)
https://www.ssl.com/how-to/create-a-pfx-p12-certificate-file-using-openssl/

@poornachandratejasvi
Copy link

this program looks nice, is there a docker container made out of this ?

@milindpatel63
Copy link

@Rihcus can you help?
I did all that...generated a certificate using this command...
sudo openssl pkcs12 -export -out certificate.p12 -inkey privkey.pem -in fullchain.pem
added it to my plex settings...in domain i added "mydomain.duckdns.org"
But still i cant open remotely via app.plex.tv
If i manually try the custom url remotely, i still get certificate error....

@milindpatel63
Copy link

Nevermind, i solved it...Turns out, i specified domain in the script as "xxx.duckdns.org" instead of just "xxx"...so the certificate wasn't matching...

@Rihcus
Copy link
Author

Rihcus commented May 29, 2021

Nevermind, i solved it...Turns out, i specified domain in the script as "xxx.duckdns.org" instead of just "xxx"...so the certificate wasn't matching...

Interesting. I wonder if there is a way to automate the renewal process. I recently found an alternative to duckdns https://desec.io/. Main perk is it supports generating let's encrypt certs automatic with no port forwarding in attended (next cloud uses this)

Edit scratch that there seems to be a python script to do this with duck DNS

https://pypi.org/project/certbot-dns-duckdns/

@origamiofficial
Copy link

origamiofficial commented Jul 14, 2021

Do I need to add the script to cron or It needed to run only once? If yes then can you suggest me the cron interval?

@Rihcus
Copy link
Author

Rihcus commented Jul 15, 2021

Do I need to add the script to cron or It needed to run only once? If yes then can you suggest me the cron interval?

It does require cron (or similar program) and doesnt out of the box run update automatically. As for the frequency maybe run it every 1-2 hrs and/or immediately after sleep/upon boot (haven't really used it long term just forked it as a test 😉). Realistically the ngrok domain and port should stay the same indefinitely as long as ngrok isn't closed/restarted (only part that could change mid ngrok session is the ip of the tcp domain).

@origamiofficial
Copy link

I made a fork of this script here:
https://gist.github.com/anjumrafidofficial/fe3b986ff15246a47851f9d0da0e202f

Main change is this scripts will use token instead of password.

@Rihcus
Copy link
Author

Rihcus commented Sep 7, 2021

I made a fork of this script here:
https://gist.github.com/anjumrafidofficial/fe3b986ff15246a47851f9d0da0e202f

Main change is this scripts will use token instead of password.

Thanks for the fix 👍

to add here is a guide on getting the token

https://unraid-guides.com/2021/01/31/how-to-connect-plex-to-apps-that-dont-support-two-factor-authentication-2fa/

@famewolf
Copy link

I made a fork of this script here:
https://gist.github.com/anjumrafidofficial/fe3b986ff15246a47851f9d0da0e202f
Main change is this scripts will use token instead of password.

Thanks for the fix +1

to add here is a guide on getting the token

https://unraid-guides.com/2021/01/31/how-to-connect-plex-to-apps-that-dont-support-two-factor-authentication-2fa/

Was a docker container of your script ever made?

@Rihcus
Copy link
Author

Rihcus commented Aug 31, 2022

I made a fork of this script here:
https://gist.github.com/anjumrafidofficial/fe3b986ff15246a47851f9d0da0e202f
Main change is this scripts will use token instead of password.

Thanks for the fix +1
to add here is a guide on getting the token
https://unraid-guides.com/2021/01/31/how-to-connect-plex-to-apps-that-dont-support-two-factor-authentication-2fa/

Was a docker container of your script ever made?

Truth be told I have little experience making docker containers and haven't used Plex in a while. There seems to be a ngrok Plex docker solution present though not by me

https://github.com/andriinuts/plex-ngrok-docker

@origamiofficial
Copy link

I made a fork of this script here:
https://gist.github.com/anjumrafidofficial/fe3b986ff15246a47851f9d0da0e202f
Main change is this scripts will use token instead of password.

Thanks for the fix +1
to add here is a guide on getting the token
https://unraid-guides.com/2021/01/31/how-to-connect-plex-to-apps-that-dont-support-two-factor-authentication-2fa/

Was a docker container of your script ever made?

I've created one. Have a look: https://github.com/origamiofficial/docker-ngrok-plex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment