This commit is contained in:
jackdaw@loafle.com 2017-04-10 18:03:31 +09:00
commit 8ecf62ec30
6 changed files with 308 additions and 0 deletions

61
.gitignore vendored Normal file
View File

@ -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/

66
crawler.go Normal file
View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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))
}

34
crawler_test.go Normal file
View File

@ -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)
}

81
socket_health_crawler.go Normal file
View File

@ -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
}