This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
vpn.py
53 lines (46 loc) · 1.71 KB
/
vpn.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
import requests
from bs4 import BeautifulSoup
import openvpn.client
import os
LOGIN_URL = 'https://www.vpnbook.com/'
SERVER_LIST_URL = 'https://www.vpnbook.com/freevpn'
def main():
response = requests.get(LOGIN_URL)
soup = BeautifulSoup(response.text, 'html.parser')
csrf_token = soup.find('input', {'name': 'csrf_token'}).get('value')
username = input('Enter your VPNbook username: ')
password = input('Enter your VPNbook password: ')
data = {
'username': username,
'password': password,
'csrf_token': csrf_token
}
response = requests.post(LOGIN_URL, data=data, allow_redirects=False)
if response.status_code == 302:
print('Login successful!')
else:
print('Login failed!')
return
response = requests.get(SERVER_LIST_URL)
soup = BeautifulSoup(response.text, 'html.parser')
servers = []
for li in soup.find_all('li'):
server = li.get_text().strip()
if server.startswith('Free '):
servers.append(server)
for i, server in enumerate(servers):
print(f'{i+1}. {server}')
selected_server_index = int(input('Enter the server number you want to connect to: ')) - 1
selected_server = servers[selected_server_index]
config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'vpnbook.ovpn')
with open(config_file_path, 'w') as f:
f.write(f'<connection>\nremote {selected_server} 443\nauth-user-pass\n</connection>\n')
vpn = openvpn.client.Client()
try:
vpn.connect(config_file=config_file_path)
except Exception as e:
print(f'Error: {e}')
return
print(f'Connected to {selected_server}!')
if __name__ == '__main__':
main()