-
Notifications
You must be signed in to change notification settings - Fork 663
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
6 changed files
with
217 additions
and
27 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
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,199 @@ | ||
import { Result, isPresent } from './utils'; | ||
import { isNotBlank } from '@/utils'; | ||
// import $ from '@/core/app'; | ||
|
||
const targetPlatform = 'Surfboard'; | ||
|
||
export default function Surfboard_Producer() { | ||
const produce = (proxy) => { | ||
switch (proxy.type) { | ||
case 'ss': | ||
return shadowsocks(proxy); | ||
case 'trojan': | ||
return trojan(proxy); | ||
case 'vmess': | ||
return vmess(proxy); | ||
case 'http': | ||
return http(proxy); | ||
case 'socks5': | ||
return socks5(proxy); | ||
case 'wireguard-surge': | ||
return wireguard(proxy); | ||
} | ||
throw new Error( | ||
`Platform ${targetPlatform} does not support proxy type: ${proxy.type}`, | ||
); | ||
}; | ||
return { produce }; | ||
} | ||
|
||
function shadowsocks(proxy) { | ||
const result = new Result(proxy); | ||
result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`); | ||
result.append(`,encrypt-method=${proxy.cipher}`); | ||
result.appendIfPresent(`,password=${proxy.password}`, 'password'); | ||
|
||
// obfs | ||
if (isPresent(proxy, 'plugin')) { | ||
if (proxy.plugin === 'obfs') { | ||
result.append(`,obfs=${proxy['plugin-opts'].mode}`); | ||
result.appendIfPresent( | ||
`,obfs-host=${proxy['plugin-opts'].host}`, | ||
'plugin-opts.host', | ||
); | ||
result.appendIfPresent( | ||
`,obfs-uri=${proxy['plugin-opts'].path}`, | ||
'plugin-opts.path', | ||
); | ||
} else { | ||
throw new Error(`plugin ${proxy.plugin} is not supported`); | ||
} | ||
} | ||
|
||
// udp | ||
result.appendIfPresent(`,udp-relay=${proxy.udp}`, 'udp'); | ||
|
||
return result.toString(); | ||
} | ||
|
||
function trojan(proxy) { | ||
const result = new Result(proxy); | ||
result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`); | ||
result.appendIfPresent(`,password=${proxy.password}`, 'password'); | ||
|
||
// transport | ||
handleTransport(result, proxy); | ||
|
||
// tls | ||
result.appendIfPresent(`,tls=${proxy.tls}`, 'tls'); | ||
|
||
// tls verification | ||
result.appendIfPresent(`,sni=${proxy.sni}`, 'sni'); | ||
result.appendIfPresent( | ||
`,skip-cert-verify=${proxy['skip-cert-verify']}`, | ||
'skip-cert-verify', | ||
); | ||
|
||
// tfo | ||
result.appendIfPresent(`,tfo=${proxy.tfo}`, 'tfo'); | ||
|
||
// udp | ||
result.appendIfPresent(`,udp-relay=${proxy.udp}`, 'udp'); | ||
|
||
return result.toString(); | ||
} | ||
|
||
function vmess(proxy) { | ||
const result = new Result(proxy); | ||
result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`); | ||
result.appendIfPresent(`,username=${proxy.uuid}`, 'uuid'); | ||
|
||
// transport | ||
handleTransport(result, proxy); | ||
|
||
// AEAD | ||
if (isPresent(proxy, 'aead')) { | ||
result.append(`,vmess-aead=${proxy.aead}`); | ||
} else { | ||
result.append(`,vmess-aead=${proxy.alterId === 0}`); | ||
} | ||
|
||
// tls | ||
result.appendIfPresent(`,tls=${proxy.tls}`, 'tls'); | ||
|
||
// tls verification | ||
result.appendIfPresent(`,sni=${proxy.sni}`, 'sni'); | ||
result.appendIfPresent( | ||
`,skip-cert-verify=${proxy['skip-cert-verify']}`, | ||
'skip-cert-verify', | ||
); | ||
|
||
// udp | ||
result.appendIfPresent(`,udp-relay=${proxy.udp}`, 'udp'); | ||
|
||
return result.toString(); | ||
} | ||
|
||
function http(proxy) { | ||
const result = new Result(proxy); | ||
const type = proxy.tls ? 'https' : 'http'; | ||
result.append(`${proxy.name}=${type},${proxy.server},${proxy.port}`); | ||
result.appendIfPresent(`,${proxy.username}`, 'username'); | ||
result.appendIfPresent(`,${proxy.password}`, 'password'); | ||
|
||
// tls verification | ||
result.appendIfPresent(`,sni=${proxy.sni}`, 'sni'); | ||
result.appendIfPresent( | ||
`,skip-cert-verify=${proxy['skip-cert-verify']}`, | ||
'skip-cert-verify', | ||
); | ||
|
||
// udp | ||
result.appendIfPresent(`,udp-relay=${proxy.udp}`, 'udp'); | ||
|
||
return result.toString(); | ||
} | ||
|
||
function socks5(proxy) { | ||
const result = new Result(proxy); | ||
const type = proxy.tls ? 'socks5-tls' : 'socks5'; | ||
result.append(`${proxy.name}=${type},${proxy.server},${proxy.port}`); | ||
result.appendIfPresent(`,${proxy.username}`, 'username'); | ||
result.appendIfPresent(`,${proxy.password}`, 'password'); | ||
|
||
// tls verification | ||
result.appendIfPresent(`,sni=${proxy.sni}`, 'sni'); | ||
result.appendIfPresent( | ||
`,skip-cert-verify=${proxy['skip-cert-verify']}`, | ||
'skip-cert-verify', | ||
); | ||
|
||
// udp | ||
result.appendIfPresent(`,udp-relay=${proxy.udp}`, 'udp'); | ||
|
||
return result.toString(); | ||
} | ||
|
||
function wireguard(proxy) { | ||
const result = new Result(proxy); | ||
|
||
result.append(`${proxy.name}=wireguard`); | ||
|
||
result.appendIfPresent( | ||
`,section-name=${proxy['section-name']}`, | ||
'section-name', | ||
); | ||
|
||
return result.toString(); | ||
} | ||
|
||
function handleTransport(result, proxy) { | ||
if (isPresent(proxy, 'network')) { | ||
if (proxy.network === 'ws') { | ||
result.append(`,ws=true`); | ||
if (isPresent(proxy, 'ws-opts')) { | ||
result.appendIfPresent( | ||
`,ws-path=${proxy['ws-opts'].path}`, | ||
'ws-opts.path', | ||
); | ||
if (isPresent(proxy, 'ws-opts.headers')) { | ||
const headers = proxy['ws-opts'].headers; | ||
const value = Object.keys(headers) | ||
.map((k) => { | ||
let v = headers[k]; | ||
if (['Host'].includes(k)) { | ||
v = `"${v}"`; | ||
} | ||
return `${k}:${v}`; | ||
}) | ||
.join('|'); | ||
if (isNotBlank(value)) { | ||
result.append(`,ws-headers=${value}`); | ||
} | ||
} | ||
} | ||
} else { | ||
throw new Error(`network ${proxy.network} is unsupported`); | ||
} | ||
} | ||
} |
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