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)
@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