Skip to content

Commit

Permalink
add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
xxnet committed Feb 16, 2015
1 parent acd1054 commit b7da21e
Show file tree
Hide file tree
Showing 153 changed files with 1,027 additions and 262 deletions.
5 changes: 5 additions & 0 deletions data/goagent/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[proxy]
enable = 0
type = SOCKS5
host = 127.0.0.1
port = 1080
3 changes: 0 additions & 3 deletions data/goagent/config.yaml

This file was deleted.

8 changes: 4 additions & 4 deletions data/launcher/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
modules:
goagent: {auto_start: 1, current_version: 3.1.32, ignore_version: 3.1.32}
launcher: {current_version: 1.0.0, ignore_version: 1.0.0}
update: {check_update: 0, last_path: /media/release/XX-Net/launcher/1.0.0, node_id: !!python/long '8796754053427',
uuid: fe2d0e80-ed75-43ed-83f0-409194ca8566}
goagent: {auto_start: 1, current_version: 3.1.33, ignore_version: 3.1.33}
launcher: {current_version: 1.0.1, ignore_version: 1.0.1}
update: {check_update: 1, last_path: /, node_id: '0',
uuid: 0}
34 changes: 0 additions & 34 deletions goagent/3.1.32/web_ui/config.html

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import sys
import os
import httplib
import time
import socket

current_path = os.path.dirname(os.path.abspath(__file__))
python_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, os.pardir, 'python27', '1.0'))
Expand All @@ -14,15 +17,15 @@
import OpenSSL
SSLError = OpenSSL.SSL.WantReadError

noarch_lib = os.path.abspath( os.path.join(python_path, 'lib', 'noarch'))
sys.path.append(noarch_lib)
import socks

import httplib
import time
import socket

from config import config
import cert_util
from openssl_wrap import SSLConnection

if __name__ == "__main__" and False:
if __name__ == "__main__":
import logging
else:
# hide log in working mode.
Expand All @@ -43,6 +46,21 @@ def warn(fmt, *args, **kwargs):
g_handshake_timeout = 2


if config.PROXY_TYPE == "HTTP":
proxy_type = socks.HTTP
elif config.PROXY_TYPE == "SOCKS4":
proxy_type = socks.SOCKS4
elif config.PROXY_TYPE == "SOCKS5":
proxy_type = socks.SOCKS5
else:
config.PROXY_ENABLE = 0
logging.warn("proxy type %s unknown, disable proxy", config.PROXY_TYPE)

if config.PROXY_ENABLE:
socks.set_default_proxy(proxy_type, config.PROXY_HOST, config.PROXY_PORT)
default_socket = socket.socket
socket.socket = socks.socksocket




Expand Down Expand Up @@ -276,7 +294,7 @@ def search_more_google_ip(self):
p.start()

if __name__ == "__main__":
#test("203.165.14.230", 10) #gws
test("208.117.224.103", 10) #gws
#test('208.117.224.213', 10)
#test("218.176.242.24")
#test_main()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def load(self):
self.CONTROL_IP = self.CONFIG.get('control', 'ip')
self.CONTROL_PORT = self.CONFIG.getint('control', 'port')

self.PROXY_ENABLE = self.CONFIG.getint('proxy', 'enable')
self.PROXY_TYPE = self.CONFIG.get('proxy', 'type')
self.PROXY_HOST = self.CONFIG.get('proxy', 'host')
self.PROXY_PORT = self.CONFIG.getint('proxy', 'port')

self.LOVE_ENABLE = self.CONFIG.getint('love', 'enable')
self.LOVE_TIP = self.CONFIG.get('love', 'tip').encode('utf8').decode('unicode-escape').split('|')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
import errno
import binascii
import time
import collections
import random
import thread
import socket
import select
import Queue
import httplib
import urlparse
import struct
import traceback
import logging
import threading
import operator
Expand All @@ -28,10 +23,30 @@
elif sys.platform == "linux" or sys.platform == "linux2":
win32_lib = os.path.abspath( os.path.join(python_path, 'lib', 'linux'))
sys.path.append(win32_lib)

import OpenSSL
SSLError = OpenSSL.SSL.WantReadError

from config import config
noarch_lib = os.path.abspath( os.path.join(python_path, 'lib', 'noarch'))
sys.path.append(noarch_lib)
import socks
if config.PROXY_TYPE == "HTTP":
proxy_type = socks.HTTP
elif config.PROXY_TYPE == "SOCKS4":
proxy_type = socks.SOCKS4
elif config.PROXY_TYPE == "SOCKS5":
proxy_type = socks.SOCKS5
else:
config.PROXY_ENABLE = 0
logging.warn("proxy type %s unknown, disable proxy", config.PROXY_TYPE)

if config.PROXY_ENABLE:
socks.set_default_proxy(proxy_type, config.PROXY_HOST, config.PROXY_PORT)
default_socket = socket.socket
socket.socket = socks.socksocket



from google_ip import google_ip

from openssl_wrap import SSLConnection
Expand Down Expand Up @@ -86,11 +101,6 @@ def get_nowait(self):
return self.get(block=False)

def _get(self):
#pool = sorted(self.pool.items(), key=operator.itemgetter(1))
#k,v = pool[0]
#self.pool.pop(k)
#return (v, k)

fastest_time = 9999
fastest_sock = None
for sock in self.pool:
Expand Down Expand Up @@ -142,52 +152,7 @@ class Https_connection_manager(object):
thread_num_lock = threading.Lock()
thread_num = 0

protocol_version = 'HTTP/1.1'
skip_headers = frozenset(['Vary',
'Via',
'X-Forwarded-For',
'Proxy-Authorization',
'Proxy-Connection',
'Upgrade',
'X-Chrome-Variations',
'Connection',
'Cache-Control'])

ssl_ciphers = ['ECDHE-ECDSA-AES256-SHA',
'ECDHE-RSA-AES256-SHA',
'DHE-RSA-CAMELLIA256-SHA',
'DHE-DSS-CAMELLIA256-SHA',
'DHE-RSA-AES256-SHA',
'DHE-DSS-AES256-SHA',
'ECDH-RSA-AES256-SHA',
'ECDH-ECDSA-AES256-SHA',
'CAMELLIA256-SHA',
'AES256-SHA',
'ECDHE-ECDSA-RC4-SHA',
'ECDHE-ECDSA-AES128-SHA',
'ECDHE-RSA-RC4-SHA',
'ECDHE-RSA-AES128-SHA',
'DHE-RSA-CAMELLIA128-SHA',
'DHE-DSS-CAMELLIA128-SHA',
'DHE-RSA-AES128-SHA',
'DHE-DSS-AES128-SHA',
'ECDH-RSA-RC4-SHA',
'ECDH-RSA-AES128-SHA',
'ECDH-ECDSA-RC4-SHA',
'ECDH-ECDSA-AES128-SHA',
'SEED-SHA',
'CAMELLIA128-SHA',
'RC4-SHA',
'RC4-MD5',
'AES128-SHA',
'ECDHE-ECDSA-DES-CBC3-SHA',
'ECDHE-RSA-DES-CBC3-SHA',
'EDH-RSA-DES-CBC3-SHA',
'EDH-DSS-DES-CBC3-SHA',
'ECDH-RSA-DES-CBC3-SHA',
'ECDH-ECDSA-DES-CBC3-SHA',
'DES-CBC3-SHA',
'TLS_EMPTY_RENEGOTIATION_INFO_SCSV']


def __init__(self):
# http://docs.python.org/dev/library/ssl.html
Expand All @@ -203,12 +168,7 @@ def __init__(self):

self.conn_pool = Connect_pool() #Queue.PriorityQueue()


# set_ciphers as Modern Browsers
# http://www.openssl.org/docs/apps/ciphers.html
#ssl_ciphers = [x for x in self.ssl_ciphers if random.random() > 0.5]

self.openssl_context = SSLConnection.context_builder(ssl_version="TLSv1") #, ca_certs=g_cacertfile) #, cipher_suites=ssl_ciphers)
self.openssl_context = SSLConnection.context_builder(ssl_version="TLSv1")

# ref: http://vincent.bernat.im/en/blog/2011-ssl-session-reuse-rfc5077.html
self.openssl_context.set_session_id(binascii.b2a_hex(os.urandom(10)))
Expand Down Expand Up @@ -273,7 +233,7 @@ def _create_ssl_connection(ip_port):
ssl_sock.sock = sock
ssl_sock.create_time = time_begin
ssl_sock.handshake_time = handshake_time

ssl_sock.host = ''

def verify_SSL_certificate_issuer(ssl_sock):
cert = ssl_sock.get_peer_certificate()
Expand Down Expand Up @@ -363,71 +323,10 @@ def create_more_connection():



def _request(self, sock, method, path, protocol_version, headers, payload, bufsize=8192):
skip_headers = self.skip_headers

request_data = '%s %s %s\r\n' % (method, path, protocol_version)
request_data += ''.join('%s: %s\r\n' % (k, v) for k, v in headers.items() if k not in skip_headers)
request_data += '\r\n'

if isinstance(payload, bytes):
sock.sendall(request_data.encode() + payload)
elif hasattr(payload, 'read'):
sock.sendall(request_data)
while 1:
data = payload.read(bufsize)
if not data:
break
sock.sendall(data)
else:
raise TypeError('_request(payload) must be a string or buffer, not %r' % type(payload))

response = httplib.HTTPResponse(sock, buffering=True)
try:
response.begin()
except httplib.BadStatusLine:
response = None
return response

# Caller: gae_urlfetch
# so scheme always be https
def request(self, method, url, payload=None, headers={}):
scheme, netloc, path, _, query, _ = urlparse.urlparse(url)
if netloc.rfind(':') <= netloc.rfind(']'):
host = netloc
else:
host, _, port = netloc.rpartition(':')
path += '?' + query

if 'Host' not in headers:
headers['Host'] = host

for i in range(self.max_retry):
ssl_sock = None
try:
ssl_sock = self.create_ssl_connection()
if not ssl_sock:
logging.debug('request "%s %s" create_ssl_connection fail', method, url)
continue

response = self._request(ssl_sock, method, path, self.protocol_version, headers, payload)
if response:
response.ssl_sock = ssl_sock
return response

except Exception as e:
logging.debug('request "%s %s" failed:%s', method, url, e)
if ssl_sock:
ssl_sock.close()
if i == self.max_retry - 1:
raise
else:
continue


class Forward_connection_manager():
max_retry = 3
max_timeout = 3
timeout = 3
max_timeout = 5
tcp_connection_cache = Queue.PriorityQueue()

def create_connection(self, sock_life=5):
Expand All @@ -436,6 +335,8 @@ def _create_connection(ip_port, queobj, delay=0):
time.sleep(delay)
ip = ip_port[0]
sock = None
# start connection time record
start_time = time.time()
try:
# create a ipv4/ipv6 socket object
sock = socket.socket(socket.AF_INET if ':' not in ip else socket.AF_INET6)
Expand All @@ -446,9 +347,8 @@ def _create_connection(ip_port, queobj, delay=0):
# disable negal algorithm to send http request quickly.
sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True)
# set a short timeout to trigger timeout retry more quickly.
sock.settimeout(self.max_timeout)
# start connection time record
start_time = time.time()
sock.settimeout(self.timeout)


conn_time = 0
# TCP connect
Expand Down Expand Up @@ -487,7 +387,8 @@ def recycle_connection(count, queobj):


port = 443
for j in range(self.max_retry):
start_time = time.time()
while time.time() - start_time < self.max_timeout:
addresses = []
for i in range(3):
ip = google_ip.get_gws_ip()
Expand All @@ -507,7 +408,7 @@ def recycle_connection(count, queobj):
if not isinstance(result, (socket.error, OSError)):
thread.start_new_thread(recycle_connection, (len(addrs)-i-1, queobj))
return result
logging.warning('create_connection fail.')
logging.warning('create tcp connection fail.')


def forward_socket(self, local, remote, timeout=60, tick=2, bufsize=8192):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit b7da21e

Please sign in to comment.