KRPCProtocol is an elixir package for decoding and encoding mainline DHT messages.
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
iex> KRPCProtocol.encode(:ping, tid: "aa", node_id: "aa")
"d1:ad2:id2:aae1:q4:ping1:t2:aa1:y1:qe"
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"
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"
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"
iex> KRPCProtocol.decode("d1:ad2:id20:aaaaaaaaaaaaaaaaaaaae1:q4:ping1:t2:aa1:y1:qe")
{:ping, %{node_id: "aaaaaaaaaaaaaaaaaaaa", tid: "aa"}}
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\""}