ing
This commit is contained in:
		
							parent
							
								
									726b24f878
								
							
						
					
					
						commit
						5a9359c3af
					
				
							
								
								
									
										70
									
								
								auth/auth.go
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								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
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,6 @@ package commons
 | 
			
		||||
import "context"
 | 
			
		||||
 | 
			
		||||
type Handler interface {
 | 
			
		||||
	Start() error
 | 
			
		||||
	Listen() error
 | 
			
		||||
	Shutdown(ctx context.Context) error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
  "domain": {
 | 
			
		||||
    "apikey": "asdfsafsdfsfadsakakdsfladsfgk"
 | 
			
		||||
    "apikey": "52abd6fd57e511e7ac52080027658d13"
 | 
			
		||||
  },
 | 
			
		||||
  "central": {
 | 
			
		||||
    "url": "ws://127.0.0.1:19190",
 | 
			
		||||
 | 
			
		||||
@ -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"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										5
									
								
								config/probe.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								config/probe.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
package config
 | 
			
		||||
 | 
			
		||||
type ProbeConfig struct {
 | 
			
		||||
	ID string `json:"id" yaml:"id" toml:"id"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								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
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user