python套接字代理无法拦截所有数据

时间:2016-08-15 08:47:17

标签: python sockets proxy http-proxy python-sockets

我有以下python代理(在网上找到它并根据我的需要调整它),它应该拦截java应用程序和它的服务器之间的通信。通过HTTP完成通信,然后将其升级到标准套接字。我的问题是,在连接升级后,每次传输一定数量的数据后通信都会停止。我在这里缺少什么,为什么在发送/接收所有数据之前关闭连接?

使用/不使用代理进行流量捕获:

Without proxy | With proxy

我的代理:

import socket
import threading
import sys
import time

changeport = False

def server_loop(local_host, local_port, remote_host, remote_port, receive_first):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        server.bind(( local_host, local_port))
    except:
        print "[!!] Failed to listen on %s:%d" % (local_host, local_port)
        sys.exit()

    print "[*] Listening on %s:%d" % (local_host, local_port)
    server.listen(5)

    while 1:
        client_socket, addr = server.accept()
        print "[==>] Received incoming connection from %s:%d" %(addr[0], addr[1])

        # start a thread to talk to the remote host
        if (changeport):
            proxy = threading.Thread(target=proxy_handler, \
            args=(client_socket, remote_host, 1026, receive_first))
        else:
            proxy = threading.Thread(target=proxy_handler, \
            args=(client_socket, remote_host, remote_port, receive_first))
        proxy.start()


def receive_from(connection):
    buffer = ''
    connection.settimeout(2)
    begin=time.time()
    while True:
        if time.time() - begin > 2:
            break
        try:
            data = connection.recv(8192)
            if data:
                buffer += data
            else:
                time.sleep(0.1)
        except:
            pass
    return buffer


def request_handler(buffer):
    # perform packet modifications
    #buffer.replace("http://localhost" , "sw80.sw.qa.opsware.com")
    return buffer


def response_handler(buffer):
    # perform packet modifications
    return buffer


def hexdump(src, length=16):
    result = []
    digists = 4 if isinstance(src, unicode) else 2
    for i in range(len(src), length):
        s = src[i:i+length]
        hexa = b' '.join(['%0*X' % (digits, ord(x)) for x in s])
        text = b''.join([x if 0x20 <= ord(x) < 0x7F else b'.' for x in s])
        result.append(b"%04X %-*s %s" % (i, length*(digits + 1), hexa, text))


def proxy_handler(client_socket, remote_host, remote_port, receive_first):
    global changeport
    remote_socket = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
    remote_socket.connect(( remote_host, remote_port ))
    remote_socket.setblocking(0)
    if receive_first:
        remote_buffer = receive_from(remote_socket)
        #hexdump(remote_buffer)
        remote_buffer = response_handler(remote_buffer)

        # if we have data to send to client, send it:
        if len(remote_buffer):
            print "[<==] Sending %d bytes to localhost." %len(remote_buffer)
            print remote_buffer
            #hexdump(remote_buffer)
            client_socket.send(remote_buffer)

    while 1:
        local_buffer = receive_from(client_socket)
        if len(local_buffer):
            print "[==>] Received %d bytes from localhost." % len(local_buffer)
            print local_buffer
            #hexdump(local_buffer)
            local_buffer = request_handler(local_buffer)
            remote_socket.send(local_buffer)
            print "[==>] Sent to remote."

        remote_buffer = receive_from(remote_socket)
        if len(remote_buffer):
            print "[==>] Received %d bytes from remote." % len(remote_buffer)
            print remote_buffer
            #hexdump(remote_buffer)
            if "DDEA0ABBFD43FC832BE3D7950190C9BE" in remote_buffer:
                print "CHANGING REMOTE PORT TO 1026"
                changeport = True
            remote_buffer = response_handler(remote_buffer)
            client_socket.send(remote_buffer)
            print "[==>] Sent to localhost."

        if not len(local_buffer) or not len(remote_buffer):
            client_socket.close()
            remote_socket.close()
            print "[*] No more data. Closing connections"
            break



def main():
    if len(sys.argv[1:]) != 5:
        print "Usage: ./proxy.py <localhost> <localport> <remotehost> <remoteport> <receive_first>"
        print "Example: ./proxy.py 127.0.0.1 9000 10.12.122.1 9999 True"
        sys.exit()

    local_host = sys.argv[1]
    local_port = int(sys.argv[2])
    remote_host = sys.argv[3]
    remote_port = int(sys.argv[4])

    if sys.argv[5] == 'True':
        receive_first = True
    else:
        receive_first = False

    server_loop(local_host, local_port, remote_host, remote_port, receive_first)
main()

0 个答案:

没有答案