Check-in [52ff9de4cf]
Overview
Comment:Use built-in HTTP client to get local IP
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 52ff9de4cf4a2173ca99444aa37e0473d2d652b5304dd7f22316a19ad903038a
User & Date: rkeene on 2019-01-23 04:36:44
Other Links: manifest | tags
Context
2019-01-23
04:39
Only pull in the http package when it is needed check-in: e817d93da0 user: rkeene tags: trunk
04:36
Use built-in HTTP client to get local IP check-in: 52ff9de4cf user: rkeene tags: trunk
2019-01-22
18:32
Clean up amalgamation coverage bits check-in: 224abfa34b user: rkeene tags: trunk
Changes

Modified README.md from [a75cbc04db] to [9f59146d59].

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

   - The Tcl package [ip](https://core.tcl.tk/tcllib/dir?ci=trunk&name=modules/dns&type=tree)
   - The Tcl package [dns](https://core.tcl.tk/tcllib/dir?ci=trunk&name=modules/dns&type=tree)
   - The Tcl package [defer](https://core.tcl.tk/tcllib/dir?ci=trunk&name=modules/defer&type=tree)
   - The Tcl package [lmdb](https://github.com/ray2501/tcl-lmdb)
   - The Tcl package [udp](http://tcludp.sourceforge.net/)
   - The Tcl package [tclreadline](http://tclreadline.sourceforge.net/)
   - The curl command

On a Debian system you should be able to do the following (to use the package):

```
$ sudo apt install -y tcl8.6 tcllib tcl-udp tcl-tclreadline curl
```

The following packages are sufficient to compile the package:

```
$ sudo apt install -y build-essential tcl8.6-dev
```







<












32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
47
48
49
50

   - The Tcl package [ip](https://core.tcl.tk/tcllib/dir?ci=trunk&name=modules/dns&type=tree)
   - The Tcl package [dns](https://core.tcl.tk/tcllib/dir?ci=trunk&name=modules/dns&type=tree)
   - The Tcl package [defer](https://core.tcl.tk/tcllib/dir?ci=trunk&name=modules/defer&type=tree)
   - The Tcl package [lmdb](https://github.com/ray2501/tcl-lmdb)
   - The Tcl package [udp](http://tcludp.sourceforge.net/)
   - The Tcl package [tclreadline](http://tclreadline.sourceforge.net/)


On a Debian system you should be able to do the following (to use the package):

```
$ sudo apt install -y tcl8.6 tcllib tcl-udp tcl-tclreadline curl
```

The following packages are sufficient to compile the package:

```
$ sudo apt install -y build-essential tcl8.6-dev
```

Modified nano.tcl from [648175fcaf] to [9a8c392f17].

1
2
3
4

5
6
7
8
9
10
11
#! /usr/bin/env tclsh

package require Tcl 8.6.4


package require json
package require json::write

namespace eval ::nano {}
namespace eval ::nano::address {}
namespace eval ::nano::key {}
namespace eval ::nano::block {}




>







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
	return [dict create extensions $extensions data $data]
}

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



	## 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 ""
		}


	}

	set localIP [exec curl -sS $url]





	if {$localIP eq ""} {










		return -code error "Unable to lookup local IP $version"
	}



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

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

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







>
>











>
>
|
|
|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|

>
>







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\""
		}
	}

	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)