Skip to content

Commit

Permalink
Handle other network cases
Browse files Browse the repository at this point in the history
- Fails to start app if user is not connected to any network
- Falls back to looping over interfaces if user is not connected
  to internet
  • Loading branch information
robgonnella committed Jun 24, 2023
1 parent 17f2ba3 commit 683c4b1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
36 changes: 18 additions & 18 deletions internal/ui/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ func NewUI() *UI {
func (u *UI) Launch() error {
log := logger.New()

userIP, cidr, err := util.GetNetworkInfo()

if err != nil {
log.Fatal().Err(err).Msg("failed to get default network info")
}

appCore, err := util.CreateNewAppCore(*cidr)

if err != nil {
log.Fatal().Err(err).Msg("failed to create app core")
}

allConfigs, err := appCore.GetConfigs()

if err != nil {
log.Fatal().Err(err).Msg("failed to retrieve configs")
}

level := zerolog.GlobalLevel()

if level != zerolog.Disabled {
Expand Down Expand Up @@ -63,24 +81,6 @@ func (u *UI) Launch() error {
}
}

userIP, cidr, err := util.GetNetworkInfo()

if err != nil {
log.Fatal().Err(err).Msg("failed to get default network info")
}

appCore, err := util.CreateNewAppCore(*cidr)

if err != nil {
log.Fatal().Err(err).Msg("failed to create app core")
}

allConfigs, err := appCore.GetConfigs()

if err != nil {
log.Fatal().Err(err).Msg("failed to retrieve configs")
}

u.view = newView(*userIP, allConfigs, appCore)

os.Stdout, _ = os.Open(os.DevNull)
Expand Down
46 changes: 39 additions & 7 deletions internal/util/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,57 @@ func getIPNetByIP(ip net.IP) (*net.IPNet, error) {
// GetNetworkInfo returns userIP and cidr block for preferred
// outbound ip of this machine
func GetNetworkInfo() (*string, *string, error) {
// udp doesn't make a full connection and will find the default ip
// that traffic will use if say 2 are configured (wired and wireless)
conn, err := net.Dial("udp", "8.8.8.8:80")

var foundIP net.IP

if err != nil {
return nil, nil, err
}
// resort to looping through interfaces
ifaces, err := net.Interfaces()

if err != nil {
return nil, nil, err
}

defer conn.Close()
OUTER:
for _, i := range ifaces {
addrs, err := i.Addrs()

localAddr := conn.LocalAddr().(*net.UDPAddr)
if err != nil {
continue
}

for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPAddr:
if !v.IP.IsLoopback() {
foundIP = v.IP
break OUTER
}
}
}
}

if foundIP == nil {
return nil, nil, fmt.Errorf("failed to find IP address for this machine")
}
} else {
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
foundIP = localAddr.IP
}

ipnet, err := getIPNetByIP(localAddr.IP)
ipnet, err := getIPNetByIP(foundIP)

if err != nil {
return nil, nil, err
}

size, _ := ipnet.Mask.Size()

ipCidr := fmt.Sprintf("%s/%d", localAddr.IP, size)
ipCidr := fmt.Sprintf("%s/%d", foundIP, size)

ip, ipnet, err := net.ParseCIDR(ipCidr)
if err != nil {
Expand All @@ -84,7 +116,7 @@ func GetNetworkInfo() (*string, *string, error) {
}
}

userIP := localAddr.IP.String()
userIP := foundIP.String()
cidr := fmt.Sprintf("%s/%d", firstCidrIP, size)

return &userIP, &cidr, nil
Expand Down

0 comments on commit 683c4b1

Please sign in to comment.