~ehmry/nim_tox

77b8b71cbabdbb4b1fdbd0a96c638aaa5806dc9b — Emery Hemingway 10 months ago fbcbdea
Add boostrap module

Add a submodule for bootstrapping from a centralized node list.
This is not a part of the core module because it introduces a
single-point-of-failure.
1 files changed, 28 insertions(+), 0 deletions(-)

A src/toxcore/bootstrap.nim
A src/toxcore/bootstrap.nim => src/toxcore/bootstrap.nim +28 -0
@@ 0,0 1,28 @@
# SPDX-FileCopyrightText: 2020 Emery Hemingway
# SPDX-License-Identifier: GPL-3.0-or-later

import ../toxcore

import std/asyncdispatch, std/httpclient, std/json

const spofNodeList* = "https://nodes.tox.chat/json"

proc bootstrapFromSpof*(tox: Tox) {.async.} =
  let
    http = newAsyncHttpClient()
    txt = await http.getContent(spofNodeList)
    js = parseJson(txt)
  close http
  for num, node in js.pairs:
    try:
      let
        ipv4 = node["ipv4"].getStr("-")
        ipv6 = node["ipv6"].getStr("-")
        port = node["port"].getInt(toxcore.defaultPort.int).uint16
        key = node["public_key"].getStr.toPublicKey
      if ipv4 != "-":
        tox.bootstrap(ipv4, key, port)
      if ipv6 != "-":
        tox.bootstrap(ipv6, key, port)
    except:
      discard