Add JA3 fingerprint rule item

This commit is contained in:
世界 2023-01-02 14:09:40 +08:00
parent a9dcb2d092
commit 3cbaca666e
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
5 changed files with 55 additions and 0 deletions

View File

@ -98,6 +98,7 @@ type DefaultDNSRule struct {
PackageName Listable[string] `json:"package_name,omitempty"` PackageName Listable[string] `json:"package_name,omitempty"`
User Listable[string] `json:"user,omitempty"` User Listable[string] `json:"user,omitempty"`
UserID Listable[int32] `json:"user_id,omitempty"` UserID Listable[int32] `json:"user_id,omitempty"`
JA3Fingerprint Listable[string] `json:"ja3_fingerprint,omitempty"`
Outbound Listable[string] `json:"outbound,omitempty"` Outbound Listable[string] `json:"outbound,omitempty"`
ClashMode string `json:"clash_mode,omitempty"` ClashMode string `json:"clash_mode,omitempty"`
Invert bool `json:"invert,omitempty"` Invert bool `json:"invert,omitempty"`

View File

@ -101,6 +101,7 @@ type DefaultRule struct {
PackageName Listable[string] `json:"package_name,omitempty"` PackageName Listable[string] `json:"package_name,omitempty"`
User Listable[string] `json:"user,omitempty"` User Listable[string] `json:"user,omitempty"`
UserID Listable[int32] `json:"user_id,omitempty"` UserID Listable[int32] `json:"user_id,omitempty"`
JA3Fingerprint Listable[string] `json:"ja3_fingerprint,omitempty"`
ClashMode string `json:"clash_mode,omitempty"` ClashMode string `json:"clash_mode,omitempty"`
Invert bool `json:"invert,omitempty"` Invert bool `json:"invert,omitempty"`
Outbound string `json:"outbound,omitempty"` Outbound string `json:"outbound,omitempty"`

View File

@ -194,6 +194,11 @@ func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options opt
rule.items = append(rule.items, item) rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item) rule.allItems = append(rule.allItems, item)
} }
if len(options.JA3Fingerprint) > 0 {
item := NewJA3FingerprintItem(options.JA3Fingerprint)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}
if options.ClashMode != "" { if options.ClashMode != "" {
item := NewClashModeItem(router, options.ClashMode) item := NewClashModeItem(router, options.ClashMode)
rule.items = append(rule.items, item) rule.items = append(rule.items, item)

View File

@ -178,6 +178,11 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
rule.items = append(rule.items, item) rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item) rule.allItems = append(rule.allItems, item)
} }
if len(options.JA3Fingerprint) > 0 {
item := NewJA3FingerprintItem(options.JA3Fingerprint)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}
if len(options.Outbound) > 0 { if len(options.Outbound) > 0 {
item := NewOutboundRule(options.Outbound) item := NewOutboundRule(options.Outbound)
rule.items = append(rule.items, item) rule.items = append(rule.items, item)

View File

@ -0,0 +1,43 @@
package route
import (
"strings"
"github.com/sagernet/sing-box/adapter"
)
var _ RuleItem = (*JA3FingerprintItem)(nil)
type JA3FingerprintItem struct {
fingerprints []string
fingerprintMap map[string]bool
}
func NewJA3FingerprintItem(hashList []string) *JA3FingerprintItem {
rule := &JA3FingerprintItem{
fingerprints: hashList,
fingerprintMap: make(map[string]bool),
}
for _, hash := range hashList {
rule.fingerprintMap[strings.ToLower(hash)] = true
}
return rule
}
func (r *JA3FingerprintItem) Match(metadata *adapter.InboundContext) bool {
if metadata.JA3Fingerprint == "" {
return false
}
return r.fingerprintMap[metadata.JA3Fingerprint]
}
func (r *JA3FingerprintItem) String() string {
var description string
pLen := len(r.fingerprints)
if pLen == 1 {
description = "ja3_fingerprint=" + r.fingerprints[0]
} else {
description = "ja3_fingerprint=[" + strings.Join(r.fingerprints, " ") + "]"
}
return description
}