This commit is contained in:
crusader 2017-12-02 01:14:22 +09:00
parent d720b5eed2
commit d938e396f9
5 changed files with 98 additions and 98 deletions

View File

@ -4,16 +4,14 @@ import (
"fmt" "fmt"
"path" "path"
"sync" "sync"
"time"
cc "git.loafle.net/commons_go/config" cc "git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
crr "git.loafle.net/commons_go/rpc/registry"
ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe" 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" oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client"
"git.loafle.net/overflow/overflow_probes/auth/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" "git.loafle.net/overflow/overflow_probes/config"
) )
@ -30,9 +28,7 @@ type auth struct {
configPath string configPath string
config ooccn.NoAuthProbeConfig config ooccn.NoAuthProbeConfig
tempProbeKeyChan chan string serviceDoneChan chan error
acceptChan chan *probeM.Probe
denyChan chan *noauthprobeM.NoAuthProbe
stopChan chan struct{} stopChan chan struct{}
stopWg sync.WaitGroup 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()) return fmt.Errorf("Cannot start because this probe have been denied from overFlow at %s", a.config.DenyDate.String())
} }
registerRequestHeader := "" a.serviceDoneChan = make(chan error, 1)
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.tempProbeKeyChan = make(chan string, 1) rpcRegistry := crr.NewRPCRegistry()
a.acceptChan = make(chan *probeM.Probe, 1)
a.denyChan = make(chan *noauthprobeM.NoAuthProbe, 1)
rpcInvoker := initRPCRegistry(a) napService := &oopar.NoAuthProbeService{
ch := client.NewClientHandler(rpcInvoker) DoneChan: a.serviceDoneChan,
sb := client.NewSocketBuilder(&a.config, a.tempProbeKeyChan, registerRequestHeader) ConfigPath: a.configPath,
Config: a.config,
}
rpcRegistry.RegisterService(napService, "")
ch := client.NewClientHandler(rpcRegistry)
sb := client.NewSocketBuilder(napService)
if nil == sb { if nil == sb {
return fmt.Errorf("Auth: Cannot create SocketBuilder") return fmt.Errorf("Auth: Cannot create SocketBuilder")
} }
a.cClient = client.NewClient(ch, sb) a.cClient = client.NewClient(ch, sb)
a.doneChan = doneChan a.doneChan = doneChan
@ -106,9 +98,7 @@ func (a *auth) destroy(err error) {
a.stopChan = nil a.stopChan = nil
a.cClient.Close() a.cClient.Close()
close(a.tempProbeKeyChan) close(a.serviceDoneChan)
close(a.acceptChan)
close(a.denyChan)
logging.Logger().Info(fmt.Sprintf("Auth: stopped")) logging.Logger().Info(fmt.Sprintf("Auth: stopped"))
a.doneChan <- err a.doneChan <- err
@ -127,47 +117,10 @@ func (a *auth) handleAuth() {
for { for {
select { select {
case tempProbeKey := <-a.tempProbeKeyChan: case err = <-a.serviceDoneChan:
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)
return return
case <-a.stopChan: case <-a.stopChan:
return 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
}

View File

@ -5,16 +5,14 @@ import (
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
cwfc "git.loafle.net/commons_go/websocket_fasthttp/client" 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" 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" 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{ sb := &SocketBuilders{
config: config, napService: napService,
tempProbeKeyChan: tempProbeKeyChan,
registerRequestHeader: registerRequestHeader,
} }
sb.SocketBuilders = oopcc.NewSocketBuilder(oocmn.HTTPEntry_NoAuthProbe) sb.SocketBuilders = oopcc.NewSocketBuilder(oocmn.HTTPEntry_NoAuthProbe)
if nil == sb.SocketBuilders { if nil == sb.SocketBuilders {
@ -27,35 +25,27 @@ func NewSocketBuilder(config *ooccn.NoAuthProbeConfig, tempProbeKeyChan chan str
type SocketBuilders struct { type SocketBuilders struct {
*oopcc.SocketBuilders *oopcc.SocketBuilders
config *ooccn.NoAuthProbeConfig napService *oopar.NoAuthProbeService
tempProbeKeyChan chan string
registerRequestHeader string
} }
func (sb *SocketBuilders) SocketHandler() cwfc.SocketHandler { func (sb *SocketBuilders) SocketHandler() cwfc.SocketHandler {
return newSocketHandler(sb.config, sb.tempProbeKeyChan) return newSocketHandler(sb.napService)
} }
func (sb *SocketBuilders) GetRequestHeader() http.Header { func (sb *SocketBuilders) GetRequestHeader() http.Header {
reqHeader := http.Header{} h := sb.napService.GetRequestHeader()
header := http.Header{}
switch sb.config.State() { for k, v := range h {
case ooccn.NoAuthProbeStateTypeNotRegisterd: header[k] = v
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}
} }
return reqHeader return header
} }
func (sb *SocketBuilders) Validate() { func (sb *SocketBuilders) Validate() {
sb.SocketBuilders.Validate() sb.SocketBuilders.Validate()
if nil == sb.config { if nil == sb.napService {
logging.Logger().Panic("Auth: NoAuthProbeConfig must be specified") logging.Logger().Panic("Auth: NoAuthProbeService must be specified")
} }
} }

View File

@ -8,22 +8,22 @@ import (
cwfc "git.loafle.net/commons_go/websocket_fasthttp/client" cwfc "git.loafle.net/commons_go/websocket_fasthttp/client"
ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe" ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe"
oocmn "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe" oocmn "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe"
oopar "git.loafle.net/overflow/overflow_probes/auth/rpc"
) )
type SocketHandlers struct { type SocketHandlers struct {
cwfc.SocketHandlers cwfc.SocketHandlers
config *ooccn.NoAuthProbeConfig napService *oopar.NoAuthProbeService
tempProbeKeyChan chan string
} }
func (sh *SocketHandlers) OnConnect(socketContext cwfc.SocketContext, res *http.Response) { func (sh *SocketHandlers) OnConnect(socketContext cwfc.SocketContext, res *http.Response) {
logging.Logger().Info(fmt.Sprintf("Auth: client has been connected res[%v]", res)) 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: case ooccn.NoAuthProbeStateTypeNotRegisterd:
tempProbeKey := res.Header.Get(oocmn.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey) tempProbeKey := res.Header.Get(oocmn.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
sh.tempProbeKeyChan <- tempProbeKey sh.napService.SetTempProbeKey(tempProbeKey)
case ooccn.NoAuthProbeStateTypeRegisterd: 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{ return &SocketHandlers{
config: config, napService: napService,
tempProbeKeyChan: tempProbeKeyChan,
} }
} }

View File

@ -9,8 +9,9 @@ func initRPCRegistry(a *auth) crr.RPCInvoker {
rpcRegistry := crr.NewRPCRegistry() rpcRegistry := crr.NewRPCRegistry()
napService := &oopar.NoAuthProbeService{ napService := &oopar.NoAuthProbeService{
AcceptChan: a.acceptChan, DoneChan: a.serviceDoneChan,
DenyChan: a.denyChan, ConfigPath: a.configPath,
Config: a.config,
} }
rpcRegistry.RegisterService(napService, "") rpcRegistry.RegisterService(napService, "")

View File

@ -1,20 +1,77 @@
package rpc package rpc
import ( 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" noauthprobeM "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe/model"
probeM "git.loafle.net/overflow/overflow_commons_go/modules/probe/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 { type NoAuthProbeService struct {
AcceptChan chan *probeM.Probe DoneChan chan error
DenyChan chan *noauthprobeM.NoAuthProbe
ConfigPath string
Config ooccn.NoAuthProbeConfig
} }
func (s *NoAuthProbeService) Accept(probe *probeM.Probe) { 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) { 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
}
}