This commit is contained in:
crusader
2017-09-18 18:21:58 +09:00
commit b1adb63ea8
15 changed files with 597 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
package auth
import (
"context"
"encoding/json"
"fmt"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_probes/probe/handler"
"github.com/gorilla/websocket"
)
type AuthHandler interface {
handler.Handler
}
type authHandlers struct {
c *websocket.Conn
apiKey *string
tempKey *string
}
type NoAuthProbe struct {
Host *Host `json:"host"`
Network *Network `json:"network"`
}
func NewHandler() AuthHandler {
h := &authHandlers{}
return h
}
func (h *authHandlers) Start() {
// c, _, err := websocket.DefaultDialer.Dial("ws://192.168.1.50:19190/auth", nil)
// if err != nil {
// logging.Logger.Fatal(fmt.Sprintf("auth: %v", err))
// }
// h.c = c
p := &NoAuthProbe{}
if h, err := getHost(); nil == err {
p.Host = h
}
if n, err := getNetwork(); nil == err {
p.Network = n
}
if buf, err := json.Marshal(*p); nil == err {
logging.Logger.Debug(fmt.Sprintf("p: %s", string(buf)))
}
}
func (h *authHandlers) listen() {
// 1. regist
// 2. wait for accept auth
}
func (h *authHandlers) Shutdown(ctx context.Context) {
}

View File

@@ -0,0 +1,32 @@
package auth
import (
"github.com/shirou/gopsutil/host"
)
type Host struct {
Name string `json:"name"`
OS string `json:"os"`
Platform string `json:"paltform"`
PlatformFamily string `json:"platformFamily"`
PlatformVersion string `json:"platformVersion"`
KernelVersion string `json:"kernelVersion"`
HostID string `json:"hostID"`
}
func getHost() (*Host, error) {
h := &Host{}
if i, err := host.Info(); nil == err {
h.Name = i.Hostname
h.OS = i.OS
h.Platform = i.Platform
h.PlatformFamily = i.PlatformFamily
h.KernelVersion = i.KernelVersion
h.HostID = i.HostID
} else {
return nil, err
}
return h, nil
}

View File

@@ -0,0 +1,64 @@
package auth
import (
"bytes"
"net"
"git.loafle.net/commons_go/util/net/gateway"
)
type Network struct {
Name string `json:"name"`
Address string `json:"address"`
Gateway string `json:"gateway"`
MacAddress string `json:"macAddress"`
}
func getNetwork() (*Network, error) {
var ip net.IP
var iface string
var err error
if ip, iface, err = gateway.DiscoverGateway(); nil != err {
return nil, err
}
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
idx := -1
for _idx, i := range interfaces {
if i.Name == iface {
idx = _idx
break
}
}
if -1 == idx {
return nil, nil
}
n := &Network{}
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 {
return nil, err
}
return n, nil
}