Change: always access 127.0.0.1 via http (#64)

This commit is contained in:
CzBiX 2021-02-02 20:58:44 +08:00 committed by GitHub
parent ef7c1cde14
commit a85b0721cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 7 deletions

View File

@ -105,30 +105,33 @@ export async function getExternalControllerConfig () {
return { return {
hostname: info.host, hostname: info.host,
port: info.port, port: info.port,
secret: info.secret secret: info.secret,
protocol: 'http:'
} }
} }
const hostname = getLocalStorageItem('externalControllerAddr', '127.0.0.1') const hostname = getLocalStorageItem('externalControllerAddr', '127.0.0.1')
const port = getLocalStorageItem('externalControllerPort', '9090') const port = getLocalStorageItem('externalControllerPort', '9090')
const secret = getLocalStorageItem('secret', '') const secret = getLocalStorageItem('secret', '')
const protocol = hostname === '127.0.0.1' ? 'http:' : window.location.protocol
if (!hostname || !port) { if (!hostname || !port) {
throw new Error('can\'t get hostname or port') throw new Error('can\'t get hostname or port')
} }
return { hostname, port, secret } return { hostname, port, secret, protocol }
} }
export const getInstance = createAsyncSingleton(async () => { export const getInstance = createAsyncSingleton(async () => {
const { const {
hostname, hostname,
port, port,
secret secret,
protocol
} = await getExternalControllerConfig() } = await getExternalControllerConfig()
return axios.create({ return axios.create({
baseURL: `//${hostname}:${port}`, baseURL: `${protocol}//${hostname}:${port}`,
headers: secret ? { Authorization: `Bearer ${secret}` } : {} headers: secret ? { Authorization: `Bearer ${secret}` } : {}
}) })
}) })
@ -242,7 +245,7 @@ export const getLogsStreamReader = createAsyncSingleton(async function () {
const version = err ? 'unkonwn version' : data.data.version const version = err ? 'unkonwn version' : data.data.version
const useWebsocket = !!version || true const useWebsocket = !!version || true
const logUrl = `${window.location.protocol}//${externalController.hostname}:${externalController.port}/logs?level=${config['log-level']}` const logUrl = `${externalController.protocol}//${externalController.hostname}:${externalController.port}/logs?level=${config['log-level']}`
return new StreamReader<Log>({ url: logUrl, bufferLength: 200, token: externalController.secret, useWebsocket }) return new StreamReader<Log>({ url: logUrl, bufferLength: 200, token: externalController.secret, useWebsocket })
}) })
@ -252,6 +255,6 @@ export const getConnectionStreamReader = createAsyncSingleton(async function ()
const version = err ? 'unkonwn version' : data.data.version const version = err ? 'unkonwn version' : data.data.version
const useWebsocket = !!version || true const useWebsocket = !!version || true
const logUrl = `${window.location.protocol}//${externalController.hostname}:${externalController.port}/connections` const logUrl = `${externalController.protocol}//${externalController.hostname}:${externalController.port}/connections`
return new StreamReader<Snapshot>({ url: logUrl, bufferLength: 200, token: externalController.secret, useWebsocket }) return new StreamReader<Snapshot>({ url: logUrl, bufferLength: 200, token: externalController.secret, useWebsocket })
}) })

View File

@ -33,7 +33,7 @@ export class StreamReader<T> {
protected websocketLoop () { protected websocketLoop () {
const url = new URL(this.config.url) const url = new URL(this.config.url)
url.protocol = window.location.protocol === 'http:' ? 'ws:' : 'wss:' url.protocol = url.protocol === 'http:' ? 'ws:' : 'wss:'
url.searchParams.set('token', this.config.token ?? '') url.searchParams.set('token', this.config.token ?? '')
const connection = new WebSocket(url.toString()) const connection = new WebSocket(url.toString())