refactoring
This commit is contained in:
parent
6e88452b1b
commit
0ece619ffb
24
auth/auth.go
24
auth/auth.go
@ -26,53 +26,47 @@ type AuthHandler interface {
|
||||
}
|
||||
|
||||
type authHandlers struct {
|
||||
c client.Client
|
||||
entryURL string
|
||||
configDir string
|
||||
c client.Client
|
||||
entryURL string
|
||||
|
||||
noAuthConfigPath string
|
||||
noAuthConfig config.NoAuthProbeConfig
|
||||
|
||||
probeConfigPath string
|
||||
probeConfig config.ProbeConfig
|
||||
|
||||
shutdownChan chan bool
|
||||
acceptedChan chan bool
|
||||
deniedChan chan error
|
||||
}
|
||||
|
||||
func New(configDir string) (AuthHandler, error) {
|
||||
func New() (AuthHandler, error) {
|
||||
var err error
|
||||
|
||||
h := &authHandlers{
|
||||
configDir: configDir,
|
||||
shutdownChan: make(chan bool),
|
||||
acceptedChan: make(chan bool),
|
||||
deniedChan: make(chan error),
|
||||
}
|
||||
|
||||
if h.entryURL, err = opuu.Join(config.Config.Central.URL, noAuthEntryPoint); nil != err {
|
||||
if h.entryURL, err = opuu.Join(*config.CFG.Central.URL, noAuthEntryPoint); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
h.noAuthConfigPath = path.Join(configDir, config.NoAuthProbeConfigFileName)
|
||||
h.probeConfigPath = path.Join(configDir, config.ProbeConfigFileName)
|
||||
h.noAuthConfigPath = path.Join(*config.ConfigDir, config.NoAuthProbeConfigFileName)
|
||||
|
||||
conf := lfcc.New()
|
||||
if lfcc.Exists(h.noAuthConfigPath) {
|
||||
if err = conf.Load(&h.noAuthConfig, h.noAuthConfigPath); nil != err {
|
||||
return nil, fmt.Errorf("Auth: Loading of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err)
|
||||
}
|
||||
} else {
|
||||
if err = lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
|
||||
return nil, fmt.Errorf("Auth: Saving of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func (h *authHandlers) Serve() error {
|
||||
if nil != config.CFG.Probe.Key || "" != *config.CFG.Probe.Key {
|
||||
return nil
|
||||
}
|
||||
|
||||
if nil != h.noAuthConfig.DenyDate {
|
||||
return fmt.Errorf("Cannot start because this probe have been denied from overFlow[%s]", h.noAuthConfig.DenyDate.String())
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
lfcc "git.loafle.net/commons_go/config"
|
||||
"git.loafle.net/commons_go/logging"
|
||||
"git.loafle.net/overflow/overflow_probes/central/api/module"
|
||||
"git.loafle.net/overflow/overflow_probes/config"
|
||||
)
|
||||
|
||||
func (h *authHandlers) onNotify(method string, params []string) {
|
||||
@ -23,17 +24,20 @@ func (h *authHandlers) onNotify(method string, params []string) {
|
||||
|
||||
func (h *authHandlers) onNoAuthProbeAccept(params []string) {
|
||||
var err error
|
||||
probeID := params[0]
|
||||
probeKey := params[0]
|
||||
|
||||
if lfcc.Exists(h.probeConfigPath) {
|
||||
if err = lfcc.Load(&h.probeConfig, h.probeConfigPath); nil != err {
|
||||
logging.Logger.Error(fmt.Sprintf("Auth: Loading of Probe config file[%s] failed error[%v]", h.probeConfigPath, err))
|
||||
}
|
||||
}
|
||||
// if lfcc.Exists(h.probeConfigPath) {
|
||||
// if err = lfcc.Load(&h.probeConfig, h.probeConfigPath); nil != err {
|
||||
// logging.Logger.Error(fmt.Sprintf("Auth: Loading of Probe config file[%s] failed error[%v]", h.probeConfigPath, err))
|
||||
// }
|
||||
// }
|
||||
|
||||
h.probeConfig.ID = &probeID
|
||||
if err = lfcc.Save(h.probeConfig, h.probeConfigPath, true); nil != err {
|
||||
logging.Logger.Error(fmt.Sprintf("Auth: Saving of Probe config file[%s] failed error[%v]", h.probeConfigPath, err))
|
||||
config.CFG.Probe.Key = &probeKey
|
||||
|
||||
if err = lfcc.Save(*config.CFG, *config.ConfigFilePath, true); nil != err {
|
||||
logging.Logger.Error(fmt.Sprintf("Auth: Saving of config file[%s] failed error[%v]", *config.ConfigFilePath, err))
|
||||
h.shutdownChan <- true
|
||||
return
|
||||
}
|
||||
|
||||
h.acceptedChan <- true
|
||||
@ -44,6 +48,8 @@ func (h *authHandlers) onNoAuthProbeDeny(params []string) {
|
||||
h.noAuthConfig.DenyDate = &n
|
||||
if err := lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
|
||||
logging.Logger.Error(fmt.Sprintf("Auth: Saving of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err))
|
||||
h.shutdownChan <- true
|
||||
return
|
||||
}
|
||||
|
||||
h.deniedChan <- fmt.Errorf("This probe have been denied from overFlow")
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
func getRegistHeader() (string, error) {
|
||||
var err error
|
||||
nap := module.NoAuthProbe{
|
||||
APIKey: config.Config.Domain.APIKey,
|
||||
APIKey: *config.CFG.Central.APIKey,
|
||||
}
|
||||
var nad *module.NoAuthProbeDescription
|
||||
if nad, err = getDescription(); nil != err {
|
||||
|
@ -1,8 +1,10 @@
|
||||
{
|
||||
"domain": {
|
||||
"central": {
|
||||
"url": "ws://127.0.0.1:19190",
|
||||
"apikey": "52abd6fd57e511e7ac52080027658d13"
|
||||
},
|
||||
"central": {
|
||||
"url": "ws://127.0.0.1:19190"
|
||||
|
||||
"probe": {
|
||||
"key": ""
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package config
|
||||
|
||||
type CentralConfig struct {
|
||||
URL string `json:"url" yaml:"url" toml:"url"`
|
||||
EntryPoints map[string]string `json:"entryPoints" yaml:"entryPoints" toml:"entryPoints"`
|
||||
}
|
@ -4,9 +4,20 @@ const (
|
||||
ConfigFileName = "config.json"
|
||||
)
|
||||
|
||||
var Config AllConfig
|
||||
var ConfigDir *string
|
||||
var ConfigFilePath *string
|
||||
var CFG *Config
|
||||
|
||||
type AllConfig struct {
|
||||
Domain DomainConfig `json:"domain" yaml:"domain" toml:"domain"`
|
||||
type Config struct {
|
||||
Central CentralConfig `json:"central" yaml:"central" toml:"central"`
|
||||
Probe ProbeConfig `json:"probe" yaml:"probe" toml:"probe"`
|
||||
}
|
||||
|
||||
type CentralConfig struct {
|
||||
URL *string `json:"url" yaml:"url" toml:"url"`
|
||||
APIKey *string `json:"apiKey" yaml:"apiKey" toml:"apiKey"`
|
||||
}
|
||||
|
||||
type ProbeConfig struct {
|
||||
Key *string `json:"key,omitempty" yaml:"key" toml:"key"`
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package config
|
||||
|
||||
type DomainConfig struct {
|
||||
APIKey string `json:"apiKey" yaml:"apiKey" toml:"apiKey"`
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package config
|
||||
|
||||
const (
|
||||
ProbeConfigFileName = "probe.json"
|
||||
)
|
||||
|
||||
type ProbeConfig struct {
|
||||
ID *string `json:"id,omitempty" yaml:"id" toml:"id"`
|
||||
}
|
81
main.go
81
main.go
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@ -18,20 +19,17 @@ import (
|
||||
"github.com/takama/daemon"
|
||||
)
|
||||
|
||||
const (
|
||||
version = "1.0.0"
|
||||
website = "https://www.overflow.cloud"
|
||||
banner = `
|
||||
/*
|
||||
/bin/
|
||||
probe.exe
|
||||
/config/
|
||||
/sensor/
|
||||
noauthprobe.json
|
||||
central.json
|
||||
probe.json
|
||||
logging.json
|
||||
|
||||
██████╗ ██╗ ██╗███████╗██████╗ ███████╗██╗ ██████╗ ██╗ ██╗
|
||||
██╔═══██╗██║ ██║██╔════╝██╔══██╗██╔════╝██║ ██╔═══██╗██║ ██║
|
||||
██║ ██║██║ ██║█████╗ ██████╔╝█████╗ ██║ ██║ ██║██║ █╗ ██║
|
||||
██║ ██║╚██╗ ██╔╝██╔══╝ ██╔══██╗██╔══╝ ██║ ██║ ██║██║███╗██║
|
||||
╚██████╔╝ ╚████╔╝ ███████╗██║ ██║██║ ███████╗╚██████╔╝╚███╔███╔╝
|
||||
╚═════╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═════╝ ╚══╝╚══╝
|
||||
|
||||
`
|
||||
)
|
||||
*/
|
||||
|
||||
const (
|
||||
// name of the service
|
||||
@ -113,28 +111,33 @@ func main() {
|
||||
var status string
|
||||
var handler commons.Handler
|
||||
isRunning := true
|
||||
var confDir string
|
||||
|
||||
defer logging.Logger.Sync()
|
||||
|
||||
fmt.Println(banner)
|
||||
fmt.Printf("Version: %s\n", version)
|
||||
fmt.Printf("URL: %s\n", website)
|
||||
fmt.Println()
|
||||
printBanner()
|
||||
|
||||
if dir, err := lfcc.ABSPathify(*configDir); nil != err {
|
||||
logging.Logger.Panic(fmt.Sprintf("Probe: config path[%s] is not valid", *configDir))
|
||||
} else {
|
||||
logging.Logger.Debug(fmt.Sprintf("Probe: config path[%s]", dir))
|
||||
confDir = dir
|
||||
config.ConfigDir = &dir
|
||||
}
|
||||
cfp := path.Join(*config.ConfigDir, config.ConfigFileName)
|
||||
config.ConfigFilePath = &cfp
|
||||
|
||||
conf := lfcc.New()
|
||||
conf.SetConfigPath(*configDir)
|
||||
if err := conf.Load(&config.Config, "config.json"); nil != err {
|
||||
config.CFG = &config.Config{}
|
||||
if err := conf.Load(config.CFG, *config.ConfigFilePath); nil != err {
|
||||
logging.Logger.Panic(fmt.Sprintf("Probe: config is not valid error[%v]", err))
|
||||
}
|
||||
|
||||
if nil == config.CFG.Central.APIKey {
|
||||
logging.Logger.Panic("Probe: APIKey is required")
|
||||
}
|
||||
if nil == config.CFG.Central.URL {
|
||||
logging.Logger.Panic("Probe: URL of overFlow Central is required")
|
||||
}
|
||||
|
||||
if srv, err = daemon.New(serviceName, serviceDescription); nil != err {
|
||||
logging.Logger.Panic(fmt.Sprintf("Probe: %v", err))
|
||||
}
|
||||
@ -150,18 +153,17 @@ func main() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if handler, err = auth.New(confDir); nil != err {
|
||||
logging.Logger.Error(fmt.Sprintf("Auth: error: %v", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if handler, err = auth.New(); nil != err {
|
||||
logging.Logger.Error(fmt.Sprintf("Auth: error: %v", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := handler.Serve(); err != nil {
|
||||
logging.Logger.Error(fmt.Sprintf("Probe: Authenticator error: %v", err))
|
||||
logging.Logger.Error(fmt.Sprintf("Auth: Stopped[%v]", err))
|
||||
return
|
||||
}
|
||||
|
||||
if handler, err = probe.New(confDir); nil != err {
|
||||
if handler, err = probe.New(); nil != err {
|
||||
logging.Logger.Error(fmt.Sprintf("Probe: error: %v", err))
|
||||
return
|
||||
}
|
||||
@ -213,3 +215,26 @@ func main() {
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
const (
|
||||
version = "1.0.0"
|
||||
website = "https://www.overflow.cloud"
|
||||
banner = `
|
||||
|
||||
██████╗ ██╗ ██╗███████╗██████╗ ███████╗██╗ ██████╗ ██╗ ██╗
|
||||
██╔═══██╗██║ ██║██╔════╝██╔══██╗██╔════╝██║ ██╔═══██╗██║ ██║
|
||||
██║ ██║██║ ██║█████╗ ██████╔╝█████╗ ██║ ██║ ██║██║ █╗ ██║
|
||||
██║ ██║╚██╗ ██╔╝██╔══╝ ██╔══██╗██╔══╝ ██║ ██║ ██║██║███╗██║
|
||||
╚██████╔╝ ╚████╔╝ ███████╗██║ ██║██║ ███████╗╚██████╔╝╚███╔███╔╝
|
||||
╚═════╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═════╝ ╚══╝╚══╝
|
||||
|
||||
`
|
||||
)
|
||||
|
||||
func printBanner() {
|
||||
fmt.Println(banner)
|
||||
fmt.Printf("Version: %s\n", version)
|
||||
fmt.Printf("URL: %s\n", website)
|
||||
fmt.Println()
|
||||
|
||||
}
|
||||
|
BIN
overflow_probes
BIN
overflow_probes
Binary file not shown.
@ -1,3 +0,0 @@
|
||||
{
|
||||
"id": ""
|
||||
}
|
15
probe/on_notify.go
Normal file
15
probe/on_notify.go
Normal file
@ -0,0 +1,15 @@
|
||||
package probe
|
||||
|
||||
import "git.loafle.net/overflow/overflow_probes/central/api/module"
|
||||
|
||||
func (p *probe) onNotify(method string, params []string) {
|
||||
switch method {
|
||||
case module.NoAuthProbeService_AcceptNoAuthProbe:
|
||||
|
||||
break
|
||||
case module.NoAuthProbeService_DenyNoauthProbe:
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
}
|
@ -5,9 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
lfcc "git.loafle.net/commons_go/config"
|
||||
"git.loafle.net/commons_go/logging"
|
||||
"git.loafle.net/overflow/overflow_probes/central/api/module"
|
||||
"git.loafle.net/overflow/overflow_probes/central/client"
|
||||
@ -19,6 +17,7 @@ import (
|
||||
const (
|
||||
probeEntryPoint = "/probe"
|
||||
metricsEntryPoint = "/metrics"
|
||||
fileEntryPoint = "/file"
|
||||
)
|
||||
|
||||
type Probe interface {
|
||||
@ -26,10 +25,6 @@ type Probe interface {
|
||||
}
|
||||
|
||||
type probe struct {
|
||||
configDir string
|
||||
probeConfigPath string
|
||||
probeConfig config.ProbeConfig
|
||||
|
||||
probeEntryURL string
|
||||
metricsEntryURL string
|
||||
|
||||
@ -39,31 +34,19 @@ type probe struct {
|
||||
shutdownChan chan bool
|
||||
}
|
||||
|
||||
func New(configDir string) (Probe, error) {
|
||||
func New() (Probe, error) {
|
||||
p := &probe{
|
||||
configDir: configDir,
|
||||
shutdownChan: make(chan bool),
|
||||
}
|
||||
var err error
|
||||
|
||||
if p.probeEntryURL, err = opuu.Join(config.Config.Central.URL, probeEntryPoint); nil != err {
|
||||
if p.probeEntryURL, err = opuu.Join(*config.CFG.Central.URL, probeEntryPoint); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
if p.metricsEntryURL, err = opuu.Join(config.Config.Central.URL, metricsEntryPoint); nil != err {
|
||||
if p.metricsEntryURL, err = opuu.Join(*config.CFG.Central.URL, metricsEntryPoint); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p.probeConfigPath = path.Join(configDir, config.ProbeConfigFileName)
|
||||
|
||||
conf := lfcc.New()
|
||||
if !lfcc.Exists(p.probeConfigPath) {
|
||||
return nil, fmt.Errorf("Probe: Config file[%s] is not exist", p.probeConfigPath)
|
||||
}
|
||||
|
||||
if err = conf.Load(&p.probeConfig, p.probeConfigPath); nil != err {
|
||||
return nil, fmt.Errorf("Probe: Loading of Probe config file[%s] failed error[%v]", p.probeConfigPath, err)
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
@ -74,16 +57,15 @@ func (p *probe) Serve() error {
|
||||
return err
|
||||
}
|
||||
|
||||
ListenLoop:
|
||||
// ListenLoop:
|
||||
for {
|
||||
select {
|
||||
case <-p.shutdownChan:
|
||||
err = errors.New("Shutting down")
|
||||
break ListenLoop
|
||||
return errors.New("Shutting down")
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *probe) Shutdown(ctx context.Context) error {
|
||||
@ -93,7 +75,7 @@ func (p *probe) Shutdown(ctx context.Context) error {
|
||||
|
||||
func (p *probe) connectToCentral() error {
|
||||
header := http.Header{}
|
||||
header[module.ProbeHeader_ProbeKey] = []string{*p.probeConfig.ID}
|
||||
header[module.ProbeHeader_ProbeKey] = []string{*config.CFG.Probe.Key}
|
||||
|
||||
var res *http.Response
|
||||
var err error
|
||||
@ -112,15 +94,3 @@ func (p *probe) connectToCentral() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *probe) onNotify(method string, params []string) {
|
||||
switch method {
|
||||
case module.NoAuthProbeService_AcceptNoAuthProbe:
|
||||
|
||||
break
|
||||
case module.NoAuthProbeService_DenyNoauthProbe:
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user