|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/gin-gonic/gin" |
| 6 | + "github.com/wgpsec/ENScan/common" |
| 7 | + "github.com/wgpsec/ENScan/common/outputfile" |
| 8 | + "github.com/wgpsec/ENScan/common/utils" |
| 9 | + "github.com/wgpsec/ENScan/common/utils/gologger" |
| 10 | + "github.com/wgpsec/ENScan/db" |
| 11 | + "github.com/wgpsec/ENScan/runner" |
| 12 | + "go.mongodb.org/mongo-driver/bson" |
| 13 | + "golang.org/x/net/context" |
| 14 | + "strconv" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +func RunApiWeb(options *common.ENOptions) { |
| 19 | + gin.SetMode(gin.TestMode) |
| 20 | + r := gin.Default() |
| 21 | + |
| 22 | + r.GET("/", func(c *gin.Context) { |
| 23 | + c.JSON(200, gin.H{ |
| 24 | + "code": 200, |
| 25 | + "message": "OK", |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + r.GET("/status", func(c *gin.Context) { |
| 30 | + queues, err := db.RmqC.GetOpenQueues() |
| 31 | + stats, err := db.RmqC.CollectStats(queues) |
| 32 | + if err != nil { |
| 33 | + fmt.Println(err.Error()) |
| 34 | + } |
| 35 | + fmt.Println(stats) |
| 36 | + c.JSON(200, gin.H{ |
| 37 | + "code": 200, |
| 38 | + "message": "OK", |
| 39 | + "data": stats, |
| 40 | + }) |
| 41 | + }) |
| 42 | + r.POST("/api/info", func(ginCtx *gin.Context) { |
| 43 | + token := ginCtx.PostForm("orgname") |
| 44 | + if token == "" { |
| 45 | + ginCtx.JSON(400, gin.H{ |
| 46 | + "code": 400, |
| 47 | + "message": "orgname is empty", |
| 48 | + }) |
| 49 | + return |
| 50 | + } |
| 51 | + if ginCtx.PostForm("update") == "" { |
| 52 | + var result common.DBEnInfos |
| 53 | + collection := db.MongoBb.Database("ENScan").Collection("enterprise_info") |
| 54 | + err := collection.FindOne(context.TODO(), bson.M{"name": token}).Decode(&result) |
| 55 | + if err == nil { |
| 56 | + if result.InTime.Sub(time.Now()).Hours() < 720 { |
| 57 | + ginCtx.JSON(200, gin.H{ |
| 58 | + "code": 400, |
| 59 | + "message": "30DAY", |
| 60 | + }) |
| 61 | + return |
| 62 | + } |
| 63 | + } else { |
| 64 | + gologger.Errorf("NO") |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + options.ScanType = ginCtx.PostForm("type") |
| 69 | + |
| 70 | + if ginCtx.PostForm("invest_rd") == "true" { |
| 71 | + options.IsInvestRd = true |
| 72 | + } |
| 73 | + if ginCtx.PostForm("branch") == "true" { |
| 74 | + options.IsGetBranch = true |
| 75 | + } |
| 76 | + //if ginCtx.PostForm("is_branch") == "true" { |
| 77 | + // options.IsSearchBranch = true |
| 78 | + //} |
| 79 | + if ginCtx.PostForm("proxy") != "" { |
| 80 | + options.Proxy = ginCtx.PostForm("proxy") |
| 81 | + } |
| 82 | + field := ginCtx.PostForm("field") |
| 83 | + options.GetFlags = field |
| 84 | + options.DelayMaxTime = 5 |
| 85 | + options.InvestNum = utils.FormatInvest(ginCtx.PostForm("invest")) |
| 86 | + |
| 87 | + options.KeyWord = token |
| 88 | + err := runner.AddTask(options) |
| 89 | + if err != nil { |
| 90 | + ginCtx.JSON(200, gin.H{ |
| 91 | + "code": 500, |
| 92 | + "message": err.Error(), |
| 93 | + }) |
| 94 | + return |
| 95 | + } |
| 96 | + ginCtx.JSON(200, gin.H{ |
| 97 | + "code": 200, |
| 98 | + "message": "OK", |
| 99 | + "data": "Add OK", |
| 100 | + }) |
| 101 | + |
| 102 | + }) |
| 103 | + r.GET("/api/stockchart", func(ginCtx *gin.Context) { |
| 104 | + orgname := ginCtx.Query("orgname") |
| 105 | + search := ginCtx.Query("search") |
| 106 | + //默认去重 |
| 107 | + duplicate := ginCtx.Query("duplicate") |
| 108 | + IsDuplicate := true |
| 109 | + if duplicate == "true" { |
| 110 | + IsDuplicate = true |
| 111 | + } else if duplicate == "false" { |
| 112 | + IsDuplicate = false |
| 113 | + } |
| 114 | + if orgname == "" && search == "" { |
| 115 | + ginCtx.JSON(400, gin.H{ |
| 116 | + "code": 400, |
| 117 | + "message": "orgname or search is empty", |
| 118 | + }) |
| 119 | + return |
| 120 | + } |
| 121 | + if search != "" { |
| 122 | + orgname = runner.SearchByKeyword(search, options.ScanType, options) |
| 123 | + } |
| 124 | + options.GetFlags = "invest,partner" |
| 125 | + options.InvestNum = -1 |
| 126 | + common.Parse(options) |
| 127 | + reEnsList := make(map[string][]map[string]interface{}) |
| 128 | + reEnsList = runner.GetInfo(runner.InfoDto{OrgName: orgname, SearchList: []string{"enterprise_info", "partner", "invest"}, InvestNum: -1, Depth: 0, IsGetNext: true, IsDuplicate: IsDuplicate, DuMap: make(map[string]map[string]bool)}, reEnsList, 0, options) |
| 129 | + if len(reEnsList) > 0 { |
| 130 | + ginCtx.JSON(200, gin.H{ |
| 131 | + "code": 200, |
| 132 | + "message": "OK", |
| 133 | + "data": reEnsList, |
| 134 | + }) |
| 135 | + } else { |
| 136 | + ginCtx.JSON(200, gin.H{ |
| 137 | + "code": 404, |
| 138 | + "message": orgname + " NO DATA", |
| 139 | + }) |
| 140 | + } |
| 141 | + }) |
| 142 | + r.GET("/api/info", func(ginCtx *gin.Context) { |
| 143 | + //搜索参数 |
| 144 | + orgname := ginCtx.Query("orgname") |
| 145 | + search := ginCtx.Query("search") |
| 146 | + types := ginCtx.Query("type") |
| 147 | + //筛选参数,查询参数 |
| 148 | + field := ginCtx.Query("field") |
| 149 | + duplicate := ginCtx.Query("duplicate") |
| 150 | + |
| 151 | + depth, _ := strconv.Atoi(ginCtx.Query("depth")) |
| 152 | + InvestNum, _ := strconv.Atoi(ginCtx.Query("invest")) |
| 153 | + holds := ginCtx.Query("holds") |
| 154 | + supplier := ginCtx.Query("supplier") |
| 155 | + |
| 156 | + if ginCtx.Query("branch") == "true" { |
| 157 | + options.IsGetBranch = true |
| 158 | + } else { |
| 159 | + options.IsGetBranch = false |
| 160 | + } |
| 161 | + outputs := ginCtx.Query("output") |
| 162 | + if orgname == "" && search == "" { |
| 163 | + ginCtx.JSON(400, gin.H{ |
| 164 | + "code": 400, |
| 165 | + "message": "orgname or search is empty", |
| 166 | + }) |
| 167 | + return |
| 168 | + } |
| 169 | + if search != "" { |
| 170 | + if types == "" { |
| 171 | + types = "aqc" |
| 172 | + } else { |
| 173 | + if _, ok := common.ScanTypeKeys[types]; !ok { |
| 174 | + gologger.Errorf("没有这个%s查询方式\n支持列表\n%s", types, common.ScanTypeKeys) |
| 175 | + ginCtx.JSON(500, gin.H{ |
| 176 | + "code": 500, |
| 177 | + "message": fmt.Sprintf("没有%s方式,支持列表:%s", types, common.ScanTypeKeys), |
| 178 | + }) |
| 179 | + return |
| 180 | + } |
| 181 | + } |
| 182 | + orgname = runner.SearchByKeyword(search, types, options) |
| 183 | + } |
| 184 | + IsDuplicate := true |
| 185 | + if duplicate == "true" { |
| 186 | + IsDuplicate = true |
| 187 | + } else if duplicate == "false" { |
| 188 | + IsDuplicate = false |
| 189 | + } |
| 190 | + if types != "" { |
| 191 | + options.ScanType = types |
| 192 | + } |
| 193 | + options.GetFlags = field |
| 194 | + options.ScanType = "all" |
| 195 | + options.GetType = []string{} |
| 196 | + common.Parse(options) |
| 197 | + reEnsList := make(map[string][]map[string]interface{}) |
| 198 | + searchList := []string{"enterprise_info"} |
| 199 | + searchList = append(searchList, options.GetField...) |
| 200 | + if holds == "true" { |
| 201 | + searchList = append(searchList, "holds") |
| 202 | + } |
| 203 | + if supplier == "true" { |
| 204 | + searchList = append(searchList, "supplier") |
| 205 | + } |
| 206 | + if InvestNum != 0 { |
| 207 | + searchList = append(searchList, "invest") |
| 208 | + } |
| 209 | + gologger.Debugf("searchList: %s\n", searchList) |
| 210 | + if depth == 0 { |
| 211 | + depth = 0 |
| 212 | + } |
| 213 | + reEnsList = runner.GetInfo(runner.InfoDto{OrgName: orgname, SearchType: options.GetType, SearchList: searchList, InvestNum: InvestNum, Depth: depth, IsGetNext: false, IsDuplicate: IsDuplicate, DuMap: make(map[string]map[string]bool)}, reEnsList, 0, options) |
| 214 | + |
| 215 | + if len(reEnsList) > 0 { |
| 216 | + if outputs == "file" { |
| 217 | + ginCtx.Header("Content-Type", "application/octet-stream") |
| 218 | + ginCtx.Header("Content-Disposition", "attachment; filename="+orgname+".xlsx") |
| 219 | + ginCtx.Header("Content-Transfer-Encoding", "binary") |
| 220 | + //fmt.Println(reEnsList) |
| 221 | + err := outputfile.OutPutExcelByMergeJson(reEnsList, ginCtx.Writer) |
| 222 | + if err != nil { |
| 223 | + ginCtx.JSON(500, gin.H{ |
| 224 | + "code": 500, |
| 225 | + "message": "导出失败", |
| 226 | + }) |
| 227 | + return |
| 228 | + } |
| 229 | + } else { |
| 230 | + ginCtx.JSON(200, gin.H{ |
| 231 | + "code": 200, |
| 232 | + "message": "OK", |
| 233 | + "data": reEnsList, |
| 234 | + "columns": outputfile.ENSMapLN, |
| 235 | + }) |
| 236 | + return |
| 237 | + } |
| 238 | + |
| 239 | + } else { |
| 240 | + if res, _ := db.RedisBb.Get("ENScanK:" + orgname).Result(); orgname != "" && res != "" { |
| 241 | + ginCtx.JSON(200, gin.H{ |
| 242 | + "code": 000, |
| 243 | + "inTime": res, |
| 244 | + "message": fmt.Sprintf("关键词 %s 于 %s 入库,正在队列查询,请刷新重试", orgname, res), |
| 245 | + }) |
| 246 | + return |
| 247 | + } |
| 248 | + |
| 249 | + ginCtx.JSON(200, gin.H{ |
| 250 | + "code": 404, |
| 251 | + "message": fmt.Sprintf("没有查询到 %s,任务队列已添加", orgname), |
| 252 | + }) |
| 253 | + return |
| 254 | + } |
| 255 | + |
| 256 | + }) |
| 257 | + |
| 258 | + gologger.Infof("API 模式已开启 :31000\n") |
| 259 | + err := r.Run(":31000") |
| 260 | + if err != nil { |
| 261 | + gologger.Fatalf("web api run error: %v", err) |
| 262 | + return |
| 263 | + } else { |
| 264 | + gologger.Infof("web api run success\n\n") |
| 265 | + } |
| 266 | +} |
0 commit comments