diff --git a/crawler/crawler.go b/crawler/crawler.go new file mode 100644 index 0000000..4f5bd96 --- /dev/null +++ b/crawler/crawler.go @@ -0,0 +1,68 @@ +package crawler + +import ( + configM "git.loafle.net/overflow/overflow_commons_go/modules/config/model" +) + +type Crawler interface { + Init(config *configM.Config) (bool, error) + Add(config *configM.Config) (bool, error) + Remove(id string) (bool, error) + Get(id string) (map[string]string, error) + + Name() string +} + +type InternalCrawler interface { + InternalGet(config *configM.Config) (map[string]string, error) +} + +type Crawlers struct { + configs map[string]*configM.Config + + Internal InternalCrawler +} + +func (c *Crawlers) GetConfig(id string) *configM.Config { + if nil == c.configs { + return nil + } + return c.configs[id] +} + +func (c *Crawlers) RemoveConfig(id string) { + if nil == c.configs { + return + } + delete(c.configs, id) +} + +func (c *Crawlers) PutConfig(id string, config *configM.Config) { + if nil == c.configs { + c.configs = make(map[string]*configM.Config, 0) + } + c.configs[id] = config +} + +func (c *Crawlers) Init(config *configM.Config) (bool, error) { + c.PutConfig(config.ID.String(), config) + return true, nil +} + +func (c *Crawlers) Add(config *configM.Config) (bool, error) { + c.PutConfig(config.ID.String(), config) + return true, nil +} + +func (c *Crawlers) Remove(id string) (bool, error) { + c.RemoveConfig(id) + return true, nil +} + +func (c *Crawlers) Get(id string) (map[string]string, error) { + rss, err := c.Internal.InternalGet(c.GetConfig(id)) + if nil != err { + return nil, err + } + return rss, nil +} diff --git a/crawler/impl/health/SocketHeahthCrawler.go b/crawler/impl/health/SocketHeahthCrawler.go new file mode 100644 index 0000000..399bbd9 --- /dev/null +++ b/crawler/impl/health/SocketHeahthCrawler.go @@ -0,0 +1,125 @@ +package health + +import ( + "crypto/tls" + "fmt" + "net" + "time" + + cnsm "git.loafle.net/commons_go/network_service_matcher" + cuej "git.loafle.net/commons_go/util/encoding/json" + configM "git.loafle.net/overflow/overflow_commons_go/modules/config/model" + "git.loafle.net/overflow/overflow_probe_container_network/crawler" +) + +type SocketHeahthCrawler struct { + crawler.Crawlers + m cnsm.Matcher +} + +func (s *SocketHeahthCrawler) SetMatcher(m cnsm.Matcher) { + s.m = m +} + +func (s *SocketHeahthCrawler) getConnection(config *configM.Config) (net.Conn, error) { + + connection := config.Target.Connection + + ip := connection.IP + port := connection.Port + portType := connection.PortType + ssl := connection.SSL + + addr := fmt.Sprintf("%s:%s", ip, port) + + if ssl == false { + conn, err := net.Dial(portType, addr) + if err != nil { + return nil, err + } + return conn, nil + } else { + conn, err := tls.Dial( + portType, + addr, + &tls.Config{ + InsecureSkipVerify: true, + ServerName: ip, + ClientAuth: tls.RequestClientCert, + }, + ) + if err != nil { + return nil, err + } + return conn, nil + } +} + +func (s *SocketHeahthCrawler) CheckHeahth(config *configM.Config) (result map[string]string, err error) { + result = make(map[string]string, 0) + + result["StartTime"] = time.Now().String() + + conn, cErr := s.getConnection(config) + if cErr != nil { + result["Error"] = cErr.Error() + err = cErr + return + } + defer conn.Close() + + connection := config.Target.Connection + port, _ := cuej.NumberToInt(connection.Port) + info := cnsm.NewMatchInfo(connection.IP, port) + + if s.m.IsPrePacket() == true { + bytes := make([]byte, 1024) + n, _ := conn.Read(bytes) + p := cnsm.NewPacket(bytes, n) + if s.m.Match(info, 0, p) == false { + result["Error"] = "Not Matched" + return + } else { + + for i := 0; i < s.m.PacketCount(); i++ { + pack := s.m.Packet(i) + conn.Write(pack.Buffer) + bytes := make([]byte, 1024) + n, _ := conn.Read(bytes) + + if !s.m.HasResponse(i + 1) { // empty last response + break + } + + p := cnsm.NewPacket(bytes, n) + if s.m.Match(info, i+1, p) == false { + result["Error"] = "Not Matched" + return + } + } + + } + + } else { + for i := 0; i < s.m.PacketCount(); i++ { + pack := s.m.Packet(i) + conn.Write(pack.Buffer) + bytes := make([]byte, 1024) + n, _ := conn.Read(bytes) + + if !s.m.HasResponse(i) { // empty last response + break + } + + p := cnsm.NewPacket(bytes, n) + if s.m.Match(info, i, p) == false { + result["Error"] = "Not Matched" + return + } + } + } + + result["EndTime"] = time.Now().String() + + return +} diff --git a/crawler/impl/health/activedirectory/ActiveDirectoryHealthCrawler.go b/crawler/impl/health/activedirectory/ActiveDirectoryHealthCrawler.go new file mode 100644 index 0000000..a9d3e32 --- /dev/null +++ b/crawler/impl/health/activedirectory/ActiveDirectoryHealthCrawler.go @@ -0,0 +1,32 @@ +package activedirectory + +import ( + cnsma "git.loafle.net/commons_go/network_service_matcher/activedirectory" + configM "git.loafle.net/overflow/overflow_commons_go/modules/config/model" + "git.loafle.net/overflow/overflow_probe_container_network/crawler" + "git.loafle.net/overflow/overflow_probe_container_network/crawler/impl/health" +) + +type ActiveDirectoryHealthCrawler struct { + health.SocketHeahthCrawler +} + +func (r *ActiveDirectoryHealthCrawler) Name() string { + return "ACTIVEDIRECTORY_HEALTH_CRAWLER" +} + +func (r *ActiveDirectoryHealthCrawler) InternalGet(config *configM.Config) (map[string]string, error) { + + rss, err := r.CheckHeahth(config) + if err != nil { + return nil, err + } + return rss, nil +} + +func NewCrawler() crawler.Crawler { + ad := &ActiveDirectoryHealthCrawler{} + ad.Internal = ad + ad.SetMatcher(cnsma.NewMatcher()) + return ad +} diff --git a/crawler/impl/health/activedirectory/ActiveDirectoryHealthCrawler_test.go b/crawler/impl/health/activedirectory/ActiveDirectoryHealthCrawler_test.go new file mode 100644 index 0000000..860d11d --- /dev/null +++ b/crawler/impl/health/activedirectory/ActiveDirectoryHealthCrawler_test.go @@ -0,0 +1,69 @@ +package activedirectory + +import ( + "encoding/json" + "log" + "testing" + "time" + + "git.loafle.net/overflow/overflow_probe_container_network/crawler" +) + +func setConfig() crawler.Crawler { + + r := NewCrawler().* + + m := make(map[string]interface{}, 0) + + m["ip"] = "192.168.1.1" + m["port"] = "389" + m["portType"] = "tcp" + m["ssl"] = false + + r.PutConfig("ad", m) + + return r +} + +func TestMatch(t *testing.T) { + // test config + r := setConfig() + + out := param.Output{} + r.Get("ad", &out) + + var check bool + json.Unmarshal(out.Data, &check) + + assert.Equal(t, true, check) +} + +func start() { + r := setConfig() + + of_rpc.AddDelegate("ad", r) + err := of_rpc.StartJSONRPC() + if err != nil { + log.Fatal(err) + } +} + +func TestRPC(t *testing.T) { + go start() + time.Sleep(2 * time.Second) + + in := param.Input{ + Name: "ad", + Id: "ad", + } + + result, err := client.InvokeJSONRPCGet("50000", in) + + if err != nil { + t.Fatal(err) + } + + var check bool + json.Unmarshal(result.Data, &check) + assert.Equal(t, true, check) +} diff --git a/service/ConfigService.go b/service/ConfigService.go index 70f1a85..58c2c51 100644 --- a/service/ConfigService.go +++ b/service/ConfigService.go @@ -1,5 +1,9 @@ package service +import ( + configM "git.loafle.net/overflow/overflow_commons_go/modules/config/model" +) + type ConfigService struct { } @@ -11,6 +15,12 @@ func (cs *ConfigService) Remove() { } -func (cs *ConfigService) Init() { +func (cs *ConfigService) Init(configs []map[string]configM.Config) bool { + for _, item := range configs { + for k, v := range item { + + } + } + return true }