2018-04-17 14:11:13 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-04-18 14:56:13 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2018-04-17 14:11:13 +00:00
|
|
|
"reflect"
|
2018-07-02 08:44:53 +00:00
|
|
|
"compress/gzip"
|
2018-04-17 14:11:13 +00:00
|
|
|
|
|
|
|
cda "git.loafle.net/commons/di-go/annotation"
|
|
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
2018-07-02 08:44:53 +00:00
|
|
|
"git.loafle.net/commons/logging-go"
|
2018-04-29 09:41:14 +00:00
|
|
|
occp "git.loafle.net/overflow/commons-go/config/probe"
|
2018-04-26 08:39:32 +00:00
|
|
|
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
|
2018-04-26 08:43:40 +00:00
|
|
|
ocsp "git.loafle.net/overflow/commons-go/service/probe"
|
2018-04-18 14:56:13 +00:00
|
|
|
"git.loafle.net/overflow/probe/config"
|
|
|
|
|
|
|
|
// For annotation
|
2018-04-17 14:11:13 +00:00
|
|
|
_ "git.loafle.net/overflow/commons-go/core/annotation"
|
2018-07-02 08:44:53 +00:00
|
|
|
"bytes"
|
2018-07-02 12:22:45 +00:00
|
|
|
"crypto/des"
|
|
|
|
"errors"
|
2018-04-17 14:11:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var SensorConfigServiceType = reflect.TypeOf((*SensorConfigService)(nil))
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cdr.RegisterType(SensorConfigServiceType)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SensorConfigService struct {
|
2018-04-26 08:43:40 +00:00
|
|
|
ocsp.SensorConfigService
|
2018-04-17 14:11:13 +00:00
|
|
|
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
|
2018-04-18 14:56:13 +00:00
|
|
|
|
2018-04-29 09:41:14 +00:00
|
|
|
ContainerService *ContainerService `annotation:"@Inject()"`
|
2018-07-02 08:44:53 +00:00
|
|
|
ProbeClientService *ProbeClientService `annotation:"@Inject()"`
|
2018-04-29 09:41:14 +00:00
|
|
|
|
2018-05-03 11:24:07 +00:00
|
|
|
sensorConfigs map[string]*ocmsc.SensorConfig
|
|
|
|
sensorConfigsPerContainer map[occp.ContainerType][]*ocmsc.SensorConfig
|
2018-04-17 14:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) InitService() error {
|
2018-04-26 08:39:32 +00:00
|
|
|
s.sensorConfigs = make(map[string]*ocmsc.SensorConfig)
|
2018-04-18 14:56:13 +00:00
|
|
|
|
2018-04-17 14:11:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) StartService() error {
|
2018-04-18 14:56:13 +00:00
|
|
|
if err := s.loadConfigAll(); nil != err {
|
2018-05-03 11:24:07 +00:00
|
|
|
return fmt.Errorf("SensorConfigService: StartService failed %v", err)
|
2018-04-18 14:56:13 +00:00
|
|
|
}
|
2018-04-17 14:11:13 +00:00
|
|
|
|
2018-04-29 09:41:14 +00:00
|
|
|
if nil != s.sensorConfigs || 0 < len(s.sensorConfigs) {
|
2018-05-03 11:24:07 +00:00
|
|
|
s.sortSensorConfigPerContainer()
|
|
|
|
|
|
|
|
for containerType := range s.sensorConfigsPerContainer {
|
2018-05-04 12:03:23 +00:00
|
|
|
s.ContainerService.RunContainer(containerType)
|
2018-04-29 09:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:11:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) StopService() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) DestroyService() {
|
|
|
|
|
|
|
|
}
|
2018-04-18 14:56:13 +00:00
|
|
|
|
2018-05-03 11:24:07 +00:00
|
|
|
func (s *SensorConfigService) SendInitConfig(containerType occp.ContainerType) error {
|
|
|
|
configs, ok := s.sensorConfigsPerContainer[containerType]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.ContainerService.Send(containerType, "SensorConfigService.InitConfig", configs); nil != err {
|
|
|
|
return fmt.Errorf("SensorConfigService: StartService failed %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-18 14:56:13 +00:00
|
|
|
func (s *SensorConfigService) AddConfig(tempFilePath string) error {
|
2018-07-02 08:44:53 +00:00
|
|
|
//sc, buf, err := s.loadConfigFile(tempFilePath)
|
|
|
|
|
2018-07-03 04:28:36 +00:00
|
|
|
sc, _, err := s.loadConfigFile([]byte(tempFilePath))
|
2018-04-18 14:56:13 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetPath := config.SensorConfigFilePath(sc)
|
2018-07-02 12:22:45 +00:00
|
|
|
err = ioutil.WriteFile(targetPath, []byte(tempFilePath), 0644)
|
2018-04-18 14:56:13 +00:00
|
|
|
|
|
|
|
if nil != err {
|
2018-07-02 12:22:45 +00:00
|
|
|
fmt.Println(err.Error())
|
2018-04-18 14:56:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 04:28:36 +00:00
|
|
|
s.sensorConfigs[sc.SensorID.String()] = sc
|
|
|
|
s.sortSensorConfigPerContainer()
|
2018-07-02 12:22:45 +00:00
|
|
|
|
2018-07-03 04:28:36 +00:00
|
|
|
if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.MetaCrawlerContainerKey), "SensorConfigService.AddConfig", sc); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-03 11:24:07 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) UpdateConfig(tempFilePath string) error {
|
2018-07-03 04:28:36 +00:00
|
|
|
loadByte, err := ioutil.ReadFile(tempFilePath)
|
|
|
|
|
|
|
|
sc, buf, err := s.loadConfigFile(loadByte)
|
2018-05-03 11:24:07 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetPath := config.SensorConfigFilePath(sc)
|
|
|
|
ioutil.WriteFile(targetPath, buf, 0644)
|
|
|
|
|
|
|
|
// tempfile remove
|
|
|
|
err = os.Remove(tempFilePath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-03 05:29:26 +00:00
|
|
|
delete(s.sensorConfigs, sc.SensorID.String())
|
|
|
|
s.sensorConfigs[sc.SensorID.String()] = sc
|
|
|
|
s.sortSensorConfigPerContainer()
|
|
|
|
|
|
|
|
if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.MetaCrawlerContainerKey), "SensorConfigService.UpdateConfig", sc); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-18 14:56:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) RemoveConfig(sensorConfigID string) error {
|
|
|
|
sc, ok := s.sensorConfigs[sensorConfigID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("SensorConfig[%s] is not exist", sensorConfigID)
|
|
|
|
}
|
|
|
|
|
|
|
|
targetPath := config.SensorConfigFilePath(sc)
|
|
|
|
err := os.Remove(targetPath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(s.sensorConfigs, sensorConfigID)
|
2018-05-03 11:24:07 +00:00
|
|
|
s.sortSensorConfigPerContainer()
|
|
|
|
|
2018-07-03 05:29:26 +00:00
|
|
|
if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.MetaCrawlerContainerKey), "SensorConfigService.RemoveConfig", sensorConfigID); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-18 14:56:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) loadConfigAll() error {
|
|
|
|
configDirPath := config.ConfigDir()
|
|
|
|
files, err := ioutil.ReadDir(configDirPath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() == true {
|
|
|
|
if err := s.loadConfigDir(path.Join(configDirPath, file.Name())); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) loadConfigDir(dirPath string) error {
|
|
|
|
files, err := ioutil.ReadDir(dirPath)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
filePath := path.Join(dirPath, file.Name())
|
|
|
|
|
|
|
|
if file.IsDir() == true {
|
|
|
|
if err := s.loadConfigDir(filePath); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-03 04:28:36 +00:00
|
|
|
buf, err := ioutil.ReadFile(filePath)
|
|
|
|
sc, _, err := s.loadConfigFile(buf)
|
2018-04-18 14:56:13 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.sensorConfigs[file.Name()] = sc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-03 04:28:36 +00:00
|
|
|
func (s *SensorConfigService) loadConfigFile1(filePath string) (*ocmsc.SensorConfig, []byte, error) {
|
2018-05-03 11:24:07 +00:00
|
|
|
logging.Logger().Debugf("filePath: %s", filePath)
|
2018-04-18 14:56:13 +00:00
|
|
|
buf, err := ioutil.ReadFile(filePath)
|
2018-07-02 08:44:53 +00:00
|
|
|
|
2018-04-18 14:56:13 +00:00
|
|
|
if nil != err {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-02 08:44:53 +00:00
|
|
|
// probe ecncreiption key decode
|
|
|
|
// gzip decode
|
|
|
|
|
2018-04-26 08:39:32 +00:00
|
|
|
var m = &ocmsc.SensorConfig{}
|
2018-04-18 14:56:13 +00:00
|
|
|
if err := json.Unmarshal(buf, m); nil != err {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, buf, nil
|
|
|
|
}
|
2018-04-29 09:41:14 +00:00
|
|
|
|
2018-07-03 04:28:36 +00:00
|
|
|
func (s *SensorConfigService) loadConfigFile(configStrByte []byte) (*ocmsc.SensorConfig, []byte, error) {
|
2018-07-02 08:44:53 +00:00
|
|
|
//keyStr := s.ProbeClientService.EncryptionKey
|
2018-07-03 04:28:36 +00:00
|
|
|
//configStrByte := []byte(decodeStr)
|
2018-07-02 12:22:45 +00:00
|
|
|
|
2018-07-09 06:59:55 +00:00
|
|
|
keyStr := "f84ld984"
|
2018-07-02 08:44:53 +00:00
|
|
|
key := []byte(keyStr)
|
|
|
|
block, err := des.NewCipher(key)
|
|
|
|
|
|
|
|
if nil != err {
|
2018-07-02 12:22:45 +00:00
|
|
|
fmt.Println(err.Error())
|
2018-07-02 08:44:53 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-03 04:28:36 +00:00
|
|
|
out := make([]byte, len(configStrByte))
|
2018-07-02 12:22:45 +00:00
|
|
|
dst := out
|
|
|
|
bs := block.BlockSize()
|
2018-07-03 04:28:36 +00:00
|
|
|
if len(configStrByte)%bs != 0 {
|
2018-07-02 12:22:45 +00:00
|
|
|
return nil, nil, errors.New("crypto/cipher: input not full blocks")
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(configStrByte) > 0 {
|
|
|
|
block.Decrypt(dst, configStrByte[:bs])
|
|
|
|
configStrByte = configStrByte[bs:]
|
|
|
|
dst = dst[bs:]
|
|
|
|
}
|
|
|
|
|
|
|
|
out = s.PKCS5UnPadding(out)
|
|
|
|
|
|
|
|
//fmt.Println(string(out))
|
|
|
|
gr, err := gzip.NewReader(bytes.NewBuffer(out))
|
|
|
|
if nil != err {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
2018-07-02 08:44:53 +00:00
|
|
|
defer gr.Close()
|
|
|
|
data, err := ioutil.ReadAll(gr)
|
|
|
|
|
2018-07-03 04:28:36 +00:00
|
|
|
//fmt.Println(string(data))
|
2018-07-02 08:44:53 +00:00
|
|
|
var m = &ocmsc.SensorConfig{}
|
|
|
|
if err := json.Unmarshal(data, m); nil != err {
|
2018-07-02 12:22:45 +00:00
|
|
|
fmt.Println(err.Error())
|
2018-07-02 08:44:53 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SensorConfigService) PKCS5UnPadding(src []byte) []byte {
|
|
|
|
length := len(src)
|
|
|
|
unpadding := int(src[length-1])
|
|
|
|
return src[:(length - unpadding)]
|
|
|
|
}
|
|
|
|
|
2018-05-03 11:24:07 +00:00
|
|
|
func (s *SensorConfigService) sortSensorConfigPerContainer() {
|
|
|
|
if nil == s.sensorConfigs || 0 == len(s.sensorConfigs) {
|
|
|
|
return
|
2018-04-29 09:41:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 11:24:07 +00:00
|
|
|
s.sensorConfigsPerContainer = nil
|
|
|
|
s.sensorConfigsPerContainer = make(map[occp.ContainerType][]*ocmsc.SensorConfig)
|
2018-04-29 09:41:14 +00:00
|
|
|
|
2018-07-03 05:29:26 +00:00
|
|
|
for _, sensorConfig := range s.sensorConfigs {
|
|
|
|
containerType := occp.ToContainerType(sensorConfig.Crawler.MetaCrawlerContainerKey)
|
|
|
|
s.sensorConfigsPerContainer[containerType] = append(s.sensorConfigsPerContainer[containerType], sensorConfig)
|
|
|
|
}
|
2018-04-29 09:41:14 +00:00
|
|
|
}
|