Feature: external controller from meta tag (#66)

This commit is contained in:
Septs 2021-03-15 00:18:57 +08:00 committed by GitHub
parent 8ed9368b3e
commit d44c5472e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 8 deletions

View File

@ -5,10 +5,8 @@
<link rel="icon" type="image/x-icon" href="https://cdn.jsdelivr.net/gh/Dreamacro/clash/docs/logo.png" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Clash web port"
/>
<meta name="description" content="Clash web port" />
<!--meta name="external-controller" content="http://secret@example.com:9090"-->
<title>Clash</title>
</head>
<body>

View File

@ -110,10 +110,19 @@ export async function getExternalControllerConfig () {
}
}
const hostname = getLocalStorageItem('externalControllerAddr', '127.0.0.1')
const port = getLocalStorageItem('externalControllerPort', '9090')
const secret = getLocalStorageItem('secret', '')
const protocol = hostname === '127.0.0.1' ? 'http:' : window.location.protocol
let url: URL | undefined;
{
const meta = document.querySelector<HTMLMetaElement>('meta[name="external-controller"]')
if (meta?.content?.match(/^https?:/)) {
// [protocol]://[secret]@[hostname]:[port]
url = new URL(meta.content)
}
}
const hostname = getLocalStorageItem('externalControllerAddr', url?.hostname ?? '127.0.0.1')
const port = getLocalStorageItem('externalControllerPort', url?.port ?? '9090')
const secret = getLocalStorageItem('secret', url?.username ?? '')
const protocol = hostname === '127.0.0.1' ? 'http:' : (url?.protocol ?? window.location.protocol)
if (!hostname || !port) {
throw new Error('can\'t get hostname or port')