This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
forked from DevilRange/filelist-api-whitelist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filelist_whitelist_ip.py
executable file
·224 lines (184 loc) · 8.06 KB
/
filelist_whitelist_ip.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import requests
from bs4 import BeautifulSoup
from time import sleep
import logging
import random
import re
import os
import sys
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
def is_ipv4_address(ip):
return bool(re.match(r'^(\d{1,3}\.){3}\d{1,3}$', ip))
def authenticate(user, password):
session = requests.Session()
url = 'https://filelist.io/login.php'
while True:
try:
response = session.get(url)
break
except requests.exceptions.RequestException as e:
logging.error(f"Error with {url}: {e}")
sleep(600) # 10 minutes before retrying
soup = BeautifulSoup(response.content, "html.parser")
validator = soup.find('input', {'name': 'validator'}).get('value', '') if soup.find('input',
{'name': 'validator'}) else ''
data = {
"validator": validator,
"username": user,
"password": password,
"unlock": "1",
"returnto": "%252F",
}
login_url = 'https://filelist.io/takelogin.php'
while True:
try:
session.post(login_url, data=data)
if not session.cookies.get("pass"):
logging.error("Login not successful, check credentials...")
logging.error("Will try again after 6 hours")
# 6 hour wait because wrong credentials can block the account.
sleep(21600)
return session
except requests.exceptions.RequestException as e:
logging.error(f"Error with {login_url}: {e}")
sleep(600) # 10 minutes before retrying
def get_current_wan_ip():
urls = [
'https://checkip.amazonaws.com/',
'https://ifconfig.io/ip',
'https://icanhazip.com/'
]
# Randomising which endpoint is used to not abuse.
random.shuffle(urls)
while True:
for url in urls:
try:
response = requests.get(url)
response.raise_for_status()
logging.info(f"Got IP from {url}!")
ip = response.text.strip()
if is_ipv4_address(ip):
return ip # Return if successful
else:
logging.warning(f"IP format is wrong - {ip}")
except requests.exceptions.RequestException as e:
logging.warning(f"Error with {url}: {e}")
logging.error("Cannot get WAN IP; possibly a connection issue, retrying in 5 minutes...")
sleep(300)
def is_authenticated(session):
profile_page_url = 'https://filelist.io/my.php'
while True:
try:
response = session.get(profile_page_url)
soup = BeautifulSoup(response.content, 'html.parser')
# Check for the presence of the 'whitelistip' input field
whitelist_ip_field = soup.find('input', {'name': 'whitelistip'})
return whitelist_ip_field is not None
except requests.exceptions.RequestException as e:
logging.error(f"Error with {profile_page_url}: {e}")
logging.error(f"Cannot get {profile_page_url}, retrying in 5 minutes...")
sleep(300)
def fetch_profile_ip(session):
my_page_url = "https://filelist.io/my.php"
while True:
try:
response = session.get(my_page_url)
soup = BeautifulSoup(response.content, "html.parser")
whitelist_ip_field = soup.find('input', {'name': 'whitelistip'})
profile_ip = whitelist_ip_field.get('value', '') if whitelist_ip_field else ''
return profile_ip
except requests.exceptions.RequestException as e:
logging.error(f"Error with {my_page_url}: {e}")
logging.error(f"Cannot get {my_page_url}, retrying in 5 minutes...")
sleep(300)
def fetch_and_update_profile(session, current_wan_ip):
my_page_url = "https://filelist.io/my.php"
update_page_url = "https://filelist.io/takeprofedit.php"
while True:
try:
response = session.get(my_page_url)
soup = BeautifulSoup(response.content, "html.parser")
form_data = {}
# Handle text inputs, radio buttons, and hidden inputs
for input_tag in soup.find_all(lambda tag: (tag.name == 'input' and (not tag.has_attr('type') or tag['type'] not in ['checkbox', 'select'])) or tag.name == 'textarea'):
name = input_tag.get('name')
value = input_tag.get('value', '')
if input_tag.name == 'textarea':
value = input_tag.string
if input_tag.get('type') == 'radio' and not input_tag.has_attr('checked'):
continue
if name:
form_data[name] = value
# Handle checkboxes, include only if checked
for checkbox in soup.find_all('input', type='checkbox'):
name = checkbox.get('name')
if name == 'resetpasskey': # Skip adding resetpasskey to form_data
continue
if checkbox.has_attr('checked'):
form_data[name] = 'on'
# Handle select dropdowns
for select in soup.find_all('select'):
name = select.get('name')
selected_option = select.find('option', selected=True)
if selected_option:
form_data[name] = selected_option.get('value', '')
# Update 'whitelistip' with the current WAN IP if it's different
if form_data.get('whitelistip') != current_wan_ip:
form_data['whitelistip'] = current_wan_ip
# Submit the update request with all form data, excluding resetpasskey
update_response = session.post(update_page_url, data=form_data)
if update_response.status_code == 200:
logging.info("Successfully updated the whitelist IP.")
break
else:
logging.error(f"Failed to update. Status Code: {update_response.status_code}")
else:
logging.info("No update needed; WAN IP matches the Whitelist IP.")
break
except requests.exceptions.RequestException as e:
logging.error(f"Error with updating whitelist IP: {e}")
logging.info(f"Retrying in 5 minutes...")
sleep(300)
if __name__ == '__main__':
username = os.environ.get('FL_USERNAME', None)
password = os.environ.get('FL_PASSWORD', None)
check_delay = os.environ.get('FL_CHECK_DELAY', None)
if username is None:
logging.error("Error: FL_USERNAME is not set.")
sys.exit(1)
if password is None:
logging.error("Error: FL_PASSWORD is not set.")
sys.exit(1)
if check_delay is None:
logging.error("Error: FL_CHECK_DELAY is not set.")
sys.exit(1)
session = authenticate(username, password)
profile_ip = fetch_profile_ip(session)
current_wan_ip = get_current_wan_ip()
logging.info(f"Current WAN IP: {current_wan_ip}")
logging.info(f"Profile Whitelist IP: {profile_ip}")
# Initial check and update if necessary
if current_wan_ip != profile_ip:
logging.info("WAN IP does not match profile IP. Updating...")
fetch_and_update_profile(session, current_wan_ip)
else:
logging.info("WAN IP matches profile IP. No update needed.")
# Loop to check for IP changes
while True:
new_wan_ip = get_current_wan_ip()
if new_wan_ip != current_wan_ip:
logging.info(f"WAN IP changed to: {new_wan_ip}")
current_wan_ip = new_wan_ip
if not is_authenticated(session):
logging.warning("Session expired, re-authenticating...")
session = authenticate(username, password)
fetch_and_update_profile(session, current_wan_ip)
else:
logging.info("No change in WAN IP.")
# Wait for 15 minutes before checking again
sleep(int(check_delay))