-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing native config with requests version. Still need to figure o…
…ut why the native version doesn't work.
- Loading branch information
Showing
3 changed files
with
62 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import httplib | ||
import json | ||
import random | ||
import socket | ||
import string | ||
import urllib | ||
|
||
PIA_SERVER = 'www.privateinternetaccess.com' | ||
|
||
def get_active_local_ip(): | ||
# Get active local IP | ||
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
try: | ||
tcp_socket.connect((PIA_SERVER, 0)) | ||
return tcp_socket.getsockname()[0] | ||
finally: | ||
tcp_socket.close() | ||
|
||
def generate_client_id(): | ||
# Generate client ID | ||
return ''.join( random.choice(string.hexdigits) for char in xrange(32) ).lower() | ||
|
||
def acquire_port( user_name, password, client_id, local_ip, log ): | ||
# Set up parameters | ||
values = urllib.urlencode({'user':user_name, | ||
'pass':password, | ||
'client_id':client_id, | ||
'local_ip':local_ip}) | ||
|
||
# Send request | ||
connection = httplib.HTTPSConnection(PIA_SERVER) | ||
connection.request('POST', '/vpninfo/port_forward_assignment', values) | ||
response = connection.getresponse() | ||
|
||
# Process response | ||
status_code_ok = 200 | ||
if response.status != status_code_ok: | ||
log( '{}: '.format(response.status) + response.reason ) | ||
return | ||
|
||
# Extract port from json data | ||
data = json.load(response) | ||
|
||
if 'port' not in data: | ||
log( data['error'] ) | ||
return | ||
|
||
return data['port'] |