291 lines
6.8 KiB
Go
291 lines
6.8 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"reflect"
|
|
"compress/gzip"
|
|
|
|
cda "git.loafle.net/commons/di-go/annotation"
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
|
"git.loafle.net/commons/logging-go"
|
|
occp "git.loafle.net/overflow/commons-go/config/probe"
|
|
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
|
|
ocsp "git.loafle.net/overflow/commons-go/service/probe"
|
|
"git.loafle.net/overflow/probe/config"
|
|
|
|
// For annotation
|
|
_ "git.loafle.net/overflow/commons-go/core/annotation"
|
|
"bytes"
|
|
"crypto/des"
|
|
"errors"
|
|
)
|
|
|
|
var SensorConfigServiceType = reflect.TypeOf((*SensorConfigService)(nil))
|
|
|
|
func init() {
|
|
cdr.RegisterType(SensorConfigServiceType)
|
|
}
|
|
|
|
type SensorConfigService struct {
|
|
ocsp.SensorConfigService
|
|
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
|
|
|
|
ContainerService *ContainerService `annotation:"@Inject()"`
|
|
ProbeClientService *ProbeClientService `annotation:"@Inject()"`
|
|
|
|
sensorConfigs map[string]*ocmsc.SensorConfig
|
|
sensorConfigsPerContainer map[occp.ContainerType][]*ocmsc.SensorConfig
|
|
}
|
|
|
|
func (s *SensorConfigService) InitService() error {
|
|
s.sensorConfigs = make(map[string]*ocmsc.SensorConfig)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SensorConfigService) StartService() error {
|
|
if err := s.loadConfigAll(); nil != err {
|
|
return fmt.Errorf("SensorConfigService: StartService failed %v", err)
|
|
}
|
|
|
|
if nil != s.sensorConfigs || 0 < len(s.sensorConfigs) {
|
|
s.sortSensorConfigPerContainer()
|
|
|
|
for containerType := range s.sensorConfigsPerContainer {
|
|
s.ContainerService.RunContainer(containerType)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SensorConfigService) StopService() {
|
|
|
|
}
|
|
|
|
func (s *SensorConfigService) DestroyService() {
|
|
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (s *SensorConfigService) AddConfig(tempFilePath string) error {
|
|
//sc, buf, err := s.loadConfigFile(tempFilePath)
|
|
|
|
sc, _, err := s.loadConfigFile([]byte(tempFilePath))
|
|
if nil != err {
|
|
return err
|
|
}
|
|
|
|
targetPath := config.SensorConfigFilePath(sc)
|
|
err = ioutil.WriteFile(targetPath, []byte(tempFilePath), 0644)
|
|
|
|
if nil != err {
|
|
fmt.Println(err.Error())
|
|
}
|
|
|
|
s.sensorConfigs[sc.SensorID.String()] = sc
|
|
s.sortSensorConfigPerContainer()
|
|
|
|
if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.MetaCrawlerContainerKey), "SensorConfigService.AddConfig", sc); nil != err {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SensorConfigService) UpdateConfig(tempFilePath string) error {
|
|
loadByte, err := ioutil.ReadFile(tempFilePath)
|
|
|
|
sc, buf, err := s.loadConfigFile(loadByte)
|
|
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
|
|
}
|
|
|
|
//delete(s.sensorConfigs, sc.ConfigID)
|
|
//s.sensorConfigs[sc.ID.String()] = sc
|
|
//s.sortSensorConfigPerContainer()
|
|
//
|
|
//if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.Container), "SensorConfigService.UpdateConfig", sc); nil != err {
|
|
// return err
|
|
//}
|
|
|
|
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)
|
|
s.sortSensorConfigPerContainer()
|
|
|
|
//if err := s.ContainerService.Send(occp.ToContainerType(sc.Crawler.Container), "SensorConfigService.RemoveConfig", sensorConfigID); nil != err {
|
|
// return err
|
|
//}
|
|
|
|
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 {
|
|
buf, err := ioutil.ReadFile(filePath)
|
|
sc, _, err := s.loadConfigFile(buf)
|
|
if nil != err {
|
|
return err
|
|
}
|
|
s.sensorConfigs[file.Name()] = sc
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SensorConfigService) loadConfigFile1(filePath string) (*ocmsc.SensorConfig, []byte, error) {
|
|
logging.Logger().Debugf("filePath: %s", filePath)
|
|
buf, err := ioutil.ReadFile(filePath)
|
|
|
|
if nil != err {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// probe ecncreiption key decode
|
|
// gzip decode
|
|
|
|
var m = &ocmsc.SensorConfig{}
|
|
if err := json.Unmarshal(buf, m); nil != err {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return m, buf, nil
|
|
}
|
|
|
|
func (s *SensorConfigService) loadConfigFile(configStrByte []byte) (*ocmsc.SensorConfig, []byte, error) {
|
|
//keyStr := s.ProbeClientService.EncryptionKey
|
|
//configStrByte := []byte(decodeStr)
|
|
|
|
keyStr := "aaaaaaaa"
|
|
key := []byte(keyStr)
|
|
block, err := des.NewCipher(key)
|
|
|
|
if nil != err {
|
|
fmt.Println(err.Error())
|
|
return nil, nil, err
|
|
}
|
|
|
|
out := make([]byte, len(configStrByte))
|
|
dst := out
|
|
bs := block.BlockSize()
|
|
if len(configStrByte)%bs != 0 {
|
|
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())
|
|
}
|
|
|
|
defer gr.Close()
|
|
data, err := ioutil.ReadAll(gr)
|
|
|
|
//fmt.Println(string(data))
|
|
var m = &ocmsc.SensorConfig{}
|
|
if err := json.Unmarshal(data, m); nil != err {
|
|
fmt.Println(err.Error())
|
|
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)]
|
|
}
|
|
|
|
func (s *SensorConfigService) sortSensorConfigPerContainer() {
|
|
if nil == s.sensorConfigs || 0 == len(s.sensorConfigs) {
|
|
return
|
|
}
|
|
|
|
s.sensorConfigsPerContainer = nil
|
|
s.sensorConfigsPerContainer = make(map[occp.ContainerType][]*ocmsc.SensorConfig)
|
|
|
|
//for _, sensorConfig := range s.sensorConfigs {
|
|
// containerType := occp.ToContainerType(sensorConfig.Crawler.Container)
|
|
// s.sensorConfigsPerContainer[containerType] = append(s.sensorConfigsPerContainer[containerType], sensorConfig)
|
|
//}
|
|
}
|