55 lines
2.0 KiB
Go
55 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Opt-in local visual fixture; never included in production binaries.
|
|
func TestTrafficPreview(t *testing.T) {
|
|
if os.Getenv("DASHBOARD_TRAFFIC_PREVIEW") != "1" {
|
|
t.Skip("manual visual preview")
|
|
}
|
|
s, a := trafficFixture(t)
|
|
s.SaveTrafficSettings(TrafficSettings{Enabled: true, AgentID: a.ID, ServerCountry: "RU"})
|
|
mux := http.NewServeMux()
|
|
trafficRoutes(mux, s)
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Write([]byte(dashboardHTML))
|
|
})
|
|
mux.HandleFunc("/api/agents", func(w http.ResponseWriter, r *http.Request) { items, _ := s.Agents(); writeJSON(w, items, nil) })
|
|
go func() {
|
|
i := 0
|
|
for {
|
|
i++
|
|
events := []TrafficEvent{}
|
|
for j, ip := range []string{"8.8.8.8", "1.1.1.1", "20.194.96.114", "192.168.0.10", "77.83.39.94", "51.158.145.102", "123.56.127.23"} {
|
|
kind := "request"
|
|
if j == 4 {
|
|
kind = "alert"
|
|
}
|
|
if j == 5 {
|
|
kind = "decision"
|
|
}
|
|
events = append(events, TrafficEvent{ID: fmt.Sprintf("preview-%d-%d", i, j), Time: time.Now().Unix(), Kind: kind, IP: ip, Domain: "demo.example", Method: "GET", Path: "/demo", Status: 200, Reason: "ДЕМО · crowdsecurity/http-probing"})
|
|
}
|
|
s.SaveTraffic(a.ID, TrafficBatch{Events: events, Files: 22, StartedAt: time.Now().Add(-time.Hour).Unix(), CrowdSecAt: time.Now().Unix()})
|
|
time.Sleep(3 * time.Second)
|
|
}
|
|
}()
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/traffic" {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Write([]byte(strings.Replace(trafficHTML, "HTTP-запросы и события CrowdSec · последние 24 часа", "ДЕМОНСТРАЦИЯ · искусственные данные для проверки интерфейса", 1)))
|
|
return
|
|
}
|
|
mux.ServeHTTP(w, r)
|
|
})
|
|
t.Fatal(http.ListenAndServe("127.0.0.1:19105", handler))
|
|
}
|