Created
January 15, 2025 18:24
-
-
Save strazzere/dcecbaf1598a6c159e7ecf225f8641ee to your computer and use it in GitHub Desktop.
mock proxy protocol to nginx self signed cert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import ssl | |
# connect to a local dockerized nginx instance which is explicitly set up for proxy_protocol with self signed certs | |
# for use in testing an ELB/ALB proxy locally | |
# Server details | |
server_address = ("localhost", 8443) # Replace with your server address and port | |
# Create a raw socket and connect | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(server_address) | |
# Send the PROXY protocol header | |
proxy_header = b"PROXY TCP4 192.168.1.1 192.168.1.2 12345 443\r\n" | |
sock.sendall(proxy_header) | |
# Wrap the socket with SSL after sending the PROXY header | |
context = ssl.create_default_context() | |
context.check_hostname = False | |
context.verify_mode = ssl.CERT_NONE | |
ssl_sock = context.wrap_socket(sock, server_hostname="cac.penryn.com") | |
# Send an HTTPS GET request | |
ssl_sock.sendall(b"GET / HTTP/1.1\r\nHost: cac.penryn.com\r\nConnection: close\r\n\r\n") | |
# Read and print the response | |
response = ssl_sock.recv(4096) | |
print(response.decode()) | |
# Close the connection | |
ssl_sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment