forked from blacktwin/JBOPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bypass_auth_name.py
106 lines (85 loc) · 4.35 KB
/
bypass_auth_name.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'''
Use PlexPy to pull last IP address from user and add to List of IP addresses and networks that are allowed without auth in Plex.
optional arguments:
-h, --help show this help message and exit
-u [ ...], --users [ ...]
Space separated list of case sensitive names to process. Allowed names are:
(choices: {List of all Plex users} )
(default: None)
-c [], --clear [] Clear List of IP addresses and networks that are allowed without auth in Plex:
(choices: None)
(default: None)
List of IP addresses is cleared before adding new IPs
'''
import requests
import argparse
import sys
## EDIT THESE SETTINGS ##
PLEX_TOKEN = 'xxxx'
PLEXPY_APIKEY = 'xxxx' # Your PlexPy API key
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL
def get_get_history(user_id):
# Get the user history from PlexPy
payload = {'apikey': PLEXPY_APIKEY,
'cmd': 'get_history',
'user_id': user_id,
'length': 1}
try:
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
res_data = response['response']['data']['data']
return [d['ip_address'] for d in res_data]
except Exception as e:
sys.stderr.write("PlexPy API 'get_get_history' request failed: {0}.".format(e))
def get_get_user_names(username):
# Get the user names from PlexPy
payload = {'apikey': PLEXPY_APIKEY,
'cmd': 'get_user_names'}
try:
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
res_data = response['response']['data']
if username:
return [d['user_id'] for d in res_data if d['friendly_name'] == username]
else:
return [d['friendly_name'] for d in res_data]
except Exception as e:
sys.stderr.write("PlexPy API 'get_get_user_names' request failed: {0}.".format(e))
def add_auth_bypass(net_str):
headers = {"X-Plex-Token": PLEX_TOKEN}
params = {"allowedNetworks": net_str}
requests.put("http://localhost:32400/:/prefs", headers=headers, params=params)
if __name__ == '__main__':
user_lst = get_get_user_names('')
parser = argparse.ArgumentParser(description="Use PlexPy to pull last IP address from user and add to List of "
"IP addresses and networks that are allowed without auth in Plex.",
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-u', '--users', nargs='+', type=str, choices=user_lst, metavar='',
help='Space separated list of case sensitive names to process. Allowed names are: \n'
'(choices: %(choices)s) \n(default: %(default)s)')
parser.add_argument('-c', '--clear', nargs='?',default=None, metavar='',
help='Clear List of IP addresses and networks that are allowed without auth in Plex: \n'
'(default: %(default)s)')
opts = parser.parse_args()
if opts.clear and opts.users is None:
print('Clearing List of IP addresses and networks that are allowed without auth in Plex.')
add_auth_bypass('')
elif opts.clear and len(opts.users) == 1:
print('Clearing List of IP addresses and networks that are allowed without auth in Plex.')
add_auth_bypass('')
user_id = get_get_user_names(opts.users)
user_ip = get_get_history(user_id)
print('Adding {} to List of IP addresses and networks that are allowed without auth in Plex.'
.format(''.join(user_ip)))
add_auth_bypass(user_ip)
elif opts.clear and len(opts.users) > 1:
print('Clearing List of IP addresses and networks that are allowed without auth in Plex.')
add_auth_bypass('')
userid_lst = [get_get_user_names(user_names) for user_names in opts.users]
userip_lst = [get_get_history(user_id) for user_id in userid_lst]
flat_list = [item for sublist in userip_lst for item in sublist]
print('Adding {} to List of IP addresses and networks that are allowed without auth in Plex.'
.format(', '.join(flat_list)))
add_auth_bypass(', '.join(flat_list))
else:
print('I don\'t know what else you want.')