probe/auth/service/NoAuthProbeService.go

113 lines
2.8 KiB
Go
Raw Normal View History

2018-04-13 11:59:46 +00:00
package service
import (
2018-04-14 08:57:01 +00:00
"context"
"path"
2018-04-13 11:59:46 +00:00
"reflect"
2018-04-14 08:57:01 +00:00
"time"
2018-04-13 11:59:46 +00:00
2018-04-14 08:57:01 +00:00
"git.loafle.net/commons/configuration-go"
2018-04-13 11:59:46 +00:00
cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry"
2018-04-14 08:57:01 +00:00
logging "git.loafle.net/commons/logging-go"
2018-04-17 10:28:37 +00:00
crc "git.loafle.net/commons/rpc-go/client"
2018-04-14 08:57:01 +00:00
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
ocpc "git.loafle.net/overflow/commons-go/probe/config"
2018-04-17 10:28:37 +00:00
"git.loafle.net/overflow/probe/client/central"
2018-04-14 08:57:01 +00:00
"git.loafle.net/overflow/probe/config"
// For annotation
2018-04-13 11:59:46 +00:00
_ "git.loafle.net/overflow/commons-go/core/annotation"
)
var NoAuthProbeServiceType = reflect.TypeOf((*NoAuthProbeService)(nil))
func init() {
cdr.RegisterType(NoAuthProbeServiceType)
}
type NoAuthProbeService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
2018-04-14 08:57:01 +00:00
ConfigDir string `annotation:"@Resource(name='ConfigDir')"`
Config *config.Config `annotation:"@Resource(name='Config')"`
AuthConfig *ocnc.Auth `annotation:"@Resource(name='AuthConfig')"`
AuthDoneChan chan error `annotation:"@Resource(name='AuthDoneChan')"`
2018-04-17 10:28:37 +00:00
client *crc.Client
2018-04-14 08:57:01 +00:00
}
2018-04-17 10:28:37 +00:00
func (s *NoAuthProbeService) Init() error {
client, err := central.NewAuth(s.HandleTempKey, s)
if nil != err {
return err
}
s.client = client
2018-04-14 08:57:01 +00:00
return nil
}
2018-04-17 10:28:37 +00:00
func (s *NoAuthProbeService) Start() error {
if err := s.client.Start(); nil != err {
return err
}
2018-04-14 08:57:01 +00:00
return nil
2018-04-13 11:59:46 +00:00
}
2018-04-17 10:28:37 +00:00
func (s *NoAuthProbeService) Stop() {
if err := s.client.Stop(context.Background()); nil != err {
logging.Logger().Error(err)
}
}
func (s *NoAuthProbeService) Destroy() {
s.client = nil
}
2018-04-13 11:59:46 +00:00
func (s *NoAuthProbeService) Accept(probeKey string) error {
2018-04-14 08:57:01 +00:00
logging.Logger().Infof("accepted by central")
n := time.Now()
s.AuthConfig.AcceptedDate = &n
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err {
logging.Logger().Error(err)
s.AuthDoneChan <- err
return nil
}
s.Config.Probe.Key = &probeKey
if err := configuration.Save(s.Config, path.Join(s.ConfigDir, ocpc.ConfigFileName), true); nil != err {
logging.Logger().Error(err)
s.AuthDoneChan <- err
return nil
}
s.AuthDoneChan <- nil
2018-04-13 11:59:46 +00:00
return nil
}
func (s *NoAuthProbeService) Deny() error {
2018-04-14 08:57:01 +00:00
logging.Logger().Infof("denied by central")
n := time.Now()
s.AuthConfig.DeniedDate = &n
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err {
logging.Logger().Error(err)
s.AuthDoneChan <- err
return nil
}
s.AuthDoneChan <- nil
2018-04-13 11:59:46 +00:00
return nil
}
2018-04-17 10:28:37 +00:00
func (s *NoAuthProbeService) HandleTempKey(tempKey string) {
logging.Logger().Infof("registered by central")
s.AuthConfig.TempKey = &tempKey
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err {
logging.Logger().Error(err)
s.AuthDoneChan <- err
return
}
}