#!/usr/bin/env python3
##############################################################################
#The MIT License (MIT)
#
#Copyright (c) 2016 Hajime Nakagami
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
##############################################################################
import sys
import socket
import binascii

def asc_dump(s):
    r = ''
    for c in s:
        r += chr(c) if (c >= 32 and c < 128) else '.'
    if r:
        print('[' + r + ']')


def parse_message(server_sock, client_sock):
    while True:
        server_head = server_sock.recv(5)
        server_code = server_head[0]
        server_ln = int.from_bytes(server_head[1:], byteorder='big') -4
        server_data = server_sock.recv(server_ln)
        print('<<', chr(server_code), binascii.b2a_hex(server_data), end='')
        asc_dump(server_data)
        client_sock.send(server_head)
        client_sock.send(server_data)
        if server_code == 90 and server_data == b'I':
            break

def read_login_packet(sock):
    head = sock.recv(4)
    ln = int.from_bytes(head, byteorder='big')
    return head + sock.recv(ln-4)


def proxy_wire(server_name, server_port, listen_host, listen_port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind((listen_host, listen_port))
    sock.listen(1)
    client_sock, addr = sock.accept()
    server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_sock.connect((server_name, server_port))

    login_packet = read_login_packet(client_sock)
    print('>> login ', binascii.b2a_hex(login_packet))
    server_sock.send(login_packet)
    parse_message(server_sock, client_sock)

    while True:
        client_head = client_sock.recv(5)
        client_code = client_head[0]
        client_ln = int.from_bytes(client_head[1:], byteorder='big')
        client_data = client_sock.recv(client_ln)
        print('>>', chr(client_code), binascii.b2a_hex(client_data), end='')
        asc_dump(client_data)
        server_sock.send(client_head)
        server_sock.send(client_data)
        parse_message(server_sock, client_sock)

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('Usage : ' + sys.argv[0] + ' server[:port] [listen_host:]listen_port')
        sys.exit()

    server = sys.argv[1].split(':')
    server_name = server[0]
    if len(server) == 1:
        server_port = 5432
    else:
        server_port = int(server[1])

    listen = sys.argv[2].split(':')
    if len(listen) == 1:
        listen_host = 'localhost'
        listen_port = int(listen[0])
    else:
        listen_host = listen[0]
        listen_port = int(listen[1])

    proxy_wire(server_name, server_port, listen_host, listen_port)

@@ -1319,16 +1319,188 @@
 nascii%0A%0A
+def recv_from_sock(sock, nbytes):%0A    n = nbytes%0A    recieved = b''%0A    while n:%0A        bs = sock.recv(n)%0A        recieved += bs%0A        n -= len(bs)%0A    return recieved%0A%0A
 def asc_
@@ -1504,24 +1504,24 @@
 sc_dump(s):%0A
-
     r = ''%0A 
@@ -1713,16 +1713,31 @@
 _head = 
+recv_from_sock(
 server_s
@@ -1735,30 +1735,26 @@
 (server_sock
-.recv(
+, 
 5)%0A        s
@@ -1875,16 +1875,31 @@
 _data = 
+recv_from_sock(
 server_s
@@ -1901,22 +1901,18 @@
 ver_sock
-.recv(
+, 
 server_l
@@ -2089,32 +2089,33 @@
 nd(server_data)%0A
+#
         if serve
@@ -2152,16 +2152,46 @@
 = b'I':%0A
+        if server_code == 90:%0A
         
@@ -2201,16 +2201,17 @@
  break%0A%0A
+%0A
 def read
@@ -2242,26 +2242,37 @@
  head = 
-sock.recv(
+recv_from_sock(sock, 
 4)%0A    l
@@ -2331,26 +2331,37 @@
  head + 
-sock.recv(
+recv_from_sock(sock, 
 ln-4)%0A%0A%0A
@@ -2932,16 +2932,31 @@
 _head = 
+recv_from_sock(
 client_s
@@ -2958,22 +2958,18 @@
 ent_sock
-.recv(
+, 
 5)%0A     
@@ -3095,35 +3095,152 @@
 a = 
-client_sock.recv(client_ln)
+recv_from_sock(client_sock, client_ln-4)%0A        client_tail = recv_from_sock(client_sock, 5)%0A        assert client_tail == b'H%5Cx00%5Cx00%5Cx00%5Cx04'
 %0A   
@@ -3377,24 +3377,24 @@
 lient_head)%0A
-
         serv
@@ -3411,32 +3411,70 @@
 nd(client_data)%0A
+        server_sock.send(client_tail)%0A
         parse_me

fix proxy