Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
add trojan support #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Apr 10, 2021
1 parent c38e4b7 commit 7c82fcf
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 163 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,3 @@ https://clash-converter.kookxiang.workers.dev/?url=https%3A%2F%2Fexample.com%2Fc
```
...&filter=HK&filter=TW
```

### debug

没卵用的 debug 模式

目前唯一的功能是将订阅地址信息记录到日志中方便排查(默认不会记录)
14 changes: 13 additions & 1 deletion src/Extractor/Base64.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProxyServer, ShadowsocksProxyServer, ShadowsocksRProxyServer, VmessProxyServer } from "../ProxyServer"
import { decode } from 'js-base64';
import { ProxyServer, ShadowsocksProxyServer, ShadowsocksRProxyServer, TrojanProxyServer, VmessProxyServer } from "../ProxyServer";

export default function GetProxyListFromBase64(content: string): ProxyServer[] {
const data = decode(content).split('\n')
Expand All @@ -16,6 +16,8 @@ export default function GetProxyListFromBase64(content: string): ProxyServer[] {
return GetProxyFromShadowsocksURL(url)
} else if (protocol === 'ssr') {
return GetProxyFromShadowsocksRURL(url)
} else if (protocol === 'trojan') {
return GetProxyFromTrojanURL(url)
} else {
throw new Error(`unsupported protocol: ${protocol}`)
}
Expand Down Expand Up @@ -102,3 +104,13 @@ function GetProxyFromShadowsocksRURL(url: URL): ShadowsocksRProxyServer {
}
return result
}

function GetProxyFromTrojanURL(url: URL): TrojanProxyServer {
return {
Name: url.hash || `${url.host}:${url.port}`,
Password: url.username,
ServerAddress: url.host,
ServerPort: Number(url.port),
Type: 'trojan',
};
}
148 changes: 80 additions & 68 deletions src/Extractor/Clash.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,80 @@
import YAML from 'yaml'
import { ProxyServer, ShadowsocksProxyServer, ShadowsocksRProxyServer, VmessProxyServer } from "../ProxyServer"

const PossibleKeys = ['proxies', 'Proxies', 'proxy', 'Proxy']

export default function GetProxyListFromClash(content: string): ProxyServer[] {
const data = YAML.parse(content)
let dataList = []
for (const key of PossibleKeys) {
if (Array.isArray(data[key])) {
dataList = data[key]
break
}
}
if (!dataList.length) {
throw new Error('cannot find proxy list.')
}
return dataList.map((config: any) => {
if (config.type === 'vmess') {
const proxy: VmessProxyServer = {
Cipher: config.cipher || 'auto',
ClientAlterID: config.alterId || 0,
ClientID: config.uuid,
Name: config.name,
ServerAddress: config.server,
ServerPort: config.port,
SupportUDP: config.udp,
Transport: config.network || 'tcp',
TransportSecurity: config.tls ? 'tls' : 'none',
Type: 'vmess',
}
if (proxy.Transport === 'ws' && config['ws-path']) {
proxy.WebSocketPath = config['ws-path']
}
if (proxy.Transport === 'ws' && config['ws-headers']) {
proxy.WebSocketHost = config['ws-headers']?.Host ?? config['ws-headers']?.host
}
return proxy
} else if (config.type === 'ss') {
const proxy: ShadowsocksProxyServer = {
Cipher: config.cipher,
Name: config.name,
Password: config.password,
ServerAddress: config.server,
ServerPort: config.port,
Type: 'ss',
}
return proxy
} else if (config.type === 'ssr') {
const proxy: ShadowsocksRProxyServer = {
Cipher: config.cipher,
Name: config.name,
Obfs: config.obfs,
ObfsParams: config['obfs-param'],
Password: config.password,
Protocol: config.protocol,
ProtocolParams: config['protocol-param'],
ServerAddress: config.server,
ServerPort: config.port,
SupportUDP: config.udp,
Type: 'ssr',
}
return proxy
} else {
throw new Error(`unknown type: ${config.type}`)
}
})
}
import YAML from 'yaml'
import { ProxyServer, ShadowsocksProxyServer, ShadowsocksRProxyServer, TrojanProxyServer, VmessProxyServer } from "../ProxyServer"

const PossibleKeys = ['proxies', 'Proxies', 'proxy', 'Proxy']

export default function GetProxyListFromClash(content: string): ProxyServer[] {
const data = YAML.parse(content)
let dataList = []
for (const key of PossibleKeys) {
if (Array.isArray(data[key])) {
dataList = data[key]
break
}
}
if (!dataList.length) {
throw new Error('cannot find proxy list.')
}
return dataList.map((config: any) => {
if (config.type === 'vmess') {
const proxy: VmessProxyServer = {
Cipher: config.cipher || 'auto',
ClientAlterID: config.alterId || 0,
ClientID: config.uuid,
Name: config.name,
ServerAddress: config.server,
ServerPort: config.port,
SupportUDP: config.udp,
Transport: config.network || 'tcp',
TransportSecurity: config.tls ? 'tls' : 'none',
Type: 'vmess',
}
if (proxy.Transport === 'ws' && config['ws-path']) {
proxy.WebSocketPath = config['ws-path']
}
if (proxy.Transport === 'ws' && config['ws-headers']) {
proxy.WebSocketHost = config['ws-headers']?.Host ?? config['ws-headers']?.host
}
return proxy
} else if (config.type === 'ss') {
const proxy: ShadowsocksProxyServer = {
Cipher: config.cipher,
Name: config.name,
Password: config.password,
ServerAddress: config.server,
ServerPort: config.port,
Type: 'ss',
}
return proxy
} else if (config.type === 'ssr') {
const proxy: ShadowsocksRProxyServer = {
Cipher: config.cipher,
Name: config.name,
Obfs: config.obfs,
ObfsParams: config['obfs-param'],
Password: config.password,
Protocol: config.protocol,
ProtocolParams: config['protocol-param'],
ServerAddress: config.server,
ServerPort: config.port,
SupportUDP: config.udp,
Type: 'ssr',
}
return proxy
} else if (config.type === 'trojan') {
const proxy: TrojanProxyServer = {
AllowInsecure: config['skip-cert-verify'],
Name: config.name,
Password: config.password,
ServerAddress: config.server,
ServerName: config.sni,
ServerPort: config.port,
SupportUDP: config.udp,
Type: 'trojan',
}
return proxy
} else {
throw new Error(`unknown type: ${config.type}`)
}
})
}
18 changes: 17 additions & 1 deletion src/Formatter/Clash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import YAML from 'yaml'
import { ProxyServer, ShadowsocksProxyServer, ShadowsocksRProxyServer, VmessProxyServer } from '../ProxyServer'
import { ProxyServer, ShadowsocksProxyServer, ShadowsocksRProxyServer, TrojanProxyServer, VmessProxyServer } from '../ProxyServer'

export default function FormatProxyForClash(ProxyList: ProxyServer[]): string {
const proxies: any[] = []
Expand Down Expand Up @@ -55,6 +55,22 @@ export default function FormatProxyForClash(ProxyList: ProxyServer[]): string {
config.server = proxy.ServerAddress
config.type = 'ssr'
config.udp = proxy.SupportUDP
} else if (rawProxy.Type === 'trojan') {
let proxy = rawProxy as TrojanProxyServer
config.name = proxy.Name
config.password = proxy.Password
config.port = proxy.ServerPort
config.server = proxy.ServerAddress
config.type = 'trojan'
if (proxy.SupportUDP) {
config.udp = proxy.SupportUDP
}
if (proxy.ServerName) {
config.sni = proxy.ServerName
}
if (proxy.AllowInsecure) {
config['skip-cert-verify'] = proxy.SupportUDP
}
} else {
throw new Error(`unknown type: ${rawProxy.Type}`)
}
Expand Down
97 changes: 53 additions & 44 deletions src/Formatter/Surge.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
import { ProxyServer, ShadowsocksProxyServer, VmessProxyServer } from '../ProxyServer'

export default function FormatProxyForSurge(ProxyList: ProxyServer[]): string {
let result: string[] = []
for (let proxy of ProxyList) {
let row: string[] = []
if (proxy.Type === 'vmess') {
proxy = proxy as VmessProxyServer
row.push(`${proxy.Name} = vmess`)
row.push(proxy.ServerAddress)
row.push(`${proxy.ServerPort}`)
row.push(proxy.ClientID)
if (proxy.Cipher !== "auto") {
row.push(`encrypt-method=${proxy.Cipher}`)
}
if (proxy.TransportSecurity === 'tls') {
row.push('tls=true')
}
if (proxy.Transport === 'ws') {
row.push('ws=true')
} else if (proxy.Transport !== 'tcp') {
console.warn(`${proxy.Transport} is not supported in surge`)
continue
}
if (proxy.Transport === 'ws' && proxy.WebSocketPath) {
row.push(`ws-path=${encodeURI(proxy.WebSocketPath)}`)
}
if (proxy.Transport === 'ws' && proxy.WebSocketHost) {
row.push(`ws-headers=host:${JSON.stringify(proxy.WebSocketHost)}`)
}
} else if (proxy.Type === 'ss') {
proxy = proxy as ShadowsocksProxyServer
row.push(`${proxy.Name} = ss`)
row.push(proxy.ServerAddress)
row.push(`${proxy.ServerPort}`)
row.push(`encrypt-method=${proxy.Cipher}`)
row.push(`password=${proxy.Password}`)
} else {
throw new Error(`unknown type: ${proxy.Type}`)
}
result.push(row.join(', '))
}
return result.join('\n')
}
import { ProxyServer, ShadowsocksProxyServer, TrojanProxyServer, VmessProxyServer } from '../ProxyServer'

export default function FormatProxyForSurge(ProxyList: ProxyServer[]): string {
let result: string[] = []
for (let proxy of ProxyList) {
let row: string[] = []
if (proxy.Type === 'vmess') {
proxy = proxy as VmessProxyServer
row.push(`${proxy.Name} = vmess`)
row.push(proxy.ServerAddress)
row.push(`${proxy.ServerPort}`)
row.push(proxy.ClientID)
if (proxy.Cipher !== "auto") {
row.push(`encrypt-method=${proxy.Cipher}`)
}
if (proxy.TransportSecurity === 'tls') {
row.push('tls=true')
}
if (proxy.Transport === 'ws') {
row.push('ws=true')
} else if (proxy.Transport !== 'tcp') {
console.warn(`${proxy.Transport} is not supported in surge`)
continue
}
if (proxy.Transport === 'ws' && proxy.WebSocketPath) {
row.push(`ws-path=${encodeURI(proxy.WebSocketPath)}`)
}
if (proxy.Transport === 'ws' && proxy.WebSocketHost) {
row.push(`ws-headers=host:${JSON.stringify(proxy.WebSocketHost)}`)
}
} else if (proxy.Type === 'ss') {
proxy = proxy as ShadowsocksProxyServer
row.push(`${proxy.Name} = ss`)
row.push(proxy.ServerAddress)
row.push(`${proxy.ServerPort}`)
row.push(`encrypt-method=${proxy.Cipher}`)
row.push(`password=${proxy.Password}`)
} else if (proxy.Type === 'trojan') {
proxy = proxy as TrojanProxyServer
row.push(`${proxy.Name} = trojan`)
row.push(proxy.ServerAddress)
row.push(`${proxy.ServerPort}`)
row.push(`password=${proxy.Password}`)
if (proxy.ServerName) {
row.push(`sni=${proxy.ServerName}`)
}
} else {
throw new Error(`unknown type: ${proxy.Type}`)
}
result.push(row.join(', '))
}
return result.join('\n')
}
Loading

0 comments on commit 7c82fcf

Please sign in to comment.