overflow_probes/auth/auth.go

127 lines
2.6 KiB
Go
Raw Normal View History

2017-09-21 08:38:05 +00:00
package auth
import (
"fmt"
"path"
2017-12-01 13:01:46 +00:00
"sync"
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 16:14:22 +00:00
crr "git.loafle.net/commons_go/rpc/registry"
2017-12-01 13:01:46 +00:00
ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe"
oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client"
"git.loafle.net/overflow/overflow_probes/auth/client"
2017-12-02 03:13:08 +00:00
oopas "git.loafle.net/overflow/overflow_probes/auth/service"
2017-09-21 08:38:05 +00:00
"git.loafle.net/overflow/overflow_probes/config"
)
2017-12-02 03:13:08 +00:00
func New() AuthManager {
am := &authManagers{}
return am
}
type AuthManager interface {
2017-12-01 13:01:46 +00:00
EndableStart(doneChan chan<- error) error
Stop()
2017-10-12 10:02:38 +00:00
}
2017-12-02 03:13:08 +00:00
type authManagers 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 16:14:22 +00:00
serviceDoneChan chan error
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-02 03:13:08 +00:00
func (am *authManagers) EndableStart(doneChan chan<- error) error {
if nil != am.stopChan {
2017-12-01 13:01:46 +00:00
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-02 03:13:08 +00:00
am.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()
2017-12-02 03:13:08 +00:00
if cc.Exists(am.configPath) {
if err := conf.Load(&am.config, am.configPath); nil != err {
return fmt.Errorf("Auth: Loading of NoAuth config file[%s] failed error[%v]", am.configPath, err)
2017-09-21 08:38:05 +00:00
}
}
2017-12-02 03:13:08 +00:00
if nil != am.config.DenyDate {
return fmt.Errorf("Cannot start because this probe have been denied from overFlow at %s", am.config.DenyDate.String())
2017-12-01 13:01:46 +00:00
}
2017-09-28 10:09:33 +00:00
2017-12-02 03:13:08 +00:00
am.serviceDoneChan = make(chan error, 1)
2017-12-01 16:14:22 +00:00
rpcRegistry := crr.NewRPCRegistry()
2017-09-22 09:20:07 +00:00
2017-12-02 03:13:08 +00:00
napService := &oopas.NoAuthProbeService{
DoneChan: am.serviceDoneChan,
ConfigPath: am.configPath,
Config: am.config,
2017-12-01 16:14:22 +00:00
}
rpcRegistry.RegisterService(napService, "")
2017-10-12 10:02:38 +00:00
2017-12-01 16:14:22 +00:00
ch := client.NewClientHandler(rpcRegistry)
sb := client.NewSocketBuilder(napService)
2017-12-01 13:01:46 +00:00
if nil == sb {
return fmt.Errorf("Auth: Cannot create SocketBuilder")
2017-10-12 10:02:38 +00:00
}
2017-12-02 03:13:08 +00:00
am.cClient = client.NewClient(ch, sb)
2017-10-12 10:02:38 +00:00
2017-12-02 03:13:08 +00:00
am.doneChan = doneChan
am.stopChan = make(chan struct{})
2017-12-01 13:01:46 +00:00
2017-12-02 03:13:08 +00:00
am.stopWg.Add(1)
go am.handleAuth()
2017-10-12 10:02:38 +00:00
return nil
}
2017-12-02 03:13:08 +00:00
func (am *authManagers) Stop() {
am.destroy(nil)
2017-10-12 10:02:38 +00:00
}
2017-12-02 03:13:08 +00:00
func (am *authManagers) destroy(err error) {
if am.stopChan == nil {
2017-12-01 13:20:01 +00:00
logging.Logger().Warn("Auth: auth must be started before stopping it")
2017-12-01 13:01:46 +00:00
}
2017-12-02 03:13:08 +00:00
close(am.stopChan)
am.stopWg.Wait()
am.stopChan = nil
2017-12-01 13:01:46 +00:00
2017-12-02 03:13:08 +00:00
am.cClient.Close()
close(am.serviceDoneChan)
2017-12-01 13:01:46 +00:00
logging.Logger().Info(fmt.Sprintf("Auth: stopped"))
2017-12-02 03:13:08 +00:00
am.doneChan <- err
2017-12-01 13:01:46 +00:00
}
2017-12-02 03:13:08 +00:00
func (am *authManagers) handleAuth() {
2017-09-22 09:20:07 +00:00
var err error
2017-12-01 13:01:46 +00:00
defer func() {
2017-12-02 03:13:08 +00:00
am.stopWg.Done()
am.destroy(err)
2017-12-01 13:01:46 +00:00
}()
2017-09-22 09:20:07 +00:00
2017-12-02 03:13:08 +00:00
if err = am.cClient.Connect(); nil != err {
2017-12-01 13:01:46 +00:00
return
2017-09-22 09:20:07 +00:00
}
2017-12-01 13:01:46 +00:00
for {
select {
2017-12-02 03:13:08 +00:00
case err = <-am.serviceDoneChan:
2017-12-01 13:01:46 +00:00
return
2017-12-02 03:13:08 +00:00
case <-am.stopChan:
2017-12-01 13:01:46 +00:00
return
}
2017-09-21 08:38:05 +00:00
}
2017-12-01 13:01:46 +00:00
}