|
| 1 | +import Joi from '@hapi/joi'; |
| 2 | +import assert from 'assert'; |
| 3 | +import axios from 'axios'; |
| 4 | +import chalk from 'chalk'; |
| 5 | +import yaml from 'yaml'; |
| 6 | +import _ from 'lodash'; |
| 7 | + |
| 8 | +import { |
| 9 | + ClashProviderConfig, |
| 10 | + HttpsNodeConfig, |
| 11 | + NodeTypeEnum, |
| 12 | + ShadowsocksNodeConfig, |
| 13 | + ShadowsocksrNodeConfig, |
| 14 | + SnellNodeConfig, |
| 15 | + VmessNodeConfig, |
| 16 | +} from '../types'; |
| 17 | +import { ConfigCache } from '../utils'; |
| 18 | +import { NETWORK_TIMEOUT } from '../utils/constant'; |
| 19 | +import Provider from './Provider'; |
| 20 | + |
| 21 | +type SupportConfigTypes = ShadowsocksNodeConfig|VmessNodeConfig|HttpsNodeConfig|ShadowsocksrNodeConfig|SnellNodeConfig; |
| 22 | + |
| 23 | +export default class ClashProvider extends Provider { |
| 24 | + public static async getClashSubscription(url: string): Promise<ReadonlyArray<SupportConfigTypes>> { |
| 25 | + assert(url, '未指定订阅地址 url'); |
| 26 | + |
| 27 | + return ConfigCache.has(url) ? |
| 28 | + ConfigCache.get(url) : |
| 29 | + await requestConfigFromRemote(url); |
| 30 | + } |
| 31 | + |
| 32 | + public readonly url: string; |
| 33 | + |
| 34 | + constructor(config: ClashProviderConfig) { |
| 35 | + super(config); |
| 36 | + |
| 37 | + const schema = Joi.object({ |
| 38 | + url: Joi |
| 39 | + .string() |
| 40 | + .uri({ |
| 41 | + scheme: [ |
| 42 | + /https?/, |
| 43 | + ], |
| 44 | + }) |
| 45 | + .required(), |
| 46 | + }) |
| 47 | + .unknown(); |
| 48 | + |
| 49 | + const { error } = schema.validate(config); |
| 50 | + |
| 51 | + if (error) { |
| 52 | + throw error; |
| 53 | + } |
| 54 | + |
| 55 | + this.url = config.url; |
| 56 | + } |
| 57 | + |
| 58 | + public getNodeList(): ReturnType<typeof ClashProvider.getClashSubscription> { |
| 59 | + return ClashProvider.getClashSubscription(this.url); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +async function requestConfigFromRemote(url): Promise<ReadonlyArray<SupportConfigTypes>> { |
| 64 | + const response = await axios.get(url, { |
| 65 | + timeout: NETWORK_TIMEOUT, |
| 66 | + responseType: 'text', |
| 67 | + }); |
| 68 | + const clashConfig = yaml.parse(response.data); |
| 69 | + const proxyList: any[] = clashConfig.Proxy; |
| 70 | + const result = proxyList.map<SupportConfigTypes>(item => { |
| 71 | + switch (item.type) { |
| 72 | + case 'ss': |
| 73 | + if (item.plugin && item.plugin !== 'obfs') { |
| 74 | + console.log(); |
| 75 | + console.log(`不支持读取 ${item.plugin} 类型的 Clash 节点,节点 ${item.name} 会被省略`); |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + type: NodeTypeEnum.Shadowsocks, |
| 81 | + nodeName: item.name, |
| 82 | + hostname: item.server, |
| 83 | + port: item.port, |
| 84 | + method: item.cipher, |
| 85 | + password: item.password, |
| 86 | + 'udp-relay': item.udp ? 'true' : 'false', |
| 87 | + ...(item.plugin && item.plugin === 'obfs' ? { |
| 88 | + obfs: item['plugin-opts'].mode, |
| 89 | + 'obfs-host': item['plugin-opts'].host || 'www.bing.com', |
| 90 | + } : null), |
| 91 | + }; |
| 92 | + |
| 93 | + case 'vmess': |
| 94 | + return { |
| 95 | + type: NodeTypeEnum.Vmess, |
| 96 | + nodeName: item.name, |
| 97 | + hostname: item.server, |
| 98 | + port: item.port, |
| 99 | + uuid: item.uuid, |
| 100 | + alterId: item.alterId ? `${item.alterId}` : '0', |
| 101 | + method: item.cipher || 'auto', |
| 102 | + udp: item.udp !== void 0 ? item.udp : false, |
| 103 | + tls: item.tls !== void 0 ? item.tls : false, |
| 104 | + network: item.network || 'tcp', |
| 105 | + ...(item.network === 'ws' ? { |
| 106 | + path: _.get(item, 'ws-path', '/'), |
| 107 | + host: _.get(item, 'ws-headers.Host', ''), |
| 108 | + } : null), |
| 109 | + }; |
| 110 | + |
| 111 | + case 'http': |
| 112 | + if (item.tls !== 'https') { |
| 113 | + console.log(); |
| 114 | + console.log(`不支持读取 HTTP 类型的 Clash 节点,节点 ${item.name} 会被省略`); |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + type: NodeTypeEnum.HTTPS, |
| 120 | + nodeName: item.name, |
| 121 | + hostname: item.server, |
| 122 | + port: item.port, |
| 123 | + username: item.username || '', |
| 124 | + password: item.password || '', |
| 125 | + }; |
| 126 | + |
| 127 | + case 'snell': |
| 128 | + return { |
| 129 | + type: NodeTypeEnum.Snell, |
| 130 | + nodeName: item.name, |
| 131 | + hostname: item.server, |
| 132 | + port: item.port, |
| 133 | + psk: item.psk, |
| 134 | + obfs: _.get(item, 'obfs-opts.mode', 'http'), |
| 135 | + }; |
| 136 | + |
| 137 | + case 'ssr': |
| 138 | + return { |
| 139 | + type: NodeTypeEnum.Shadowsocksr, |
| 140 | + nodeName: item.name, |
| 141 | + hostname: item.server, |
| 142 | + port: item.port, |
| 143 | + password: item.password, |
| 144 | + obfs: item.obfs, |
| 145 | + obfsparam: item.obfsparam, |
| 146 | + protocol: item.protocol, |
| 147 | + protoparam: item.protocolparam, |
| 148 | + method: item.cipher, |
| 149 | + }; |
| 150 | + |
| 151 | + default: |
| 152 | + console.log(); |
| 153 | + console.log(chalk.yellow(`不支持读取 ${item.type} 的节点,节点 ${item.name} 会被省略`)); |
| 154 | + return null; |
| 155 | + } |
| 156 | + }) |
| 157 | + .filter(item => !!item); |
| 158 | + |
| 159 | + ConfigCache.set(url, result); |
| 160 | + |
| 161 | + return result; |
| 162 | +} |
0 commit comments