Skip to content

Commit

Permalink
Replacing native config with requests version. Still need to figure o…
Browse files Browse the repository at this point in the history
…ut why the native version doesn't work.
  • Loading branch information
SillyGoat committed Sep 13, 2015
1 parent 961a2dd commit e7f4a2c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
1 change: 1 addition & 0 deletions PIAPortForward.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="piaportforward\core.py" />
<Compile Include="piaportforward\gtkui.py" />
<Compile Include="piaportforward\pia_port.py" />
<Compile Include="piaportforward\pia_port_native.py" />
<Compile Include="piaportforward\pia_port_requests.py" />
<Compile Include="piaportforward\webui.py" />
<Compile Include="piaportforward\__init__.py" />
Expand Down
27 changes: 13 additions & 14 deletions piaportforward/pia_port.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import httplib
import json
import random
import requests
import socket
import string
import urllib

PIA_SERVER = 'www.privateinternetaccess.com'

Expand All @@ -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'] )
Expand Down
48 changes: 48 additions & 0 deletions piaportforward/pia_port_native.py
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']

0 comments on commit e7f4a2c

Please sign in to comment.