nodepair

package module
v0.0.0-...-0f117ce Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

go-nodepair

A small library to handle transparent remote node pairing.

Usage

On one side (that is a separate binary, or either a go routine), we generate a token, display it as QR code and we wait for pairing to complete:


import (
	"context"
	"fmt"
	"time"

	nodepair "github.com/kairos-io/go-nodepair"
	qr "github.com/kairos-io/go-nodepair/qrcode"
)

func main() {

	ctx, cancel := context.WithCancel(context.Background())

    // Generate a token, and print it out on screen as QR code
	t := nodepair.GenerateToken()
	qr.Print(t)

	// bind to the token that we previously generated until we paired with the other remote
    r := map[string]string{}
	nodepair.Receive(ctx, &r, nodepair.WithToken(t))
	fmt.Println("Just got", r)

}

Pairing is a syncronous step that waits for the other party to touch base. When pairing completes on both ends, the normal execution flow restarts.

The other party can send a payload which can be any type (interface{}).

On the other end:

import (
	"context"
	"fmt"
	"time"

	nodepair "github.com/kairos-io/go-nodepair"
	qr "github.com/kairos-io/go-nodepair/qrcode"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	if err := nodepair.Send(
		ctx, map[string]string{"foo": "Bar"},
		nodepair.WithReader(qr.Reader),
	); err != nil {
		fmt.Println("ERROR", err)
		cancel()
	}
	fmt.Println("Finished sending pairing payload")
}

The pairing options can be used in both sender/receiver, allowing to use QR code also for receiving payload, and vice-versa.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateToken

func GenerateToken() string

GenerateToken returns a token which can be used for pairing

func Receive

func Receive(ctx context.Context, payload interface{}, opts ...PairOption) error

Receive a payload during pairing

func Send

func Send(ctx context.Context, payload interface{}, opts ...PairOption) error

Send a payload during device pairing

Types

type PairConfig

type PairConfig struct {
	// contains filtered or unexported fields
}

PairConfig is the pairing configuration structure

func (*PairConfig) Apply

func (c *PairConfig) Apply(opts ...PairOption) error

Apply sets the given option to the configuration

type PairOption

type PairOption func(c *PairConfig) error

PairOption is a config pair option

func WithLogLevel

func WithLogLevel(t string) PairOption

WithLogLevel sets the loglevel of the pairing operation

func WithReader

func WithReader(t TokenReader) PairOption

WithReader sets the token reader. If set, during send is invoked to retrieve a token from the specified string from the client (if any)

func WithToken

func WithToken(t string) PairOption

WithToken sets the token as a pair option The token is consumed by TokenReader to parse the string and retrieve a token from it.

type TokenReader

type TokenReader func(string) string

TokenReader is a function that reads a string and returns a token from it. A string can represent anything (uri, image file, etc.) which can be used to retrieve the connection token

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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