Skip to content

Moghaddm/simple-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Proxy Server

A mini HTTP proxy server written in Go that listens on a local port and forwards HTTP requests to real servers. It logs headers, URLs, and response times for all requests.

Features

  • Listens on TCP socket (port 8080 by default)
  • Forwards HTTP requests from clients to target servers
  • Logs request headers, URLs, and response times
  • Client <-> Proxy <-> Server architecture
  • Handles HTTP request/response using TCP sockets

Building

go build -o bin/proxy main.go

Running

./bin/proxy

The proxy server will start listening on port 8080.

Testing

Method 1: Using curl

Open a new terminal and run:

# Test with a simple HTTP request
curl -x http://localhost:8080 http://httpbin.org/get

# Test with headers
curl -x http://localhost:8080 -H "User-Agent: Test-Proxy" http://httpbin.org/headers

# Test POST request
curl -x http://localhost:8080 -X POST -d "test=data" http://httpbin.org/post

Method 2: Using wget

wget -e http_proxy=http://localhost:8080 http://httpbin.org/get

Method 3: Configure browser proxy

  1. Open your browser settings
  2. Configure HTTP proxy to: localhost:8080
  3. Visit any HTTP website
  4. Check the proxy server logs for request details

Method 4: Using environment variable

export http_proxy=http://localhost:8080
curl http://httpbin.org/get

Logs

The proxy server logs the following information for each request:

  • Timestamp: When the request was received
  • Method: HTTP method (GET, POST, etc.)
  • URL: Full URL of the request
  • Headers: All request headers
  • Status Code: HTTP response status code
  • Response Time: Time taken to get response from server
  • Response Headers: All response headers

Example Output

2024/01/15 10:30:45 HTTP Proxy Server listening on port 8080
2024/01/15 10:30:45 Ready to accept connections...
2024/01/15 10:31:12 
=== Incoming Request ===
2024/01/15 10:31:12 Time: 2024-01-15 10:31:12
2024/01/15 10:31:12 Method: GET
2024/01/15 10:31:12 URL: http://httpbin.org/get
2024/01/15 10:31:12 Headers:
2024/01/15 10:31:12   User-Agent: curl/7.68.0
2024/01/15 10:31:12   Accept: */*
2024/01/15 10:31:12 
=== Response ===
2024/01/15 10:31:12 Status Code: 200
2024/01/15 10:31:12 Response Time: 245ms
2024/01/15 10:31:12 Response Headers:
2024/01/15 10:31:12   Content-Type: application/json
2024/01/15 10:31:12   Content-Length: 123
2024/01/15 10:31:12 ===================

Project Structure

proxy/
├── main.go      # Main proxy server implementation
├── go.mod       # Go module file
├── README.md    # This file
└── bin/         # Build output directory (created after build)

Requirements

  • Go 1.21 or higher

Presentation Tips

  1. Start the proxy server in one terminal
  2. Make test requests using curl in another terminal
  3. Show the logs displaying headers, URLs, and response times
  4. Explain the architecture: Client sends request to proxy, proxy forwards to server, server responds through proxy back to client
  5. Demonstrate TCP socket usage by showing network connections
  6. Show HTTP request/response handling through the logs

About

an extra simple golang proxy server.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published