Documentation ¶
Overview ¶
a few network helpers
Index ¶
- Constants
- Variables
- func ChooseCompression(client MessageCapabilities, server MessageCapabilities) string
- type Client
- func (c *Client) Announce(e common.Encoder) error
- func (c *Client) Bootstrap(d *dht.DHT, address dht.Address) error
- func (c *Client) Close() (err error)
- func (c *Client) Collection(address dht.Address, entry dht.Entry) (*MessageCollection, error)
- func (c *Client) Decode(i interface{}) error
- func (c *Client) FindClosest(address dht.Address) ([]*dht.Entry, error)
- func (c *Client) Pieces(address dht.Address, id, length int) chan *data.Piece
- func (c *Client) Popular(page int) ([]*data.Post, error)
- func (c *Client) Query(address dht.Address) (*dht.Entry, error)
- func (c *Client) ReadMessage() (*Message, error)
- func (c *Client) Recent(page int) ([]*data.Post, error)
- func (c *Client) RequestAddPeer(addr dht.Address) error
- func (c *Client) Search(search string, page int) ([]*data.Post, error)
- func (c *Client) SendStruct(e common.Encoder) error
- func (c *Client) Terminate()
- func (c *Client) WriteErr(toSend error) error
- func (c *Client) WriteMessage(v interface{}) error
- type ConnHeader
- type Message
- type MessageCapabilities
- type MessageCollection
- type MessagePiece
- type MessageRequestPiece
- type MessageSearchQuery
- type NetworkPeer
- type ProtocolHandler
- type Server
- func (s *Server) Close()
- func (s *Server) HandleStream(peer NetworkPeer, handler ProtocolHandler, stream net.Conn)
- func (s *Server) Handshake(conn net.Conn, lp ProtocolHandler, data common.Encoder)
- func (s *Server) Listen(addr string, handler ProtocolHandler, data common.Encoder)
- func (s *Server) ListenStream(peer NetworkPeer, handler ProtocolHandler)
- func (s *Server) RouteMessage(msg *Message, handler ProtocolHandler)
- type StreamManager
- func (sm *StreamManager) AddStream(conn net.Conn)
- func (sm *StreamManager) Close()
- func (sm *StreamManager) ConnectClient() (*yamux.Session, error)
- func (sm *StreamManager) ConnectServer() (*yamux.Session, error)
- func (sm *StreamManager) GetSession() *yamux.Session
- func (sm *StreamManager) GetStream(conn net.Conn) *Client
- func (sm *StreamManager) Handshake(conn net.Conn, lp ProtocolHandler, data common.Encoder) (*dht.Entry, *MessageCapabilities, error)
- func (sm *StreamManager) OpenSocks(addr string, lp ProtocolHandler, data common.Encoder) (*ConnHeader, error)
- func (sm *StreamManager) OpenStream() (*Client, error)
- func (sm *StreamManager) OpenTCP(addr string, lp ProtocolHandler, data common.Encoder) (*ConnHeader, error)
- func (sm *StreamManager) RemoveStream(conn net.Conn)
- func (sm *StreamManager) SetConnection(conn ConnHeader)
- func (sm *StreamManager) Setup()
Constants ¶
const ( EntryLengthMax = 1024 MaxPageSize = 25 )
Variables ¶
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 (*Client) Announce ¶
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 ¶
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) Collection ¶
Download a hash list for a peer. Expects said hash list to be valid and signed.
func (*Client) FindClosest ¶
func (*Client) Pieces ¶
Download a piece from a peer, given the address and id of the piece we want.
func (*Client) ReadMessage ¶
Blocks until a message is read from c.conn, decodes it into a *Message and returns.
func (*Client) SendStruct ¶
Sends a DHT entry to a peer.
func (*Client) WriteMessage ¶
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 }
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 ¶
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 ¶
func (*MessageRequestPiece) Encode ¶
func (mrp *MessageRequestPiece) Encode() ([]byte, error)
type MessageSearchQuery ¶
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) HandleStream ¶
func (s *Server) HandleStream(peer NetworkPeer, handler ProtocolHandler, stream net.Conn)
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 ¶
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) 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()