From 5a9359c3af3785471286e9a756c91af1691d50c4 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 21 Sep 2017 20:04:30 +0900 Subject: [PATCH] ing --- auth/auth.go | 70 +++++++++++++++++++++++++++++++++------- auth/host.go | 8 ++--- auth/network.go | 14 ++++---- central/client/client.go | 4 +-- commons/handler.go | 2 +- config.json | 2 +- config/noauth.go | 5 ++- config/probe.go | 5 +++ main.go | 12 +++++-- probe/probe.go | 7 ++-- 10 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 config/probe.go diff --git a/auth/auth.go b/auth/auth.go index aa0e50d..abc141b 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "path" + "time" lfcc "git.loafle.net/commons_go/config" "git.loafle.net/commons_go/logging" @@ -21,6 +22,7 @@ const ( noAuthHeaderNoAuthID = "overFlow-NoAuth-ID" noAuthHeaderNoAuthRegist = "overFlow-NoAuth-Regist" noAuthHeaderSetNoAuthID = "overFlow-Set-NoAuth-ID" + probeConfigFileName = "probe.json" ) type AuthHandler interface { @@ -33,16 +35,18 @@ type authHandlers struct { noAuthConfig config.NoAuthProbeConfig entryURL string c client.Client + probeConfigPath string + probeConfig config.ProbeConfig } type NoAuthProbe struct { - APIKey string `json:"apiKey"` - Description NoAuthDescription `json:"description"` + APIKey string `json:"apiKey"` + Description string `json:"description"` } type NoAuthDescription struct { - Host *Host `json:"host"` - Network *Network `json:"network"` + Host Host `json:"host"` + Network Network `json:"network"` } func New(configDir string) AuthHandler { @@ -68,6 +72,7 @@ func New(configDir string) AuthHandler { h.entryURL = centralURL.String() h.noAuthConfigPath = path.Join(configDir, noAuthConfigFileName) + h.probeConfigPath = path.Join(configDir, probeConfigFileName) conf := lfcc.New() if lfcc.Exists(h.noAuthConfigPath) { @@ -83,16 +88,19 @@ func New(configDir string) AuthHandler { return h } -func (h *authHandlers) Start() error { +func (h *authHandlers) Listen() error { var err error isRegist := true h.c = client.New() + h.c.OnNotify("NoAuthProbeService.accept", h.onNoAuthProbeAccept) + h.c.OnNotify("NoAuthProbeService.deny", h.onNoAuthProbeDeny) + header := http.Header{} - if "" != h.noAuthConfig.TempID { + if "" != h.noAuthConfig.TempKey { isRegist = false - header[noAuthHeaderNoAuthID] = []string{h.noAuthConfig.TempID} + header[noAuthHeaderNoAuthID] = []string{h.noAuthConfig.TempKey} } else { var enc string if enc, err = h.getRegistHeader(); nil != err { @@ -106,34 +114,72 @@ func (h *authHandlers) Start() error { } if isRegist { - noAuthID := res.Header.Get(noAuthHeaderSetNoAuthID) - logging.Logger.Debug(fmt.Sprintf("Auth: NoAuthID: %s", noAuthID)) + h.noAuthConfig.TempKey = res.Header.Get(noAuthHeaderSetNoAuthID) + if err = lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err { + return err + } + } + + for { + } 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) { var err error nap := NoAuthProbe{ APIKey: config.Config.Domain.APIKey, } nad := NoAuthDescription{} - nap.Description = nad - if nad.Host, err = getHost(); nil != err { + if err = getHost(&nad.Host); nil != err { return "", err } - if nad.Network, err = getNetwork(); nil != err { + if err = getNetwork(&nad.Network); nil != err { return "", err } 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 { return "", err } + logging.Logger.Debug(fmt.Sprintf("%s", string(buf))) + enc := base64.StdEncoding.EncodeToString(buf) return enc, nil diff --git a/auth/host.go b/auth/host.go index a49d3db..c1fc7c9 100644 --- a/auth/host.go +++ b/auth/host.go @@ -14,9 +14,7 @@ type Host struct { HostID string `json:"hostID"` } -func getHost() (*Host, error) { - h := &Host{} - +func getHost(h *Host) error { if i, err := host.Info(); nil == err { h.Name = i.Hostname h.OS = i.OS @@ -25,8 +23,8 @@ func getHost() (*Host, error) { h.KernelVersion = i.KernelVersion h.HostID = i.HostID } else { - return nil, err + return err } - return h, nil + return nil } diff --git a/auth/network.go b/auth/network.go index b1ee26a..a653ff2 100644 --- a/auth/network.go +++ b/auth/network.go @@ -2,6 +2,7 @@ package auth import ( "bytes" + "errors" "net" "git.loafle.net/commons_go/util/net/gateway" @@ -14,17 +15,17 @@ type Network struct { MacAddress string `json:"macAddress"` } -func getNetwork() (*Network, error) { +func getNetwork(n *Network) error { var ip net.IP var iface string var err error if ip, iface, err = gateway.DiscoverGateway(); nil != err { - return nil, err + return err } interfaces, err := net.Interfaces() if err != nil { - return nil, err + return err } idx := -1 @@ -37,10 +38,9 @@ func getNetwork() (*Network, error) { } if -1 == idx { - return nil, nil + return errors.New("Interface of gateway is not exist") } - n := &Network{} i := interfaces[idx] n.Name = i.Name @@ -57,8 +57,8 @@ func getNetwork() (*Network, error) { } n.Address = buffer.String() } else { - return nil, err + return err } - return n, nil + return nil } diff --git a/central/client/client.go b/central/client/client.go index b0dfc6c..b0b2a6d 100644 --- a/central/client/client.go +++ b/central/client/client.go @@ -19,7 +19,7 @@ const ( ) type ( - OnNotifyFunc func(params interface{}) + OnNotifyFunc func(method string, params interface{}) ) type ServerError string @@ -286,7 +286,7 @@ func (c *client) onNotification(noti protocol.Notification) error { var ok bool if hs, ok = c.onNotifyHandlers[noti.Method]; ok { for _, h := range hs { - h(noti.Params) + h(noti.Method, noti.Params) } } diff --git a/commons/handler.go b/commons/handler.go index a2f2458..c192efe 100644 --- a/commons/handler.go +++ b/commons/handler.go @@ -3,6 +3,6 @@ package commons import "context" type Handler interface { - Start() error + Listen() error Shutdown(ctx context.Context) error } diff --git a/config.json b/config.json index 4fb562f..4194558 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { "domain": { - "apikey": "asdfsafsdfsfadsakakdsfladsfgk" + "apikey": "52abd6fd57e511e7ac52080027658d13" }, "central": { "url": "ws://127.0.0.1:19190", diff --git a/config/noauth.go b/config/noauth.go index 2ba71b6..da21ec6 100644 --- a/config/noauth.go +++ b/config/noauth.go @@ -1,5 +1,8 @@ package config +import "time" + 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"` } diff --git a/config/probe.go b/config/probe.go new file mode 100644 index 0000000..763cc2b --- /dev/null +++ b/config/probe.go @@ -0,0 +1,5 @@ +package config + +type ProbeConfig struct { + ID string `json:"id" yaml:"id" toml:"id"` +} diff --git a/main.go b/main.go index ea010d8..fd6105d 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "git.loafle.net/overflow/overflow_probes/auth" "git.loafle.net/overflow/overflow_probes/commons" "git.loafle.net/overflow/overflow_probes/config" + "git.loafle.net/overflow/overflow_probes/probe" "github.com/takama/daemon" ) @@ -152,8 +153,15 @@ func main() { handler = auth.New(confDir) go func() { - if err := handler.Start(); err != nil { - logging.Logger.Error(fmt.Sprintf("Probe: cannot start authenticator error: %v", err)) + if err := handler.Listen(); err != nil { + 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 } }() diff --git a/probe/probe.go b/probe/probe.go index c1e04ab..cb7a882 100644 --- a/probe/probe.go +++ b/probe/probe.go @@ -2,6 +2,8 @@ package probe import ( "context" + + "git.loafle.net/overflow/overflow_probes/commons" ) func New(configDir string) Probe { @@ -13,15 +15,14 @@ func New(configDir string) Probe { } type Probe interface { - Start() error - Shutdown(ctx context.Context) error + commons.Handler } type probe struct { configDir string } -func (p *probe) Start() error { +func (p *probe) Listen() error { return nil }