main.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/grpc-ecosystem/go-grpc-prometheus"
  6. "github.com/importcjj/sensitive"
  7. "github.com/prometheus/client_golang/prometheus"
  8. "github.com/prometheus/client_golang/prometheus/promhttp"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "google.golang.org/grpc"
  11. "gopkg.in/olivere/elastic.v1"
  12. "gopkg.in/yaml.v2"
  13. "io/ioutil"
  14. "log"
  15. "math/big"
  16. "net"
  17. "net/http"
  18. "sensitiveWords.udp/proto_grpc"
  19. "sensitiveWords.udp/util"
  20. "strconv"
  21. "strings"
  22. )
  23. const (
  24. YAMLFILE = "./server.yaml"
  25. )
  26. var YamlConfig YAMLConfig
  27. var MixDataMgo *util.MongodbSim
  28. var Filter *sensitive.Filter
  29. var es_type, es_index string
  30. var Client_Es *elastic.Client
  31. func init() {
  32. yamlFile, err := ioutil.ReadFile(YAMLFILE)
  33. if err != nil {
  34. log.Fatalln("load conf error")
  35. }
  36. err = yaml.Unmarshal(yamlFile, &YamlConfig)
  37. if err != nil {
  38. fmt.Println(err.Error())
  39. }
  40. log.Printf("%#v", YamlConfig)
  41. MixDataMgo = &util.MongodbSim{
  42. MongodbAddr: YamlConfig.MixdataMgoAddr,
  43. Size: YamlConfig.MongodbPoolSize,
  44. DbName: YamlConfig.DbName,
  45. UserName: YamlConfig.UserName,
  46. PassWord: YamlConfig.PassWord,
  47. }
  48. MixDataMgo.InitPool()
  49. Client_Es, _ = elastic.NewClient(http.DefaultClient, "http://192.168.3.11:9800")
  50. es_type, es_index = "azktest", "azktest"
  51. reg.MustRegister(grpcMetrics, customizedCounterMetric)
  52. }
  53. var (
  54. // Create a metrics registry.
  55. reg = prometheus.NewRegistry()
  56. // Create some standard server metrics.
  57. grpcMetrics = grpc_prometheus.NewServerMetrics()
  58. // Create a customized counter metric.
  59. customizedCounterMetric = prometheus.NewCounterVec(prometheus.CounterOpts{
  60. Name: "demo_server_search_method_handle_count",
  61. Help: "Total number of RPCs handled on the server.",
  62. }, []string{"name"})
  63. )
  64. func main() {
  65. //淡赌跑断
  66. if YamlConfig.IsAddTask == 0 {
  67. initSensitiveWordsData() //初始化敏感词数据
  68. } else {
  69. go addTaskSensitiveWordsData() //增量-改配置文件
  70. }
  71. lis, err := net.Listen("tcp", YamlConfig.Port)
  72. if err != nil {
  73. log.Fatalf("failed to listen: %v", err)
  74. }
  75. defer lis.Close()
  76. // Create a HTTP server for prometheus.
  77. httpServer := &http.Server{
  78. Handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{}),
  79. Addr: fmt.Sprintf("0.0.0.0:%d",
  80. 2092),
  81. }
  82. grpcServer := grpc.NewServer(
  83. //grpc.StreamInterceptor(grpcMetrics.StreamServerInterceptor()),
  84. grpc.UnaryInterceptor(grpcMetrics.UnaryServerInterceptor()))
  85. proto_grpc.RegisterSensitiveWordsServer(grpcServer, &server{})
  86. // Initialize all metrics.
  87. grpcMetrics.InitializeMetrics(grpcServer)
  88. // Start your http server for prometheus.
  89. go func() {
  90. if err := httpServer.ListenAndServe(); err != nil {
  91. log.Fatal("Unable to start a http server.")
  92. }
  93. }()
  94. log.Println("server start:", YamlConfig.Port)
  95. if err := grpcServer.Serve(lis); err != nil {
  96. log.Fatalf("failed to serve: %v", err)
  97. }
  98. }
  99. //协议方法---等
  100. type server struct {
  101. proto_grpc.SensitiveWordsServer
  102. }
  103. func (s *server) Search(ctx context.Context, in *proto_grpc.Request) (*proto_grpc.ResultSensitiveWords, error) {
  104. customizedCounterMetric.WithLabelValues("search_server").Inc()
  105. text := in.GetText()
  106. //log.Println(text)
  107. findAll := Filter.FindAll(text)
  108. rada := strings.Join(findAll, ",")
  109. return &proto_grpc.ResultSensitiveWords{SensitiveWords: rada}, nil
  110. }
  111. func (s *server) Registration(ctx context.Context, in *proto_grpc.NumberOfRegistrations) (*proto_grpc.RequestComplete, error) {
  112. customizedCounterMetric.WithLabelValues("Registration").Inc()
  113. return &proto_grpc.RequestComplete{Text: "ok"}, nil
  114. }
  115. type YAMLConfig struct {
  116. MixdataMgoAddr string `yaml:"mixdataMgoAddr"`
  117. UserName string `yaml:"userName"`
  118. PassWord string `yaml:"passWord"`
  119. DbName string `yaml:"dbName"`
  120. MongodbPoolSize int `yaml:"mongodbPoolSize"`
  121. TaskGteId string `yaml:"taskGteId"`
  122. TaskLteId string `yaml:"taskLteId"`
  123. IsAddTask int `yaml:"isAddTask"`
  124. Port string `yaml:"port"`
  125. }
  126. //其他方法
  127. func StringTOBsonId(id string) primitive.ObjectID {
  128. objectId, _ := primitive.ObjectIDFromHex(id)
  129. return objectId
  130. }
  131. func BsonTOStringId(id interface{}) string {
  132. return id.(primitive.ObjectID).Hex()
  133. }
  134. func toMegaBytes(bytes uint64) float64 {
  135. return float64(bytes) / 1024 / 1024
  136. }
  137. func IntAll(num interface{}) int {
  138. return IntAllDef(num, 0)
  139. }
  140. func Int64All(num interface{}) int64 {
  141. if i, ok := num.(int64); ok {
  142. return int64(i)
  143. } else if i0, ok0 := num.(int32); ok0 {
  144. return int64(i0)
  145. } else if i1, ok1 := num.(float64); ok1 {
  146. return int64(i1)
  147. } else if i2, ok2 := num.(int); ok2 {
  148. return int64(i2)
  149. } else if i3, ok3 := num.(float32); ok3 {
  150. return int64(i3)
  151. } else if i4, ok4 := num.(string); ok4 {
  152. i64, _ := strconv.ParseInt(i4, 10, 64)
  153. //in, _ := strconv.Atoi(i4)
  154. return i64
  155. } else if i5, ok5 := num.(int16); ok5 {
  156. return int64(i5)
  157. } else if i6, ok6 := num.(int8); ok6 {
  158. return int64(i6)
  159. } else if i7, ok7 := num.(*big.Int); ok7 {
  160. in, _ := strconv.ParseInt(fmt.Sprint(i7), 10, 64)
  161. return int64(in)
  162. } else if i8, ok8 := num.(*big.Float); ok8 {
  163. in, _ := strconv.ParseInt(fmt.Sprint(i8), 10, 64)
  164. return int64(in)
  165. } else {
  166. return 0
  167. }
  168. }
  169. func Float64All(num interface{}) float64 {
  170. if i, ok := num.(float64); ok {
  171. return float64(i)
  172. } else if i0, ok0 := num.(int32); ok0 {
  173. return float64(i0)
  174. } else if i1, ok1 := num.(int64); ok1 {
  175. return float64(i1)
  176. } else if i2, ok2 := num.(int); ok2 {
  177. return float64(i2)
  178. } else if i3, ok3 := num.(float32); ok3 {
  179. return float64(i3)
  180. } else if i4, ok4 := num.(string); ok4 {
  181. in, _ := strconv.ParseFloat(i4, 64)
  182. return in
  183. } else if i5, ok5 := num.(int16); ok5 {
  184. return float64(i5)
  185. } else if i6, ok6 := num.(int8); ok6 {
  186. return float64(i6)
  187. } else if i6, ok6 := num.(uint); ok6 {
  188. return float64(i6)
  189. } else if i6, ok6 := num.(uint8); ok6 {
  190. return float64(i6)
  191. } else if i6, ok6 := num.(uint16); ok6 {
  192. return float64(i6)
  193. } else if i6, ok6 := num.(uint32); ok6 {
  194. return float64(i6)
  195. } else if i6, ok6 := num.(uint64); ok6 {
  196. return float64(i6)
  197. } else if i7, ok7 := num.(*big.Float); ok7 {
  198. in, _ := strconv.ParseFloat(fmt.Sprint(i7), 64)
  199. return float64(in)
  200. } else if i8, ok8 := num.(*big.Int); ok8 {
  201. in, _ := strconv.ParseFloat(fmt.Sprint(i8), 64)
  202. return float64(in)
  203. } else {
  204. return 0
  205. }
  206. }
  207. func IntAllDef(num interface{}, defaultNum int) int {
  208. if i, ok := num.(int); ok {
  209. return int(i)
  210. } else if i0, ok0 := num.(int32); ok0 {
  211. return int(i0)
  212. } else if i1, ok1 := num.(float64); ok1 {
  213. return int(i1)
  214. } else if i2, ok2 := num.(int64); ok2 {
  215. return int(i2)
  216. } else if i3, ok3 := num.(float32); ok3 {
  217. return int(i3)
  218. } else if i4, ok4 := num.(string); ok4 {
  219. in, _ := strconv.Atoi(i4)
  220. return int(in)
  221. } else if i5, ok5 := num.(int16); ok5 {
  222. return int(i5)
  223. } else if i6, ok6 := num.(int8); ok6 {
  224. return int(i6)
  225. } else if i7, ok7 := num.(*big.Int); ok7 {
  226. in, _ := strconv.Atoi(fmt.Sprint(i7))
  227. return int(in)
  228. } else if i8, ok8 := num.(*big.Float); ok8 {
  229. in, _ := strconv.Atoi(fmt.Sprint(i8))
  230. return int(in)
  231. } else {
  232. return defaultNum
  233. }
  234. }
  235. func ObjToString(old interface{}) string {
  236. if nil == old {
  237. return ""
  238. } else {
  239. r, _ := old.(string)
  240. return r
  241. }
  242. }
  243. func ObjToStringDef(old interface{}, defaultstr string) string {
  244. if nil == old {
  245. return defaultstr
  246. } else {
  247. r, _ := old.(string)
  248. if r == "" {
  249. return defaultstr
  250. }
  251. return r
  252. }
  253. }
  254. //对象数组转成string数组
  255. func ObjArrToStringArr(old []interface{}) []string {
  256. if old != nil {
  257. new := make([]string, len(old))
  258. for i, v := range old {
  259. new[i] = v.(string)
  260. }
  261. return new
  262. } else {
  263. return nil
  264. }
  265. }
  266. //对象数组转成map数组
  267. func ObjArrToMapArr(old []interface{}) []map[string]interface{} {
  268. if old != nil {
  269. new := make([]map[string]interface{}, len(old))
  270. for i, v := range old {
  271. new[i] = v.(map[string]interface{})
  272. }
  273. return new
  274. } else {
  275. return nil
  276. }
  277. }
  278. //map数组转成对象数组
  279. func MapArrToObjArr(old []map[string]interface{}) []interface{} {
  280. if old != nil {
  281. new := make([]interface{}, len(old))
  282. for i, v := range old {
  283. new[i] = v
  284. }
  285. return new
  286. } else {
  287. return nil
  288. }
  289. }