Diff

Differences From Artifact [648175fcaf]:

To Artifact [9a8c392f17]:


1
2
3
4

5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
12




+







#! /usr/bin/env tclsh

package require Tcl 8.6.4

package require http 2
package require json
package require json::write

namespace eval ::nano {}
namespace eval ::nano::address {}
namespace eval ::nano::key {}
namespace eval ::nano::block {}
2447
2448
2449
2450
2451
2452
2453


2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464


2465
2466
2467
2468
2469




















2470


2471
2472
2473
2474
2475
2476
2477
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469





2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499







+
+











+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+







	return [dict create extensions $extensions data $data]
}

proc ::nano::network::_localIP {version} {
	if {[info exists ::nano::network::_localIP($version)]} {
		return $::nano::network::_localIP($version)
	}

	package require http 2

	## XXX:TODO: Work out a better system for determining ones own IP
	switch -exact -- $version {
		v4 {
			set url "http://ipv4.rkeene.org/whatismyip"
			set localIPPrefix "::ffff:"
		}
		v6 {
			set url "http://ipv6.rkeene.org/whatismyip"
			set localIPPrefix ""
		}
		default {
			return -code error "version must be \"v4\" or \"v6\""
	}

	set localIP [exec curl -sS $url]
	if {$localIP eq ""} {
		return -code error "Unable to lookup local IP $version"
		}
	}

	catch {
		set token [http::geturl $url -timeout 30000]
		set ncode [http::ncode $token]
		set data [http::data $token]
	} error

	if {![info exists data]} {
		set ncode -1
		set data $error
	}

	if {[info exists token]} {
		http::cleanup $token
	}

	if {$ncode ne "200"} {
		return -code error "Unable to lookup local IP $version ($ncode: $data)"
	}

	set localIP $data

	set localIP [string trim $localIP]
	set localIP "${localIPPrefix}${localIP}"

	set ::nano::network::_localIP($version) $localIP

	return $::nano::network::_localIP($version)