2017-09-21 08:38:05 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
2017-12-01 13:01:46 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-09-21 08:38:05 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
cc "git.loafle.net/commons_go/config"
|
2017-09-21 08:38:05 +00:00
|
|
|
"git.loafle.net/commons_go/logging"
|
2017-12-01 13:01:46 +00:00
|
|
|
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"
|
2017-09-21 08:38:05 +00:00
|
|
|
"git.loafle.net/overflow/overflow_probes/config"
|
|
|
|
)
|
|
|
|
|
2017-10-12 10:02:38 +00:00
|
|
|
type Auther interface {
|
2017-12-01 13:01:46 +00:00
|
|
|
EndableStart(doneChan chan<- error) error
|
|
|
|
Stop()
|
2017-10-12 10:02:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 12:30:27 +00:00
|
|
|
type auth struct {
|
2017-12-01 13:01:46 +00:00
|
|
|
doneChan chan<- error
|
|
|
|
|
|
|
|
cClient oogwc.Client
|
2017-09-22 09:20:07 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
configPath string
|
|
|
|
config ooccn.NoAuthProbeConfig
|
2017-09-21 08:38:05 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
tempProbeKeyChan chan string
|
|
|
|
acceptChan chan *probeM.Probe
|
|
|
|
denyChan chan *noauthprobeM.NoAuthProbe
|
2017-10-12 10:02:38 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
stopChan chan struct{}
|
|
|
|
stopWg sync.WaitGroup
|
2017-09-21 08:38:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
func New() Auther {
|
|
|
|
a := &auth{}
|
2017-09-21 08:38:05 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
return a
|
|
|
|
}
|
2017-09-21 08:38:05 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
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")
|
2017-09-22 09:20:07 +00:00
|
|
|
}
|
2017-09-21 08:38:05 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
a.configPath = path.Join(*config.ConfigDir, ooccn.ConfigFileName)
|
2017-09-21 08:38:05 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
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)
|
2017-09-21 08:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
if nil != a.config.DenyDate {
|
|
|
|
return fmt.Errorf("Cannot start because this probe have been denied from overFlow at %s", a.config.DenyDate.String())
|
|
|
|
}
|
2017-09-28 10:09:33 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
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
|
|
|
|
}
|
2017-09-22 09:20:07 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
a.tempProbeKeyChan = make(chan string, 1)
|
|
|
|
a.acceptChan = make(chan *probeM.Probe, 1)
|
|
|
|
a.denyChan = make(chan *noauthprobeM.NoAuthProbe, 1)
|
2017-10-12 10:02:38 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
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")
|
2017-10-12 10:02:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
a.cClient = client.NewClient(ch, sb)
|
2017-10-12 10:02:38 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
a.doneChan = doneChan
|
|
|
|
a.stopChan = make(chan struct{})
|
|
|
|
|
|
|
|
a.stopWg.Add(1)
|
|
|
|
go a.handleAuth()
|
2017-10-12 10:02:38 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
func (a *auth) Stop() {
|
|
|
|
a.destroy(nil)
|
2017-10-12 10:02:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
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() {
|
2017-09-22 09:20:07 +00:00
|
|
|
var err error
|
2017-12-01 13:01:46 +00:00
|
|
|
defer func() {
|
|
|
|
a.stopWg.Done()
|
|
|
|
a.destroy(err)
|
|
|
|
}()
|
2017-09-22 09:20:07 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
if err = a.cClient.Connect(); nil != err {
|
|
|
|
return
|
2017-09-22 09:20:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
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
|
|
|
|
}
|
2017-09-21 08:38:05 +00:00
|
|
|
}
|
2017-12-01 13:01:46 +00:00
|
|
|
}
|
2017-09-21 08:38:05 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
func (a *auth) handleTempProbeKey(tempProbeKey string) error {
|
|
|
|
a.config.TempKey = &tempProbeKey
|
|
|
|
if err := cc.Save(a.config, a.configPath, true); nil != err {
|
2017-09-22 09:20:07 +00:00
|
|
|
return err
|
2017-09-21 11:04:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 09:20:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
func (a *auth) handleAccept(p *probeM.Probe) error {
|
|
|
|
config.Config.Probe.Key = &p.ProbeKey
|
2017-09-21 11:04:30 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
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)
|
2017-09-21 08:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-12 10:02:38 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
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)
|
2017-10-12 10:02:38 +00:00
|
|
|
}
|
2017-12-01 13:01:46 +00:00
|
|
|
return nil
|
2017-10-12 10:02:38 +00:00
|
|
|
}
|