N
N
Nadim Zakirov2020-10-21 23:31:27
JavaScript
Nadim Zakirov, 2020-10-21 23:31:27

Why doesn't the shExpMatch function inside pacScript work?

I am writing an extension for Google Chrome that would change the proxy in the browser on the fly depending on the address.

Added the following config to background.js:

config = {
  mode: "pac_script",
  pacScript: {
  data: `
      function FindProxyForURL(url, host) {
        if (shExpMatch(url, "*1")) {
          return 'PROXY 193.56.186.165:65233';
        }
      }
    `
  }
};

chrome.proxy.settings.set(
  {value: config, scope: 'regular'},
  function() {}
);


That is, in theory, if the site address contains a unit at the end, then the specified proxy should have been used, but alas, this does not happen. And I don’t even know how to look inside the pack script to understand what’s going on under the soot (

For tests, I went here:
https://yandex.ru/internet/?proxy=1

And yes, there are no errors in the code I see, because if, for example, in the conditions, instead of the regular expression, you just set true, then the proxy change works quite well and Yandex shows a different IP, which indicates that the method of changing the proxy is fundamentally correct.The only question is why the regular expression does not work...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nadim Zakirov, 2020-10-22
@zkrvndm

I found what is the problem. It turned out that the full address of the open page is passed to the url variable inside the pac script only if the site is opened via the http protocol . In the event that the site is opened using the https protocol, the browser cuts paths leaving only the domain:

spoiler
The URL being accessed. The path and query components of https:// URLs are stripped. In Chrome (versions 52 to 73), you can disable this by setting PacHttpsUrlStrippingEnabled to false in policy or by launching with the --unsafe-pac-url command-line flag (in Chrome 74, only the flag works, and from 75 onward, there is no way to disable path-stripping; as of Chrome 81, path-stripping does not apply to HTTP URLs, but there is interest in changing this behavior to match HTTPS); in Firefox, the preference is network.proxy.autoconfig_url.include_path.

To my great regret, this cannot be changed, since I did not find the PacHttpsUrlStrippingEnabled flag in the chrome settings, and launching via the --unsafe-pac-url option does not help. I don't even know what to do now

A
acwartz, 2020-10-22
@acwartz

You have a problem understanding reg. expressions and not with a function.

That is, in theory, if the site address contains a unit at the end

Your reg. the expression not only does not look for a unit at the end of the string, but is generally erroneous.
In addition, you are looking in the url, not in the IP address, you need to pass the host through dnsResolve and the output will be the IP address of the server you are going to hit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question