diff --git a/modules/dns_proxy/dns_proxy_js_query.go b/modules/dns_proxy/dns_proxy_js_query.go index 24f7ae24..bae57ad2 100644 --- a/modules/dns_proxy/dns_proxy_js_query.go +++ b/modules/dns_proxy/dns_proxy_js_query.go @@ -355,3 +355,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 +} 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 } } 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++ { diff --git a/modules/http_proxy/http_proxy_js_response.go b/modules/http_proxy/http_proxy_js_response.go index 051812ef..b464a979 100644 --- a/modules/http_proxy/http_proxy_js_response.go +++ b/modules/http_proxy/http_proxy_js_response.go @@ -76,6 +76,27 @@ func (j *JSResponse) WasModified() bool { return j.NewHash() != j.refHash } +func (j *JSResponse) CheckIfModifiedAndUpdateHash() bool { + newHash := j.NewHash() + 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++ { 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 } }