diff --git a/auth/auth.go b/auth/auth.go index b00e4a4..07c35f5 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -4,16 +4,14 @@ import ( "fmt" "path" "sync" - "time" cc "git.loafle.net/commons_go/config" "git.loafle.net/commons_go/logging" + crr "git.loafle.net/commons_go/rpc/registry" ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe" - noauthprobeM "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe/model" - probeM "git.loafle.net/overflow/overflow_commons_go/modules/probe/model" oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client" "git.loafle.net/overflow/overflow_probes/auth/client" - oopai "git.loafle.net/overflow/overflow_probes/auth/info" + oopar "git.loafle.net/overflow/overflow_probes/auth/rpc" "git.loafle.net/overflow/overflow_probes/config" ) @@ -30,9 +28,7 @@ type auth struct { configPath string config ooccn.NoAuthProbeConfig - tempProbeKeyChan chan string - acceptChan chan *probeM.Probe - denyChan chan *noauthprobeM.NoAuthProbe + serviceDoneChan chan error stopChan chan struct{} stopWg sync.WaitGroup @@ -62,26 +58,22 @@ func (a *auth) EndableStart(doneChan chan<- error) error { return fmt.Errorf("Cannot start because this probe have been denied from overFlow at %s", a.config.DenyDate.String()) } - registerRequestHeader := "" - if ooccn.NoAuthProbeStateTypeNotRegisterd == a.config.State() { - i, err := oopai.GetRegistHeader() - if nil != err { - return fmt.Errorf("Auth: Gathering system information has been failed %v", err) - } - registerRequestHeader = i + a.serviceDoneChan = make(chan error, 1) + + rpcRegistry := crr.NewRPCRegistry() + + napService := &oopar.NoAuthProbeService{ + DoneChan: a.serviceDoneChan, + ConfigPath: a.configPath, + Config: a.config, } + rpcRegistry.RegisterService(napService, "") - a.tempProbeKeyChan = make(chan string, 1) - a.acceptChan = make(chan *probeM.Probe, 1) - a.denyChan = make(chan *noauthprobeM.NoAuthProbe, 1) - - rpcInvoker := initRPCRegistry(a) - ch := client.NewClientHandler(rpcInvoker) - sb := client.NewSocketBuilder(&a.config, a.tempProbeKeyChan, registerRequestHeader) + ch := client.NewClientHandler(rpcRegistry) + sb := client.NewSocketBuilder(napService) if nil == sb { return fmt.Errorf("Auth: Cannot create SocketBuilder") } - a.cClient = client.NewClient(ch, sb) a.doneChan = doneChan @@ -106,9 +98,7 @@ func (a *auth) destroy(err error) { a.stopChan = nil a.cClient.Close() - close(a.tempProbeKeyChan) - close(a.acceptChan) - close(a.denyChan) + close(a.serviceDoneChan) logging.Logger().Info(fmt.Sprintf("Auth: stopped")) a.doneChan <- err @@ -127,47 +117,10 @@ func (a *auth) handleAuth() { for { select { - case tempProbeKey := <-a.tempProbeKeyChan: - err = a.handleTempProbeKey(tempProbeKey) - if nil != err { - return - } - case p := <-a.acceptChan: - err = a.handleAccept(p) - return - case nap := <-a.denyChan: - err = a.handleDeny(nap) + case err = <-a.serviceDoneChan: return case <-a.stopChan: return } } } - -func (a *auth) handleTempProbeKey(tempProbeKey string) error { - a.config.TempKey = &tempProbeKey - if err := cc.Save(a.config, a.configPath, true); nil != err { - return err - } - - return nil -} - -func (a *auth) handleAccept(p *probeM.Probe) error { - config.Config.Probe.Key = &p.ProbeKey - - if err := cc.Save(*config.Config, *config.ConfigFilePath, true); nil != err { - return fmt.Errorf("Auth: Saving of config file[%s] failed error[%v]", *config.ConfigFilePath, err) - } - - return nil -} - -func (a *auth) handleDeny(nap *noauthprobeM.NoAuthProbe) error { - n := time.Now() - a.config.DenyDate = &n - if err := cc.Save(a.config, a.configPath, true); nil != err { - return fmt.Errorf("Auth: Saving of NoAuth config file[%s] failed error[%v]", a.configPath, err) - } - return nil -} diff --git a/auth/client/socket_builders.go b/auth/client/socket_builders.go index 0526b55..28beb84 100644 --- a/auth/client/socket_builders.go +++ b/auth/client/socket_builders.go @@ -5,16 +5,14 @@ import ( "git.loafle.net/commons_go/logging" cwfc "git.loafle.net/commons_go/websocket_fasthttp/client" - ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe" oocmn "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe" + oopar "git.loafle.net/overflow/overflow_probes/auth/rpc" oopcc "git.loafle.net/overflow/overflow_probes/central/client" ) -func NewSocketBuilder(config *ooccn.NoAuthProbeConfig, tempProbeKeyChan chan string, registerRequestHeader string) cwfc.SocketBuilder { +func NewSocketBuilder(napService *oopar.NoAuthProbeService) cwfc.SocketBuilder { sb := &SocketBuilders{ - config: config, - tempProbeKeyChan: tempProbeKeyChan, - registerRequestHeader: registerRequestHeader, + napService: napService, } sb.SocketBuilders = oopcc.NewSocketBuilder(oocmn.HTTPEntry_NoAuthProbe) if nil == sb.SocketBuilders { @@ -27,35 +25,27 @@ func NewSocketBuilder(config *ooccn.NoAuthProbeConfig, tempProbeKeyChan chan str type SocketBuilders struct { *oopcc.SocketBuilders - config *ooccn.NoAuthProbeConfig - tempProbeKeyChan chan string - registerRequestHeader string + napService *oopar.NoAuthProbeService } func (sb *SocketBuilders) SocketHandler() cwfc.SocketHandler { - return newSocketHandler(sb.config, sb.tempProbeKeyChan) + return newSocketHandler(sb.napService) } func (sb *SocketBuilders) GetRequestHeader() http.Header { - reqHeader := http.Header{} - - switch sb.config.State() { - case ooccn.NoAuthProbeStateTypeNotRegisterd: - reqHeader[oocmn.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{oocmn.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist} - reqHeader[oocmn.HTTPRequestHeaderKey_NoAuthProbe_Info] = []string{sb.registerRequestHeader} - - case ooccn.NoAuthProbeStateTypeRegisterd: - reqHeader[oocmn.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{oocmn.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect} - reqHeader[oocmn.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey] = []string{oocmn.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect} + h := sb.napService.GetRequestHeader() + header := http.Header{} + for k, v := range h { + header[k] = v } - return reqHeader + return header } func (sb *SocketBuilders) Validate() { sb.SocketBuilders.Validate() - if nil == sb.config { - logging.Logger().Panic("Auth: NoAuthProbeConfig must be specified") + if nil == sb.napService { + logging.Logger().Panic("Auth: NoAuthProbeService must be specified") } } diff --git a/auth/client/socket_handlers.go b/auth/client/socket_handlers.go index cc6d077..060a73f 100644 --- a/auth/client/socket_handlers.go +++ b/auth/client/socket_handlers.go @@ -8,22 +8,22 @@ import ( cwfc "git.loafle.net/commons_go/websocket_fasthttp/client" ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe" oocmn "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe" + oopar "git.loafle.net/overflow/overflow_probes/auth/rpc" ) type SocketHandlers struct { cwfc.SocketHandlers - config *ooccn.NoAuthProbeConfig - tempProbeKeyChan chan string + napService *oopar.NoAuthProbeService } func (sh *SocketHandlers) OnConnect(socketContext cwfc.SocketContext, res *http.Response) { logging.Logger().Info(fmt.Sprintf("Auth: client has been connected res[%v]", res)) - switch sh.config.State() { + switch sh.napService.Config.State() { case ooccn.NoAuthProbeStateTypeNotRegisterd: tempProbeKey := res.Header.Get(oocmn.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey) - sh.tempProbeKeyChan <- tempProbeKey + sh.napService.SetTempProbeKey(tempProbeKey) case ooccn.NoAuthProbeStateTypeRegisterd: } @@ -34,9 +34,8 @@ func (sh *SocketHandlers) OnDisconnect(soc cwfc.Socket) { } -func newSocketHandler(config *ooccn.NoAuthProbeConfig, tempProbeKeyChan chan string) cwfc.SocketHandler { +func newSocketHandler(napService *oopar.NoAuthProbeService) cwfc.SocketHandler { return &SocketHandlers{ - config: config, - tempProbeKeyChan: tempProbeKeyChan, + napService: napService, } } diff --git a/auth/rpc.go b/auth/rpc.go index 7eaf9ba..e0a83a2 100644 --- a/auth/rpc.go +++ b/auth/rpc.go @@ -9,8 +9,9 @@ func initRPCRegistry(a *auth) crr.RPCInvoker { rpcRegistry := crr.NewRPCRegistry() napService := &oopar.NoAuthProbeService{ - AcceptChan: a.acceptChan, - DenyChan: a.denyChan, + DoneChan: a.serviceDoneChan, + ConfigPath: a.configPath, + Config: a.config, } rpcRegistry.RegisterService(napService, "") diff --git a/auth/rpc/NoAuthProbeService.go b/auth/rpc/NoAuthProbeService.go index 684aa47..6fd283c 100644 --- a/auth/rpc/NoAuthProbeService.go +++ b/auth/rpc/NoAuthProbeService.go @@ -1,20 +1,77 @@ package rpc import ( + "fmt" + "time" + + "git.loafle.net/commons_go/logging" + + cc "git.loafle.net/commons_go/config" + ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe" + oocmn "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe" noauthprobeM "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe/model" probeM "git.loafle.net/overflow/overflow_commons_go/modules/probe/model" + oopai "git.loafle.net/overflow/overflow_probes/auth/info" + "git.loafle.net/overflow/overflow_probes/config" ) type NoAuthProbeService struct { - AcceptChan chan *probeM.Probe - DenyChan chan *noauthprobeM.NoAuthProbe + DoneChan chan error + + ConfigPath string + Config ooccn.NoAuthProbeConfig } func (s *NoAuthProbeService) Accept(probe *probeM.Probe) { - s.AcceptChan <- probe + config.Config.Probe.Key = &probe.ProbeKey + + if err := cc.Save(*config.Config, *config.ConfigFilePath, true); nil != err { + s.DoneChan <- fmt.Errorf("Auth: Saving of config file[%s] failed error[%v]", *config.ConfigFilePath, err) + return + } + + s.DoneChan <- nil } func (s *NoAuthProbeService) Deny(noAuthProbe *noauthprobeM.NoAuthProbe) { - s.DenyChan <- noAuthProbe + n := time.Now() + s.Config.DenyDate = &n + if err := cc.Save(s.Config, s.ConfigPath, true); nil != err { + s.DoneChan <- fmt.Errorf("Auth: Saving of NoAuth config file[%s] failed error[%v]", s.ConfigPath, err) + return + } + + s.DoneChan <- nil } +func (s *NoAuthProbeService) GetRequestHeader() map[string][]string { + header := make(map[string][]string) + switch s.Config.State() { + case ooccn.NoAuthProbeStateTypeNotRegisterd: + header[oocmn.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{oocmn.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist} + header[oocmn.HTTPRequestHeaderKey_NoAuthProbe_Info] = []string{s.GetRegisterRequestHeader()} + + case ooccn.NoAuthProbeStateTypeRegisterd: + header[oocmn.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{oocmn.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect} + header[oocmn.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey] = []string{oocmn.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect} + } + + return header +} + +func (s *NoAuthProbeService) GetRegisterRequestHeader() string { + i, err := oopai.GetRegistHeader() + if nil != err { + logging.Logger().Error(fmt.Sprintf("Auth: Gathering system information has been failed %v", err)) + } + return i +} + +func (s *NoAuthProbeService) SetTempProbeKey(tempProbeKey string) { + s.Config.TempKey = &tempProbeKey + if err := cc.Save(s.Config, s.ConfigPath, true); nil != err { + s.DoneChan <- fmt.Errorf("Auth: Saving of NoAuth config file[%s] failed error[%v]", s.ConfigPath, err) + return + } + +}