Check-in [e82a0e097b]
Overview
Comment:Better handling of looking up local IP address
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e82a0e097bc13b8c8ca24d14c8f09f76ba591eb3c65ab0c40fb93dd135b5edd9
User & Date: rkeene on 2019-01-23 05:27:30
Other Links: manifest | tags
Context
2019-01-23
05:29
Removed debugging code check-in: 6d73b93560 user: rkeene tags: trunk
05:27
Better handling of looking up local IP address check-in: e82a0e097b user: rkeene tags: trunk
04:39
Only pull in the http package when it is needed check-in: e817d93da0 user: rkeene tags: trunk
Changes

Modified nano.tcl from [029f9b07a2] to [2c5c4683c8].

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
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)]} {
		return $::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

	## XXX:TODO: Work out a better system for determining ones own IP
	switch -exact -- $version {
		v4 {
			set urls {
				http://api.ipify.org/
			set url "http://ipv4.rkeene.org/whatismyip"
				http://ipv4.rkeene.org/whatismyip
				http://ifconfig.co/
			}

			set localIPPrefix "::ffff:"
		}
		v6 {
			set urls {
			set url "http://ipv6.rkeene.org/whatismyip"
				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
		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 data]} {
			set ncode -1
			set data $error
		}

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

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

	set localIP $data

	set localIP [string trim $localIP]
		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) $localIP
	set ::nano::network::_localIP($version) [dict create lastCheckTime $now value $localIP]

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

proc ::nano::protocol::create::keepalive {ipPortDictList args} {
	set retval ""

	# Allow creating an empty keepalive
	array set argInfo {