From f1900219c0ab86c57e6265ca11627a2024701fb5 Mon Sep 17 00:00:00 2001 From: "jackdaw@loafle.com" Date: Wed, 12 Apr 2017 20:14:26 +0900 Subject: [PATCH] of_rpc => grpc --- network_crawler.go | 128 ++++++++++++++++++------- network_crawler_test.go | 208 ++++++++++++++++++++++++---------------- 2 files changed, 222 insertions(+), 114 deletions(-) diff --git a/network_crawler.go b/network_crawler.go index c2fda35..b1612bf 100644 --- a/network_crawler.go +++ b/network_crawler.go @@ -1,8 +1,15 @@ package main import ( - "loafle.com/overflow/activedirectory_protocol_crawler_go" + "flag" + "golang.org/x/net/context" + "google.golang.org/grpc" "loafle.com/overflow/crawler_go" + pb "loafle.com/overflow/crawler_go/grpc" + "log" + "net" + "loafle.com/overflow/redis_protocol_crawler_go" + "loafle.com/overflow/activedirectory_protocol_crawler_go" "loafle.com/overflow/dns_protocol_crawler_go" "loafle.com/overflow/ftp_protocol_crawler_go" "loafle.com/overflow/ftps_protocol_crawler_go" @@ -11,12 +18,10 @@ import ( "loafle.com/overflow/mongodb_protocol_crawler_go" "loafle.com/overflow/mssql_protocol_crawler_go" "loafle.com/overflow/mysql_protocol_crawler_go" - "loafle.com/overflow/netbios_protocol_crawler_go" - "loafle.com/overflow/of_rpc_go" - "loafle.com/overflow/oracle_protocol_crawler_go" "loafle.com/overflow/pgsql_protocol_crawler_go" + "loafle.com/overflow/netbios_protocol_crawler_go" + "loafle.com/overflow/oracle_protocol_crawler_go" "loafle.com/overflow/pop3_protocol_crawler_go" - "loafle.com/overflow/redis_protocol_crawler_go" "loafle.com/overflow/rmi_protocol_crawler_go" "loafle.com/overflow/smb_protocol_crawler_go" "loafle.com/overflow/smtp_protocol_crawler_go" @@ -25,43 +30,98 @@ import ( "loafle.com/overflow/ssh_protocol_crawler_go" "loafle.com/overflow/telnet_protocol_crawler_go" "loafle.com/overflow/wmi_protocol_crawler_go" - "log" ) -func initCrawlers() { - of_rpc.AddDelegate(crawler.ACTIVEDIRECTORY.String(), activedirectory_protocol_crawler_go.NewActiveDirectoryHealthCrawler()) - of_rpc.AddDelegate(crawler.DNS.String(), dns_protocol_crawler_go.NewDNSHealthCrawler()) - of_rpc.AddDelegate(crawler.FTP.String(), ftp_protocol_crawler_go.NewFTPHealthCrawler()) - of_rpc.AddDelegate(crawler.FTPS.String(), ftps_protocol_crawler_go.NewFTPSHealthCrawler()) - of_rpc.AddDelegate(crawler.IMAP.String(), imap_protocol_crawler_go.NewIMAPHealthCrawler()) - of_rpc.AddDelegate(crawler.LDAP.String(), ldap_protocol_crawler_go.NewLDAPHealthCrawler()) - of_rpc.AddDelegate(crawler.MONGODB.String(), mongodb_protocol_crawler_go.NewMongoDBHealthCrawler()) - of_rpc.AddDelegate(crawler.MSSQL.String(), mssql_protocol_crawler_go.NewMSSqlHeahthCrawler()) - of_rpc.AddDelegate(crawler.MYSQL.String(), mysql_protocol_crawler_go.NewMysqlHeahthCrawler()) - of_rpc.AddDelegate(crawler.MARIADB.String(), mysql_protocol_crawler_go.NewMysqlHeahthCrawler()) - of_rpc.AddDelegate(crawler.PGSQL.String(), pgsql_protocol_crawler_go.NewPGSqlHeahthCrawler()) - of_rpc.AddDelegate(crawler.NETBIOS.String(), netbios_protocol_crawler_go.NewNetbiosHeahthCrawler()) - of_rpc.AddDelegate(crawler.ORACLE.String(), oracle_protocol_crawler_go.NewOracleHeahthCrawler()) - of_rpc.AddDelegate(crawler.POP3.String(), pop3_protocol_crawler_go.NewPOP3HeahthCrawler()) - of_rpc.AddDelegate(crawler.REDIS.String(), redis_protocol_crawler_go.NewRedisHeahthCrawler()) - of_rpc.AddDelegate(crawler.RMI.String(), rmi_protocol_crawler_go.NewRMIHeahthCrawler()) - of_rpc.AddDelegate(crawler.SMB.String(), smb_protocol_crawler_go.NewSMBHeahthCrawler()) - of_rpc.AddDelegate(crawler.SMTP.String(), smtp_protocol_crawler_go.NewSMTPHeahthCrawler()) - of_rpc.AddDelegate(crawler.SNMPV2C.String(), snmpv2c_protocol_crawler_go.NewSNMPV2CHeahthCrawler()) - of_rpc.AddDelegate(crawler.SNMPV3.String(), snmpv3_protocol_crawler_go.NewSNMPV3HeahthCrawler()) - of_rpc.AddDelegate(crawler.SSH.String(), ssh_protocol_crawler_go.NewSSHHeahthCrawler()) - of_rpc.AddDelegate(crawler.TELNET.String(), telnet_protocol_crawler_go.NewTelnetHeahthCrawler()) - of_rpc.AddDelegate(crawler.WMI.String(), wmi_protocol_crawler_go.NewWMIHeahthCrawler()) +var g_crawlers map[string]crawler.Crawler + +func AddDelegate(name string, c crawler.Crawler) { + if g_crawlers == nil { + g_crawlers = make(map[string]crawler.Crawler, 0) + } + g_crawlers[name] = c } -func start() { - err := of_rpc.StartJSONRPC() +type ConfigServer struct { +} + +func (s *ConfigServer) Add(c context.Context, in *pb.Input) (*pb.Output, error) { + return nil, nil +} +func (s *ConfigServer) Remove(c context.Context, in *pb.Input) (*pb.Output, error) { + return nil, nil +} +func (s *ConfigServer) Init(c context.Context, in *pb.InputArray) (*pb.Output, error) { + return nil, nil +} + +type DataServer struct { +} + +func (s *DataServer) Get(c context.Context, in *pb.Input) (*pb.Output, error) { + + output := &pb.Output{} + + if c, ok := g_crawlers[in.Name.String()]; ok { + rd,err := c.Get(in.Id) + if err != nil { + // process error + } + output.Data = rd + } else { + output.Data = []byte("Not Assign Crawler") + } + return output, nil +} + + +func initCrawlers() { + g_crawlers = make(map[string]crawler.Crawler,0) + + AddDelegate(pb.Crawlers_HEALTH_ACTIVEDIRECTORY.String(), activedirectory_protocol_crawler_go.NewActiveDirectoryHealthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_DNS.String(), dns_protocol_crawler_go.NewDNSHealthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_FTP.String(), ftp_protocol_crawler_go.NewFTPHealthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_FTPS.String(), ftps_protocol_crawler_go.NewFTPSHealthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_IMAP.String(), imap_protocol_crawler_go.NewIMAPHealthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_LDAP.String(), ldap_protocol_crawler_go.NewLDAPHealthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_MONGODB.String(), mongodb_protocol_crawler_go.NewMongoDBHealthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_MSSQL.String(), mssql_protocol_crawler_go.NewMSSqlHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_MYSQL.String(), mysql_protocol_crawler_go.NewMysqlHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_MARIADB.String(), mysql_protocol_crawler_go.NewMysqlHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_PGSQL.String(), pgsql_protocol_crawler_go.NewPGSqlHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_NETBIOS.String(), netbios_protocol_crawler_go.NewNetbiosHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_ORACLE.String(), oracle_protocol_crawler_go.NewOracleHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_POP3.String(), pop3_protocol_crawler_go.NewPOP3HeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_REDIS.String(), redis_protocol_crawler_go.NewRedisHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_RMI.String(), rmi_protocol_crawler_go.NewRMIHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_SMB.String(), smb_protocol_crawler_go.NewSMBHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_SMTP.String(), smtp_protocol_crawler_go.NewSMTPHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_SNMPV2C.String(), snmpv2c_protocol_crawler_go.NewSNMPV2CHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_SNMPV3.String(), snmpv3_protocol_crawler_go.NewSNMPV3HeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_SSH.String(), ssh_protocol_crawler_go.NewSSHHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_TELNET.String(), telnet_protocol_crawler_go.NewTelnetHeahthCrawler()) + AddDelegate(pb.Crawlers_HEALTH_WMI.String(), wmi_protocol_crawler_go.NewWMIHeahthCrawler()) +} + +func start(rc pb.ConfigServer,rd pb.DataServer) { + + port := flag.String("Port", "50000", "RPC Port Default 50000") + flag.Parse() + address := "localhost:" + *port + + log.Println("address : " + address) + lis, err := net.Listen("tcp", address) if err != nil { - log.Fatal(err) + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + pb.RegisterConfigServer(s,rc) + pb.RegisterDataServer(s,rd) + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) } } func main() { initCrawlers() - start() + start(&ConfigServer{},&DataServer{}) } diff --git a/network_crawler_test.go b/network_crawler_test.go index 3dff184..ea00a9f 100644 --- a/network_crawler_test.go +++ b/network_crawler_test.go @@ -1,97 +1,145 @@ package main import ( - "encoding/json" + "log" "github.com/stretchr/testify/assert" - "loafle.com/overflow/crawler_go" - "loafle.com/overflow/of_rpc_go" - "loafle.com/overflow/of_rpc_go/client" - "loafle.com/overflow/of_rpc_go/models/param" - "loafle.com/overflow/redis_protocol_crawler_go" + "golang.org/x/net/context" + "google.golang.org/grpc" + pb "loafle.com/overflow/crawler_go/grpc" "testing" + "encoding/json" + "loafle.com/overflow/redis_protocol_crawler_go" "time" - "loafle.com/overflow/ftps_protocol_crawler_go" - "loafle.com/overflow/imap_protocol_crawler_go" + "loafle.com/overflow/crawler_go" ) -type configcall func() -////////////////////////////////////// redis rpc test start --> -func TestRedis(t *testing.T) { - go initTestServer(func() { - c := of_rpc.GetCrawler(crawler.REDIS.String()).(*redis_protocol_crawler_go.RedisHeahthCrawler) + +type call func() crawler.Crawler + +func initRPCServer(cc call) { + rr:=cc() + AddDelegate(pb.Crawlers_HEALTH_REDIS.String(),rr) + go start(&ConfigServer{},&DataServer{}) +} + +func clientCall(t *testing.T,cl pb.Crawlers) { + time.Sleep(2 * time.Second) + conn, err := grpc.Dial("localhost:50000", grpc.WithInsecure()) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + c := pb.NewDataClient(conn) + + in := &pb.Input{ + Name: cl, + Id:"test_redis_sid", + } + + out, err := c.Get(context.Background(), in) + if err != nil { + log.Fatalf("could not greet: %v", err) + } + + var check bool + json.Unmarshal(out.Data, &check) + assert.Equal(t, true, check) +} + + +func TestRedisGRPC(t *testing.T) { + go initRPCServer( func() crawler.Crawler { + rr := redis_protocol_crawler_go.NewRedisHeahthCrawler() m := make(map[string]interface{}, 0) m["ip"] = "192.168.1.104" m["port"] = "6379" m["portType"] = "tcp" m["ssl"] = false - c.PutConfig("test_redis_sid", m) + rr.PutConfig("test_redis_sid", m) + return rr }) - time.Sleep(2 * time.Second) - in := param.Input{ - Name: crawler.REDIS.String(), - Id: "test_redis_sid", - } - checkhealth(t, in) -} -////////////////////////////////////// redis rpc test end <-- - -////////////////////////////////////// imap rpc test start --> -func TestImap(t *testing.T) { - go initTestServer(func() { - c := of_rpc.GetCrawler(crawler.IMAP.String()).(*imap_protocol_crawler_go.IMAPHealthCrawler) - m := make(map[string]interface{}, 0) - m["ip"] = "192.168.1.215" - m["port"] = "993" - m["portType"] = "tcp" - m["ssl"] = true - c.PutConfig("test_imap_sid", m) - }) - time.Sleep(2 * time.Second) - in := param.Input{ - Name: crawler.IMAP.String(), - Id: "test_imap_sid", - } - checkhealth(t, in) -} -////////////////////////////////////// imap rpc test end <-- - -////////////////////////////////////// ftp rpc test start --> -func TestFTP(t *testing.T) { - go initTestServer(func() { - c := of_rpc.GetCrawler(crawler.FTPS.String()).(*ftps_protocol_crawler_go.FTPSHealthCrawler) - m := make(map[string]interface{}, 0) - m["ip"] = "192.168.1.103" - m["port"] = "2121" - m["portType"] = "tcp" - m["ssl"] = false - c.PutConfig("test_ftps_sid", m) - }) - time.Sleep(2 * time.Second) - in := param.Input{ - Name: crawler.FTPS.String(), - Id: "test_ftps_sid", - } - checkhealth(t, in) -} -////////////////////////////////////// ftp rpc test end <-- - - - -func initTestServer(config configcall) { - initCrawlers() - config() - start() + clientCall(t,pb.Crawlers_HEALTH_REDIS) } -func checkhealth(t *testing.T, in param.Input) { - 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) -} +//type configcall func() +// +//////////////////////////////////////// redis rpc test start --> +//func TestRedis(t *testing.T) { +// go initTestServer(func() { +// c := of_rpc.GetCrawler(crawler.REDIS.String()).(*redis_protocol_crawler_go.RedisHeahthCrawler) +// m := make(map[string]interface{}, 0) +// m["ip"] = "192.168.1.104" +// m["port"] = "6379" +// m["portType"] = "tcp" +// m["ssl"] = false +// c.PutConfig("test_redis_sid", m) +// }) +// time.Sleep(2 * time.Second) +// in := param.Input{ +// Name: crawler.REDIS.String(), +// Id: "test_redis_sid", +// } +// checkhealth(t, in) +//} +//////////////////////////////////////// redis rpc test end <-- +// +//////////////////////////////////////// imap rpc test start --> +//func TestImap(t *testing.T) { +// go initTestServer(func() { +// c := of_rpc.GetCrawler(crawler.IMAP.String()).(*imap_protocol_crawler_go.IMAPHealthCrawler) +// m := make(map[string]interface{}, 0) +// m["ip"] = "192.168.1.215" +// m["port"] = "993" +// m["portType"] = "tcp" +// m["ssl"] = true +// c.PutConfig("test_imap_sid", m) +// }) +// time.Sleep(2 * time.Second) +// in := param.Input{ +// Name: crawler.IMAP.String(), +// Id: "test_imap_sid", +// } +// checkhealth(t, in) +//} +//////////////////////////////////////// imap rpc test end <-- +// +//////////////////////////////////////// ftp rpc test start --> +//func TestFTP(t *testing.T) { +// go initTestServer(func() { +// c := of_rpc.GetCrawler(crawler.FTPS.String()).(*ftps_protocol_crawler_go.FTPSHealthCrawler) +// m := make(map[string]interface{}, 0) +// m["ip"] = "192.168.1.103" +// m["port"] = "2121" +// m["portType"] = "tcp" +// m["ssl"] = false +// c.PutConfig("test_ftps_sid", m) +// }) +// time.Sleep(2 * time.Second) +// in := param.Input{ +// Name: crawler.FTPS.String(), +// Id: "test_ftps_sid", +// } +// checkhealth(t, in) +//} +//////////////////////////////////////// ftp rpc test end <-- +// +// +// +//func initTestServer(config configcall) { +// initCrawlers() +// config() +// start() +//} +// +//func checkhealth(t *testing.T, in param.Input) { +// 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) +//}