更新Sogou代理服务器程序,支持HTTPS

2011年12月5日 | 分类: 代理工具 | 标签: , ,

这个是对之前用Python写的一个Sogou本地代理服务器的更新版本,新版本支持了HTTPS连接,实现了CONNECT请求。
我发现,Sogou上的squid服务器,限制了CONNECT命令只能够连接远程主机的443端口,其他的端口都返回403 Forbidden状态。

教育网的用户,可以使用我在学校建立的HTTP代理,scut.tk:1998。支持IPv4和IPv6的访问。

Windows Binary 下载

sogou.zip (Win32, IPv4)

上代码:

  1. ””’
  2.     Author:  Xiaoxia
  3.     Contact: xiaoxia@xiaoxia.org
  4.     Website: xiaoxia.org
  5. ”’
  6. from threading import Thread, Lock
  7. from struct import unpack
  8. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  9. from httplib import HTTPResponse
  10. from SocketServer import ThreadingMixIn
  11. import socket, os, select
  12. import time, sys, random
  13. import threading
  14. # Minimize Memory Usage
  15. threading.stack_size(128*1024)
  16. x_sogou_auth = “9CD285F1E7ADB0BD403C22AD1D545F40/30/853edc6d49ba4e27”
  17. proxy_host = “h0.edu.bj.ie.sogou.com”
  18. proxy_port = 80
  19. BufferSize = 8192
  20. RemoteTimeout = 15
  21. def calc_sogou_hash(t, host):
  22.     s = (t + host + ‘SogouExplorerProxy’).encode(‘ascii’)
  23.     code = len(s)
  24.     dwords = int(len(s)/4)
  25.     rest = len(s) % 4
  26.     v = unpack(str(dwords) + ‘i’+str(rest)+’s’, s)
  27.     for vv in v:
  28.         if(type(vv)==type(‘i’)):
  29.             break
  30.         a = (vv & 0xFFFF)
  31.         b = (vv >> 16)
  32.         code += a
  33.         code = code ^ (((code<<5)^b) << 0xb)
  34.         # To avoid overflows
  35.         code &= 0xffffffff
  36.         code += code >> 0xb
  37.     if rest == 3:
  38.         code += ord(s[len(s)-2]) * 256 + ord(s[len(s)-3])
  39.         code = code ^ ((code ^ (ord(s[len(s)-1])*4)) << 0x10)
  40.         code &= 0xffffffff
  41.         code += code >> 0xb
  42.     elif rest == 2:
  43.         code += ord(s[len(s)-1]) * 256 + ord(s[len(s)-2])
  44.         code ^= code << 0xb
  45.         code &= 0xffffffff
  46.         code += code >> 0x11
  47.     elif rest == 1:
  48.         code += ord(s[len(s)-1])
  49.         code ^= code << 0xa
  50.         code &= 0xffffffff
  51.         code += code >> 0x1
  52.     code ^= code * 8
  53.     code &= 0xffffffff
  54.     code += code >> 5
  55.     code ^= code << 4
  56.     code = code & 0xffffffff
  57.     code += code >> 0x11
  58.     code ^= code << 0x19
  59.     code = code & 0xffffffff
  60.     code += code >> 6
  61.     code = code & 0xffffffff
  62.     return hex(code)[2:].rstrip(‘L’).zfill(8)
  63. class Handler(BaseHTTPRequestHandler):
  64.     remote = None
  65.     # Ignore Connection Failure
  66.     def handle(self):
  67.         try:
  68.             BaseHTTPRequestHandler.handle(self)
  69.         except socket.error: pass
  70.     def finish(self):
  71.         try:
  72.             BaseHTTPRequestHandler.finish(self)
  73.         except socket.error: pass
  74.     # CONNECT Data Transfer
  75.     def transfer(self, a, b):
  76.         fdset = [a, b]
  77.         while True:
  78.             r,w,e = select.select(fdset, [], [])
  79.             if a in r:
  80.                 data = a.recv(BufferSize)
  81.                 if not data: break
  82.                 b.sendall(data)
  83.             if b in r:
  84.                 data = b.recv(BufferSize)
  85.                 if not data: break
  86.                 a.sendall(data)
  87.     def sogouProxy(self):
  88.         if self.remote is None or self.lastHost != self.headers[“Host”]:
  89.             self.remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  90.             self.remote.settimeout(RemoteTimeout)
  91.             self.remote.connect((proxy_host, proxy_port))
  92.         self.remote.sendall(self.requestline.encode(‘ascii’) + b”\r\n”)
  93.         # Add Sogou Verification Tags
  94.         self.headers[“X-Sogou-Auth”] = x_sogou_auth
  95.         t = hex(int(time.time()))[2:].rstrip(‘L’).zfill(8)
  96.         self.headers[“X-Sogou-Tag”] = calc_sogou_hash(t, self.headers[‘Host’])
  97.         self.headers[“X-Sogou-Timestamp”] = t
  98.         headerstr = str(self.headers).replace(“\r\n”, “\n”).replace(“\n”, “\r\n”)
  99.         self.remote.sendall(headerstr.encode(‘ascii’) + b”\r\n”)
  100.         # Send Post data
  101.         if self.command == ‘POST’:
  102.             self.remote.sendall(self.rfile.read(int(self.headers[‘Content-Length’])))
  103.         response = HTTPResponse(self.remote, method=self.command)
  104.         response.begin()
  105.         # Reply to the browser
  106.         status = “HTTP/1.1 ” + str(response.status) + ” ” + response.reason
  107.         self.wfile.write(status.encode(‘ascii’) + b’\r\n’)
  108.         hlist = []
  109.         for line in response.msg.headers: # Fixed multiple values of a same name
  110.             if ‘TRANSFER-ENCODING’ not in line.upper():
  111.                 hlist.append(line)
  112.         self.wfile.write(“”.join(hlist) + b’\r\n’)
  113.         if self.command == “CONNECT” and response.status == 200:
  114.             return self.transfer(self.remote, self.connection)
  115.         else:
  116.             while True:
  117.                 response_data = response.read(BufferSize)
  118.                 if not response_data: break
  119.                 self.wfile.write(response_data)
  120.     do_POST = do_GET = do_CONNECT = sogouProxy
  121. class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
  122.     address_family = socket.AF_INET6
  123. server_address = (“”, 1998)
  124. server = ThreadingHTTPServer(server_address, Handler)
  125. # Random Target Proxy Server
  126. proxy_host = ‘h’ + str(random.randint(0,5)) + ‘.edu.bj.ie.sogou.com’
  127. print(‘Proxy over %s.\nPlease set your browser\’s proxy to %s.’ % (proxy_host, server_address))
  128. try:
  129.     server.serve_forever()
  130. except:
  131.     os._exit(1)

来源http://xiaoxia.org/2011/11/14/update-sogou-proxy-program-with-https-support/

  1. 世纪
    2011年12月6日14:39

    问大家一个问题哈,请知道的墙友回复,先谢谢哈

    问题是:假设我博客的地址为www.blue.com,为了加速,我在speedymirror注册了帐号,弄了个镜像地址blue.speedymirror.com,现在是在我的域名面板里怎么设置cname,我设置了感觉不对,ping http://www.blue.com的时候,其IP还是空间商提供的那个IP,而不是speedymirror提供的IP,并且要访问博客的时候,用www.blue.com还是用blue.sspeedymirror.com???请知道的墙友帮忙解答,谢谢!!

  2. Ben
    2011年12月6日10:25

    scut.tk:1998 这个是不是只有教育网的IP才能用啊?
    还有,现在还能不能申请到不要钱的.tk域名呢???

    • iGFW
      2011年12月6日10:40

      免费.tk域名应该是还可以申请,不过听说现在申请的只能免费使用1年了,到期删除。