diff --git a/.travis.yml b/.travis.yml index 0df8414d..126adec2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,8 @@ language: go + +env: + - MONDAY_NETWORK_INTERFACE=lo:0 + matrix: include: - go: 1.x @@ -17,6 +21,7 @@ install: - go get -t -v ./... script: + - sudo ifconfig lo:0 127.0.0.2 netmask 255.0.0.0 up - go test -v ./... - if [ "${LATEST}" = "true" ]; then gox -ldflags "-s -w -X main.Version=$TRAVIS_TAG" -os="linux darwin" -arch="386 amd64" -osarch="linux/arm" -output="monday-{{.OS}}-{{.Arch}}" -verbose ./...; fi diff --git a/pkg/proxy/network.go b/pkg/proxy/network.go index 7a711c63..57033dcd 100644 --- a/pkg/proxy/network.go +++ b/pkg/proxy/network.go @@ -3,20 +3,27 @@ package proxy import ( "fmt" "net" + "os" "os/exec" ) -const ( - networkInterface = "lo0" -) +func getNetworkInterface() string { + if value := os.Getenv("MONDAY_NETWORK_INTERFACE"); value != "" { + return value + } + + return "lo0" +} func generateIP(a byte, b byte, c byte, d int, port string) (net.IP, error) { + networkInterface := getNetworkInterface() + ip := net.IPv4(a, b, c, byte(d)) for i := d; i < 255; i++ { ip = net.IPv4(a, b, c, byte(i)) - // Check lo0 interface exists + // Check if network interface exists iface, err := net.InterfaceByName(networkInterface) if err != nil { return net.IP{}, err @@ -40,9 +47,9 @@ func generateIP(a byte, b byte, c byte, d int, port string) (net.IP, error) { // Add a new IP address on the network interface command := "ifconfig" - args := []string{"lo0", "alias", ip.String(), "up"} + args := []string{networkInterface, "alias", ip.String(), "up"} if err := exec.Command(command, args...).Run(); err != nil { - return net.IP{}, fmt.Errorf("Cannot run ifconfig command to add new IP address (%s) on lo0 interface: %v", ip.String(), err) + return net.IP{}, fmt.Errorf("Cannot run ifconfig command to add new IP address (%s) on %s network interface: %v", ip.String(), networkInterface, err) } conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", ip.String(), port))