probe/auth/service/NoAuthProbeService.go

119 lines
2.9 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"
2018-04-13 11:59:46 +00:00
"reflect"
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-26 08:39:32 +00:00
occn "git.loafle.net/overflow/commons-go/config/noauthprobe"
"git.loafle.net/overflow/commons-go/core/util"
2018-05-03 11:24:07 +00:00
"git.loafle.net/overflow/probe/client/auth"
2018-04-14 08:57:01 +00:00
"git.loafle.net/overflow/probe/config"
// For annotation
2018-04-17 14:11:13 +00:00
_ "git.loafle.net/overflow/probe/auth/annotation"
2018-04-13 11:59:46 +00:00
)
var NoAuthProbeServiceType = reflect.TypeOf((*NoAuthProbeService)(nil))
func init() {
cdr.RegisterType(NoAuthProbeServiceType)
}
type NoAuthProbeService struct {
2018-04-17 14:11:13 +00:00
cda.TypeAnnotation `annotation:"@overflow:AuthRPCService()"`
2018-04-13 11:59:46 +00:00
2018-04-14 08:57:01 +00:00
Config *config.Config `annotation:"@Resource(name='Config')"`
2018-04-26 08:39:32 +00:00
AuthConfig *occn.Auth `annotation:"@Resource(name='AuthConfig')"`
2018-04-14 08:57:01 +00:00
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 14:11:13 +00:00
func (s *NoAuthProbeService) InitService() error {
2018-05-10 10:18:00 +00:00
client, err := auth.New(s.HandleResponse, s)
2018-04-17 10:28:37 +00:00
if nil != err {
return err
}
s.client = client
2018-04-14 08:57:01 +00:00
return nil
}
2018-04-17 14:11:13 +00:00
func (s *NoAuthProbeService) StartService() error {
2018-04-17 10:28:37 +00:00
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 14:11:13 +00:00
func (s *NoAuthProbeService) StopService() {
2018-04-17 10:28:37 +00:00
if err := s.client.Stop(context.Background()); nil != err {
logging.Logger().Error(err)
}
}
2018-04-17 14:11:13 +00:00
func (s *NoAuthProbeService) DestroyService() {
2018-04-17 10:28:37 +00:00
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")
2018-04-26 08:39:32 +00:00
n := util.Now()
2018-04-14 08:57:01 +00:00
s.AuthConfig.AcceptedDate = &n
2018-04-18 14:56:13 +00:00
if err := configuration.Save(s.AuthConfig, config.NoAuthProbeConfigFilePath(), true); nil != err {
2018-04-14 08:57:01 +00:00
logging.Logger().Error(err)
s.AuthDoneChan <- err
return nil
}
s.Config.Probe.Key = &probeKey
2018-04-18 14:56:13 +00:00
if err := configuration.Save(s.Config, config.ProbeConfigFilePath(), true); nil != err {
2018-04-14 08:57:01 +00:00
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")
2018-04-26 08:39:32 +00:00
n := util.Now()
2018-04-14 08:57:01 +00:00
s.AuthConfig.DeniedDate = &n
2018-04-18 14:56:13 +00:00
if err := configuration.Save(s.AuthConfig, config.NoAuthProbeConfigFilePath(), true); nil != err {
2018-04-14 08:57:01 +00:00
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
2018-05-10 10:18:00 +00:00
func (s *NoAuthProbeService) HandleResponse(method string, param string) {
switch method {
case occn.HTTPResponseHeaderValue_NoAuthProbe_Method_TempProbeKey:
logging.Logger().Infof("registered by central")
s.AuthConfig.TempKey = &param
if err := configuration.Save(s.AuthConfig, config.NoAuthProbeConfigFilePath(), true); nil != err {
logging.Logger().Error(err)
s.AuthDoneChan <- err
return
}
case occn.HTTPResponseHeaderValue_NoAuthProbe_Method_Accept:
s.Accept(param)
case occn.HTTPResponseHeaderValue_NoAuthProbe_Method_Deny:
s.Deny()
2018-04-17 10:28:37 +00:00
}
2018-05-10 10:18:00 +00:00
2018-04-17 10:28:37 +00:00
}