overflow_probes/auth/auth.go
crusader d789464eeb ing
2017-12-01 22:01:46 +09:00

174 lines
4.0 KiB
Go

package auth
import (
"fmt"
"path"
"sync"
"time"
cc "git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
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"
"git.loafle.net/overflow/overflow_probes/config"
)
type Auther interface {
EndableStart(doneChan chan<- error) error
Stop()
}
type auth struct {
doneChan chan<- error
cClient oogwc.Client
configPath string
config ooccn.NoAuthProbeConfig
tempProbeKeyChan chan string
acceptChan chan *probeM.Probe
denyChan chan *noauthprobeM.NoAuthProbe
stopChan chan struct{}
stopWg sync.WaitGroup
}
func New() Auther {
a := &auth{}
return a
}
func (a *auth) EndableStart(doneChan chan<- error) error {
if nil != a.stopChan {
logging.Logger().Panic("Auth: auth is already running. Stop it before starting it again")
}
a.configPath = path.Join(*config.ConfigDir, ooccn.ConfigFileName)
conf := cc.New()
if cc.Exists(a.configPath) {
if err := conf.Load(&a.config, a.configPath); nil != err {
return fmt.Errorf("Auth: Loading of NoAuth config file[%s] failed error[%v]", a.configPath, err)
}
}
if nil != a.config.DenyDate {
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.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)
if nil == sb {
return fmt.Errorf("Auth: Cannot create SocketBuilder")
}
a.cClient = client.NewClient(ch, sb)
a.doneChan = doneChan
a.stopChan = make(chan struct{})
a.stopWg.Add(1)
go a.handleAuth()
return nil
}
func (a *auth) Stop() {
a.destroy(nil)
}
func (a *auth) destroy(err error) {
if a.stopChan == nil {
logging.Logger().Panic("Auth: auth must be started before stopping it")
}
close(a.stopChan)
a.stopWg.Wait()
a.stopChan = nil
a.cClient.Close()
close(a.tempProbeKeyChan)
close(a.acceptChan)
close(a.denyChan)
logging.Logger().Info(fmt.Sprintf("Auth: stopped"))
a.doneChan <- err
}
func (a *auth) handleAuth() {
var err error
defer func() {
a.stopWg.Done()
a.destroy(err)
}()
if err = a.cClient.Connect(); nil != err {
return
}
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)
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
}