Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/chii.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ program
.option('--https', 'serve chii over https')
.option('--ssl-cert <cert>', 'provide an ssl certificate')
.option('--ssl-key <key>', 'provide an ssl key')
.option('--secrets <secret1,secret2...>', 'provide secrets', (value) => !value ? [] : value.split(','))
.action(options => {
server.start(options);
});
Expand Down
4 changes: 2 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ async function start({
sslCert,
sslKey,
basePath = '/',
secrets = [],
} = {}) {
domain = domain || 'localhost:' + port;
if (!endWith(basePath, '/')) {
basePath += '/';
}

const app = new Koa();
const wss = new WebSocketServer();
const wss = new WebSocketServer( secrets );

app.use(compress()).use(router(wss.channelManager, domain, cdn, basePath));

Expand Down
14 changes: 12 additions & 2 deletions server/lib/WebSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ const ChannelManager = require('./ChannelManager');
const query = require('licia/query');

module.exports = class WebSocketServer {
constructor() {
constructor( secrets ) {
this.channelManager = new ChannelManager();

const wss = (this._wss = new WebSocket.Server({ noServer: true }));

wss.on('connection', (ws, req) => {
const type = ws.type;
if (type === 'target') {
const { id, chiiUrl, title, favicon } = ws;
const { id, chiiUrl, title, favicon, secret: targetSecret } = ws;

const isSecretRequired = Array.isArray(secrets) && secrets.length > 0;
const isSecretValid = targetSecret && secrets.includes(targetSecret);

if (isSecretRequired && !isSecretValid) {
console.log('wrong or no secret provided');
ws.close();
}

let ip = req.socket.remoteAddress;
const userAgent = req.headers['user-agent'];
if (req.headers['x-forwarded-for']) {
Expand Down Expand Up @@ -47,6 +56,7 @@ module.exports = class WebSocketServer {
ws.favicon = q.favicon;
ws.userAgent = q.userAgent;
ws.rtc = q.rtc === 'true';
ws.secret = q.secret;
} else {
ws.target = q.target;
}
Expand Down
5 changes: 4 additions & 1 deletion src/target/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if (!startWith(serverUrl, 'http')) {
let embedded = false;
let rtc = false;
let cdn = '';
let secret = '';

const element = getTargetScriptEl();
if (element) {
Expand All @@ -41,6 +42,7 @@ if (element) {
rtc = true;
}
cdn = element.getAttribute('cdn') || '';
secret = element.getAttribute('secret') || '';
}

if (cdn && endWith(cdn, '/')) {
Expand All @@ -51,7 +53,7 @@ const sessionStore = safeStorage('session');

let id = sessionStore.getItem('chii-id');
if (!id) {
id = randomId(6);
id = randomId(32);
sessionStore.setItem('chii-id', id);
}

Expand All @@ -62,4 +64,5 @@ export {
rtc,
cdn,
id,
secret
};
3 changes: 2 additions & 1 deletion src/target/connectServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Socket from 'licia/Socket';
import query from 'licia/query';
import chobitsu from 'chobitsu';
import { serverUrl, id } from './config';
import { serverUrl, id, secret } from './config';
import { getFavicon } from './util';

let isInit = false;
Expand All @@ -24,6 +24,7 @@ export default function () {
title: (window as any).ChiiTitle || document.title,
favicon: getFavicon(),
'__chobitsu-hide__': true,
...(secret ? { secret } : {}),
})}`
);

Expand Down