commit 8ecf62ec30a626128e1c428e3add3475bfa5071d Author: jackdaw@loafle.com Date: Mon Apr 10 18:03:31 2017 +0900 . diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e2ebe9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,61 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +### Go template +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ + diff --git a/crawler.go b/crawler.go new file mode 100644 index 0000000..4d16a87 --- /dev/null +++ b/crawler.go @@ -0,0 +1,66 @@ +package crawler + +import ( + "loafle.com/overflow/of_rpc/models/param" +) + +type Internal interface { + Internal(params map[string]interface{}) (param.Output, error) +} + +type Crawler interface { + Init(path string, result *param.Output) error + Add(id string, result *param.Output) error + Remove(id string, result *param.Output) error + Get(id string, result *param.Output) error +} + +type CrawlerImpl struct { + configs map[string]interface{} + internal Internal +} + +func (c *CrawlerImpl) Init(path string, result *param.Output) error { + if c.configs == nil { + c.configs = make(map[string]interface{}, 0) + } + + // load all file in path + return nil +} +func (c *CrawlerImpl) Add(id string, result *param.Output) error { + + //file load , input config + return nil +} +func (c *CrawlerImpl) Remove(id string, result *param.Output) error { + //remove in config + return nil +} +func (c *CrawlerImpl) Get(id string, result *param.Output) error { + + if c.internal != nil { + out, err := c.internal.Internal(c.GetConfig(id)) + if err != nil { + //set error fail, message + } + + *result = out + } + + return nil +} + +// internal methods +func (c *CrawlerImpl) GetConfig(id string) map[string]interface{} { + return c.configs[id].(map[string]interface{}) +} +func (c *CrawlerImpl) SetInternal(i Internal) { + c.internal = i +} +func (c *CrawlerImpl) PutConfig(name string, m map[string]interface{}) { + if c.configs == nil { + c.configs = make(map[string]interface{}, 0) + } + c.configs[name] = m +} diff --git a/crawler_generalization_test.go b/crawler_generalization_test.go new file mode 100644 index 0000000..5c594ed --- /dev/null +++ b/crawler_generalization_test.go @@ -0,0 +1,47 @@ +package crawler + +import ( + "fmt" + "testing" +) + +type Super interface { + Method() +} + +type sss struct{} + +func (s *sss) Method() { + fmt.Println("Super") +} + +type Child struct { + sss +} + +func (c *Child) Method() { + c.sss.Method() + fmt.Println("Child") +} + +func TestGene(t *testing.T) { + c := Child{} + c.Method() + + var s Super = &Child{} + s.Method() + +} + +func TestExistInterface(t *testing.T) { + + m := make(map[string]Super, 0) + m["test"] = &Child{} + + _, ok := m["test1"] + if ok { + fmt.Println(ok) + } else { + fmt.Println(ok) + } +} diff --git a/crawler_map_convert_test.go b/crawler_map_convert_test.go new file mode 100644 index 0000000..adf4b26 --- /dev/null +++ b/crawler_map_convert_test.go @@ -0,0 +1,19 @@ +package crawler + +import ( + "fmt" + "testing" +) + +func TestInterfaceValueToTypeCast(t *testing.T) { + + m := make(map[string]interface{}) + + m["a"] = "1" + m["b"] = 2 + m["c"] = false + + fmt.Println(m["a"].(string)) + fmt.Println(m["b"].(int)) + fmt.Println(m["c"].(bool)) +} diff --git a/crawler_test.go b/crawler_test.go new file mode 100644 index 0000000..0a99987 --- /dev/null +++ b/crawler_test.go @@ -0,0 +1,34 @@ +package crawler + +import ( + "encoding/json" + "github.com/stretchr/testify/assert" + "loafle.com/overflow/of_rpc/models/param" + "testing" +) + +func TestCrawler(t *testing.T) { + + r := NewRedisHeahthCrawler() + + // test config + + m := make(map[string]interface{}, 0) + + r.configs = make(map[string]interface{}, 0) + m["ip"] = "192.168.1.104" + m["port"] = "6379" + m["portType"] = "tcp" + m["ssl"] = false + + r.configs["redis"] = m + + out := param.Output{} + r.Get("redis", &out) + + var check bool + json.Unmarshal(out.Data, &check) + + assert.Equal(t, true, check) + +} diff --git a/socket_health_crawler.go b/socket_health_crawler.go new file mode 100644 index 0000000..290b97b --- /dev/null +++ b/socket_health_crawler.go @@ -0,0 +1,81 @@ +package crawler + +import ( + "crypto/tls" + "loafle.com/overflow/collector/core/scan/service/matcher" + "loafle.com/overflow/collector/core/scan/service/matcher/packet" + "net" +) + +type SocketHeahthCrawler struct { + CrawlerImpl +} + +func (s *SocketHeahthCrawler) getConnection(params map[string]interface{}) (net.Conn, error) { + + ip := params["ip"].(string) + port := params["port"].(string) + portType := params["portType"].(string) + ssl := params["ssl"].(bool) + + var addr string = ip + addr += ":" + addr += 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, + }, + ) + if err != nil { + return nil, err + } + return conn, nil + } +} + +func (s *SocketHeahthCrawler) CheckHeahth(params map[string]interface{}, mat matcher.Matcher) (bool, error) { + + conn, err := s.getConnection(params) + if err != nil { + return false, err + } + defer conn.Close() + + if mat.IsPrePacket() == true { + bytes := make([]byte, 1024) + n, _ := conn.Read(bytes) + p := packet.NewPacket(bytes, n) + if mat.Match(0, p, nil) == false { + return false, nil + } + } + + for i := 0; i < mat.PacketCount(); i++ { + pack := mat.Packet(i) + conn.Write(pack.Buffer) + bytes := make([]byte, 1024) + n, _ := conn.Read(bytes) + + if mat.IsNoResponse(i) == true { // empty last response + break + } + + p := packet.NewPacket(bytes, n) + if mat.Match(i, p, nil) == false { + return false, nil + } + } + + return true, nil +}