From e7f4a2cb7bcaf2ba0890fba99c0946c0115a5d65 Mon Sep 17 00:00:00 2001 From: SillyGoat Date: Sun, 13 Sep 2015 02:29:15 -0700 Subject: [PATCH] Replacing native config with requests version. Still need to figure out why the native version doesn't work. --- PIAPortForward.pyproj | 1 + piaportforward/pia_port.py | 27 +++++++++-------- piaportforward/pia_port_native.py | 48 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 piaportforward/pia_port_native.py diff --git a/PIAPortForward.pyproj b/PIAPortForward.pyproj index 84ecd28..fffa016 100644 --- a/PIAPortForward.pyproj +++ b/PIAPortForward.pyproj @@ -26,6 +26,7 @@ + diff --git a/piaportforward/pia_port.py b/piaportforward/pia_port.py index 29b55e4..4f47a7b 100644 --- a/piaportforward/pia_port.py +++ b/piaportforward/pia_port.py @@ -1,9 +1,7 @@ -import httplib -import json import random +import requests import socket import string -import urllib PIA_SERVER = 'www.privateinternetaccess.com' @@ -22,24 +20,25 @@ def generate_client_id(): 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}) + values = {'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() + try: + response = requests.post('https://' + PIA_SERVER + '/vpninfo/port_forward_assignment', params=values) + except requests.exceptions.RequestException as request_exception: + log( request_exception.message ) + return # Process response status_code_ok = 200 - if response.status != status_code_ok: - log( '{}: '.format(response.status) + response.reason ) + if response.status_code != status_code_ok: + log( '{}: '.format(response.status_code) + response.reason ) return - # Extract port from json data - data = json.load(response) + data = response.json() if 'port' not in data: log( data['error'] ) diff --git a/piaportforward/pia_port_native.py b/piaportforward/pia_port_native.py new file mode 100644 index 0000000..29b55e4 --- /dev/null +++ b/piaportforward/pia_port_native.py @@ -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']