proto

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2017 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

a few network helpers

Index

Constants

View Source
const (
	EntryLengthMax = 1024
	MaxPageSize    = 25
)

Variables

View Source
var (
	// Protocol header, so we know this is a zif client.
	// Version should follow.
	ProtoZif     int16 = 0x7a66
	ProtoVersion int16 = 0x0000

	ProtoHeader = "header"
	ProtoCap    = ":ap"

	// inform a peer on the status of the latest request
	ProtoOk        = "ok"
	ProtoNo        = "no"
	ProtoTerminate = "term"
	ProtoCookie    = "cookie"
	ProtoSig       = "sig"
	ProtoDone      = "done"

	ProtoSearch  = "search"  // Request a search
	ProtoRecent  = "recent"  // Request recent posts
	ProtoPopular = "popular" // Request popular posts

	// Request a signed hash list
	// The content field should contain the bytes for a Zif address.
	// This is the peer we are requesting a hash list for.
	ProtoRequestHashList = "req.hashlist"
	ProtoRequestPiece    = "req.piece"
	// Requests that this peer be added to the remotes Peers slice for a given
	// entry. This must be called at least once every hour to ensure that the peer
	// stays registered as a seed, otherwise it is culled.
	// TODO: Look into how Bittorrent trackers keep peer lists up to date properly.
	ProtoRequestAddPeer = "req.addpeer"

	ProtoPosts    = "posts" // A list of posts in Content
	ProtoHashList = "hashlist"

	ProtoDhtEntry       = "dht.entry" // An individual DHT entry in Content
	ProtoDhtEntries     = "dht.entries"
	ProtoDhtQuery       = "dht.query"
	ProtoDhtAnnounce    = "dht.announce"
	ProtoDhtFindClosest = "dht.findclosest"
)

Functions

func ChooseCompression

func ChooseCompression(client MessageCapabilities, server MessageCapabilities) string

Types

type Client

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

func NewClient

func NewClient(conn net.Conn) (*Client, error)

Creates a new client, automatically setting up the json encoder/decoder.

func (*Client) Announce

func (c *Client) Announce(e common.Encoder) error

Announce the given DHT entry to a peer, passes on this peers details, meaning that it can be reached by other peers on the network.

func (*Client) Bootstrap

func (c *Client) Bootstrap(d *dht.DHT, address dht.Address) error

Adds the initial entries into the given routing table. Essentially queries for both it's own and the peers address, storing the result. This means that after a bootstrap, it should be possible to connect to *any* peer!

func (*Client) Close

func (c *Client) Close() (err error)

Close the client connection.

func (*Client) Collection

func (c *Client) Collection(address dht.Address, entry dht.Entry) (*MessageCollection, error)

Download a hash list for a peer. Expects said hash list to be valid and signed.

func (*Client) Decode

func (c *Client) Decode(i interface{}) error

func (*Client) FindClosest

func (c *Client) FindClosest(address dht.Address) ([]*dht.Entry, error)

func (*Client) Pieces

func (c *Client) Pieces(address dht.Address, id, length int) chan *data.Piece

Download a piece from a peer, given the address and id of the piece we want.

func (*Client) Popular

func (c *Client) Popular(page int) ([]*data.Post, error)

func (*Client) Query

func (c *Client) Query(address dht.Address) (*dht.Entry, error)

func (*Client) ReadMessage

func (c *Client) ReadMessage() (*Message, error)

Blocks until a message is read from c.conn, decodes it into a *Message and returns.

func (*Client) Recent

func (c *Client) Recent(page int) ([]*data.Post, error)

func (*Client) RequestAddPeer

func (c *Client) RequestAddPeer(addr dht.Address) error

func (*Client) Search

func (c *Client) Search(search string, page int) ([]*data.Post, error)

TODO: Paginate searches

func (*Client) SendStruct

func (c *Client) SendStruct(e common.Encoder) error

Sends a DHT entry to a peer.

func (*Client) Terminate

func (c *Client) Terminate()

func (*Client) WriteErr

func (c *Client) WriteErr(toSend error) error

func (*Client) WriteMessage

func (c *Client) WriteMessage(v interface{}) error

Encodes v as json and writes it to c.conn.

type ConnHeader

type ConnHeader struct {
	Client       Client
	Entry        dht.Entry
	Capabilities MessageCapabilities
}

type Message

type Message struct {
	Header      string
	Stream      net.Conn
	Client      *Client
	From        *dht.Address
	Compression string

	Content []byte
}

func (*Message) Json

func (m *Message) Json() ([]byte, error)

func (*Message) Ok

func (m *Message) Ok() bool

Ok() is just an easier way to check if the peer has sent an "ok" response, rather than comparing the header member to a constant repeatedly.

func (*Message) Read

func (m *Message) Read(iface interface{}) error

func (*Message) ReadInt

func (m *Message) ReadInt() (int, error)

func (*Message) Write

func (m *Message) Write(iface interface{}) error

type MessageCapabilities

type MessageCapabilities struct {
	// an array of strings, each a compression type, in order of preference.
	// Index 0 is the preferred method. The method used is the shared method
	// with the lowest index.
	Compression []string
}

type MessageCollection

type MessageCollection struct {
	Hash      []byte
	HashList  []byte
	Size      int
	Signature []byte
}

func (*MessageCollection) Encode

func (mhl *MessageCollection) Encode() ([]byte, error)

func (*MessageCollection) Verify

func (mhl *MessageCollection) Verify(root []byte) error

type MessagePiece

type MessagePiece struct {
	Posts interface{}
}

Allows us to decode a pieces without also decoding all of the posts within it.

func (*MessagePiece) Hash

func (mp *MessagePiece) Hash() ([]byte, error)

type MessageRequestPiece

type MessageRequestPiece struct {
	Address string
	Id      int
	Length  int
}

func (*MessageRequestPiece) Encode

func (mrp *MessageRequestPiece) Encode() ([]byte, error)

type MessageSearchQuery

type MessageSearchQuery struct {
	Query string
	Page  int
}

func (*MessageSearchQuery) Encode

func (sq *MessageSearchQuery) Encode() ([]byte, error)

type NetworkPeer

type NetworkPeer interface {
	Session() *yamux.Session
	AddStream(net.Conn)

	Address() *dht.Address
	Query(dht.Address) (common.Verifier, error)
	FindClosest(dht.Address) ([]common.Verifier, error)
	SetCapabilities(MessageCapabilities)
	UpdateSeen()
}

Allows the protocol stuff to work with Peers, while libzif/peer can interface peers with the DHT properly.

type ProtocolHandler

type ProtocolHandler interface {
	common.Signer
	NetworkPeer

	HandleAnnounce(*Message) error
	HandleQuery(*Message) error
	HandleFindClosest(*Message) error
	HandleSearch(*Message) error
	HandleRecent(*Message) error
	HandlePopular(*Message) error
	HandleHashList(*Message) error
	HandlePiece(*Message) error
	HandleAddPeer(*Message) error

	HandleHandshake(ConnHeader) (NetworkPeer, error)
	HandleCloseConnection(*dht.Address)

	GetNetworkPeer(dht.Address) NetworkPeer
	SetNetworkPeer(NetworkPeer)
	GetCapabilities() *MessageCapabilities
}

type Server

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

func NewServer

func NewServer(cap *MessageCapabilities) *Server

func (*Server) Close

func (s *Server) Close()

func (*Server) HandleStream

func (s *Server) HandleStream(peer NetworkPeer, handler ProtocolHandler, stream net.Conn)

func (*Server) Handshake

func (s *Server) Handshake(conn net.Conn, lp ProtocolHandler, data common.Encoder)

func (*Server) Listen

func (s *Server) Listen(addr string, handler ProtocolHandler, data common.Encoder)

func (*Server) ListenStream

func (s *Server) ListenStream(peer NetworkPeer, handler ProtocolHandler)

func (*Server) RouteMessage

func (s *Server) RouteMessage(msg *Message, handler ProtocolHandler)

type StreamManager

type StreamManager struct {
	Socks     bool
	SocksPort int
	// contains filtered or unexported fields
}

func (*StreamManager) AddStream

func (sm *StreamManager) AddStream(conn net.Conn)

These streams should be coming from Server.ListenStream, as they will be started by the peer.

func (*StreamManager) Close

func (sm *StreamManager) Close()

func (*StreamManager) ConnectClient

func (sm *StreamManager) ConnectClient() (*yamux.Session, error)

func (*StreamManager) ConnectServer

func (sm *StreamManager) ConnectServer() (*yamux.Session, error)

func (*StreamManager) GetSession

func (sm *StreamManager) GetSession() *yamux.Session

func (*StreamManager) GetStream

func (sm *StreamManager) GetStream(conn net.Conn) *Client

func (*StreamManager) Handshake

func (sm *StreamManager) Handshake(conn net.Conn, lp ProtocolHandler, data common.Encoder) (*dht.Entry, *MessageCapabilities, error)

func (*StreamManager) OpenSocks

func (sm *StreamManager) OpenSocks(addr string, lp ProtocolHandler, data common.Encoder) (*ConnHeader, error)

func (*StreamManager) OpenStream

func (sm *StreamManager) OpenStream() (*Client, error)

func (*StreamManager) OpenTCP

func (sm *StreamManager) OpenTCP(addr string, lp ProtocolHandler, data common.Encoder) (*ConnHeader, error)

func (*StreamManager) RemoveStream

func (sm *StreamManager) RemoveStream(conn net.Conn)

func (*StreamManager) SetConnection

func (sm *StreamManager) SetConnection(conn ConnHeader)

func (*StreamManager) Setup

func (sm *StreamManager) Setup()

Jump to

Keyboard shortcuts

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