1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.parseProxyParam = exports.isProxyTxt = exports.isProxyList = exports.isProxyHub = exports.getProxyType = exports["default"] = void 0;
|
8 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
9 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
10 | var _fs = _interopRequireDefault(require("fs"));
|
11 | var _parseProxies = require("./parseProxies");
|
12 | var _tryURLparse = require("./tryURLparse");
|
13 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
14 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
15 | var isProxyList = function isProxyList(proxy) {
|
16 | return ((0, _tryURLparse.tryURLparse)(proxy) || {}).pathname !== '/';
|
17 | };
|
18 | exports.isProxyList = isProxyList;
|
19 | var isProxyHub = isProxyList;
|
20 | exports.isProxyHub = isProxyHub;
|
21 | var isProxyTxt = function isProxyTxt(proxy) {
|
22 | return /^([^:/@]+)?(:[0-9]{0,4})?(:[^:/@]+)?(:[^:/@]+)?(\?[^:/=?@]+=[^:/=?@]+)*$/gm.test(proxy);
|
23 | };
|
24 | exports.isProxyTxt = isProxyTxt;
|
25 | var getProxyType = function getProxyType(proxy) {
|
26 | if (!proxy) return null;
|
27 | if (proxy[0] === '.' || proxy[0] === '/') return 'file';
|
28 | if (isProxyTxt(proxy)) return 'txt';
|
29 | if (isProxyList(proxy)) return 'list';
|
30 |
|
31 |
|
32 | return 'proxy';
|
33 | };
|
34 | exports.getProxyType = getProxyType;
|
35 | var parseProxyParam = function parseProxyParam(proxyStr) {
|
36 | var proxyType = getProxyType(proxyStr);
|
37 | if (proxyType === 'file') {
|
38 | var path = proxyStr.startsWith('.') ? "".concat(process.cwd()).concat(proxyStr.substr(1)) : proxyStr;
|
39 | var proxies = '';
|
40 | try {
|
41 |
|
42 | proxies = require(path)["default"];
|
43 | } catch (error) {
|
44 | proxies = _fs["default"].readFileSync(path, {
|
45 | encoding: 'utf8',
|
46 | flag: 'r'
|
47 | });
|
48 | }
|
49 | return {
|
50 | proxies: proxies
|
51 | };
|
52 | }
|
53 | if (proxyType === 'proxy') {
|
54 | var _proxies = (0, _parseProxies.parseProxies)(proxyStr).map(function (p) {
|
55 | return _objectSpread({
|
56 | provider: 'env'
|
57 | }, p);
|
58 | });
|
59 | return {
|
60 | proxies: _proxies
|
61 | };
|
62 | }
|
63 | if (proxyType === 'list') {
|
64 | var urlObj = (0, _tryURLparse.tryURLparse)(proxyStr);
|
65 | var options = Object.fromEntries(urlObj.searchParams);
|
66 | urlObj.search = '';
|
67 | var baseURL = urlObj.toString();
|
68 | return {
|
69 | client: {
|
70 | baseURL: baseURL,
|
71 | options: options
|
72 | }
|
73 | };
|
74 | }
|
75 | if (proxyType === 'txt') {
|
76 | var _proxyStr$split = proxyStr.split('?'),
|
77 | _proxyStr$split2 = (0, _slicedToArray2["default"])(_proxyStr$split, 2),
|
78 | params = _proxyStr$split2[0],
|
79 | args = _proxyStr$split2[1];
|
80 | var _params$split = params.split(':'),
|
81 | _params$split2 = (0, _slicedToArray2["default"])(_params$split, 4),
|
82 | _params$split2$ = _params$split2[0],
|
83 | host = _params$split2$ === void 0 ? '' : _params$split2$,
|
84 | _params$split2$2 = _params$split2[1],
|
85 | port = _params$split2$2 === void 0 ? '' : _params$split2$2,
|
86 | _params$split2$3 = _params$split2[2],
|
87 | username = _params$split2$3 === void 0 ? '' : _params$split2$3,
|
88 | _params$split2$4 = _params$split2[3],
|
89 | password = _params$split2$4 === void 0 ? '' : _params$split2$4;
|
90 | var proxyUrl = "http://".concat(username, ":").concat(password, "@").concat(host, ":").concat(port);
|
91 | if (!port) proxyUrl = "http://".concat(username, ":").concat(password, "@").concat(host, "/");
|
92 | if (!password) proxyUrl = "http://".concat(username, "@").concat(host, ":").concat(port, "/");
|
93 | if (!username && !password) proxyUrl = "http://".concat(host, ":").concat(port, "/");
|
94 | if (!password && !port) proxyUrl = "http://".concat(username, "@").concat(host, "/");
|
95 | if (!username && !password && !port) proxyUrl = "http://".concat(host, "/");
|
96 | if (args) proxyUrl += "?".concat(args);
|
97 | var _proxies2 = (0, _parseProxies.parseProxies)(proxyUrl).map(function (p) {
|
98 | return _objectSpread({
|
99 | provider: 'env'
|
100 | }, p);
|
101 | });
|
102 | return {
|
103 | proxies: _proxies2
|
104 | };
|
105 | }
|
106 | return {
|
107 | disabled: true
|
108 | };
|
109 | };
|
110 | exports.parseProxyParam = parseProxyParam;
|
111 | var _default = parseProxyParam;
|
112 | exports["default"] = _default;
|
113 |
|
\ | No newline at end of file |