gocage/cmd/jailhost.go

127 lines
3.4 KiB
Go
Raw Normal View History

package cmd
import (
"fmt"
"golang.org/x/net/route"
2022-04-24 16:49:54 +02:00
"net"
2022-04-24 16:55:33 +02:00
"regexp"
"strings"
)
var defaultRoute4 = [4]byte{0, 0, 0, 0}
var defaultRoute6 = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
var local6 = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
func Inet4AddrToString(ip4 route.Inet4Addr) string {
return fmt.Sprintf("%v.%v.%v.%v", ip4.IP[0], ip4.IP[1], ip4.IP[2], ip4.IP[3])
}
func Inet6AddrToString(ip6 route.Inet6Addr) string {
2022-04-24 16:49:54 +02:00
return fmt.Sprintf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
ip6.IP[0], ip6.IP[1], ip6.IP[2], ip6.IP[3], ip6.IP[4], ip6.IP[5], ip6.IP[6], ip6.IP[7],
ip6.IP[8], ip6.IP[9], ip6.IP[10], ip6.IP[11], ip6.IP[12], ip6.IP[13], ip6.IP[14], ip6.IP[15])
}
/*****************************************************************************
* Initialize default_interface, default_gatway4, default_gateway6
*****************************************************************************/
func (jh *JailHost) InitNetworkProperties() {
rib, _ := route.FetchRIB(0, route.RIBTypeRoute, 0)
messages, err := route.ParseRIB(route.RIBTypeRoute, rib)
if err != nil {
panic(err)
}
2022-04-24 16:49:54 +02:00
for _, message := range messages {
route_message := message.(*route.RouteMessage)
addresses := route_message.Addrs
card_index := route_message.Index
2022-04-24 16:49:54 +02:00
if addresses[0].Family() == 2 {
var destination4, gateway4 *route.Inet4Addr
ok := false
2022-04-24 16:49:54 +02:00
if destination4, ok = addresses[0].(*route.Inet4Addr); !ok {
continue
}
2022-04-24 16:49:54 +02:00
if gateway4, ok = addresses[1].(*route.Inet4Addr); !ok {
continue
}
2022-04-24 16:49:54 +02:00
if destination4 == nil || gateway4 == nil {
continue
}
2022-04-24 16:49:54 +02:00
if destination4.IP == defaultRoute4 {
card, _ := net.InterfaceByIndex(card_index)
//fmt.Printf("Default IPv4 gateway is %v on card %s\n", Inet4AddrToString(*gateway4), card.Name)
jh.default_interface = card.Name
jh.default_gateway4 = Inet4AddrToString(*gateway4)
}
} else if addresses[0].Family() == 28 {
var destination6, gateway6 *route.Inet6Addr
ok := false
2022-04-24 16:49:54 +02:00
if destination6, ok = addresses[0].(*route.Inet6Addr); !ok {
continue
}
2022-04-24 16:49:54 +02:00
if gateway6, ok = addresses[1].(*route.Inet6Addr); !ok {
continue
}
2022-04-24 16:49:54 +02:00
if destination6 == nil || gateway6 == nil {
continue
}
2022-04-24 16:49:54 +02:00
if destination6.IP == defaultRoute6 && gateway6.IP != local6 {
card, _ := net.InterfaceByIndex(card_index)
//fmt.Printf("Default IPv6 gateway is %v on card %s\n", Inet6AddrToString(*gateway6), card.Name)
jh.default_interface = card.Name
jh.default_gateway6 = Inet6AddrToString(*gateway6)
}
}
}
}
func (jh *JailHost) GetDefaultInterface() string {
if len(jh.default_interface) == 0 {
jh.InitNetworkProperties()
}
return jh.default_interface
}
func (jh *JailHost) GetDefaultGateway4() string {
if len(jh.default_gateway4) == 0 {
jh.InitNetworkProperties()
}
return jh.default_gateway4
}
2022-04-24 16:49:54 +02:00
func (jh *JailHost) GetDefaultGateway6() string {
if len(jh.default_gateway6) == 0 {
jh.InitNetworkProperties()
}
return jh.default_gateway6
}
2022-04-24 16:55:33 +02:00
func getHostInUseIPv4() ([]string, error) {
var ips []string
re := regexp.MustCompile(ifconfigipv4re)
out, err := executeCommand("/sbin/ifconfig")
if err != nil {
return ips, fmt.Errorf("ERROR executing \"/sbin/ifconfig\": %s", err)
}
for _, line := range strings.Split(out, "\n") {
if re.MatchString(line) {
ips = append(ips, re.FindStringSubmatch(line)[1])
}
}
return ips, nil
}