Skip to content

Commit

Permalink
more options to resolve 'docker' ip also use socket
Browse files Browse the repository at this point in the history
  • Loading branch information
andresvia committed Apr 21, 2016
1 parent af369c6 commit ea949cc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

VERSION:

1.0.2
1.0.3

AUTHOR(S):

Expand All @@ -22,7 +22,7 @@
router Do a DNS request to myip.opendns.com to get your router IP address
ip Creates a simple UDP/53 connection to Google or OpenDNS and returns the source IP address
lan alias of 'ip' command
docker Attempts to obtain docker host address from $DOCKER_HOST, docker.local or local.docker, defaults to loopback (127.0.0.1) if nothing works
docker Obtain address from $DOCKER_HOST, docker.local or local.docker. If /var/run/docker.sock is a socket uses 'lan' or 127.0.0.1 if something answers on 2375 or 2376
version Version number only
help, h Shows a list of commands or help for one command

Expand Down
4 changes: 2 additions & 2 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
iptool [global options] command [command options] [arguments...]
VERSION:
1.0.2
1.0.3
AUTHOR(S):
Andres Villarroel <[email protected]>
Expand All @@ -15,7 +15,7 @@
router Do a DNS request to myip.opendns.com to get your router IP address
ip Creates a simple UDP/53 connection to Google or OpenDNS and returns the source IP address
lan alias of 'ip' command
docker Attempts to obtain docker host address from $DOCKER_HOST, docker.local or local.docker, defaults to loopback (127.0.0.1) if nothing works
docker Obtain address from $DOCKER_HOST, docker.local or local.docker. If /var/run/docker.sock is a socket uses 'lan' or 127.0.0.1 if something answers on 2375 or 2376
version Version number only
help, h Shows a list of commands or help for one command
Expand Down
63 changes: 49 additions & 14 deletions iptool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"
)

var VERSION = "1.0.2"
var VERSION = "1.0.3"

var (
opendns_servers = map[string]int{
Expand Down Expand Up @@ -50,7 +50,7 @@ func main() {
},
cli.Command{
Name: "docker",
Usage: "Attempts to obtain docker host address from $DOCKER_HOST, docker.local or local.docker, defaults to loopback (127.0.0.1) if nothing works",
Usage: "Obtain address from $DOCKER_HOST, docker.local or local.docker. If /var/run/docker.sock is a socket uses 'lan' or 127.0.0.1 if something answers on 2375 or 2376",
Action: docker_action,
},
cli.Command{
Expand Down Expand Up @@ -79,29 +79,64 @@ func dns_servers() map[string]int {
}

func docker_action(ctx *cli.Context) {
func_map := map[string]func(chan string, chan bool){
resolve_func_map := map[string]func(chan string, chan bool){
"DOCKER_HOST": resolve_from_env,
"docker.local": func(s chan string, b chan bool) { resolve_from_lookup("docker.local", s, b) },
"local.docker": func(s chan string, b chan bool) { resolve_from_lookup("local.docker", s, b) },
}
resolve := make(chan string, len(func_map))
done := make(chan bool, len(func_map))
all_done := make(chan bool)
for _, fun := range func_map {
go fun(resolve, done)
port_func_map := map[string]func(chan bool, chan bool){
"2375": func(b1, b2 chan bool) { tcp_check("2375", b1, b2) },
"2376": func(b1, b2 chan bool) { tcp_check("2376", b1, b2) },
}
resolve_resolve := make(chan string, len(resolve_func_map))
resolve_done := make(chan bool, len(resolve_func_map))
resolve_all_done := make(chan bool)
port_resolve := make(chan bool, len(port_func_map))
port_done := make(chan bool, len(port_func_map))
port_all_done := make(chan bool)
for _, fun := range resolve_func_map {
go fun(resolve_resolve, resolve_done)
}
go func() {
for i := len(resolve_func_map); i > 0; i-- {
<-resolve_done
}
resolve_all_done <- true
}()
for _, fun := range port_func_map {
go fun(port_resolve, port_done)
}
go func() {
for i := len(func_map); i > 0; i-- {
<-done
for i := len(port_func_map); i > 0; i-- {
<-port_done
}
all_done <- true
port_all_done <- true
}()
select {
case info := <-resolve:
case info := <-resolve_resolve:
putinfo(info)
case <-all_done:
putinfo("127.0.0.1")
case <-resolve_all_done:
if file_info, err := os.Stat("/var/run/docker.sock"); err == nil {
if file_info.Mode()&os.ModeSocket == os.ModeSocket {
ip_action(ctx)
return
}
}
select {
case <-port_resolve:
putinfo("127.0.0.1")
case <-port_all_done:
log.Fatal("All options to get docker address exhausted")
}
}
}

func tcp_check(port string, resolve, done chan bool) {
if conn, err := net.Dial("tcp", "127.0.0.1:"+port); err == nil {
defer conn.Close()
resolve <- true
}
done <- true
}

func resolve_from_env(resolve chan string, done chan bool) {
Expand Down

0 comments on commit ea949cc

Please sign in to comment.