relay

package
v0.0.0-...-b361ec5 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client can send to or receive from a relay server.

func NewClient

func NewClient(addr string) *Client

NewClient creates a new Client that will communicate with the server at the specified address.

func (*Client) Offer

func (c *Client) Offer(filename string, file io.ReadCloser) (secret string, send SendFn, err error)

Offer offers a file, with a proposed filename, to a recipient via the relay server.

It does not block on sending the file, but instead returns the file's secret immediately along with a blocking function to send the file's contents.

secret, send, _ := client.Offer(filename, file)
fmt.Println(secret)	// immediately show the secret
_ = send()					// wait for the file to be sent

func (*Client) Receive

func (c *Client) Receive(secret string) (filename string, stream io.ReadCloser, err error)

Receive receives a file stored with the given secret.

It returns immediately with a proposed filename and a stream from which to read the file contents. The filename has been suggested by the sender and should not be trusted without validation.

type Handler

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

Handler is the HTTP request handler that relays messages between clients.

func NewHandler

func NewHandler(secrets fmt.Stringer, logger io.Writer) *Handler

NewHandler returns a new Handler.

It generates secret strings via the provided Stringer and logs events to the provided Writer.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP allows the handler to serve HTTP requests.

type Secrets

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

Secrets is a generator that provides unique secret strings in the form "first-second-third."

func NewSecrets

func NewSecrets(rng *rand.Rand) Secrets

NewSecrets returns a new secret generator based on the provided random-number generator.

If `rng` is deterministic, the secret strings will be as well, so be sure to seed your generator with something pseudo-random like the current time in order to generate unique secrets.

Example
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
secrets := NewSecrets(rng)
fmt.Println(secrets.String())
Output:

func (Secrets) String

func (s Secrets) String() string

String returns the next random secret from the generator.

type SendFn

type SendFn func() error

SendFn is a function that blocks until a file being sent has been completely downloaded.

Jump to

Keyboard shortcuts

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