Skip to content
Simple to use but full configurable p2p framework
Go
Branch: master
Clone or download
Latest commit b1c56c5 Aug 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github initial implementation Jun 30, 2019
.vscode code docs Jul 7, 2019
crypt improved test coverage Jul 20, 2019
idea changed overview Jun 30, 2019
.editorconfig initial implementation Jun 30, 2019
.gitignore initial implementation Jun 30, 2019
.travis.yml Update .travis.yml Aug 10, 2019
CODE_OF_CONDUCT.md initial implementation Jun 30, 2019
CONTRIBUTING.md initial implementation Jun 30, 2019
LICENSE initial implementation Jun 30, 2019
README.md Update README.md Aug 10, 2019
adapter.go test for routing middleware Jul 7, 2019
adapter_io.go refactored connection end handling Jul 6, 2019
emitter.go refactorings & documentation Jul 2, 2019
example_test.go improved code coverage Jul 20, 2019
log.go updated docs, added logging, added examples Jul 8, 2019
message.go improved test coverage Jul 20, 2019
middleware.go code docs Jul 7, 2019
middleware_crypt.go removed unused code Jul 20, 2019
middleware_headers.go code docs Jul 7, 2019
middleware_log.go updated docs, added logging, added examples Jul 8, 2019
middleware_routes.go updated docs, added logging, added examples Jul 8, 2019
mock_Adapter_test.go initial implementation Jun 30, 2019
mock_PeerStore_test.go tests for invalid adds Jul 20, 2019
mock_extensions_test.go refactorings & documentation Jul 2, 2019
network_connection.go tests for unexpected peer error Jul 20, 2019
network_connection_test.go improved test coverage Jul 20, 2019
peer.go updated docs, added logging, added examples Jul 8, 2019
peer_operator.go code docs Jul 7, 2019
peer_store.go improved code coverage Jul 20, 2019
peer_store_test.go tests for unexpected peer error Jul 20, 2019
pipe.go refactored tests Jul 8, 2019
pipe_test.go tests for unexpected error during pipe processing Jul 20, 2019
tcp_adapter.go documentations Jul 8, 2019
tcp_operator.go updated docs Jul 8, 2019
tcp_operator_test.go refactored tests Jul 8, 2019

README.md

go2p

golang p2p framework

By v-braun - viktor-braun.de.

Build Status codecov Go Report Card Documentation PR welcome

Description

GO2P is a P2P framework, designed with flexibility and simplicity in mind. You can use a pre configured stack (encryption, compression, etc.) or built your own based on the existing modules.

GO2P use the middleware pattern as a core pattern for messages. If you have used expressJS, OWIN or other HTTP/Web based frameworks you should be familiar with that.
The basic idea is that an outgoing message is passed through multiple middleware functions. Each of this functions can manipulate the message.
A middleware function could encrypt, compress, log or sign the message.
Outgoing messages will be processed through the middleware functions and incomming messages in the inverted order:

Installation

go get github.com/v-braun/go2p

Usage

You like code? Checkout the chat example

The simplest way to use this framework is to create a new instance of the full configured TCP based network stack:

    localAddr := "localhost:7077"
	net := go2p.NewNetworkConnectionTCP(*localAddr, &map[string]func(peer *go2p.Peer, msg *go2p.Message){
		"msg": func(peer *go2p.Peer, msg *go2p.Message) {
			fmt.Printf("%s > %s\n", peer.RemoteAddress(), msg.PayloadGetString())
		},
    })
    
    net.OnPeer(func(p *go2p.Peer) {
		fmt.Printf("new peer: %s\n", p.RemoteAddress())
    })
    
    err := net.Start()
	if err != nil {
		panic(err)
    }

    defer net.Stop()
    

    // connects to another peer via tcp
    net.ConnectTo("tcp", "localhost:7077")

    // send a message to the 'msg' route 
    net.SendBroadcast(go2p.NewMessageRoutedFromString("msg", "hello go2p"))


Advanced Usage

The function NewNetworkConnectionTCP is a shorthand for the advanced configuration of a network stack.

	op := go2p.NewTCPOperator("tcp", localAddr) // creates a tcp based operator (net.Dialer and net.Listener)
	peerStore := go2p.NewDefaultPeerStore(10) // creates a simple peer store that limits connections to 10

	conn := go2p.NewNetworkConnection(). // creates a new instance of the builder
		WithOperator(op). // adds the operator to the network stack
		WithPeerStore(peerStore). // adds the peer store to the network stack
		WithMiddleware(go2p.Routes(routes)). // adds the routes middleware
		WithMiddleware(go2p.Headers()). // adds the headers middleware
		WithMiddleware(go2p.Crypt()). // adds encryption
		WithMiddleware(go2p.Log()). // adds logging
		Build() // creates the network 

This code creates a new NetworkConnection that use tcp communication, a default PeerStore and some middlewares.
Outgoing messages will now pass the following middlewares:

(app logic) -> Routing -> Headers -> Crypt -> Log -> (network) 

Incomming messages will pass the following middlewares

(app logic) <- Routing <- Headers <- Crypt <- Log <- (network)

Authors

image
v-braun

Contributing

Make sure to read these guides before getting started:

License

go2p is available under the MIT License. See LICENSE for details.

You can’t perform that action at this time.