288 lines
6.8 KiB
Go
288 lines
6.8 KiB
Go
package govmtools
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strings"
|
|
|
|
xdr "github.com/stellar/go-xdr/xdr3"
|
|
"github.com/vishvananda/netlink"
|
|
)
|
|
|
|
func GetGuestNicProto() (*GuestNicProto, error) {
|
|
res := NicInfoV3{
|
|
Nics: []GuestNicV3{},
|
|
Routes: []InetCidrRouteEntry{},
|
|
DnsConfigInfo: []DnsConfigInfo{},
|
|
}
|
|
|
|
ifaces, err := netlink.LinkList()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get ifaces: %w", err)
|
|
}
|
|
|
|
ignoredPrefixes := []string{"veth", "docker", "virbr", "lo", "br-"}
|
|
for _, iface := range ifaces {
|
|
attrs := iface.Attrs()
|
|
|
|
skip := false
|
|
for _, prefix := range ignoredPrefixes {
|
|
if strings.HasPrefix(attrs.Name, prefix) {
|
|
skip = true
|
|
}
|
|
}
|
|
if skip || attrs.HardwareAddr.String() == "" {
|
|
continue
|
|
}
|
|
|
|
nic := GuestNicV3{
|
|
MacAddress: attrs.HardwareAddr.String(),
|
|
Ips: []IpAddressEntry{},
|
|
}
|
|
|
|
addrs, err := netlink.AddrList(iface, netlink.FAMILY_ALL)
|
|
if err != nil {
|
|
fmt.Println("error getting addresses:", err)
|
|
continue
|
|
}
|
|
|
|
if len(addrs) < 1 {
|
|
continue
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
// ugly
|
|
ip := addr.IP
|
|
var address InetAddress
|
|
var addrType InetAddressType
|
|
if ip.To4() != nil {
|
|
address = InetAddress(ip.To4())
|
|
addrType = IatIpv4
|
|
} else {
|
|
address = InetAddress(ip.To16())
|
|
addrType = IatIpv6
|
|
}
|
|
prefixLength, _ := addr.Mask.Size()
|
|
|
|
entry := IpAddressEntry{
|
|
IpAddressAddr: TypedIpAddress{
|
|
IpAddressAddrType: addrType,
|
|
IpAddressAddr: address,
|
|
},
|
|
IpAddressPrefixLength: InetAddressPrefixLength(prefixLength),
|
|
IpAddressOrigin: nil,
|
|
IpAddressStatus: nil,
|
|
}
|
|
nic.Ips = append(nic.Ips, entry)
|
|
}
|
|
|
|
routes, err := netlink.RouteList(iface, netlink.FAMILY_ALL)
|
|
if err != nil {
|
|
fmt.Println("error getting routes:", err)
|
|
continue
|
|
}
|
|
for _, route := range routes {
|
|
var addr TypedIpAddress
|
|
if route.Dst.IP.To4() != nil {
|
|
addr = TypedIpAddress{
|
|
IpAddressAddrType: IatIpv4,
|
|
IpAddressAddr: InetAddress(route.Dst.IP.To4()),
|
|
}
|
|
} else {
|
|
addr = TypedIpAddress{
|
|
IpAddressAddrType: IatIpv6,
|
|
IpAddressAddr: InetAddress(route.Dst.IP.To16()),
|
|
}
|
|
}
|
|
routePrefix, _ := route.Dst.Mask.Size()
|
|
r := InetCidrRouteEntry{
|
|
InetCidrRouteDest: addr,
|
|
InetCidrRoutePfxLen: InetAddressPrefixLength(routePrefix),
|
|
InetCidrRouteNextHop: route.Gw.,
|
|
InetCidrRouteIfIndex: 0,
|
|
InetCidrRouteType: 0,
|
|
InetCidrRouteMetric: 0,
|
|
}
|
|
}
|
|
|
|
res.Nics = append(res.Nics, nic)
|
|
}
|
|
|
|
return &GuestNicProto{
|
|
Type: ProtoTypeV3,
|
|
NicInfoV3: &NicInfoV3Wrapper{
|
|
NicInfoV3: []NicInfoV3{},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func GetGuestInfoNetwork() (string, error) {
|
|
data, err := GetGuestNicProto()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
buf := bytes.NewBuffer(nil)
|
|
_, err = xdr.Marshal(buf, data)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return buf.String(), nil
|
|
}
|
|
|
|
func DoStuff() {
|
|
buf := bytes.NewBuffer(nil)
|
|
xdr.Marshal(buf, &GuestNicProto{
|
|
Type: ProtoTypeV3,
|
|
NicInfoV3: &NicInfoV3Wrapper{
|
|
NicInfoV3: []NicInfoV3{
|
|
{
|
|
Nics: []GuestNicV3{
|
|
{
|
|
MacAddress: "00:50:56:9f:41:dd",
|
|
Ips: []IpAddressEntry{
|
|
{
|
|
IpAddressAddr: TypedIpAddress{
|
|
IpAddressAddrType: IatIpv4,
|
|
IpAddressAddr: []byte{10, 99, 0, 4},
|
|
},
|
|
IpAddressPrefixLength: 16,
|
|
IpAddressOrigin: nil,
|
|
IpAddressStatus: []IpAddressStatus{
|
|
IasPreferred,
|
|
},
|
|
},
|
|
{
|
|
IpAddressAddr: TypedIpAddress{
|
|
IpAddressAddrType: IatIpv4,
|
|
IpAddressAddr: []byte{10, 99, 1, 51},
|
|
},
|
|
IpAddressPrefixLength: 16,
|
|
IpAddressOrigin: nil,
|
|
IpAddressStatus: []IpAddressStatus{
|
|
IasPreferred,
|
|
},
|
|
},
|
|
{
|
|
IpAddressAddr: TypedIpAddress{
|
|
IpAddressAddrType: IatIpv6,
|
|
IpAddressAddr: []byte{
|
|
0xfe, 0x80, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
0x02, 0x50, 0x56, 0xff,
|
|
0xfe, 0x9f, 0x41, 0xdd,
|
|
},
|
|
},
|
|
IpAddressPrefixLength: 64,
|
|
IpAddressOrigin: nil,
|
|
IpAddressStatus: []IpAddressStatus{
|
|
IasUnknown,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Routes: []InetCidrRouteEntry{
|
|
{
|
|
InetCidrRouteDest: TypedIpAddress{
|
|
IpAddressAddrType: IatIpv4,
|
|
IpAddressAddr: []byte{0, 0, 0, 0},
|
|
},
|
|
InetCidrRoutePfxLen: 0,
|
|
InetCidrRouteNextHop: []TypedIpAddress{
|
|
{
|
|
IpAddressAddrType: IatIpv4,
|
|
IpAddressAddr: []byte{10, 99, 0, 1},
|
|
},
|
|
},
|
|
InetCidrRouteIfIndex: 0,
|
|
InetCidrRouteType: 0,
|
|
InetCidrRouteMetric: 0,
|
|
},
|
|
{
|
|
InetCidrRouteDest: TypedIpAddress{
|
|
IpAddressAddrType: IatIpv4,
|
|
IpAddressAddr: []byte{10, 99, 0, 0},
|
|
},
|
|
InetCidrRoutePfxLen: 16,
|
|
InetCidrRouteNextHop: nil,
|
|
InetCidrRouteIfIndex: 0,
|
|
InetCidrRouteType: 0,
|
|
InetCidrRouteMetric: 0,
|
|
},
|
|
{
|
|
InetCidrRouteDest: TypedIpAddress{
|
|
IpAddressAddrType: IatIpv6,
|
|
IpAddressAddr: []byte{
|
|
0xfe, 0x80, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
},
|
|
},
|
|
InetCidrRoutePfxLen: 64,
|
|
InetCidrRouteNextHop: nil,
|
|
InetCidrRouteIfIndex: 0,
|
|
InetCidrRouteType: 0,
|
|
InetCidrRouteMetric: 0x100,
|
|
},
|
|
{
|
|
InetCidrRouteDest: TypedIpAddress{
|
|
IpAddressAddrType: IatIpv6,
|
|
IpAddressAddr: []byte{
|
|
0xfe, 0x80, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
0x02, 0x50, 0x56, 0xff,
|
|
0xfe, 0x9f, 0x41, 0xdd,
|
|
},
|
|
},
|
|
InetCidrRoutePfxLen: 128,
|
|
InetCidrRouteNextHop: nil,
|
|
InetCidrRouteIfIndex: 0,
|
|
InetCidrRouteType: 0,
|
|
InetCidrRouteMetric: 0,
|
|
},
|
|
{
|
|
InetCidrRouteDest: TypedIpAddress{
|
|
IpAddressAddrType: IatIpv6,
|
|
IpAddressAddr: []byte{
|
|
0xff, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
},
|
|
},
|
|
InetCidrRoutePfxLen: 8,
|
|
InetCidrRouteNextHop: nil,
|
|
InetCidrRouteIfIndex: 0,
|
|
InetCidrRouteType: 0,
|
|
InetCidrRouteMetric: 256,
|
|
},
|
|
},
|
|
DnsConfigInfo: []DnsConfigInfo{
|
|
{
|
|
HostName: []string{"imagepacker"},
|
|
DomainName: []string{"t2hack.internal"},
|
|
ServerList: []TypedIpAddress{
|
|
{
|
|
IpAddressAddrType: IatIpv4,
|
|
IpAddressAddr: []byte{10, 99, 0, 8},
|
|
},
|
|
{
|
|
IpAddressAddrType: IatIpv4,
|
|
IpAddressAddr: []byte{10, 99, 0, 8},
|
|
},
|
|
},
|
|
SearchSuffixes: []string{"t2hack.internal"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
fmt.Println(hex.Dump(buf.Bytes()))
|
|
ioutil.WriteFile("/home/patrycja/newdata", buf.Bytes(), 0755)
|
|
|
|
}
|