overflow_probes/auth/info/info.go

123 lines
2.3 KiB
Go
Raw Normal View History

2017-12-01 13:01:46 +00:00
package info
2017-09-22 09:20:07 +00:00
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"net"
"git.loafle.net/commons_go/util/net/gateway"
2017-12-01 13:01:46 +00:00
noauthprobeM "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe/model"
2017-09-22 09:20:07 +00:00
"git.loafle.net/overflow/overflow_probes/config"
"github.com/shirou/gopsutil/host"
)
2017-12-01 13:01:46 +00:00
func GetRegistHeader() (string, error) {
2017-09-22 09:20:07 +00:00
var err error
2017-12-01 13:01:46 +00:00
nap := noauthprobeM.NoAuthProbe{
APIKey: config.Config.Central.APIKey,
2017-09-22 09:20:07 +00:00
}
2017-12-01 13:01:46 +00:00
var napd *noauthprobeM.NoAuthProbeDescription
if napd, err = getDescription(); nil != err {
2017-09-22 09:20:07 +00:00
return "", err
}
var buf []byte
2017-12-01 13:01:46 +00:00
if buf, err = json.Marshal(napd); nil != err {
2017-09-22 09:20:07 +00:00
return "", err
}
nap.Description = string(buf)
if buf, err = json.Marshal(nap); nil != err {
return "", err
}
enc := base64.StdEncoding.EncodeToString(buf)
return enc, nil
}
2017-12-01 13:01:46 +00:00
func getDescription() (*noauthprobeM.NoAuthProbeDescription, error) {
var err error
napd := &noauthprobeM.NoAuthProbeDescription{}
2017-09-22 09:20:07 +00:00
2017-12-01 13:01:46 +00:00
if napd.Host, err = getHost(); nil != err {
2017-09-22 09:20:07 +00:00
return nil, err
}
2017-12-01 13:01:46 +00:00
if napd.Network, err = getNetwork(); nil != err {
2017-09-22 09:20:07 +00:00
return nil, err
}
2017-12-01 13:01:46 +00:00
return napd, nil
2017-09-22 09:20:07 +00:00
}
2017-12-01 13:01:46 +00:00
func getHost() (*noauthprobeM.NoAuthProbeDescriptionHost, error) {
2017-09-22 09:20:07 +00:00
if i, err := host.Info(); nil == err {
2017-12-01 13:01:46 +00:00
h := &noauthprobeM.NoAuthProbeDescriptionHost{}
2017-09-22 09:20:07 +00:00
h.Name = i.Hostname
h.OS = i.OS
h.Platform = i.Platform
h.PlatformFamily = i.PlatformFamily
h.KernelVersion = i.KernelVersion
h.HostID = i.HostID
2017-12-01 13:01:46 +00:00
return h, nil
2017-09-22 09:20:07 +00:00
} else {
2017-12-01 13:01:46 +00:00
return nil, err
2017-09-22 09:20:07 +00:00
}
}
2017-12-01 13:01:46 +00:00
func getNetwork() (*noauthprobeM.NoAuthProbeDescriptionNetwork, error) {
2017-09-22 09:20:07 +00:00
var ip net.IP
var iface string
var err error
if ip, iface, err = gateway.DiscoverGateway(); nil != err {
2017-12-01 13:01:46 +00:00
return nil, err
2017-09-22 09:20:07 +00:00
}
interfaces, err := net.Interfaces()
if err != nil {
2017-12-01 13:01:46 +00:00
return nil, err
2017-09-22 09:20:07 +00:00
}
idx := -1
for _idx, i := range interfaces {
if i.Name == iface {
idx = _idx
break
}
}
if -1 == idx {
2017-12-01 13:01:46 +00:00
return nil, errors.New("Interface of gateway is not exist")
2017-09-22 09:20:07 +00:00
}
2017-12-01 13:01:46 +00:00
n := &noauthprobeM.NoAuthProbeDescriptionNetwork{}
2017-09-22 09:20:07 +00:00
i := interfaces[idx]
n.Name = i.Name
n.MacAddress = i.HardwareAddr.String()
n.Gateway = ip.String()
if addrs, err := i.Addrs(); nil == err {
var buffer bytes.Buffer
for _idx, a := range addrs {
if 0 < _idx {
buffer.WriteString("|")
}
buffer.WriteString(a.String())
}
n.Address = buffer.String()
} else {
2017-12-01 13:01:46 +00:00
return nil, err
2017-09-22 09:20:07 +00:00
}
2017-12-01 13:01:46 +00:00
return n, nil
2017-09-22 09:20:07 +00:00
}