ing
This commit is contained in:
parent
7137f8129e
commit
c70ce6375f
@ -12,10 +12,11 @@ import (
|
|||||||
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
|
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
|
||||||
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
|
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
|
||||||
"git.loafle.net/overflow/probe/auth/info"
|
"git.loafle.net/overflow/probe/auth/info"
|
||||||
|
"git.loafle.net/overflow/probe/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewAuth(tempkeyHandler func(tempkey string), services []interface{}) (*crc.Client, error) {
|
func NewAuth(tempKeyHandler func(tempKey string), services []interface{}) (*crc.Client, error) {
|
||||||
config := getConfig()
|
config := config.GetConfig()
|
||||||
if nil == config {
|
if nil == config {
|
||||||
return nil, fmt.Errorf("Config is not available")
|
return nil, fmt.Errorf("Config is not available")
|
||||||
}
|
}
|
||||||
@ -50,8 +51,8 @@ func NewAuth(tempkeyHandler func(tempkey string), services []interface{}) (*crc.
|
|||||||
switch authConfig.State() {
|
switch authConfig.State() {
|
||||||
case ocnc.AuthStateTypeNotRegisterd:
|
case ocnc.AuthStateTypeNotRegisterd:
|
||||||
tempProbeKey := res.Header.Get(ocncc.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
|
tempProbeKey := res.Header.Get(ocncc.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
|
||||||
if nil != tempkeyHandler {
|
if nil != tempKeyHandler {
|
||||||
tempkeyHandler(tempProbeKey)
|
tempKeyHandler(tempProbeKey)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
@ -4,21 +4,17 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"reflect"
|
|
||||||
|
|
||||||
cdr "git.loafle.net/commons/di-go/registry"
|
|
||||||
logging "git.loafle.net/commons/logging-go"
|
|
||||||
crc "git.loafle.net/commons/rpc-go/client"
|
crc "git.loafle.net/commons/rpc-go/client"
|
||||||
crpj "git.loafle.net/commons/rpc-go/protocol/json"
|
crpj "git.loafle.net/commons/rpc-go/protocol/json"
|
||||||
crr "git.loafle.net/commons/rpc-go/registry"
|
crr "git.loafle.net/commons/rpc-go/registry"
|
||||||
csc "git.loafle.net/commons/server-go/client"
|
csc "git.loafle.net/commons/server-go/client"
|
||||||
csswc "git.loafle.net/commons/server-go/socket/web/client"
|
csswc "git.loafle.net/commons/server-go/socket/web/client"
|
||||||
cur "git.loafle.net/commons/util-go/reflect"
|
|
||||||
"git.loafle.net/overflow/probe/config"
|
"git.loafle.net/overflow/probe/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newConnector(name string, entryPath string) (*csswc.Connectors, error) {
|
func newConnector(name string, entryPath string) (*csswc.Connectors, error) {
|
||||||
config := getConfig()
|
config := config.GetConfig()
|
||||||
if nil == config {
|
if nil == config {
|
||||||
return nil, fmt.Errorf("Config is not available")
|
return nil, fmt.Errorf("Config is not available")
|
||||||
}
|
}
|
||||||
@ -57,19 +53,3 @@ func newClient(name string, connector csc.Connector, services []interface{}) *cr
|
|||||||
Name: name,
|
Name: name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() *config.Config {
|
|
||||||
_config, err := cdr.GetInstanceByName("Config")
|
|
||||||
if nil != err {
|
|
||||||
logging.Logger().Error(err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
config, ok := _config.(*config.Config)
|
|
||||||
if !ok {
|
|
||||||
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_config))
|
|
||||||
logging.Logger().Errorf("Cannot convert [%s]%s to Config type", pkg, n)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
|
@ -1 +1,33 @@
|
|||||||
package central
|
package central
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
crc "git.loafle.net/commons/rpc-go/client"
|
||||||
|
ocnpc "git.loafle.net/overflow/commons-go/probe/constants"
|
||||||
|
"git.loafle.net/overflow/probe/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewData() (*crc.Client, error) {
|
||||||
|
config := config.GetConfig()
|
||||||
|
if nil == config {
|
||||||
|
return nil, fmt.Errorf("Config is not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
connector, err := newConnector("Data", ocnpc.HTTPEntry_Data)
|
||||||
|
if nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
connector.RequestHeader = func() http.Header {
|
||||||
|
header := make(map[string][]string)
|
||||||
|
header[ocnpc.HTTPRequestHeaderKey_Probe_Method] = []string{ocnpc.HTTPRequestHeaderValue_Probe_Method_Connect}
|
||||||
|
header[ocnpc.HTTPRequestHeaderKey_Probe_ProbeKey] = []string{*config.Probe.Key}
|
||||||
|
return header
|
||||||
|
}
|
||||||
|
connector.ResponseHandler = func(res *http.Response) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return newClient("Data", connector, nil), nil
|
||||||
|
}
|
||||||
|
@ -1 +1,37 @@
|
|||||||
package central
|
package central
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
crc "git.loafle.net/commons/rpc-go/client"
|
||||||
|
ocnpc "git.loafle.net/overflow/commons-go/probe/constants"
|
||||||
|
"git.loafle.net/overflow/probe/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewProbe(encryptionKeyHandler func(encryptionKey string), services []interface{}) (*crc.Client, error) {
|
||||||
|
config := config.GetConfig()
|
||||||
|
if nil == config {
|
||||||
|
return nil, fmt.Errorf("Config is not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
connector, err := newConnector("Probe", ocnpc.HTTPEntry_Probe)
|
||||||
|
if nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
connector.RequestHeader = func() http.Header {
|
||||||
|
header := make(map[string][]string)
|
||||||
|
header[ocnpc.HTTPRequestHeaderKey_Probe_Method] = []string{ocnpc.HTTPRequestHeaderValue_Probe_Method_Connect}
|
||||||
|
header[ocnpc.HTTPRequestHeaderKey_Probe_ProbeKey] = []string{*config.Probe.Key}
|
||||||
|
return header
|
||||||
|
}
|
||||||
|
connector.ResponseHandler = func(res *http.Response) {
|
||||||
|
encryptionKey := res.Header.Get(ocnpc.HTTPResponseHeaderKey_Probe_SetEncryptionKey)
|
||||||
|
if nil != encryptionKeyHandler {
|
||||||
|
encryptionKeyHandler(encryptionKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newClient("Probe", connector, services), nil
|
||||||
|
}
|
||||||
|
@ -14,13 +14,6 @@
|
|||||||
"pongTimeout": 60,
|
"pongTimeout": 60,
|
||||||
"pingTimeout": 10,
|
"pingTimeout": 10,
|
||||||
"pingPeriod": 9
|
"pingPeriod": 9
|
||||||
},
|
|
||||||
"proxy": {
|
|
||||||
"host": "",
|
|
||||||
"port": 9090,
|
|
||||||
"useAuth": true,
|
|
||||||
"user": "",
|
|
||||||
"password": ""
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"probe": {
|
"probe": {
|
||||||
|
@ -1,12 +1,54 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
cdr "git.loafle.net/commons/di-go/registry"
|
||||||
|
"git.loafle.net/commons/logging-go"
|
||||||
|
cur "git.loafle.net/commons/util-go/reflect"
|
||||||
ocpc "git.loafle.net/overflow/commons-go/probe/config"
|
ocpc "git.loafle.net/overflow/commons-go/probe/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ConfigKey = "Config"
|
||||||
|
ConfigDirKey = "ConfigDir"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Account *ocpc.Account `required:"true" json:"account" yaml:"account" toml:"account"`
|
Account *ocpc.Account `required:"true" json:"account" yaml:"account" toml:"account"`
|
||||||
Central *ocpc.Central `required:"true" json:"central" yaml:"central" toml:"central"`
|
Central *ocpc.Central `required:"true" json:"central" yaml:"central" toml:"central"`
|
||||||
Probe *ocpc.Probe `required:"true" json:"probe" yaml:"probe" toml:"probe"`
|
Probe *ocpc.Probe `required:"true" json:"probe" yaml:"probe" toml:"probe"`
|
||||||
Paths map[string]string `required:"true" json:"paths" yaml:"paths" toml:"paths"`
|
Paths map[string]string `required:"true" json:"paths" yaml:"paths" toml:"paths"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetConfig() *Config {
|
||||||
|
_config, err := cdr.GetInstanceByName(ConfigKey)
|
||||||
|
if nil != err {
|
||||||
|
logging.Logger().Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
config, ok := _config.(*Config)
|
||||||
|
if !ok {
|
||||||
|
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_config))
|
||||||
|
logging.Logger().Errorf("Cannot convert [%s]%s to Config type", pkg, n)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetConfigDir() string {
|
||||||
|
_configDir, err := cdr.GetInstanceByName(ConfigDirKey)
|
||||||
|
if nil != err {
|
||||||
|
logging.Logger().Error(err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
configDir, ok := _configDir.(string)
|
||||||
|
if !ok {
|
||||||
|
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_configDir))
|
||||||
|
logging.Logger().Errorf("Cannot convert [%s]%s to string type", pkg, n)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return configDir
|
||||||
|
}
|
||||||
|
5
main.go
5
main.go
@ -35,8 +35,9 @@ func main() {
|
|||||||
if err := configuration.Load(_config, ocpc.ConfigFileName); nil != err {
|
if err := configuration.Load(_config, ocpc.ConfigFileName); nil != err {
|
||||||
logging.Logger().Panic(err)
|
logging.Logger().Panic(err)
|
||||||
}
|
}
|
||||||
cdr.RegisterResource("Config", _config)
|
|
||||||
cdr.RegisterResource("ConfigDir", *configDir)
|
cdr.RegisterResource(config.ConfigKey, _config)
|
||||||
|
cdr.RegisterResource(config.ConfigDirKey, *configDir)
|
||||||
|
|
||||||
var instance interface{}
|
var instance interface{}
|
||||||
|
|
||||||
|
167
probe/probe.go
167
probe/probe.go
@ -3,33 +3,13 @@ package probe
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"path"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.loafle.net/commons/configuration-go"
|
// For service
|
||||||
cdr "git.loafle.net/commons/di-go/registry"
|
_ "git.loafle.net/overflow/probe/service"
|
||||||
logging "git.loafle.net/commons/logging-go"
|
|
||||||
crc "git.loafle.net/commons/rpc-go/client"
|
|
||||||
crpj "git.loafle.net/commons/rpc-go/protocol/json"
|
|
||||||
crr "git.loafle.net/commons/rpc-go/registry"
|
|
||||||
csswc "git.loafle.net/commons/server-go/socket/web/client"
|
|
||||||
occa "git.loafle.net/overflow/commons-go/core/annotation"
|
|
||||||
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
|
|
||||||
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
|
|
||||||
ocpc "git.loafle.net/overflow/commons-go/probe/config"
|
|
||||||
"git.loafle.net/overflow/probe/auth/info"
|
|
||||||
_ "git.loafle.net/overflow/probe/auth/service"
|
|
||||||
"git.loafle.net/overflow/probe/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Probe struct {
|
type Probe struct {
|
||||||
Config *config.Config
|
|
||||||
ConfigDir string
|
|
||||||
|
|
||||||
client *crc.Client
|
|
||||||
|
|
||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
stopWg sync.WaitGroup
|
stopWg sync.WaitGroup
|
||||||
}
|
}
|
||||||
@ -39,13 +19,15 @@ func (p *Probe) Start() error {
|
|||||||
return fmt.Errorf("already running. Stop it before starting it again")
|
return fmt.Errorf("already running. Stop it before starting it again")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.initClient(); nil != err {
|
// services, err := cdr.GetInstancesByAnnotationType(occa.RPCServiceAnnotationType)
|
||||||
return err
|
// if nil != err {
|
||||||
}
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
if err := p.client.Start(); nil != err {
|
// client, err := central.NewProbe(p.HandleEncryptionKey, services)
|
||||||
return err
|
// if nil != err {
|
||||||
}
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
p.stopChan = make(chan struct{})
|
p.stopChan = make(chan struct{})
|
||||||
|
|
||||||
@ -71,117 +53,32 @@ func (p *Probe) logHeader() string {
|
|||||||
return "Probe:"
|
return "Probe:"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Probe) initService() (crr.RPCInvoker, error) {
|
func (p *Probe) HandleEncryptionKey(encryptionKey string) {
|
||||||
services, err := cdr.GetInstancesByAnnotationType(occa.RPCServiceAnnotationType)
|
|
||||||
if nil != err {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rpcRegistry := crr.NewRPCRegistry()
|
|
||||||
rpcRegistry.RegisterServices(services...)
|
|
||||||
|
|
||||||
return rpcRegistry, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Probe) initClient() error {
|
func (p *Probe) handleProbe() {
|
||||||
_connector := p.Config.Central.Connector.Clone()
|
// var err error
|
||||||
connector := _connector.(*csswc.Connectors)
|
// defer func() {
|
||||||
|
// if nil != client {
|
||||||
|
// err = client.Stop(context.Background())
|
||||||
|
// }
|
||||||
|
|
||||||
header := make(map[string][]string)
|
// a.stopWg.Done()
|
||||||
|
// endChan <- err
|
||||||
|
// }()
|
||||||
|
|
||||||
switch p.authConfig.State() {
|
// if err = client.Start(); nil != err {
|
||||||
case ocnc.AuthStateTypeRegisterd:
|
// logging.Logger().Error(err)
|
||||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect}
|
// return
|
||||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey] = []string{*p.authConfig.TempKey}
|
// }
|
||||||
default:
|
|
||||||
rh, err := info.GetRegistHeader(p.Config.Account.APIKey)
|
|
||||||
if nil != err {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist}
|
|
||||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Info] = []string{rh}
|
|
||||||
}
|
|
||||||
|
|
||||||
centralURL := fmt.Sprintf("ws://%s:%d%s", p.Config.Central.Host, p.Config.Central.Port, ocncc.HTTPEntry_Auth)
|
// for {
|
||||||
|
// select {
|
||||||
logging.Logger().Debugf("%s central %s", p.logHeader(), centralURL)
|
// case err = <-authDoneChan:
|
||||||
|
// return
|
||||||
connector.URL = centralURL
|
// case <-a.stopChan:
|
||||||
connector.RequestHeader = header
|
// return
|
||||||
connector.ResponseHandler = func(res *http.Response) {
|
// }
|
||||||
switch p.authConfig.State() {
|
// }
|
||||||
case ocnc.AuthStateTypeNotRegisterd:
|
|
||||||
tempProbeKey := res.Header.Get(ocncc.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
|
|
||||||
p.tempKeyChan <- tempProbeKey
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rpcInvoker, err := p.initService()
|
|
||||||
if nil != err {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
codec := crpj.NewClientCodec()
|
|
||||||
|
|
||||||
p.client = &crc.Client{
|
|
||||||
Connector: connector,
|
|
||||||
Codec: codec,
|
|
||||||
RPCInvoker: rpcInvoker,
|
|
||||||
Name: "Authenticator",
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Probe) handleProbe(endChan chan<- error) {
|
|
||||||
var err error
|
|
||||||
defer func() {
|
|
||||||
if nil != p.client {
|
|
||||||
err = p.client.Stop(context.Background())
|
|
||||||
}
|
|
||||||
|
|
||||||
p.stopWg.Done()
|
|
||||||
endChan <- err
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case probeKey, ok := <-p.accpetedChan:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logging.Logger().Infof("%s accepted by central", p.logHeader())
|
|
||||||
p.Config.Probe.Key = &probeKey
|
|
||||||
err = configuration.Save(p.Config, path.Join(p.ConfigDir, ocpc.ConfigFileName), true)
|
|
||||||
if nil != err {
|
|
||||||
logging.Logger().Errorf("%s %v", p.logHeader(), err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
case _, ok := <-p.deniedChan:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logging.Logger().Infof("%s denied by central", p.logHeader())
|
|
||||||
n := time.Now()
|
|
||||||
p.authConfig.DenyDate = &n
|
|
||||||
err = configuration.Save(p.authConfig, path.Join(p.ConfigDir, ocnc.ConfigFileName), true)
|
|
||||||
if nil != err {
|
|
||||||
logging.Logger().Errorf("%s %v", p.logHeader(), err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
case tempKey, ok := <-p.tempKeyChan:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logging.Logger().Infof("%s registered by central", p.logHeader())
|
|
||||||
p.authConfig.TempKey = &tempKey
|
|
||||||
err = configuration.Save(p.authConfig, path.Join(p.ConfigDir, ocnc.ConfigFileName), true)
|
|
||||||
if nil != err {
|
|
||||||
logging.Logger().Errorf("%s %v", p.logHeader(), err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case <-p.stopChan:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user