@@ -2,13 +2,15 @@ package main
22
33import (
44 "encoding/json"
5+ "fmt"
56 "io"
67 "log"
78 "net"
89 "net/http"
910 "net/http/httputil"
1011 "os"
1112 "os/signal"
13+ "strconv"
1214 "strings"
1315 "syscall"
1416
@@ -18,6 +20,68 @@ import (
1820type DockerContainer struct {
1921}
2022
23+ // based off of https://github.com/dotcloud/docker/blob/2a711d16e05b69328f2636f88f8eac035477f7e4/utils/utils.go
24+ func parseHost (addr string ) (string , string , error ) {
25+
26+ var (
27+ proto string
28+ host string
29+ port int
30+ )
31+ addr = strings .TrimSpace (addr )
32+ switch {
33+ case addr == "tcp://" :
34+ return "" , "" , fmt .Errorf ("Invalid bind address format: %s" , addr )
35+ case strings .HasPrefix (addr , "unix://" ):
36+ proto = "unix"
37+ addr = strings .TrimPrefix (addr , "unix://" )
38+ if addr == "" {
39+ addr = "/var/run/docker.sock"
40+ }
41+ case strings .HasPrefix (addr , "tcp://" ):
42+ proto = "tcp"
43+ addr = strings .TrimPrefix (addr , "tcp://" )
44+ case strings .HasPrefix (addr , "fd://" ):
45+ return "fd" , addr , nil
46+ case addr == "" :
47+ proto = "unix"
48+ addr = "/var/run/docker.sock"
49+ default :
50+ if strings .Contains (addr , "://" ) {
51+ return "" , "" , fmt .Errorf ("Invalid bind address protocol: %s" , addr )
52+ }
53+ proto = "tcp"
54+ }
55+
56+ if proto != "unix" && strings .Contains (addr , ":" ) {
57+ hostParts := strings .Split (addr , ":" )
58+ if len (hostParts ) != 2 {
59+ return "" , "" , fmt .Errorf ("Invalid bind address format: %s" , addr )
60+ }
61+ if hostParts [0 ] != "" {
62+ host = hostParts [0 ]
63+ } else {
64+ host = "127.0.0.1"
65+ }
66+
67+ if p , err := strconv .Atoi (hostParts [1 ]); err == nil && p != 0 {
68+ port = p
69+ } else {
70+ return "" , "" , fmt .Errorf ("Invalid bind address format: %s" , addr )
71+ }
72+
73+ } else if proto == "tcp" && ! strings .Contains (addr , ":" ) {
74+ return "" , "" , fmt .Errorf ("Invalid bind address format: %s" , addr )
75+ } else {
76+ host = addr
77+ }
78+ if proto == "unix" {
79+ return proto , host , nil
80+
81+ }
82+ return proto , fmt .Sprintf ("%s:%d" , host , port ), nil
83+ }
84+
2185func splitDockerImage (img string ) (string , string , string ) {
2286 index := 0
2387 repository := img
@@ -39,7 +103,12 @@ func splitDockerImage(img string) (string, string, string) {
39103}
40104
41105func newConn () (* httputil.ClientConn , error ) {
42- conn , err := net .Dial ("unix" , "/var/run/docker.sock" )
106+ proto , addr , err := parseHost (os .Getenv ("DOCKER_HOST" ))
107+ if err != nil {
108+ return nil , err
109+ }
110+
111+ conn , err := net .Dial (proto , addr )
43112 if err != nil {
44113 return nil , err
45114 }
0 commit comments