From 3fe77430f2f8d1b52597c7278edb7f0e6267ccc9 Mon Sep 17 00:00:00 2001 From: wjsjwr Date: Sun, 28 Sep 2025 22:16:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=A4=E4=B8=AA=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?ASR=E6=B7=B7=E6=B7=86=E6=A6=82=E7=8E=87=E5=92=8C=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- asr.go | 15 ++++++++++++++- config.go | 16 ++++++++++++++-- config.toml | 8 +++++++- main.go | 24 ++++++++++++++---------- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/asr.go b/asr.go index 1b767db..d042e81 100644 --- a/asr.go +++ b/asr.go @@ -113,10 +113,13 @@ func (s *SpeechRecognitionResponse) asrReplaceRandom() { } } -func asrResultObfuscate(r *http.Request, body []byte) ([]byte, error) { +func asrResultObfuscate(r *http.Request, body []byte, replacePercentage int) ([]byte, error) { if r.URL.Path != "/webcast/review/client_ai/upload_asr_result/" { return nil, fmt.Errorf("not an asr request") } + if !shouldApplyASRReplacement(replacePercentage) { + return nil, nil + } obj := SpeechRecognitionResponse{} err := obj.FromJSON(string(body)) if err != nil { @@ -133,3 +136,13 @@ func asrResultObfuscate(r *http.Request, body []byte) ([]byte, error) { } return jsonData, nil } + +func shouldApplyASRReplacement(replacePercentage int) bool { + if replacePercentage <= 0 { + return false + } + if replacePercentage >= 100 { + return true + } + return rand.Intn(100) < replacePercentage +} diff --git a/config.go b/config.go index 0e9e95d..715ae47 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,8 @@ func parseConfig(filename string) (*Config, error) { config.Proxy.Port = 8080 config.Dump.OutputDir = "traffic_dumps" config.Dump.DOIDir = "interest_dumps" + config.DictFile = "dict.txt" + config.ASR.ReplacePercentage = 100 // Parse the TOML file _, err := toml.DecodeFile(filename, config) @@ -24,6 +26,16 @@ func parseConfig(filename string) (*Config, error) { return nil, fmt.Errorf("failed to parse config file %s: %v", filename, err) } + if config.DictFile == "" { + config.DictFile = "dict.txt" + } + if config.ASR.ReplacePercentage < 0 { + config.ASR.ReplacePercentage = 0 + } + if config.ASR.ReplacePercentage > 100 { + config.ASR.ReplacePercentage = 100 + } + // Create dump directories if they don't exist if err := createDumpDirectories(config); err != nil { return nil, fmt.Errorf("failed to create dump directories: %v", err) @@ -51,6 +63,6 @@ func createDumpDirectories(config *Config) error { } func (c *Config) String() string { - return fmt.Sprintf("Config{DomainsOfInterest: %v, Proxy: {Port: %d}, Dump: {OutputDir: %s, DOI_dir: %s}}", - c.DomainsOfInterest, c.Proxy.Port, c.Dump.OutputDir, c.Dump.DOIDir) + return fmt.Sprintf("Config{DomainsOfInterest: %v, DictFile: %s, Proxy: {Port: %d}, Dump: {OutputDir: %s, DOI_dir: %s}, ASR: {ReplacePercentage: %d}}", + c.DomainsOfInterest, c.DictFile, c.Proxy.Port, c.Dump.OutputDir, c.Dump.DOIDir, c.ASR.ReplacePercentage) } diff --git a/config.toml b/config.toml index 3509b4a..53a28ce 100644 --- a/config.toml +++ b/config.toml @@ -4,6 +4,8 @@ domains_of_interest = [ "amemv.com" ] +dict_file = "dict.txt" + # Proxy server configuration [proxy] port = 8080 @@ -11,4 +13,8 @@ port = 8080 # Traffic dump configuration [dump] output_dir = "traffic_dumps" -DOI_dir = "interest_dumps" \ No newline at end of file +DOI_dir = "interest_dumps" + +# ASR obfuscation configuration +[asr] +replace_percentage = 100 diff --git a/main.go b/main.go index a224b62..8e18311 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,7 @@ import ( type Config struct { DomainsOfInterest []string `toml:"domains_of_interest"` + DictFile string `toml:"dict_file"` Proxy struct { Port int `toml:"port"` } `toml:"proxy"` @@ -35,6 +36,9 @@ type Config struct { OutputDir string `toml:"output_dir"` DOIDir string `toml:"DOI_dir"` } `toml:"dump"` + ASR struct { + ReplacePercentage int `toml:"replace_percentage"` + } `toml:"asr"` } type UserData struct { @@ -66,8 +70,15 @@ func main() { fmt.Println("MITM Proxy Server v1.4") - fmt.Println("Reading dictionary...") - dict, err := NewChineseDict("dict.txt") + // Load configuration + config, err := loadConfig("config.toml") + if err != nil { + log.Fatalf("Failed to load configuration: %v", err) + } + log.Println(config.String()) + + fmt.Printf("Reading dictionary from %s...\n", config.DictFile) + dict, err := NewChineseDict(config.DictFile) if err != nil { log.Fatalf("Failed to load dictionary: %v", err) } @@ -76,13 +87,6 @@ func main() { fmt.Println("Starting MITM proxy server...") - // Load configuration - config, err := loadConfig("config.toml") - if err != nil { - log.Fatalf("Failed to load configuration: %v", err) - } - log.Println(config.String()) - // Create output directories if err := os.MkdirAll(config.Dump.OutputDir, 0755); err != nil { log.Fatalf("Failed to create output directory: %v", err) @@ -241,7 +245,7 @@ func (p *ProxyServer) setupHandlers() { var newReqBody []byte = nil if p.isDomainOfInterest(r.Host) { - newReqBody, err = asrResultObfuscate(r, reqBody) + newReqBody, err = asrResultObfuscate(r, reqBody, p.config.ASR.ReplacePercentage) if err != nil && err.Error() != "not an asr request" { log.Printf("Failed to obfuscate request body: %v", err) }