From c8c1072cc0d02f6304d22564e42cd51961ea29aa Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:28:58 +0100 Subject: [PATCH 1/6] Update dns_proxy_js_query.go --- modules/dns_proxy/dns_proxy_js_query.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/dns_proxy/dns_proxy_js_query.go b/modules/dns_proxy/dns_proxy_js_query.go index 64ea9052..c726c80f 100644 --- a/modules/dns_proxy/dns_proxy_js_query.go +++ b/modules/dns_proxy/dns_proxy_js_query.go @@ -324,3 +324,11 @@ func (j *JSQuery) WasModified() bool { // check if any of the fields has been changed return j.NewHash() != j.refHash } + +func (j *JSQuery) CheckIfModifiedAndUpdateHash() bool { + // check if query was changed and update its hash + newHash := j.NewHash() + wasModified := j.refHash != newHash + j.refHash = newHash + return wasModified +} From 12556bc6bef2ad44e6695fdead690a7fe82faa91 Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:29:57 +0100 Subject: [PATCH 2/6] Update dns_proxy_script.go --- modules/dns_proxy/dns_proxy_script.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/dns_proxy/dns_proxy_script.go b/modules/dns_proxy/dns_proxy_script.go index 4a608168..83dd6777 100644 --- a/modules/dns_proxy/dns_proxy_script.go +++ b/modules/dns_proxy/dns_proxy_script.go @@ -84,11 +84,9 @@ func (s *DnsProxyScript) OnRequest(req *dns.Msg, clientIP string) (jsreq, jsres if _, err := s.Call("onRequest", jsreq, jsres); err != nil { log.Error("%s", err) return nil, nil - } else if jsreq.WasModified() { - jsreq.UpdateHash() + } else if jsreq.CheckIfModifiedAndUpdateHash() { return jsreq, nil - } else if jsres.WasModified() { - jsres.UpdateHash() + } else if jsres.CheckIfModifiedAndUpdateHash() { return nil, jsres } } @@ -104,8 +102,7 @@ func (s *DnsProxyScript) OnResponse(req, res *dns.Msg, clientIP string) (jsreq, if _, err := s.Call("onResponse", jsreq, jsres); err != nil { log.Error("%s", err) return nil, nil - } else if jsres.WasModified() { - jsres.UpdateHash() + } else if jsres.CheckIfModifiedAndUpdateHash() { return nil, jsres } } From 5e97fbb6eb7a242e7ce1d66a853b7d0f2c9f6b35 Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:30:46 +0100 Subject: [PATCH 3/6] Update http_proxy_js_request.go --- modules/http_proxy/http_proxy_js_request.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/modules/http_proxy/http_proxy_js_request.go b/modules/http_proxy/http_proxy_js_request.go index a3c6a1da..1eee69b6 100644 --- a/modules/http_proxy/http_proxy_js_request.go +++ b/modules/http_proxy/http_proxy_js_request.go @@ -103,6 +103,19 @@ func (j *JSRequest) WasModified() bool { return j.NewHash() != j.refHash } +func (j *JSRequest) CheckIfModifiedAndUpdateHash() bool { + newHash := j.NewHash() + // body was read + if j.bodyRead { + j.refHash = newHash + return true + } + // check if req was changed and update its hash + wasModified := j.refHash != newHash + j.refHash = newHash + return wasModified +} + func (j *JSRequest) GetHeader(name, deflt string) string { headers := strings.Split(j.Headers, "\r\n") for i := 0; i < len(headers); i++ { From 25c6339275d37ef6c7832bc8bbc9cdd7b1689cfd Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:32:26 +0100 Subject: [PATCH 4/6] Update http_proxy_js_response.go --- modules/http_proxy/http_proxy_js_response.go | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/modules/http_proxy/http_proxy_js_response.go b/modules/http_proxy/http_proxy_js_response.go index 051812ef..c66cbe7f 100644 --- a/modules/http_proxy/http_proxy_js_response.go +++ b/modules/http_proxy/http_proxy_js_response.go @@ -76,6 +76,28 @@ func (j *JSResponse) WasModified() bool { return j.NewHash() != j.refHash } +func (j *JSResponse) CheckIfModifiedAndUpdateHash() bool { + newHash := j.NewHash() + // body was read + if j.bodyRead { + // body was read + j.refHash = newHash + return true + } else if j.bodyClear { + // body was cleared manually + j.refHash = newHash + return true + } else if j.Body != "" { + // body was not read but just set + j.refHash = newHash + return true + } + // check if res was changed and update its hash + wasModified := j.refHash != newHash + j.refHash = newHash + return wasModified +} + func (j *JSResponse) GetHeader(name, deflt string) string { headers := strings.Split(j.Headers, "\r\n") for i := 0; i < len(headers); i++ { From 1c657fdf53c921c446879c960ba4f097973e2b22 Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:32:52 +0100 Subject: [PATCH 5/6] Update http_proxy_script.go --- modules/http_proxy/http_proxy_script.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/http_proxy/http_proxy_script.go b/modules/http_proxy/http_proxy_script.go index 070f7e24..446f61da 100644 --- a/modules/http_proxy/http_proxy_script.go +++ b/modules/http_proxy/http_proxy_script.go @@ -84,11 +84,9 @@ func (s *HttpProxyScript) OnRequest(original *http.Request) (jsreq *JSRequest, j if _, err := s.Call("onRequest", jsreq, jsres); err != nil { log.Error("%s", err) return nil, nil - } else if jsreq.WasModified() { - jsreq.UpdateHash() + } else if jsreq.CheckIfModifiedAndUpdateHash() { return jsreq, nil - } else if jsres.WasModified() { - jsres.UpdateHash() + } else if jsres.CheckIfModifiedAndUpdateHash() { return nil, jsres } } @@ -104,8 +102,7 @@ func (s *HttpProxyScript) OnResponse(res *http.Response) (jsreq *JSRequest, jsre if _, err := s.Call("onResponse", jsreq, jsres); err != nil { log.Error("%s", err) return nil, nil - } else if jsres.WasModified() { - jsres.UpdateHash() + } else if jsres.CheckIfModifiedAndUpdateHash() { return nil, jsres } } From f3001aa5658756e1c9ea8adec14dd34c6a969f90 Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Sat, 15 Feb 2025 11:57:08 +0100 Subject: [PATCH 6/6] misc --- modules/http_proxy/http_proxy_js_response.go | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/http_proxy/http_proxy_js_response.go b/modules/http_proxy/http_proxy_js_response.go index c66cbe7f..b464a979 100644 --- a/modules/http_proxy/http_proxy_js_response.go +++ b/modules/http_proxy/http_proxy_js_response.go @@ -78,7 +78,6 @@ func (j *JSResponse) WasModified() bool { func (j *JSResponse) CheckIfModifiedAndUpdateHash() bool { newHash := j.NewHash() - // body was read if j.bodyRead { // body was read j.refHash = newHash