From 5089fc2478b1dc6d4d33062f8bb94d08fba8eed5 Mon Sep 17 00:00:00 2001 From: geek Date: Wed, 4 Jul 2018 18:31:45 +0900 Subject: [PATCH] ing --- Gopkg.lock | 12 +--- crawler/health/SocketHeahthCrawler.go | 84 ++++++++++++++++++++++---- crawler/health/ssh/SSHHealthCrawler.go | 13 +++- 3 files changed, 88 insertions(+), 21 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index b7c6120..40e4d7f 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -93,7 +93,7 @@ "time/scheduler/storage", "time/scheduler/task" ] - revision = "4d4017d214d2a8fdde59d774254f421991fabe7e" + revision = "dd4f956c587b1947f84ad5eebd64722b3d6c5224" [[projects]] branch = "master" @@ -111,7 +111,7 @@ "model/sensorconfig", "service/probe" ] - revision = "904de1632643ddb9c8862cd5bf147570c1a59878" + revision = "e2c0324eb632822050f3d0379ddd1a13c2aac2b1" [[projects]] branch = "master" @@ -169,12 +169,6 @@ revision = "792786c7400a136282c1664665ae0a8db921c6c2" version = "v1.0.0" -[[projects]] - name = "github.com/satori/go.uuid" - packages = ["."] - revision = "f58768cc1a7a7e77a3bd49e98cdd21419399b6a3" - version = "v1.2.0" - [[projects]] name = "github.com/stretchr/testify" packages = ["assert"] @@ -238,6 +232,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "864e2859063e93659cad5271506583b83051b8b1dc7c605b1a92f0c0e4b5efc4" + inputs-digest = "1df87b3131b7fea1a3a94e05b70b99d3c4f2b97eafb773486610e5f83be45a6a" solver-name = "gps-cdcl" solver-version = 1 diff --git a/crawler/health/SocketHeahthCrawler.go b/crawler/health/SocketHeahthCrawler.go index 630bb4c..6cbaaf0 100644 --- a/crawler/health/SocketHeahthCrawler.go +++ b/crawler/health/SocketHeahthCrawler.go @@ -21,7 +21,7 @@ func (s *SocketHealthCrawler) SetMatcher(m cnsm.Matcher) { s.m = m } -func (s *SocketHealthCrawler) getConnection(config *ocmsc.SensorConfig) (net.Conn, error) { +func (s *SocketHealthCrawler) GetConnection(config *ocmsc.SensorConfig) (net.Conn, error) { connection := config.Connection metaIPTypeKey := connection.MetaIPTypeKey ip := connection.IP @@ -73,16 +73,17 @@ func (s *SocketHealthCrawler) getConnection(config *ocmsc.SensorConfig) (net.Con return conn, nil } -func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (result map[string]string, err error) { - result = make(map[string]string, 0) +// Duplication Method +func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (map[string]string, error) { + result := make(map[string]string, 0) + + conn, cErr := s.GetConnection(config) + defer conn.Close() - conn, cErr := s.getConnection(config) if cErr != nil { result["Error"] = cErr.Error() - err = cErr - return + return result, cErr } - defer conn.Close() connection := config.Connection port, _ := cuej.NumberToInt(connection.Port) @@ -97,7 +98,7 @@ func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (result ma if !s.m.Match(info, 0, p) { result["Packet"] = convertBase64(buf) result["Error"] = "Not Matched" - return + return result, nil } for i := 0; i < s.m.PacketCount(); i++ { @@ -114,7 +115,7 @@ func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (result ma if s.m.Match(info, i+1, p) == false { result["Packet"] = convertBase64(buf) result["Error"] = "Not Matched" - return + return result, nil } } @@ -135,12 +136,73 @@ func (s *SocketHealthCrawler) CheckHealth(config *ocmsc.SensorConfig) (result ma if s.m.Match(info, i, p) == false { result["Packet"] = convertBase64(buf) result["Error"] = "Not Matched" - return + return result, nil } } } - return + return nil, nil +} + +func (s *SocketHealthCrawler) CheckHealth1(config *ocmsc.SensorConfig, conn net.Conn, result map[string]string) (map[string]string, error) { + + connection := config.Connection + port, _ := cuej.NumberToInt(connection.Port) + info := cnsm.NewMatchInfo(connection.IP, port) + + if s.m.IsPrePacket() { + result["PacketType"] = "Pre" + + buf := make([]byte, 1024) + n, _ := conn.Read(buf) + p := cnsm.NewPacket(buf, n) + if !s.m.Match(info, 0, p) { + result["Packet"] = convertBase64(buf) + result["Error"] = "Not Matched" + return result, nil + } + + for i := 0; i < s.m.PacketCount(); i++ { + pack := s.m.Packet(i) + conn.Write(pack.Buffer) + buf := make([]byte, 1024) + n, _ := conn.Read(buf) + + if !s.m.HasResponse(i + 1) { // empty last response + break + } + + p := cnsm.NewPacket(buf, n) + if s.m.Match(info, i+1, p) == false { + result["Packet"] = convertBase64(buf) + result["Error"] = "Not Matched" + return result, nil + } + } + + } else { + result["PacketType"] = "Post" + + for i := 0; i < s.m.PacketCount(); i++ { + pack := s.m.Packet(i) + conn.Write(pack.Buffer) + buf := make([]byte, 1024) + n, _ := conn.Read(buf) + + if !s.m.HasResponse(i) { // empty last response + break + } + + p := cnsm.NewPacket(buf, n) + if s.m.Match(info, i, p) == false { + result["Packet"] = convertBase64(buf) + result["Error"] = "Not Matched" + return result, nil + } + } + } + + return nil, nil } func convertBase64(buf []byte) string { diff --git a/crawler/health/ssh/SSHHealthCrawler.go b/crawler/health/ssh/SSHHealthCrawler.go index a60b5e6..708472d 100644 --- a/crawler/health/ssh/SSHHealthCrawler.go +++ b/crawler/health/ssh/SSHHealthCrawler.go @@ -24,7 +24,18 @@ func (c *SSHHealthCrawler) Auth(auth map[string]interface{}) error { } func (c *SSHHealthCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, error) { - rss, err := c.CheckHealth(config) + result := make(map[string]string, 0) + + conn, cErr := c.GetConnection(config) + defer conn.Close() + + if cErr != nil { + result["Error"] = cErr.Error() + return result, cErr + } + + + rss, err := c.CheckHealth1(config, conn, result) if err != nil { return nil, err }