fileshare

package
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: May 31, 2025 License: GPL-3.0 Imports: 22 Imported by: 0

README

fileshare

A p2p file sharing library

Example
download
downloader := &fileshare.Downloader{
    Server: "wss://synf.in/pg",
    ListenUDPPort: 29999,
}

read := func(fh *fileshare.FileHandle) error {
    // handshake (can set the offset to facilitate breakpoint resuming)
    if err := fh.Handshake(0, nil); err != nil {
        return err
    }
    reader, fileSize, err := fh.File()
    if err != nil {
        return err
    }
    f, err := os.Create(fh.Filename)
    if err != nil {
        return err
    }
    
    if err = io.Copy(io.MultiWriter(f, sum), reader); err != nil {
        return err
    }

    peerSum, err := fh.Sha256() // file checksum from peer
    if err != nil {
        return err
    }

    if !bytes.Equal(sum.Sum(nil), peerSum) { // assert that local and remote are consistent
        return errors.New("transfer error")
    }
    return nil
}

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
err := downloader.Request(ctx, "pg://DJX2csRurJ3DvKeh63JebVHFDqVhnFjckdVhToAAiPYf/0/my-show.pptx", read)
if err != nil {
    panic(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Downloader

type Downloader struct {
	Network       string
	Server        string
	PrivateKey    string
	ListenUDPPort int
}

func (*Downloader) Request

func (d *Downloader) Request(ctx context.Context, shareURL string, read Read) error

type FileHandle

type FileHandle struct {
	Filename string
	// contains filtered or unexported fields
}

func (*FileHandle) File

func (h *FileHandle) File() (io.Reader, uint32, error)

func (*FileHandle) Handshake

func (h *FileHandle) Handshake(offset uint32, sha256Checksum []byte) error

func (*FileHandle) Sha256

func (h *FileHandle) Sha256() ([]byte, error)

type FileManager

type FileManager struct {
	Network       string
	Server        string
	PrivateKey    string
	ListenUDPPort int
	ProgressBar   func(total int64, desc string) ProgressBar
	// contains filtered or unexported fields
}

func (*FileManager) Add

func (fm *FileManager) Add(file string) error

func (*FileManager) ListenNetwork

func (m *FileManager) ListenNetwork() (net.Listener, error)

func (*FileManager) Serve

func (m *FileManager) Serve(ctx context.Context, listener net.Listener) error

func (*FileManager) SharedURLs

func (m *FileManager) SharedURLs() ([]string, error)

type NopProgress

type NopProgress struct {
}

func (NopProgress) Add

func (w NopProgress) Add(int) error

func (NopProgress) Write

func (w NopProgress) Write(p []byte) (n int, err error)

type ProgressBar

type ProgressBar interface {
	io.Writer
	Add(progress int) error
}

type PublicNetwork

type PublicNetwork struct {
	Name       string
	Server     string
	PrivateKey string
}

func (*PublicNetwork) ListenPacket

func (pn *PublicNetwork) ListenPacket(udpPort int) (net.PacketConn, error)

type Read

type Read func(f *FileHandle) error

Jump to

Keyboard shortcuts

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