rvsTunaSession

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2022 License: Apache-2.0 Imports: 30 Imported by: 2

README

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/subhajitdas1999/rvs-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/subhajitdas1999/rvs-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/subhajitdas1999/rvs-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

Documentation

Index

Constants

View Source
const (
	DefaultSessionAllowAddr = rvs.DefaultSessionAllowAddr
	SessionIDSize           = rvs.SessionIDSize
)

Variables

This section is empty.

Functions

func DefaultSessionConfig

func DefaultSessionConfig() *ncp.Config

Types

type Config

type Config struct {
	NumTunaListeners       int
	TunaDialTimeout        int // in millisecond
	TunaMaxPrice           string
	TunaNanoPayFee         string
	TunaMinNanoPayFee      string
	TunaNanoPayFeeRatio    float64
	TunaServiceName        string
	TunaSubscriptionPrefix string
	TunaIPFilter           *geo.IPFilter
	TunaNknFilter          *filter.NknFilter
	TunaDownloadGeoDB      bool
	TunaGeoDBPath          string
	TunaMeasureBandwidth   bool
	TunaMeasureStoragePath string
	SessionConfig          *ncp.Config
}

func DefaultConfig

func DefaultConfig() *Config

func MergedConfig

func MergedConfig(conf *Config) (*Config, error)

type Conn

type Conn struct {
	net.Conn
	ReadLock  sync.Mutex
	WriteLock sync.Mutex
}

type PubAddr

type PubAddr struct {
	IP       string `json:"ip"`
	Port     uint32 `json:"port"`
	InPrice  string `json:"inPrice,omitempty"`
	OutPrice string `json:"outPrice,omitempty"`
}

type PubAddrs

type PubAddrs struct {
	Addrs []*PubAddr `json:"addrs"`
}

type Request

type Request struct {
	Action string `json:"action"`
}

type TunaSessionClient

type TunaSessionClient struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewTunaSessionClient

func NewTunaSessionClient(clientAccount *rvs.Account, m *rvs.MultiClient, wallet *rvs.Wallet, config *Config) (*TunaSessionClient, error)

func (*TunaSessionClient) Accept

func (c *TunaSessionClient) Accept() (net.Conn, error)

func (*TunaSessionClient) AcceptSession

func (c *TunaSessionClient) AcceptSession() (*ncp.Session, error)

func (*TunaSessionClient) Addr

func (c *TunaSessionClient) Addr() net.Addr

func (*TunaSessionClient) Address

func (c *TunaSessionClient) Address() string

func (*TunaSessionClient) Close

func (c *TunaSessionClient) Close() error

func (*TunaSessionClient) Dial

func (c *TunaSessionClient) Dial(remoteAddr string) (net.Conn, error)

func (*TunaSessionClient) DialSession

func (c *TunaSessionClient) DialSession(remoteAddr string) (*ncp.Session, error)

func (*TunaSessionClient) DialWithConfig

func (c *TunaSessionClient) DialWithConfig(remoteAddr string, config *rvs.DialConfig) (*ncp.Session, error)

func (*TunaSessionClient) GetPubAddrs

func (c *TunaSessionClient) GetPubAddrs() *PubAddrs

func (*TunaSessionClient) IsClosed

func (c *TunaSessionClient) IsClosed() bool

func (*TunaSessionClient) Listen

func (c *TunaSessionClient) Listen(addrsRe *nkngomobile.StringArray) error

func (*TunaSessionClient) RotateAll

func (c *TunaSessionClient) RotateAll() error

RotateOne create and replace all tuna exit. New connections accepted will use new tuna exit, existing connections will not be affected.

func (*TunaSessionClient) RotateOne

func (c *TunaSessionClient) RotateOne(i int) error

RotateOne create a new tuna exit and replace the i-th one. New connections accepted will use new tuna exit, existing connections will not be affected.

func (*TunaSessionClient) SetConfig

func (c *TunaSessionClient) SetConfig(conf *Config) error

SetConfig will set any non-empty value in conf to tuna session config.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL