Code Monkey home page Code Monkey logo

krpc_protocol's Introduction

KRPCProtocol

Build Status

KRPCProtocol is an elixir package for decoding and encoding mainline DHT messages.

Installation

First, add krpc_protocol to your mix.exs dependencies:

def deps do
    [{:krpc_protocol, "~> 0.0.4"}]
end

Then, update your dependencies:

$ mix deps.get

Usage

Encoding

ping message

iex> KRPCProtocol.encode(:ping, tid: "aa", node_id: "aa")
"d1:ad2:id2:aae1:q4:ping1:t2:aa1:y1:qe"

find node query

iex> KRPCProtocol.encode(:find_node, tid: "aa", node_id: "bb", target: "cc")
"d1:ad2:id2:bb6:target2:cc4:want2:n4e1:q9:find_node1:t2:aa1:y1:qe"

get_peers query

iex> KRPCProtocol.encode(:get_peers, node_id: "aa", info_hash: "bb", want: "n6", scrape: true, tid: "aa")
"d1:ad2:id2:aa9:info_hash2:bb6:scrapei1e4:want2:n6e1:q9:get_peers1:t2:aa1:y1:qe"

announce

iex(5)> KRPCProtocol.encode(:announce_peer, node_id: "aa", info_hash: "bb", token: "aoeusnth", tid: "cc", port: 2342)
"d1:ad2:id2:aa9:info_hash2:bb4:porti2342e5:token8:aoeusnthe1:q13:announce_peer1:t2:cc1:y1:qe"

Decoding

iex> KRPCProtocol.decode("d1:ad2:id20:aaaaaaaaaaaaaaaaaaaae1:q4:ping1:t2:aa1:y1:qe")
{:ping, %{node_id: "aaaaaaaaaaaaaaaaaaaa", tid: "aa"}}

Errors

The decode function returns {:invalid, error_msg} if an error occurs during the decoding process such as an unknown message type or invalid bencoded payload.

iex> KRPCProtocol.decode("abc")
{:invalid, "Invalid becoded payload: \"abc\""}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.