Skip to content

Commit

Permalink
Do not exit when not connected to a network; workaround for grandcat/…
Browse files Browse the repository at this point in the history
  • Loading branch information
probonopd committed Nov 5, 2019
1 parent f51680c commit 14222c4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/appimaged/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ func main() {
watchDirectories()

if *noZeroconfPtr == false {
go registerZeroconfService()
if checkIfConnectedToNetwork() == true {
go registerZeroconfService()
}
}
go browseZeroconfServices()

Expand Down
56 changes: 56 additions & 0 deletions src/appimaged/zeroconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main
import (
"context"
"log"
"net"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -75,3 +76,58 @@ func browseZeroconfServices() {
<-ctx.Done()

}

// checkIfConnectedToNetwork returns true if connected to a network
func checkIfConnectedToNetwork() bool {
var interfaces []net.Interface
ifaces, err := net.Interfaces()
if err != nil {
return false
}
for _, ifi := range ifaces {
if (ifi.Flags & net.FlagUp) == 0 {
continue
}
if (ifi.Flags & net.FlagMulticast) > 0 {
interfaces = append(interfaces, ifi)
}
}

var AddrIPv4 []net.IP
var AddrIPv6 []net.IP
for _, iface := range interfaces {
v4, v6 := addrsForInterface(&iface)
AddrIPv4 = append(AddrIPv4, v4...)
AddrIPv6 = append(AddrIPv6, v6...)
}

if AddrIPv4 == nil && AddrIPv6 == nil {
return false
} else {
return true
}

}

func addrsForInterface(iface *net.Interface) ([]net.IP, []net.IP) {
var v4, v6, v6local []net.IP
addrs, _ := iface.Addrs()
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
v4 = append(v4, ipnet.IP)
} else {
switch ip := ipnet.IP.To16(); ip != nil {
case ip.IsGlobalUnicast():
v6 = append(v6, ipnet.IP)
case ip.IsLinkLocalUnicast():
v6local = append(v6local, ipnet.IP)
}
}
}
}
if len(v6) == 0 {
v6 = v6local
}
return v4, v6
}

0 comments on commit 14222c4

Please sign in to comment.