This is a lightweight HTTP proxy server built using the Sinatra framework. It acts as a pass-through proxy, allowing requests to be forwarded to a specified target URL. Additionally, it provides JWT (JSON Web Token) authentication to secure requests.
- CORS Support: Handles CORS headers, allowing cross-origin requests.
- JWT Authentication: Verifies the presence and validity of the
x-bump-jwt-token
header to ensure requests are authorized. - Flexible HTTP Method Support: Supports
GET
,POST
,PUT
, andDELETE
methods for forwarding client requests to the target server. - Automatic Request Forwarding: Forwards requests to the specified target URL while preserving headers and request bodies.
- Ruby (>= 2.7)
- Sinatra gem (
sinatra
) - JWT gem (
jwt
)
Install the required gems:
gem install sinatra jwt
- Set the
SECRET_KEY
environment variable for JWT verification:# .env SECRET_KEY = 'your-secret-key'
Run the following command to start the server on port 4567:
ruby proxy_server.rb
- Include the
x-bump-jwt-token
header with a valid JWT in your requests. - Ensure the target URL is provided as a query parameter (e.g.,
/proxy?url=https://example.com
).
The server verifies the x-bump-jwt-token
for every request. If the token is missing or invalid, it returns a 401 Unauthorized
error.
The server provides the following endpoints for request forwarding:
- GET
/proxy?url=your-target-url
- POST
/proxy?url=your-target-url
- PUT
/proxy?url=your-target-url
- PATCH
/proxy?url=your-target-url
- DELETE
/proxy?url=your-target-url
Each endpoint forwards the request to the target URL specified in the query parameter.
GET request:
curl -X GET "http://localhost:4567/proxy?url=https://jsonplaceholder.typicode.com/posts" -H "x-bump-jwt-token: YOUR_TOKEN"
POST request:
curl -X POST "http://localhost:4567/proxy?url=https://jsonplaceholder.typicode.com/posts" \
-H "Content-Type: application/json" \
-H "x-bump-jwt-token: YOUR_TOKEN" \
-d '{"title":"foo","body":"bar","userId":1}'
The server includes CORS headers for cross-origin access. Preflight OPTIONS requests are handled by default.
- 401 Unauthorized: Returned if the
x-bump-jwt-token
header is missing or if the token is invalid. - 502 Bad Gateway: Returned if there is an issue with the target server.
This project is licensed under the MIT License.
Feel free to open issues and submit pull requests!