~ehmry/nim_tox

ref: 6c1bd56ed02f16a42a18344b6f8bebe0a1a9647f nim_tox/src/toxcore/bootstrap.nim -rw-r--r-- 839 bytes
6c1bd56eEmery Hemingway SPOF bootstrap: Make IPv4 and IPv6 optional 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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; ipv4 = true; ipv6 = true) {.async.} =
  assert(ipv4 or ipv6)
  let
    http = newAsyncHttpClient()
    txt = await http.getContent(spofNodeList)
    js = parseJson(txt)
  close http
  for num, node in js.pairs:
    try:
      let
        ip4 = node["ipv4"].getStr("-")
        ip6 = node["ipv6"].getStr("-")
        port = node["port"].getInt(toxcore.defaultPort.int).uint16
        key = node["public_key"].getStr.toPublicKey
      if ipv4 and ip4 != "-":
        tox.bootstrap(ip4, key, port)
      if ipv6 and ip6 != "-":
        tox.bootstrap(ip6, key, port)
    except:
      discard