Giter Site home page Giter Site logo

bufrr / nkn-tuna-session Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nknorg/nkn-tuna-session

0.0 0.0 0.0 106 KB

An overlay peer to peer connection based on multiple concurrent tuna connections and ncp protocol.

License: Apache License 2.0

Go 99.83% Makefile 0.17%

nkn-tuna-session's Introduction

NKN Tuna Session

NKN Tuna Session is an overlay peer to peer connection based on multiple concurrent tuna connections and ncp protocol.

A few feature highlights:

  • Performance: Use multiple parallel paths to boost overall throughput.

  • Network agnostic: Neither dialer nor listener needs to have public IP address or NAT traversal. They are guaranteed to be connected regardless of their network conditions.

  • Security: Using public key as address, which enables built-in end to end encryption while being invulnerable to man-in-the-middle attack.

A simple illustration of a session between Alice and Bob:

        X
      /   \
Alice - Y - Bob
      \   /
        Z

Listener (Bob for example) will pay relayers in the middle (X, Y, Z) for relaying the traffic using NKN tokens. The payment will be based on bandwidth usage of the session.

Usage

You first need to import both nkn-sdk-go and nkn-tuna-session:

import (
	nkn "github.com/nknorg/nkn-sdk-go"
	ts "github.com/nknorg/nkn-tuna-session"
)

Create a multi-client and wallet (see nkn-sdk-go for details) or re-use your existing ones:

multiclient, err := nkn.NewMultiClient(...)
// wallet is only needed for listener side
wallet, err := nkn.NewWallet(...)

Then you can create a tuna session client:

// wallet is only needed for listener side and will be used for payment
// price is in unit of NKN token per MB
c, err := ts.NewTunaSessionClient(account, multiclient, wallet, &ts.Config{TunaMaxPrice: "0"})

A tuna session client can start listening for incoming session where the remote address match any of the given regexp:

// Accepting any address, equivalent to c.Listen(nkn.NewStringArray(".*"))
err = c.Listen(nil)
// Only accepting pubkey 25d660916021ab1d182fb6b52d666b47a0f181ed68cf52a056041bdcf4faaf99 but with any identifiers
err = c.Listen(nkn.NewStringArray("25d660916021ab1d182fb6b52d666b47a0f181ed68cf52a056041bdcf4faaf99$"))
// Only accepting address alice.25d660916021ab1d182fb6b52d666b47a0f181ed68cf52a056041bdcf4faaf99
err = c.Listen(nkn.NewStringArray("^alice\\.25d660916021ab1d182fb6b52d666b47a0f181ed68cf52a056041bdcf4faaf99$"))

Then it can start accepting sessions:

session, err := c.Accept()

Tuna session client implements net.Listener interface, so one can use it as a drop-in replacement when net.Listener is needed, e.g. http.Serve.

On the other hand, any tuna session client can dial a session to a remote NKN address:

session, err := c.Dial("another nkn address")

Session implements net.Conn interface, so it can be used as a drop-in replacement when net.Conn is needed:

buf := make([]byte, 1024)
n, err := session.Read(buf)
n, err := session.Write(buf)

A few more complicated examples can be found at examples

Usage on iOS/Android

This library is designed to work with gomobile and run natively on iOS/Android without any modification. You can use gomobile bind to compile it to Objective-C framework for iOS:

gomobile bind -target=ios -ldflags "-s -w" github.com/nknorg/nkn-tuna-session github.com/nknorg/nkn-sdk-go github.com/nknorg/ncp-go github.com/nknorg/tuna github.com/nknorg/nkngomobile

and Java AAR for Android:

gomobile bind -target=android -ldflags "-s -w" github.com/nknorg/nkn-tuna-session github.com/nknorg/nkn-sdk-go github.com/nknorg/ncp-go github.com/nknorg/tuna github.com/nknorg/nkngomobile

Contributing

Can I submit a bug, suggestion or feature request?

Yes. Please open an issue for that.

Can I contribute patches?

Yes, we appreciate your help! To make contributions, please fork the repo, push your changes to the forked repo with signed-off commits, and open a pull request here.

Please sign off your commit. This means adding a line "Signed-off-by: Name " at the end of each commit, indicating that you wrote the code and have the right to pass it on as an open source patch. This can be done automatically by adding -s when committing:

git commit -s

Community

nkn-tuna-session's People

Contributors

yilunzhang avatar iheron avatar bufrr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.