Skip to content

setProxy doesn't work with pacScript #21130

Closed

Description

@betavebow

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 7.1.1
  • Operating System:
    • Windows 10 (1909)
  • Last Known Working Electron version:
    • 6.1.4

Expected Behavior

Setting a proxy with setProxy and a pac file works.

Actual Behavior

So i'm trying to set a proxy using setProxy with a pac file. I added a partition to my webview and use the same one when using setProxy. It's not catching an error but when I use resolveProxy the promise is empty. If i check the ip it's my normal one not the proxy one.

Additional Information

If i use proxyRules everything works as intended.
My pacFile should be fine:
function FindProxyForURL(url, host) { return "PROXY proxy.example.com:8080"; }

Activity

  1. betavebow commented on Nov 15, 2019

    @betavebow
    Author

    So I tried setting the proxy with the electron-quick-start project to minimize factors that could play a role.
    If i follow the official docs: https://electronjs.org/docs/api/session, i get an error saying: ERR_MANDATORY_PROXY_CONFIGURATION_FAILED (-131).

    This is the description in the chromium error list:
    A mandatory proxy configuration could not be used. Currently this means that a mandatory PAC script could not be fetched, parsed or executed.

  2. ymschaap commented on Nov 20, 2019

    @ymschaap

    Can you add the snippet that shows your session.setProxy(config) and your config object. Are you sure the path is correct to your pacfile? (e.g. console.log the file, or try to read it with const pacFileString = await fs.readFile(pacFile, "utf8");

  3. betavebow commented on Nov 20, 2019

    @betavebow
    Author

    This is my usage in the quick-start:

    const pacPath = "C:/proxy.pac";
    const ses = mainWindow.webContents.session;
    
    const pacFileString = await fs.readFile(pacPath, "utf8", (err, data) => {
      if (err) {
        console.log(err);
      }
      console.log("data read: " + data);
    });
    
    ses.setProxy({pacScript: pacPath}).then(() => {
      console.log("proxy successfully set");
      ses.resolveProxy("https://google.com").then(r => {
        console.log("resolved proxy: " + r)
      }).catch(r => {
        console.log(r)
      })
    }).catch(r => {
      console.log(r)
    });
    
    mainWindow.loadURL("https://google.com").then(() => {
      console.log("loaded url successfully")
    }).catch(r => {
      console.log(r)
    });
    

    When i read the file like you proposed the output is equal to my file.

    The console output looks like this:

    proxy successfully set
    data read: function FindProxyForURL(url, host) { return "PROXY proxy.example.com:8080"; }
    resolved proxy: 
    Error: ERR_MANDATORY_PROXY_CONFIGURATION_FAILED (-131) loading 'https://www.google.com/'
        at rejectAndCleanup (electron/js2c/browser_init.js:5808:21)
        at WebContents.failListener (electron/js2c/browser_init.js:5818:11)
        at WebContents.emit (events.js:203:13) {
      errno: -131,
      code: 'ERR_MANDATORY_PROXY_CONFIGURATION_FAILED',
      url: 'https://www.google.com/'
    }
    

    As you can see i can set the proxy without catching an error, but when trying to resolve the promise is empty. When i use loadURL afterwards it returns the chromium error. I've also made sure the proxy works.

    The latest version of electron I've tried is 6.1.4. This works. I don't know if this is helpful information though. setProxy since changed from using callbacks to promises.

  4. ymschaap commented on Nov 21, 2019

    @ymschaap

    Seems to make sense (I presume the proxy.example:8080 is a dummy value here). If 6.1.4 this works, and 7.1.1 not it seems like a bug. I don't work on Electron, so can maybe only help with some dev ideas:

    Two ideas:
    What if your return "DIRECT" in FindProxyForURL? Does that also fail?
    Do you need a pacscript? Can you not try what you want with the proxyRules object?

  5. betavebow commented on Nov 21, 2019

    @betavebow
    Author

    Yeah it's a dummy value. Direct doesn't seem to make a difference.
    I have decided that I'll be sticking with 6.1.4 until this is resolved.

    Thank you very much for your help!

  6. cuixiping commented on Mar 12, 2020

    @cuixiping

    Same issue.

    Electron v7.1.x (also v8.x.x) doesn't work with file: URI, but works with http: URI.
    Electron v6.1.9 is OK.

    I tested a simplest pac file.

    D:\test.pac

    function FindProxyForURL(url, host) {
        return 'DIRECT';
    }
    

    In my app.js

    Works well with http: URI:

    session.defaultSession.setProxy({
        pacScript: 'http://localhost/test.pac'
    }).then(_=>{ console.log('done'); },  err=>{ throw err; });
    

    Doesn't work with file: URI:

    session.defaultSession.setProxy({
        pacScript: 'file:///D:/test.pac'
    }).then(_=>{ console.log('done'); },  err=>{ throw err; });
    ...
    win.loadURL('https://google.com/');
    

    The error message is:

    Error: ERR_MANDATORY_PROXY_CONFIGURATION_FAILED (-131) loading 'https://google.com/'
        at rejectAndCleanup (electron/js2c/browser_init.js:5906:21)
        at WebContents.failListener (electron/js2c/browser_init.js:5916:11)
        at WebContents.emit (events.js:203:13) {
      errno: -131,
      code: 'ERR_MANDATORY_PROXY_CONFIGURATION_FAILED',
      url: 'https://google.com/'
    }
    
  7. cuixiping commented on Mar 14, 2020

    @cuixiping

    I am sure that the pacScript value should not be file: url with electron v7.x.x or higher.
    My solution is using data: url or http: url instead.

    For example:

    let content = `function FindProxyForURL(url, host) {
        return 'DIRECT';
    }`;
    session.defaultSession.setProxy({
        pacScript: 'data:text/plain;base64,' + Buffer.from(content, 'utf8').toString('base64')
    }).then(_=>{ console.log('done'); });
    
  8. iwind commented on Aug 5, 2020

    @iwind

    I am sure that the pacScript value should not be file: url with electron v7.x.x or higher.
    My solution is using data: url or http: url instead.

    For example:

    let content = `function FindProxyForURL(url, host) {
        return 'DIRECT';
    }`;
    session.defaultSession.setProxy({
        pacScript: 'data:text/plain;base64,' + Buffer.from(content, 'utf8').toString('base64')
    }).then(_=>{ console.log('done'); });
    

    This fixed my issue.

  9. iwind commented on Aug 15, 2020

    @iwind

    I am sure that the pacScript value should not be file: url with electron v7.x.x or higher.
    My solution is using data: url or http: url instead.

    For example:

    let content = `function FindProxyForURL(url, host) {
        return 'DIRECT';
    }`;
    session.defaultSession.setProxy({
        pacScript: 'data:text/plain;base64,' + Buffer.from(content, 'utf8').toString('base64')
    }).then(_=>{ console.log('done'); });
    

    this works well, and 'text/plain' should be replaced with 'application/x-ns-proxy-autoconfig' as perfect.

  10. wong1993 commented on Feb 19, 2021

    @wong1993

    I found that
    session.defaultSession.setProxy({}).then(() => {});
    worked fine to bypass proxy

  11. electron-triage commented on Mar 9, 2021

    @electron-triage

    The Electron version reported on this issue is no longer supported. See our supported versions documentation.

    If this is still reproducible on a supported version, please open a new issue with any other new information that a maintainer should know.

    Thank you for taking the time to report this issue and helping to make Electron better! Your help is appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions