-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
283 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const config = require('@/config').value; | ||
const logger = require('./logger'); | ||
|
||
const possibleProtocol = ['http', 'https', 'ftp', 'file', 'data']; | ||
|
||
const pacProxy = (pacUri, pacScript, proxyObj) => { | ||
let pacUrlHandler = null; | ||
|
||
// Validate PAC_URI / PAC_SCRIPT | ||
if (pacScript) { | ||
if (typeof pacScript === 'string') { | ||
pacUri = 'data:text/javascript;charset=utf-8,' + encodeURIComponent(pacScript); | ||
} else { | ||
logger.error('Invalid PAC_SCRIPT, use PAC_URI instead'); | ||
} | ||
} | ||
if (pacUri && typeof pacUri === 'string') { | ||
try { | ||
pacUrlHandler = new URL(pacUri); | ||
} catch (e) { | ||
pacUri = null; | ||
pacUrlHandler = null; | ||
logger.error(`Parse PAC_URI error: ${e.stack}`); | ||
} | ||
} else { | ||
pacUri = null; | ||
} | ||
|
||
// Check if PAC_URI has the right protocol | ||
if (pacUri && !possibleProtocol.includes(pacUrlHandler?.protocol?.replace(':', ''))) { | ||
logger.error(`Unsupported PAC protocol: ${pacUrlHandler?.protocol?.replace(':', '')}, expect one of ${possibleProtocol.join(', ')}`); | ||
pacUri = null; | ||
pacUrlHandler = null; | ||
} | ||
|
||
// Validate proxyObj | ||
if (pacUrlHandler) { | ||
proxyObj.host = pacUrlHandler.hostname; | ||
proxyObj.port = pacUrlHandler.port; | ||
proxyObj.protocol = pacUrlHandler.protocol.replace(':', ''); | ||
} else { | ||
proxyObj.protocol = proxyObj.host = proxyObj.port = proxyObj.auth = undefined; | ||
} | ||
|
||
// Validate PROXY_AUTH | ||
if (proxyObj.auth && pacUrlHandler) { | ||
let promptProxyUri = false; | ||
if (pacUrlHandler.username || pacUrlHandler.password) { | ||
logger.warn('PAC_URI contains username and/or password, ignoring PROXY_AUTH'); | ||
proxyObj.auth = undefined; | ||
} else if (!['http:', 'https:'].includes(pacUrlHandler.protocol)) { | ||
logger.warn(`PROXY_AUTH is only supported by HTTP(S) proxies, but got ${pacUrlHandler.protocol}, ignoring`); | ||
proxyObj.auth = undefined; | ||
promptProxyUri = true; | ||
} else { | ||
logger.info('PROXY_AUTH is set and will be used for requests from Node.js. However, requests from puppeteer will not use it'); | ||
promptProxyUri = true; | ||
} | ||
if (promptProxyUri) { | ||
logger.info('To get rid of this, set PAC_URI like protocol://username:password@host:port and clear PROXY_{AUTH,PROTOCOL,HOST,PORT}'); | ||
} | ||
} | ||
|
||
// Compatible with unify-proxy | ||
return { | ||
proxyUri: pacUri, | ||
proxyObj, | ||
proxyUrlHandler: pacUrlHandler, | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
pacProxy, | ||
...pacProxy(config.pacUri, config.pacScript, config.proxy), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { pacProxy } = require('../../lib/utils/pac-proxy'); | ||
|
||
const emptyProxyObj = { | ||
protocol: undefined, | ||
host: undefined, | ||
port: undefined, | ||
auth: undefined, | ||
url_regex: '.*', | ||
}; | ||
|
||
const effectiveExpect = ({ proxyUri }, expectUri) => { | ||
expect(proxyUri).toBe(expectUri); | ||
}; | ||
|
||
describe('pac-proxy', () => { | ||
const nullExpect = (pac) => effectiveExpect(pac, null); | ||
it('pac empty', () => { | ||
nullExpect(pacProxy(null, null, emptyProxyObj)); | ||
}); | ||
it('pac-uri invalid', () => { | ||
nullExpect(pacProxy('http://inv ild.test', null, emptyProxyObj)); | ||
}); | ||
|
||
const httpUri = 'http://rsshub.proxy/pac.pac'; | ||
it('pac-uri http', () => { | ||
effectiveExpect(pacProxy(httpUri, null, emptyProxyObj), httpUri); | ||
}); | ||
|
||
const httpsUri = 'https://rsshub.proxy/pac.pac'; | ||
it('pac-uri https', () => { | ||
effectiveExpect(pacProxy(httpsUri, null, emptyProxyObj), httpsUri); | ||
}); | ||
|
||
const ftpUri = 'ftp://rsshub.proxy/pac.pac'; | ||
it('pac-uri ftp', () => { | ||
effectiveExpect(pacProxy(ftpUri, null, emptyProxyObj), ftpUri); | ||
}); | ||
|
||
const fileUri = 'file:///path/to/pac.pac'; | ||
it('pac-uri file', () => { | ||
effectiveExpect(pacProxy(fileUri, null, emptyProxyObj), fileUri); | ||
}); | ||
|
||
const dataPacScript = "function FindProxyForURL(url, host){return 'DIRECT';}"; | ||
const dataUri = 'data:text/javascript;charset=utf-8,' + encodeURIComponent(dataPacScript); | ||
it('pac-script data', () => { | ||
effectiveExpect(pacProxy(null, dataPacScript, emptyProxyObj), dataUri); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters