Skip to content

pvarentsov/powtcp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

powtcp

This project is a simple example of Proof of work (PoW) protected TCP server. It implements challenge-response protocol and uses hashcash algorithm.

Messaging

The server and client communicate using an internal messaging protocol. Each message ends with the \n character. It's used to separeate messages from each other.

A message consists of a command and a payload. They are separated by the : character. The payload can be any string without \n character. It's not very convenient in real life, but in this project all payloads are fixed and don't contain \n character.

Supported commands:

  • 0 - Error (server -> client);
  • 1 - RequestPuzzle (client -> server);
  • 2 - ResponsePuzzle (server -> client);
  • 3 - RequestResource (client -> server);
  • 4 - ResponseResource (server -> client).

A messaging is implemented in the message package.

PoW

PoW is implemented with a challenge-response protocol:

  1. The client establishes a tcp connection with the server. The server starts to listening to client messages.

  2. The client sends the RequestPuzzle command to receive a puzzle from server.

    Message: 1:\n.

  3. The server generates a new puzzle using a hashcash algorithm, stores a puzzle in the cache with some TTL and sends the ResponsePuzzle command with this puzzle to the client.

    Message: 2:puzzle\n.

  4. The client receives a puzzle and tries to compute a puzzle hash with enough number of zero bits in the beggining. Than the client requests a resource sending a solved puzzle in the RequestResource command.

    Message: 3:solved-puzzle\n.

  5. The server receives the solved puzzle, checks TTL and sends ResponseResource command with some resource if that puzzle was solved correctly.

    Message: 4:some-resource\n.

Implementation:

How To

Requirements

Docker

# Run client and server just for demo
$ docker-compose up

# Run server listening on 8080 port
$ docker-compose up -d server

Makefile

$ make help

Usage: make [command]

Commands:

 build-server          Build server app
 build-client          Build client app

 run-server            Run server app
 run-client            Run client app

 test                  Run tests
 fmt                   Format code

Go

# Build server
$ go build -o ./bin/server ./cmd/server/*.go

# Build client
$ go build -o ./bin/client ./cmd/client/*.go

# Run server
$ ./bin/server

# Run client
$ ./bin/client

Configuration

Server and client applications support configuration from .yaml or .env files or from environment variables. Applications use default configuration if a custom configuration not passed.

Server

# Run server passing yaml config file
$ ./bin/server --config config.yaml

# Run server passing env config file
$ ./bin/server --config config.env

# Or run server using environment variables
$ ./bin/server

Client

# Run client passing yaml config file
$ ./bin/client --config config.yaml

# Run client passing env config file
$ ./bin/client --config config.env

# Or run client using environment variables
$ ./bin/client

Templates are available in the config folder.

About

Proof of Work protected TCP server.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published