ing
This commit is contained in:
63
probe/handler/auth/auth_handler.go
Normal file
63
probe/handler/auth/auth_handler.go
Normal 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) {
|
||||
|
||||
}
|
||||
32
probe/handler/auth/host.go
Normal file
32
probe/handler/auth/host.go
Normal 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
|
||||
}
|
||||
64
probe/handler/auth/network.go
Normal file
64
probe/handler/auth/network.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user