Skip to content

Commit bb6adfe

Browse files
committed
Add examples.
1 parent f54f842 commit bb6adfe

1 file changed

Lines changed: 137 additions & 1 deletion

File tree

README.md

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ compatibility when we need to break library API.
3838
TCPProxy: set of high-level classes to accept and manage inbound connections
3939
and initiate/tear-down outbound as needed, connecting them using forwarders
4040
once established. Will use ForwarderFast if available, falling back to the
41-
Forwarder if that fails.
41+
Forwarder if that fails to load or initialize.
4242

4343
## Use Cases
4444

@@ -53,3 +53,139 @@ pip install libasyncproxy/
5353
```
5454

5555
## Usage
56+
57+
### libasyncproxy — `AsyncProxy2FD` Example
58+
59+
This example shows how to set up a bidirectional relay between two socket pairs using `AsyncProxy2FD`. Data sent on one end is forwarded to the other, and vice versa.
60+
61+
```python
62+
import socket
63+
from libasyncproxy.AsyncProxy import AsyncProxy2FD
64+
65+
# 1. Create two socket pairs:
66+
# - (client_socket, proxy_in): client writes to `proxy_in`
67+
# - (proxy_out, server_socket): proxy writes to `proxy_out`, server reads
68+
client_socket, proxy_in = socket.socketpair()
69+
proxy_out, server_socket = socket.socketpair()
70+
71+
# 2. Initialize and start the proxy:
72+
proxy = AsyncProxy2FD(proxy_in.fileno(), proxy_out.fileno())
73+
proxy.start()
74+
75+
# 3. Send from client → server:
76+
client_msg = b"Hello from Client!"
77+
client_socket.sendall(client_msg)
78+
print("Client sent:", client_msg.decode())
79+
80+
server_recv = server_socket.recv(1024)
81+
print("Server received:", server_recv.decode())
82+
83+
# 4. Send from server → client:
84+
server_msg = b"Hello from Server!"
85+
server_socket.sendall(server_msg)
86+
print("Server sent:", server_msg.decode())
87+
88+
client_recv = client_socket.recv(1024)
89+
print("Client received:", client_recv.decode())
90+
91+
# 5. Shutdown and cleanup:
92+
proxy.join(shutdown=True)
93+
for sock in (client_socket, proxy_in, proxy_out, server_socket):
94+
sock.close()
95+
```
96+
97+
### libasyncproxy — `TCPProxy` Example
98+
99+
This example shows how to set up a TCP proxy accepting connections on
100+
`localhost:8080` and forwarding it to `www.google.com:80`.
101+
102+
```python
103+
import socket
104+
from time import sleep
105+
from libasyncproxy.TCPProxy import TCPProxy
106+
107+
# 1. Initialize and start the proxy:
108+
# - Listen on local port 8080
109+
# - Forward all traffic to www.google.com:80
110+
proxy = TCPProxy(port=8080, newhost='www.google.com', newport=80)
111+
proxy.start()
112+
print("TCPProxy running on:", proxy.sock.getsockname())
113+
114+
# 2. Connect via the proxy and send HTTP requests twice
115+
for _ in (1, 2):
116+
with socket.create_connection(('127.0.0.1', 8080)) as s:
117+
print("Connected to www.google.com via TCPProxy.")
118+
s.sendall(b"GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
119+
resp = s.recv(256)
120+
print("Response received from proxy:")
121+
print(resp.decode('utf-8', errors='replace'))
122+
123+
# 3. Shutdown the proxy cleanly
124+
proxy.shutdown()
125+
```
126+
127+
### libasyncproxy — `AdvancedAsyncProxy2FD` Example
128+
129+
This example shows how to subclass `AsyncProxy2FD` to inspect and modify data in transit using custom `in2out` and `out2in` hooks.
130+
131+
```python
132+
import socket
133+
from ctypes import string_at, memmove
134+
from libasyncproxy.AsyncProxy import AsyncProxy2FD
135+
136+
class NosyProxy(AsyncProxy2FD):
137+
def in2out(self, res_p):
138+
# Unpack the struct
139+
tr = res_p.contents
140+
ptr, length = tr.buf, tr.len
141+
142+
# Read original bytes, transform, and write back
143+
original = string_at(ptr, length)
144+
length -= 1
145+
transformed = original.upper()[:length]
146+
memmove(ptr, transformed, length)
147+
tr.len = length
148+
149+
print("in2out hook:", original, "", transformed)
150+
151+
def out2in(self, res_p):
152+
tr = res_p.contents
153+
ptr, length = tr.buf, tr.len
154+
155+
original = string_at(ptr, length)
156+
length -= 1
157+
transformed = original[::-1][1:]
158+
memmove(ptr, transformed, length)
159+
tr.len = length
160+
161+
print("out2in hook:", original, "", transformed)
162+
163+
# 1. Create two socket pairs for bidirectional flow
164+
client_socket, proxy_in = socket.socketpair()
165+
proxy_out, server_socket = socket.socketpair()
166+
167+
# 2. Initialize and start the custom proxy
168+
proxy = NosyProxy(proxy_in.fileno(), proxy_out.fileno())
169+
proxy.start()
170+
171+
# 3. Client → Server (uppercase transformation)
172+
client_msg = b"Hello from Client!"
173+
client_socket.sendall(client_msg)
174+
print("Client sent:", client_msg.decode())
175+
176+
srv_recv = server_socket.recv(1024)
177+
print("Server received:", srv_recv.decode())
178+
179+
# 4. Server → Client (reverse transformation)
180+
server_msg = b"Hello from Server!"
181+
server_socket.sendall(server_msg)
182+
print("Server sent:", server_msg.decode())
183+
184+
cli_recv = client_socket.recv(1024)
185+
print("Client received:", cli_recv.decode())
186+
187+
# 5. Shutdown and cleanup
188+
proxy.join(shutdown=True)
189+
for sock in (client_socket, proxy_in, proxy_out, server_socket):
190+
sock.close()
191+
```

0 commit comments

Comments
 (0)