proxy-lib
NativeScript 工具中使用的代理库。此库提供了用于获取、设置和清除 NativeScript 工具所尊重的代理设置的方法。
npm i --save proxy-lib
- 版本:0.4.0
- GitHub:
- NPM: https://npmjs.net.cn/package/proxy-lib
- 下载
- 昨天: 0
- 上周: 0
- 上个月: 0
proxy-lib
NativeScript 工具中使用的代理库。此库提供了用于获取、设置和清除 NativeScript 工具所尊重的代理设置的方法。
内容
API
getProxySettings
此方法返回代理信息。如果没有代理设置文件,返回的 promise 将解析为 null
。
getProxySettings 定义
/**
* Get information about current proxy settings.
* @param {string | { credentialsKey: string, userSpecifiedSettingsFilePath: string }} params @optional CredentialsKey and path to file from which to get the information.
* @returns {Promise<{ proxy: string, rejectUnauthorized: boolean, username: string, password: string, protocol: string, port: string, hostname: string }>} Information about proxy settings
*/
getProxySettings(params: string | { credentialsKey: string, userSpecifiedSettingsFilePath: string }): Promise<{proxy: string, rejectUnauthorized: boolean, username: string, password: string, protocol: string, port: string, hostname: string}>;
getProxySettings 示例
- 仅传递
credentials key
- 在这种情况下,设置将从默认位置读取
const proxyLib = require("proxy-lib");
proxyLib.getProxySettings({ credentialsKey: "myCredKey" })
.then(proxySettings => console.log(proxySettings))
.catch(err => console.error("Error while getting proxy settings.", err));
- 传递
credentialsKey
和userSpecifiedSettingsFilePath
- 代理设置将从这个文件读取
const proxyLib = require("proxy-lib");
proxyLib.getProxySettings({ credentialsKey: "myCredKey", userSpecifiedSettingsFilePath: "~/.local/share/myProxyFile.json" })
.then(proxySettings => console.log(proxySettings))
.catch(err => console.error("Error while getting proxy settings.", err));
- 传递 credentials key 作为字符串 - 在这种情况下,设置将从默认位置读取
const proxyLib = require("proxy-lib");
proxyLib.getProxySettings("myCredKey")
.then(proxySettings => console.log(proxySettings))
.catch(err => console.error("Error while getting proxy settings.", err));
setProxySettings
此方法将代理信息设置为指定的文件(或默认位置)。如果代理需要身份验证,您可以传递凭证,它们将被安全地存储在 Windows 凭据管理器中。
setProxySettings 定义
/**
* Sets new proxy settings.
* @param {string | { proxyUrl: string, credentialsKey: string, rejectUnauthorized: boolean, username: string, password: string, userSpecifiedSettingsFilePath: string }} params Proxy settings
* @returns {Promise<void>}
*/
setProxySettings(params: string | { proxyUrl: string, credentialsKey: string, rejectUnauthorized: boolean, username: string, password: string, userSpecifiedSettingsFilePath: string }): Promise<void>;
setProxySettings 参数和示例
- 传递所有设置
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings({ proxyUrl: "http://192.168.1.102:8888", credentialsKey: "myCredKey", rejectUnauthorized: true, username: "myUsername", password: "myPassword", userSpecifiedSettingsFilePath: "~/.local/share/myProxyFile.json" })
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
proxyUrl
- 此参数是必需的,它显示代理的 URL。您可以将它作为方法的单个字符串参数传递,或者作为对象的一部分
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings({ proxyUrl: "http://192.168.1.102:8888" })
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
或
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings("http://192.168.1.102:8888")
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
proxyUrl
也可以包含身份验证信息,在这种情况下,调用将是
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings({ proxyUrl: "http://myUsername:[email protected]:8888" })
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
或
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings("http://myUsername:[email protected]:8888")
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
rejectUnauthorized
- 此参数定义是否应尊重 Node.js 对无效证书的错误。如果您不传递此值,它将设置为true
,因此根据配置,可能会抛出像self signed certificate in certificate chain
这样的错误。将此值设置为false
将忽略此类错误。
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings({ proxyUrl: "http://192.168.1.102:8888", rejectUnauthorized: false })
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
username
和password
- 这些参数应在代理需要基本身份验证的情况下使用。目前您只能在 Windows 上使用它们。仅传递其中一个值将引发错误。您可以传递两个参数,或者像上面那样在proxyUrl
中包含身份验证。传递的值将被保存到 Windows 凭据管理器中。
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings({ proxyUrl: "http://192.168.1.102:8888", username: "myUsername", password: "myPassword" })
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
credentialsKey
- 此参数定义凭据管理器中条目的名称,其中将持久保存用户名和密码。如果未传递,则具有默认值。此参数在未传递身份验证参数的情况下没有影响。
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings({ proxyUrl: "http://192.168.1.102:8888", username: "myUsername", password: "myPassword", credentialsKey: "myKey" })
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
userSpecifiedSettingsFilePath
- 此参数定义将代理设置(不包含身份验证)持久保存到的文件的路径。如果未传递,则具有默认值。
const proxyLib = require("proxy-lib");
proxyLib.setProxySettings({ proxyUrl: "http://192.168.1.102:8888", userSpecifiedSettingsFilePath: "~/.local/share/myProxyFile.json" })
.then(() => console.log("Successfully set proxy settings."))
.catch(err => console.error("Unable to set proxy settings", err));
clearProxySettings
此方法通过删除代理文件和从凭据管理器中删除指定的条目来清除代理设置。
clearProxySettings 定义
/**
* Clears proxy settings.
* @param {string | { credentialsKey: string, userSpecifiedSettingsFilePath: string }} params @optional Options for credentials key and path to be cleaned.
* @returns {Promise<void>}
*/
clearProxySettings(params: string | { credentialsKey: string, userSpecifiedSettingsFilePath: string }): Promise<void>;
clearProxySettings 示例
- 删除默认设置
const proxyLib = require("proxy-lib");
proxyLib.clearProxySettings()
.then(() => console.log("Successfully cleared proxy settings file."))
.catch(err => console.error("Unable to clear proxy settings", err));
- 从自定义文件删除设置
const proxyLib = require("proxy-lib");
proxyLib.clearProxySettings({ userSpecifiedSettingsFilePath: "~/.local/share/myProxyFile.json" })
.then(() => console.log("Successfully cleared custom proxy settings file."))
.catch(err => console.error("Unable to clear proxy settings", err));
- 从自定义凭据管理器条目和默认设置文件中删除设置
const proxyLib = require("proxy-lib");
proxyLib.clearProxySettings({ credentialsKey: "myKey" })
.then(() => console.log("Successfully cleared custom proxy settings file and custom entry."))
.catch(err => console.error("Unable to clear proxy settings", err));