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/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

View File

@@ -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
}

View File

@@ -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
}