style: code tidy

This commit is contained in:
SunBK201 2025-11-08 17:05:20 +08:00
parent e17478f612
commit 1c97a0bdf0
2 changed files with 7 additions and 8 deletions

View File

@ -1,4 +1,4 @@
package sniff package rewrite
import ( import (
"bytes" "bytes"
@ -17,9 +17,9 @@ func toLowerASCII(b byte) byte {
return b return b
} }
// IndexFoldASCII performs case-insensitive search for needle in haystack (ASCII only) // indexFoldASCII performs case-insensitive search for needle in haystack (ASCII only)
// Returns the first occurrence index or -1 if not found // Returns the first occurrence index or -1 if not found
func IndexFoldASCII(haystack, needle []byte) int { func indexFoldASCII(haystack, needle []byte) int {
if len(needle) == 0 { if len(needle) == 0 {
return 0 return 0
} }
@ -46,10 +46,10 @@ func IndexFoldASCII(haystack, needle []byte) int {
return -1 return -1
} }
// FindUserAgentInPayload searches for User-Agent header(s) in raw HTTP payload // findUserAgentInPayload searches for User-Agent header(s) in raw HTTP payload
// Returns slice of (startPos, endPos) pairs for each User-Agent value found // Returns slice of (startPos, endPos) pairs for each User-Agent value found
// Returns empty slice if no User-Agent found, or if any UA is unterminated (missing \r) // Returns empty slice if no User-Agent found, or if any UA is unterminated (missing \r)
func FindUserAgentInPayload(payload []byte) (positions [][2]int, unterminated bool) { func findUserAgentInPayload(payload []byte) (positions [][2]int, unterminated bool) {
if len(payload) < len(uaTag) { if len(payload) < len(uaTag) {
return nil, false return nil, false
} }
@ -60,7 +60,7 @@ func FindUserAgentInPayload(payload []byte) (positions [][2]int, unterminated bo
break break
} }
idx := IndexFoldASCII(payload[searchStart:], uaTag) idx := indexFoldASCII(payload[searchStart:], uaTag)
if idx < 0 { if idx < 0 {
break break
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/google/gopacket/layers" "github.com/google/gopacket/layers"
"github.com/sunbk201/ua3f/internal/log" "github.com/sunbk201/ua3f/internal/log"
"github.com/sunbk201/ua3f/internal/sniff"
"github.com/sunbk201/ua3f/internal/statistics" "github.com/sunbk201/ua3f/internal/statistics"
) )
@ -72,7 +71,7 @@ func (r *Rewriter) buildReplacement(srcAddr, dstAddr string, originalUA string,
// Returns the rewritten payload and metadata about the operation // Returns the rewritten payload and metadata about the operation
func (r *Rewriter) RewritePacketUserAgent(payload []byte, srcAddr, dstAddr string) (newPayload []byte, hasUA, modified, inWhitelist bool) { func (r *Rewriter) RewritePacketUserAgent(payload []byte, srcAddr, dstAddr string) (newPayload []byte, hasUA, modified, inWhitelist bool) {
// Find all User-Agent positions // Find all User-Agent positions
positions, unterm := sniff.FindUserAgentInPayload(payload) positions, unterm := findUserAgentInPayload(payload)
if unterm { if unterm {
log.LogInfoWithAddr(srcAddr, dstAddr, "Unterminated User-Agent found, not rewriting") log.LogInfoWithAddr(srcAddr, dstAddr, "Unterminated User-Agent found, not rewriting")