-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (51 loc) · 2.02 KB
/
main.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
from winreg import *
import os
from fp.fp import FreeProxy
def isProxyEnabled():
aReg = ConnectRegistry(None, HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
subCount, valueCount, lastModified = QueryInfoKey(aKey)
for i in range(valueCount):
try:
n, v, t = EnumValue(aKey, i)
if n == 'ProxyEnable':
return v and True or False
except EnvironmentError:
break
CloseKey(aKey)
def changeProxyState():
aReg = ConnectRegistry(None, HKEY_CURRENT_USER)
aKey = OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", 0, KEY_WRITE)
proxy = ''
if isProxyEnabled():
print("Proxy is ENABLED, switching off...")
val = 0
else:
print("Proxy is DISABLED, finding proxy and switching on...")
val = 1
while proxy == '':
try:
proxy = FreeProxy(elite=1, https=0, country_id='US').get().replace("http://", "")
except BaseException as e:
print(str(e), "Failed to fetch a proxy. Retying...")
os.system('cls||clear')
print("Found proxy:", proxy)
try:
SetValueEx(aKey, "ProxyServer", 0, REG_SZ, proxy)
except ValueError:
print("Error in setting proxy server in registry!")
try:
SetValueEx(aKey, "ProxyEnable", 0, REG_DWORD, val)
except EnvironmentError:
print("Encountered problems writing into the Registry!")
print(f">>Proxy{' '+proxy+' ' if proxy else ' '}is now {'ENABLED!' if val == 1 else 'DISABLED!'}<<")
CloseKey(aKey)
inputVal = input(
f"\r\nPress ENTER to {'ENABLE' if val == 0 else 'DISABLE'} proxy"
"\r\nClose the program, press ctrl+c or type q to exit\r\n")
if inputVal.lower() == 'q':
exit()
if __name__ == '__main__':
while True:
os.system('cls||clear')
changeProxyState()