This commit is contained in:
crusader 2017-09-21 20:04:30 +09:00
parent 726b24f878
commit 5a9359c3af
10 changed files with 95 additions and 34 deletions

View File

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
"time"
lfcc "git.loafle.net/commons_go/config" lfcc "git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
@ -21,6 +22,7 @@ const (
noAuthHeaderNoAuthID = "overFlow-NoAuth-ID" noAuthHeaderNoAuthID = "overFlow-NoAuth-ID"
noAuthHeaderNoAuthRegist = "overFlow-NoAuth-Regist" noAuthHeaderNoAuthRegist = "overFlow-NoAuth-Regist"
noAuthHeaderSetNoAuthID = "overFlow-Set-NoAuth-ID" noAuthHeaderSetNoAuthID = "overFlow-Set-NoAuth-ID"
probeConfigFileName = "probe.json"
) )
type AuthHandler interface { type AuthHandler interface {
@ -33,16 +35,18 @@ type authHandlers struct {
noAuthConfig config.NoAuthProbeConfig noAuthConfig config.NoAuthProbeConfig
entryURL string entryURL string
c client.Client c client.Client
probeConfigPath string
probeConfig config.ProbeConfig
} }
type NoAuthProbe struct { type NoAuthProbe struct {
APIKey string `json:"apiKey"` APIKey string `json:"apiKey"`
Description NoAuthDescription `json:"description"` Description string `json:"description"`
} }
type NoAuthDescription struct { type NoAuthDescription struct {
Host *Host `json:"host"` Host Host `json:"host"`
Network *Network `json:"network"` Network Network `json:"network"`
} }
func New(configDir string) AuthHandler { func New(configDir string) AuthHandler {
@ -68,6 +72,7 @@ func New(configDir string) AuthHandler {
h.entryURL = centralURL.String() h.entryURL = centralURL.String()
h.noAuthConfigPath = path.Join(configDir, noAuthConfigFileName) h.noAuthConfigPath = path.Join(configDir, noAuthConfigFileName)
h.probeConfigPath = path.Join(configDir, probeConfigFileName)
conf := lfcc.New() conf := lfcc.New()
if lfcc.Exists(h.noAuthConfigPath) { if lfcc.Exists(h.noAuthConfigPath) {
@ -83,16 +88,19 @@ func New(configDir string) AuthHandler {
return h return h
} }
func (h *authHandlers) Start() error { func (h *authHandlers) Listen() error {
var err error var err error
isRegist := true isRegist := true
h.c = client.New() h.c = client.New()
h.c.OnNotify("NoAuthProbeService.accept", h.onNoAuthProbeAccept)
h.c.OnNotify("NoAuthProbeService.deny", h.onNoAuthProbeDeny)
header := http.Header{} header := http.Header{}
if "" != h.noAuthConfig.TempID { if "" != h.noAuthConfig.TempKey {
isRegist = false isRegist = false
header[noAuthHeaderNoAuthID] = []string{h.noAuthConfig.TempID} header[noAuthHeaderNoAuthID] = []string{h.noAuthConfig.TempKey}
} else { } else {
var enc string var enc string
if enc, err = h.getRegistHeader(); nil != err { if enc, err = h.getRegistHeader(); nil != err {
@ -106,34 +114,72 @@ func (h *authHandlers) Start() error {
} }
if isRegist { if isRegist {
noAuthID := res.Header.Get(noAuthHeaderSetNoAuthID) h.noAuthConfig.TempKey = res.Header.Get(noAuthHeaderSetNoAuthID)
logging.Logger.Debug(fmt.Sprintf("Auth: NoAuthID: %s", noAuthID)) if err = lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
return err
}
}
for {
} }
return nil return nil
} }
func (h *authHandlers) onNoAuthProbeAccept(method string, params interface{}) {
var err error
ps := params.([]string)
probeID := ps[0]
if lfcc.Exists(h.probeConfigPath) {
if err = lfcc.Load(&h.probeConfig, h.probeConfigPath); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Loading of Probe config file[%s] failed error[%v]", h.probeConfigPath, err))
}
}
h.probeConfig.ID = probeID
if err = lfcc.Save(h.probeConfig, h.probeConfigPath, true); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Saving of Probe config file[%s] failed error[%v]", h.probeConfigPath, err))
}
}
func (h *authHandlers) onNoAuthProbeDeny(method string, params interface{}) {
h.noAuthConfig.DenyDate = time.Now()
if err := lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Saving of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err))
}
}
func (h *authHandlers) getRegistHeader() (string, error) { func (h *authHandlers) getRegistHeader() (string, error) {
var err error var err error
nap := NoAuthProbe{ nap := NoAuthProbe{
APIKey: config.Config.Domain.APIKey, APIKey: config.Config.Domain.APIKey,
} }
nad := NoAuthDescription{} nad := NoAuthDescription{}
nap.Description = nad
if nad.Host, err = getHost(); nil != err { if err = getHost(&nad.Host); nil != err {
return "", err return "", err
} }
if nad.Network, err = getNetwork(); nil != err { if err = getNetwork(&nad.Network); nil != err {
return "", err return "", err
} }
var buf []byte var buf []byte
if buf, err = json.Marshal(nad); nil != err {
return "", err
}
nap.Description = string(buf)
if buf, err = json.Marshal(nap); nil != err { if buf, err = json.Marshal(nap); nil != err {
return "", err return "", err
} }
logging.Logger.Debug(fmt.Sprintf("%s", string(buf)))
enc := base64.StdEncoding.EncodeToString(buf) enc := base64.StdEncoding.EncodeToString(buf)
return enc, nil return enc, nil

View File

@ -14,9 +14,7 @@ type Host struct {
HostID string `json:"hostID"` HostID string `json:"hostID"`
} }
func getHost() (*Host, error) { func getHost(h *Host) error {
h := &Host{}
if i, err := host.Info(); nil == err { if i, err := host.Info(); nil == err {
h.Name = i.Hostname h.Name = i.Hostname
h.OS = i.OS h.OS = i.OS
@ -25,8 +23,8 @@ func getHost() (*Host, error) {
h.KernelVersion = i.KernelVersion h.KernelVersion = i.KernelVersion
h.HostID = i.HostID h.HostID = i.HostID
} else { } else {
return nil, err return err
} }
return h, nil return nil
} }

View File

@ -2,6 +2,7 @@ package auth
import ( import (
"bytes" "bytes"
"errors"
"net" "net"
"git.loafle.net/commons_go/util/net/gateway" "git.loafle.net/commons_go/util/net/gateway"
@ -14,17 +15,17 @@ type Network struct {
MacAddress string `json:"macAddress"` MacAddress string `json:"macAddress"`
} }
func getNetwork() (*Network, error) { func getNetwork(n *Network) error {
var ip net.IP var ip net.IP
var iface string var iface string
var err error var err error
if ip, iface, err = gateway.DiscoverGateway(); nil != err { if ip, iface, err = gateway.DiscoverGateway(); nil != err {
return nil, err return err
} }
interfaces, err := net.Interfaces() interfaces, err := net.Interfaces()
if err != nil { if err != nil {
return nil, err return err
} }
idx := -1 idx := -1
@ -37,10 +38,9 @@ func getNetwork() (*Network, error) {
} }
if -1 == idx { if -1 == idx {
return nil, nil return errors.New("Interface of gateway is not exist")
} }
n := &Network{}
i := interfaces[idx] i := interfaces[idx]
n.Name = i.Name n.Name = i.Name
@ -57,8 +57,8 @@ func getNetwork() (*Network, error) {
} }
n.Address = buffer.String() n.Address = buffer.String()
} else { } else {
return nil, err return err
} }
return n, nil return nil
} }

View File

@ -19,7 +19,7 @@ const (
) )
type ( type (
OnNotifyFunc func(params interface{}) OnNotifyFunc func(method string, params interface{})
) )
type ServerError string type ServerError string
@ -286,7 +286,7 @@ func (c *client) onNotification(noti protocol.Notification) error {
var ok bool var ok bool
if hs, ok = c.onNotifyHandlers[noti.Method]; ok { if hs, ok = c.onNotifyHandlers[noti.Method]; ok {
for _, h := range hs { for _, h := range hs {
h(noti.Params) h(noti.Method, noti.Params)
} }
} }

View File

@ -3,6 +3,6 @@ package commons
import "context" import "context"
type Handler interface { type Handler interface {
Start() error Listen() error
Shutdown(ctx context.Context) error Shutdown(ctx context.Context) error
} }

View File

@ -1,6 +1,6 @@
{ {
"domain": { "domain": {
"apikey": "asdfsafsdfsfadsakakdsfladsfgk" "apikey": "52abd6fd57e511e7ac52080027658d13"
}, },
"central": { "central": {
"url": "ws://127.0.0.1:19190", "url": "ws://127.0.0.1:19190",

View File

@ -1,5 +1,8 @@
package config package config
import "time"
type NoAuthProbeConfig struct { type NoAuthProbeConfig struct {
TempID string `json:"tempID" yaml:"tempID" toml:"tempID"` TempKey string `json:"tempKey" yaml:"tempKey" toml:"tempKey"`
DenyDate time.Time `json:"denyDate" yaml:"denyDate" toml:"denyDate"`
} }

5
config/probe.go Normal file
View File

@ -0,0 +1,5 @@
package config
type ProbeConfig struct {
ID string `json:"id" yaml:"id" toml:"id"`
}

12
main.go
View File

@ -14,6 +14,7 @@ import (
"git.loafle.net/overflow/overflow_probes/auth" "git.loafle.net/overflow/overflow_probes/auth"
"git.loafle.net/overflow/overflow_probes/commons" "git.loafle.net/overflow/overflow_probes/commons"
"git.loafle.net/overflow/overflow_probes/config" "git.loafle.net/overflow/overflow_probes/config"
"git.loafle.net/overflow/overflow_probes/probe"
"github.com/takama/daemon" "github.com/takama/daemon"
) )
@ -152,8 +153,15 @@ func main() {
handler = auth.New(confDir) handler = auth.New(confDir)
go func() { go func() {
if err := handler.Start(); err != nil { if err := handler.Listen(); err != nil {
logging.Logger.Error(fmt.Sprintf("Probe: cannot start authenticator error: %v", err)) logging.Logger.Error(fmt.Sprintf("Probe: Authenticator error: %v", err))
return
}
handler = probe.New(confDir)
if err := handler.Listen(); err != nil {
logging.Logger.Error(fmt.Sprintf("Probe: error: %v", err))
return
} }
}() }()

View File

@ -2,6 +2,8 @@ package probe
import ( import (
"context" "context"
"git.loafle.net/overflow/overflow_probes/commons"
) )
func New(configDir string) Probe { func New(configDir string) Probe {
@ -13,15 +15,14 @@ func New(configDir string) Probe {
} }
type Probe interface { type Probe interface {
Start() error commons.Handler
Shutdown(ctx context.Context) error
} }
type probe struct { type probe struct {
configDir string configDir string
} }
func (p *probe) Start() error { func (p *probe) Listen() error {
return nil return nil
} }