Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added a network interface on Travis CI
  • Loading branch information
eko committed Jul 22, 2019
1 parent dce4472 commit 8fc0cc6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
@@ -1,4 +1,8 @@
language: go

env:
- MONDAY_NETWORK_INTERFACE=lo:0

matrix:
include:
- go: 1.x
Expand All @@ -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

Expand Down
19 changes: 13 additions & 6 deletions pkg/proxy/network.go
Expand Up @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 8fc0cc6

Please sign in to comment.