feat: implémentation complète du pipeline JA4 + Docker + tests
Nouveaux modules: - cmd/ja4sentinel/main.go : point d'entrée avec pipeline capture→parse→fingerprint→output - internal/config/loader.go : chargement YAML + env (JA4SENTINEL_*) + validation - internal/tlsparse/parser.go : extraction ClientHello avec suivi d'état de flux (NEW/WAIT_CLIENT_HELLO/JA4_DONE) - internal/fingerprint/engine.go : génération JA4/JA3 via psanford/tlsfingerprint - internal/output/writers.go : StdoutWriter, FileWriter, UnixSocketWriter, MultiWriter Infrastructure: - Dockerfile (multi-stage), Dockerfile.dev, Dockerfile.test-server - Makefile (build, test, lint, docker-build-*) - docker-compose.test.yml pour tests d'intégration - README.md (276 lignes) avec architecture, config, exemples API (api/types.go): - Ajout Close() aux interfaces Capture et Parser - Ajout FlowTimeoutSec dans Config (défaut: 30s, env: JA4SENTINEL_FLOW_TIMEOUT) - ServiceLog: +Timestamp, +TraceID, +ConnID - LogRecord: champs flatten (ip_meta_*, tcp_meta_*, ja4*) - Helper NewLogRecord() pour conversion TLSClientHello+Fingerprints→LogRecord Architecture (architecture.yml): - Documentation module logging + interfaces LoggerFactory/Logger - Section service.systemd complète (unit, security, capabilities) - Section logging.strategy (JSON lines, champs, règles) - api.Config: +FlowTimeoutSec documenté Fixes/cleanup: - Suppression internal/api/types.go (consolidé dans api/types.go) - Correction imports logging (ja4sentinel/api) - .dockerignore / .gitignore - config.yml.example Tests: - Tous les modules ont leurs tests (*_test.go) - Tests unitaires : capture, config, fingerprint, output, tlsparse - Tests d'intégration via docker-compose.test.yml Build: - Binaires dans dist/ (make build → dist/ja4sentinel) - Docker runtime avec COPY --from=builder /app/dist/ Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@ -1,28 +1,26 @@
|
||||
// Package capture provides network packet capture functionality for ja4sentinel
|
||||
package capture
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/layers"
|
||||
"github.com/google/gopacket/pcap"
|
||||
|
||||
"ja4sentinel/internal/api"
|
||||
"ja4sentinel/api"
|
||||
)
|
||||
|
||||
// CaptureImpl implémente l'interface capture.Capture
|
||||
// CaptureImpl implements the capture.Capture interface for packet capture
|
||||
type CaptureImpl struct {
|
||||
handle *pcap.Handle
|
||||
}
|
||||
|
||||
// New crée une nouvelle instance de capture
|
||||
// New creates a new capture instance
|
||||
func New() *CaptureImpl {
|
||||
return &CaptureImpl{}
|
||||
}
|
||||
|
||||
// Run démarre la capture des paquets réseau selon la configuration
|
||||
// Run starts network packet capture according to the configuration
|
||||
func (c *CaptureImpl) Run(cfg api.Config, out chan<- api.RawPacket) error {
|
||||
var err error
|
||||
c.handle, err = pcap.OpenLive(cfg.Interface, 1600, true, pcap.BlockForever)
|
||||
@ -31,14 +29,14 @@ func (c *CaptureImpl) Run(cfg api.Config, out chan<- api.RawPacket) error {
|
||||
}
|
||||
defer c.handle.Close()
|
||||
|
||||
// Appliquer le filtre BPF s'il est fourni
|
||||
// Apply BPF filter if provided
|
||||
if cfg.BPFFilter != "" {
|
||||
err = c.handle.SetBPFFilter(cfg.BPFFilter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to set BPF filter: %w", err)
|
||||
}
|
||||
} else {
|
||||
// Créer un filtre par défaut pour les ports surveillés
|
||||
// Create default filter for monitored ports
|
||||
defaultFilter := buildBPFForPorts(cfg.ListenPorts)
|
||||
err = c.handle.SetBPFFilter(defaultFilter)
|
||||
if err != nil {
|
||||
@ -47,29 +45,29 @@ func (c *CaptureImpl) Run(cfg api.Config, out chan<- api.RawPacket) error {
|
||||
}
|
||||
|
||||
packetSource := gopacket.NewPacketSource(c.handle, c.handle.LinkType())
|
||||
|
||||
|
||||
for packet := range packetSource.Packets() {
|
||||
// Convertir le paquet en RawPacket
|
||||
// Convert packet to RawPacket
|
||||
rawPkt := packetToRawPacket(packet)
|
||||
if rawPkt != nil {
|
||||
select {
|
||||
case out <- *rawPkt:
|
||||
// Paquet envoyé avec succès
|
||||
// Packet sent successfully
|
||||
default:
|
||||
// Canal plein, ignorer le paquet
|
||||
// Channel full, drop packet
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildBPFForPorts construit un filtre BPF pour les ports TCP spécifiés
|
||||
// buildBPFForPorts builds a BPF filter for the specified TCP ports
|
||||
func buildBPFForPorts(ports []uint16) string {
|
||||
if len(ports) == 0 {
|
||||
return "tcp"
|
||||
}
|
||||
|
||||
|
||||
filterParts := make([]string, len(ports))
|
||||
for i, port := range ports {
|
||||
filterParts[i] = fmt.Sprintf("tcp port %d", port)
|
||||
@ -77,7 +75,7 @@ func buildBPFForPorts(ports []uint16) string {
|
||||
return "(" + joinString(filterParts, ") or (") + ")"
|
||||
}
|
||||
|
||||
// joinString joint des chaînes avec un séparateur
|
||||
// joinString joins strings with a separator
|
||||
func joinString(parts []string, sep string) string {
|
||||
if len(parts) == 0 {
|
||||
return ""
|
||||
@ -89,7 +87,7 @@ func joinString(parts []string, sep string) string {
|
||||
return result
|
||||
}
|
||||
|
||||
// packetToRawPacket convertit un paquet gopacket en RawPacket
|
||||
// packetToRawPacket converts a gopacket packet to RawPacket
|
||||
func packetToRawPacket(packet gopacket.Packet) *api.RawPacket {
|
||||
data := packet.Data()
|
||||
if len(data) == 0 {
|
||||
@ -102,7 +100,7 @@ func packetToRawPacket(packet gopacket.Packet) *api.RawPacket {
|
||||
}
|
||||
}
|
||||
|
||||
// Close ferme correctement la capture
|
||||
// Close properly closes the capture handle
|
||||
func (c *CaptureImpl) Close() error {
|
||||
if c.handle != nil {
|
||||
c.handle.Close()
|
||||
|
||||
Reference in New Issue
Block a user