99)
1010
1111
12- def hexdump (src : bytes | str , length = 16 , show = True ) -> list | None :
12+ def hexdump (src : bytes | str , length = 16 , show = True ) -> list :
13+ """
14+ Generate a hex dump of a given input string or bytes object.
15+
16+ This function creates a hexadecimal representation of the input with
17+ a specified length of bytes per line. It also provides a printable
18+ representation of the content alongside the hex dump. Users can opt
19+ to display the output directly or return it as a list of strings.
20+
21+ Parameters:
22+ src: bytes | str
23+ The input data to be converted to a hex dump. Can be either
24+ a 'bytes' object or a string.
25+ length: int, optional
26+ The number of bytes to display per line in the hex dump.
27+ Defaults to 16.
28+ show: bool, optional
29+ Determines whether to print the hex dump directly. If set to
30+ False, the hex dump is returned as a list of strings. Defaults
31+ to True.
32+
33+ Returns:
34+ list
35+ Returns a list of strings representing the hex dump.
36+
37+ Raises:
38+ TypeError
39+ If `src` is not of type bytes or str.
40+ """
1341 if isinstance (src , bytes ):
1442 src = src .decode ()
15- results = list ()
43+ results = []
1644 for i in range (0 , len (src ), length ):
1745 word = str (src [i : i + length ])
1846 printable = word .translate (HEX_FILTER )
@@ -22,11 +50,21 @@ def hexdump(src: bytes | str, length=16, show=True) -> list | None:
2250 if show :
2351 for line in results :
2452 print (line )
25- else :
26- return results
53+ return results
2754
2855
2956def receive_from (connection : socket ) -> bytes :
57+ """
58+ Receives data from a socket connection until no more data is sent or an
59+ exception is raised. The method assembles all received chunks into a
60+ single bytes object.
61+
62+ Args:
63+ connection (socket): The socket connection to receive data from.
64+
65+ Returns:
66+ bytes: The complete data received from the socket connection.
67+ """
3068 buffer = b""
3169 connection .settimeout (10 )
3270 try :
@@ -44,11 +82,37 @@ def receive_from(connection: socket) -> bytes:
4482
4583
4684def request_handler (buffer : bytes ) -> bytes :
85+ """
86+ Handles incoming binary packets, processes the data, and modifies the buffer
87+ before sending it back.
88+
89+ This function takes a binary data buffer as input, modifies or processes the
90+ information contained within, and returns the updated binary buffer as output.
91+
92+ Args:
93+ buffer: Binary data to be modified and processed.
94+
95+ Returns:
96+ Modified binary buffer after processing.
97+ """
4798 # perform packet modifications
4899 return buffer
49100
50101
51102def response_handler (buffer : bytes ) -> bytes :
103+ """
104+ Processes and modifies the response packet data.
105+
106+ The function takes raw packet data as input, applies any
107+ modifications required, and returns the modified packet data
108+ as output.
109+
110+ Args:
111+ buffer (bytes): The raw packet data to be processed.
112+
113+ Returns:
114+ bytes: The modified packet data.
115+ """
52116 # perform packet modifications
53117 return buffer
54118
@@ -59,19 +123,41 @@ def proxy_handler(
59123 remote_port : int ,
60124 receive_first : bool ,
61125) -> None :
126+ """
127+ Handles the proxy connection by relaying data between a local client and a remote server.
128+
129+ This function establishes a connection with the remote host and optionally receives data first,
130+ then continuously transfers data between the local client and the remote server while applying
131+ custom request and response handlers. The function is designed for bidirectional data transfer
132+ until one or both connections are closed.
133+
134+ Parameters:
135+ client_socket: socket
136+ The socket representing the local client connection.
137+ remote_host: str
138+ The hostname or IP address of the remote server to connect to.
139+ remote_port: int
140+ The port number of the remote server.
141+ receive_first: bool
142+ A flag indicating whether to receive data from the remote server before starting
143+ the bidirectional data transfer.
144+
145+ Returns:
146+ None
147+ """
62148 remote_socket = socket (AF_INET , SOCK_STREAM )
63149 remote_socket .connect ((remote_host , remote_port ))
64150
65151 if receive_first :
66152 remote_buffer = receive_from (remote_socket )
67153 if len (remote_buffer ):
68- print ("[<==] Received %d bytes from remote." % len ( remote_buffer ) )
154+ print (f "[<==] Received { len ( remote_buffer ) } bytes from remote." )
69155 hexdump (remote_buffer )
70156
71157 while True :
72158 local_buffer = receive_from (client_socket )
73159 if len (local_buffer ):
74- print ("[<==] Received %d bytes from local." % len ( local_buffer ) )
160+ print (f "[<==] Received { len ( local_buffer ) } bytes from local." )
75161 hexdump (local_buffer )
76162
77163 local_buffer = request_handler (local_buffer )
@@ -80,7 +166,7 @@ def proxy_handler(
80166
81167 remote_buffer = receive_from (remote_socket )
82168 if len (remote_buffer ):
83- print ("[<==] Received %d bytes from remote." % len ( remote_buffer ) )
169+ print (f "[<==] Received { len ( remote_buffer ) } bytes from remote." )
84170 hexdump (remote_buffer )
85171
86172 remote_buffer = response_handler (remote_buffer )
@@ -101,21 +187,24 @@ def server_loop(
101187 remote_port : int ,
102188 receive_first : bool ,
103189):
190+ """
191+ Main server loop, binds to the local host and port and starts the server
192+ """
104193 server = socket (AF_INET , SOCK_STREAM )
105194 try :
106195 server .bind ((local_host , local_port ))
107196 except Exception as e :
108- print ("[!!] Failed to listen on %s:%d" % ( local_host , local_port ) )
197+ print (f "[!!] Failed to listen on { local_host } : { local_port } " )
109198 print ("[!!] Check for other listening sockets or correct permissions." )
110199 print (e )
111200 exit (0 )
112201
113- print ("[*] Listening on %s:%d" % ( local_host , local_port ) )
202+ print (f "[*] Listening on { local_host } : { local_port } " )
114203 server .listen (5 )
115204 while True :
116205 client_socket , addr = server .accept ()
117206 # print out the local connection information
118- print ("> Received incoming connection from %s:%d" % ( addr [0 ], addr [1 ]) )
207+ print (f "> Received incoming connection from { addr [0 ]} : { addr [1 ]} " )
119208
120209 proxy_thread = Thread (
121210 target = proxy_handler ,
@@ -125,6 +214,7 @@ def server_loop(
125214
126215
127216def main ():
217+ """Main function of the proxy, parses the command line arguments and starts the server"""
128218 if len (argv [1 :]) != 5 :
129219 print ("Usage: proxies/proxy.py [localhost] [localport]" , end = " " )
130220 print ("[remotehost] [remoteport] [receive_first]" )
@@ -139,10 +229,7 @@ def main():
139229
140230 receive_first = argv [5 ]
141231
142- if "True" in receive_first :
143- receive_first = True
144- else :
145- receive_first = False
232+ receive_first = bool (receive_first )
146233
147234 server_loop (local_host , local_port , remote_host , remote_port , receive_first )
148235
0 commit comments