add queue
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
@@ -10,7 +10,21 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func routes(s *server.Server, cache *map[string]*search.Result) {
|
func searchWorker(s *server.Server, cache *map[string]*search.Result, searchQueue chan string) {
|
||||||
|
for id := range searchQueue {
|
||||||
|
s.Mu.RLock()
|
||||||
|
r, exists := (*cache)[id]
|
||||||
|
s.Mu.RUnlock()
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
search.Search(s, r.Query, r, s.Mu)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func routes(s *server.Server, cache *map[string]*search.Result, searchQueue chan string) {
|
||||||
s.Router.Use(
|
s.Router.Use(
|
||||||
func(c *gin.Context) {
|
func(c *gin.Context) {
|
||||||
if s.Settings.Password != "" {
|
if s.Settings.Password != "" {
|
||||||
@@ -45,12 +59,16 @@ func routes(s *server.Server, cache *map[string]*search.Result) {
|
|||||||
var history []historyItem
|
var history []historyItem
|
||||||
s.Mu.RLock()
|
s.Mu.RLock()
|
||||||
for _, r := range *cache {
|
for _, r := range *cache {
|
||||||
|
resultsCount := 0
|
||||||
|
if r.LeakResult.Rows != nil {
|
||||||
|
resultsCount = len(r.LeakResult.Rows)
|
||||||
|
}
|
||||||
history = append(history, historyItem{
|
history = append(history, historyItem{
|
||||||
Id: r.Id,
|
Id: r.Id,
|
||||||
Status: r.Status,
|
Status: r.Status,
|
||||||
Date: r.Date,
|
Date: r.Date,
|
||||||
Query: r.Query,
|
Query: r.Query,
|
||||||
Results: len(r.LeakResult.Rows),
|
Results: resultsCount,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
s.Mu.RUnlock()
|
s.Mu.RUnlock()
|
||||||
@@ -87,11 +105,17 @@ func routes(s *server.Server, cache *map[string]*search.Result) {
|
|||||||
}
|
}
|
||||||
r := search.Result{
|
r := search.Result{
|
||||||
Id: id,
|
Id: id,
|
||||||
|
Date: time.Now(),
|
||||||
|
Status: "queued",
|
||||||
|
Query: query,
|
||||||
}
|
}
|
||||||
go search.Search(s, query, &r, s.Mu)
|
|
||||||
s.Mu.Lock()
|
s.Mu.Lock()
|
||||||
(*cache)[id] = &r
|
(*cache)[id] = &r
|
||||||
s.Mu.Unlock()
|
s.Mu.Unlock()
|
||||||
|
|
||||||
|
searchQueue <- id
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"Id": id})
|
c.JSON(http.StatusOK, gin.H{"Id": id})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -117,6 +141,7 @@ func Init(s *server.Server) {
|
|||||||
s.Router.Use(CORSMiddleware())
|
s.Router.Use(CORSMiddleware())
|
||||||
|
|
||||||
cache := make(map[string]*search.Result)
|
cache := make(map[string]*search.Result)
|
||||||
|
searchQueue := make(chan string, 100)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@@ -125,7 +150,9 @@ func Init(s *server.Server) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
routes(s, &cache)
|
go searchWorker(s, &cache, searchQueue)
|
||||||
|
|
||||||
|
routes(s, &cache, searchQueue)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteOldCache(s *server.Server, cache *map[string]*search.Result) {
|
func deleteOldCache(s *server.Server, cache *map[string]*search.Result) {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ type Query struct {
|
|||||||
type Result struct {
|
type Result struct {
|
||||||
Id string
|
Id string
|
||||||
Date time.Time
|
Date time.Time
|
||||||
Status string // "pending", "completed"
|
Status string // "queued", "pending", "completed"
|
||||||
Query Query
|
Query Query
|
||||||
|
|
||||||
LeakResult dataleak.LeakResult
|
LeakResult dataleak.LeakResult
|
||||||
@@ -39,9 +39,7 @@ func Search(s *server.Server, q Query, r *Result, mu *sync.RWMutex) {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
r.Date = time.Now()
|
|
||||||
r.Status = "pending"
|
r.Status = "pending"
|
||||||
r.Query = q
|
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
wg.Add(3)
|
wg.Add(3)
|
||||||
|
|||||||
@@ -88,6 +88,7 @@
|
|||||||
class="badge badge-xs"
|
class="badge badge-xs"
|
||||||
class:badge-success={item.Status === "completed"}
|
class:badge-success={item.Status === "completed"}
|
||||||
class:badge-warning={item.Status === "pending"}
|
class:badge-warning={item.Status === "pending"}
|
||||||
|
class:badge-neutral={item.Status === "queued"}
|
||||||
>
|
>
|
||||||
{item.Status}
|
{item.Status}
|
||||||
</div></td
|
</div></td
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export type GravatarResult = {
|
|||||||
|
|
||||||
export type Result = {
|
export type Result = {
|
||||||
Id: string;
|
Id: string;
|
||||||
Status: "pending" | "completed";
|
Status: "queued" | "pending" | "completed";
|
||||||
Date: string;
|
Date: string;
|
||||||
Query: Query;
|
Query: Query;
|
||||||
LeakResult: LeakResult;
|
LeakResult: LeakResult;
|
||||||
@@ -46,7 +46,7 @@ export type Result = {
|
|||||||
|
|
||||||
export type HistoryItem = {
|
export type HistoryItem = {
|
||||||
Id: string;
|
Id: string;
|
||||||
Status: "pending" | "completed";
|
Status: "queued" | "pending" | "completed";
|
||||||
Date: string;
|
Date: string;
|
||||||
Query: Query;
|
Query: Query;
|
||||||
Results: number;
|
Results: number;
|
||||||
|
|||||||
Reference in New Issue
Block a user