Feb 22, 2025
Implement a simple HTTPS proxy using Go
This is a simple implementation of an HTTPS forward proxy using Go, which supports Basic Authentication. It includes handling both HTTP and HTTPS requests, and runs over TLS (SSL).
Functionality:
- Supports HTTPS CONNECT method (tunneling).
- Supports regular HTTP requests.
- Implements Basic Authentication for access control.
- Runs on port
8443
(configurable). - Uses goroutines for bidirectional data transfer.
Code Implementation
Go Code for HTTPS Forward Proxy with Basic Authentication:
1 | package main |
Usage Instructions
1. Generate SSL Certificate:
Before running the server, generate a self-signed certificate using OpenSSL:
1 | openssl req -x509 -newkey rsa:4096 -keyout server.key -out server.crt -days 365 -nodes |
This will generate:
server.crt
(SSL certificate)server.key
(SSL private key)
2. How to Use:
- Set the proxy address to
https://admin:password123@localhost:8443
in your browser or client. - Modify the authentication details in the code if needed.
3. Modify Authentication:
Update the Username
and Password
in the Go code:
1 | proxyAuth := &ProxyAuth{ |
4. Compile and Run:
Run the proxy server with the following command:
1 | go run proxy.go |
The server will start and listen on https://localhost:8443
.
Notes:
- For production, consider using a valid SSL certificate rather than a self-signed certificate.
- Add logging to improve debugging.
- Enhance error handling as needed.
- Consider adding concurrency limits to protect the server.
Features:
- Basic Authentication: Ensures that only authorized users can access the proxy.
- HTTPS CONNECT Method (Tunneling): Supports secure connections by tunneling HTTPS requests.
- Regular HTTP Requests: Proxies regular HTTP requests as well.
- TLS Configuration: Secures the proxy server with SSL/TLS encryption.
Code Structure:
- ProxyAuth struct stores authentication info.
- checkAuth method verifies Basic Authentication.
- handleTunneling handles HTTPS CONNECT method (tunneling).
- handleHTTP handles regular HTTP proxy requests.
- main function configures and starts the HTTPS proxy server.
This is a simple, extendable HTTPS forward proxy that supports Basic Authentication. You can enhance it further with features like logging, access control, rate limiting, or caching based on your needs.