This project contains a TCP server that implements Proof of Work (PoW) to protect against DDoS attacks, serving quotes from a “Word of Wisdom” collection. The project also includes a client that connects to the server, solves the PoW challenge, and receives quotes.
We will use Hashcash as the PoW algorithm. It is simple and widely used in scenarios where the server needs to limit client requests through computational effort.
This approach is computationally challenging for the client, making it harder to flood the server with requests, thus mitigating DDoS attacks.
make up
For local running this project relies on environment variables to configure certain parameters. Make sure to create an .env
filein the root directory with appropriate values (.env_example
), such as:
# The difficulty level for the Proof-of-Work (PoW) task issued to clients
APP_DIFFICULTY=4
APP_ADDR="127.0.0.1:8080"
# The number of worker processes that handle client connections concurrently.
APP_WORKERCOUNT=10
# The time (in seconds) the server will wait for active connections to finish before shutting down
APP_SHUTDOWNTIMEOUT=5
# Client-specific variables
APP_SERVERADDR="127.0.0.1:8080"
# The number of requests per second (RPS) that the client will send to the server.
APP_RPS=5
# The total number of requests the client will send to the server.
APP_TOTALREQUESTS=100
Important: the .env
file redeclare base env variables from configs
directory.
Important: youdon't need configure .env
file if you run application through docker-compose
The provided Makefile simplifies the process of building, running, testing, and linting the project, as well as building Docker images for both the server and the client.
- Go: Ensure that you have Go installed. You can install it from here.
- Docker: Make sure Docker is installed to build and run the Docker containers. Install Docker from here.
The Makefile supports various commands to manage your development workflow.
-
Install Lint Tool: Installs
golangci-lint
for running lint checks.make install-lint
-
Run Lint Checks: Runs
golangci-lint
to check the Go code for style, bugs, and best practices.make lint
-
Go Vet: Runs
go vet
to report any suspicious constructs in the code.make vet
-
Run Tests: Runs all the tests in the project using
go test
.make test
-
Build the Server: Builds the server binary and places it in the
bin/
directory.make build-server
-
Build the Client: Builds the client binary and places it in the
bin/
directory.make build-client
-
Run the Server: Runs the server locally using environment variables specified in the
.env
file.make run-server
-
Run the Client: Runs the client locally using environment variables specified in the
.env
file.make run-client
-
Build Docker Image for Server: Builds a Docker image for the server using the Dockerfile in
Dockerfile.server
.make docker-build-server
-
Build Docker Image for Client: Builds a Docker image for the client using the Dockerfile in
Dockerfile.client
.make docker-build-client
-
Build Docker Images for Both Server and Client: Builds Docker images for both the server and the client.
make docker-build-all