-
-
Save zhuzhuor/2770237 to your computer and use it in GitHub Desktop.
sogou proxy
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
# modified based on https://gist.github.com/2770237 | |
from struct import unpack | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
from httplib import HTTPResponse | |
from SocketServer import ThreadingMixIn | |
import socket | |
import time | |
x_sogou_auth = "9CD285F1E7ADB0BD403C22AD1D545F40/30/853edc6d49ba4e27" | |
def calc_sogou_hash(t, host): | |
s = (t + host + 'SogouExplorerProxy').encode('ascii') | |
code = len(s) | |
dwords = len(s) / 4 | |
rest = len(s) % 4 | |
v = unpack(str(dwords) + 'i' + str(rest) + 's', s) | |
for vv in v: | |
if type(vv) == type('i'): | |
break | |
rm = vv & 0xFFFF # right most | |
lm = vv >> 16 # left most | |
code += rm | |
code ^= code << 16 | |
code &= 0xffffffff | |
code ^= lm << 11 | |
code += code >> 11 | |
code &= 0xffffffff | |
if rest == 3: | |
code += (ord(s[-2]) << 8) + ord(s[-3]) | |
code ^= code << 16 | |
code ^= ord(s[-1]) << 18 | |
code &= 0xffffffff | |
code += code >> 11 | |
elif rest == 2: | |
code += (ord(s[-1]) << 8) + ord(s[-2]) | |
code ^= code << 11 | |
code &= 0xffffffff | |
code += code >> 17 | |
elif rest == 1: | |
code += ord(s[-1]) | |
code ^= code << 10 | |
code &= 0xffffffff | |
code += code >> 1 | |
code ^= code << 3 | |
code &= 0xffffffff | |
code += code >> 5 | |
code ^= code << 4 | |
code &= 0xffffffff | |
code += code >> 17 | |
code ^= code << 25 | |
code &= 0xffffffff | |
code += code >> 6 | |
code &= 0xffffffff | |
return hex(code)[2:].rstrip('L').zfill(8) | |
class Handler(BaseHTTPRequestHandler): | |
s = 0 | |
def do_proxy(self): | |
try: | |
if self.s == 0: | |
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.s.connect((proxy_host, 80)) | |
self.s.send(self.requestline.encode('ascii') + b"\r\n") | |
# Add Sogou Verification Tags | |
self.headers["X-Sogou-Auth"] = x_sogou_auth | |
t = hex(int(time.time()))[2:].rstrip('L').zfill(8) | |
self.headers["X-Sogou-Tag"] = \ | |
calc_sogou_hash(t, self.headers['Host']) | |
self.headers["X-Sogou-Timestamp"] = t | |
self.s.send(str(self.headers).encode('ascii') + b"\r\n") | |
# Send Post data | |
if(self.command == 'POST'): | |
self.s.send( \ | |
self.rfile.read(int(self.headers['Content-Length']))) | |
response = HTTPResponse(\ | |
self.s, method=self.command, buffering=True) | |
response.begin() | |
# Reply to the browser | |
status = "HTTP/1.1 " + str(response.status) + " " + response.reason | |
self.wfile.write(status.encode('ascii') + b'\r\n') | |
h = '' | |
for hh, vv in response.getheaders(): | |
if hh.upper() != 'TRANSFER-ENCODING': | |
h += hh + ': ' + vv + '\r\n' | |
self.wfile.write(h.encode('ascii') + b'\r\n') | |
while True: | |
response_data = response.read(8192) | |
if(len(response_data) == 0): | |
break | |
self.wfile.write(response_data) | |
except socket.error: | |
print('socket error for ' + self.requestline) | |
def do_POST(self): | |
self.do_proxy() | |
def do_GET(self): | |
self.do_proxy() | |
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer): | |
pass | |
server = ThreadingHTTPServer(("127.0.0.1", 1998), Handler) | |
#print(calc_sogou_hash('4d8cdaed', 'www.youku.com')) | |
#import random | |
#proxy_host = 'h' + str(random.randint(0, 15)) + '.edu.bj.ie.sogou.com' | |
#proxy_host = 'h' + str(random.randint(0,3)) + '.ctc.bj.ie.sogou.com' | |
#proxy_host = 'h' + str(random.randint(0,3)) + '.cnc.bj.ie.sogou.com' | |
#proxy_host = 'h' + str(random.randint(0,10)) + '.dxt.bj.ie.sogou.com' | |
proxy_host = 'h15' + '.dxt.bj.ie.sogou.com' | |
print('Proxy over ' + proxy_host) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment