2444
2445
2446
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
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
|
append data $response
}
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\""
}
}
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)
}
proc ::nano::protocol::create::keepalive {ipPortDictList args} {
set retval ""
# Allow creating an empty keepalive
array set argInfo {
|
>
>
|
>
>
>
>
>
>
>
>
<
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
>
|
|
|
|
|
|
|
|
<
>
|
|
>
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
2444
2445
2446
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
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
|
append data $response
}
return [dict create extensions $extensions data $data]
}
proc ::nano::network::_localIP {version} {
set now [clock seconds]
if {[info exists ::nano::network::_localIP($version)]} {
set cached $::nano::network::_localIP($version)
set lastCheckTime [dict get $cached lastCheckTime]
if {($lastCheckTime + 300) >= $now} {
if {[dict exists $cached value]} {
return [dict get $cached value]
} else {
return -code error "Unable to resolve address for IP $version (cached)"
}
}
}
package require http 2
switch -exact -- $version {
v4 {
set urls {
http://api.ipify.org/
http://ipv4.rkeene.org/whatismyip
http://ifconfig.co/
}
set localIPPrefix "::ffff:"
}
v6 {
set urls {
http://ipv6.rkeene.org/whatismyip
http://ifconfig.co/
}
set urls {
http://api.ipify.org/
}
set localIPPrefix ""
}
default {
return -code error "version must be \"v4\" or \"v6\""
}
}
# Cache an error in advance
set ::nano::network::_localIP($version) [dict create lastCheckTime $now]
foreach url $urls {
set oldAgent [http::config -useragent]
http::config -useragent "curl/0.0"
catch {
set token [http::geturl $url -timeout 30000]
set ncode [http::ncode $token]
set data [http::data $token]
} error
http::config -useragent $oldAgent
if {![info exists data]} {
set ncode -1
set data $error
}
if {[info exists token]} {
http::cleanup $token
}
if {$ncode ne "200"} {
continue
}
set localIP $data
set localIP [string trim $localIP]
set localIPVersion ""
catch {
set localIPVersion "v[::ip::version $localIP]"
}
if {$localIPVersion != $version} {
unset localIP
continue
}
break
}
if {![info exists localIP]} {
return -code error "Unable to lookup local IP $version"
}
set localIP "${localIPPrefix}${localIP}"
# Cache the returned value
set ::nano::network::_localIP($version) [dict create lastCheckTime $now value $localIP]
return $localIP
}
proc ::nano::protocol::create::keepalive {ipPortDictList args} {
set retval ""
# Allow creating an empty keepalive
array set argInfo {
|