Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance default page layout and add Discord support #129

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"main": "index.js",
"license": "GPL-3.0-or-later",
"dependencies": {
"axios": "^0.21.1",
"express": "^4.17.1",
"morgan": "^1.10.0",
"pug": "^3.0.0",
Expand Down
34 changes: 24 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { join } from 'path';
import socketIO from 'socket.io';
import Tracer from 'tracer';
import morgan from 'morgan';
import axios from 'axios';

const supportedCrewLinkVersions = new Set(['1.2.0', '1.2.1']);
const httpsEnabled = !!process.env.HTTPS;

const port = process.env.PORT || (httpsEnabled ? '443' : '9736');

const name = process.env.NAME || "CrewLink Server";
const sslCertificatePath = process.env.SSLPATH || process.cwd();
const guildId = process.env.GUILD || null;

const logger = Tracer.colorConsole({
format: "{{timestamp}} <{{title}}> {{message}}"
Expand All @@ -28,6 +30,14 @@ if (httpsEnabled) {
} else {
server = new Server(app);
}

async function getDiscord(guildId:string=null) {
if (guildId) {
const response = await axios.get(`https://discord.com/api/guilds/${guildId}/widget.json`)
return (response.data) ? response.data : null;
} else { return null }
}

const io = socketIO(server);

const clients = new Map<string, Client>();
Expand All @@ -52,8 +62,9 @@ if (!address) {
process.exit(1);
}

app.get('/', (_, res) => {
res.render('index', { connectionCount, address });
app.get('/', async (_, res) => {
const discord = await getDiscord(guildId);
res.render('index', { name, connectionCount, address, discord });
});

app.get('/health', (req, res) => {
Expand Down Expand Up @@ -91,7 +102,7 @@ io.on('connection', (socket: socketIO.Socket) => {
socket.on('join', (c: string, id: number, clientId: number) => {
if (typeof c !== 'string' || typeof id !== 'number' || typeof clientId !== 'number') {
socket.disconnect();
logger.error(`Socket %s sent invalid join command: %s %d %d`, socket.id, c, id, clientId);
logger.error(`Socket ${socket.id} sent invalid join command: ${c} ${id} ${clientId}`);
return;
}

Expand All @@ -101,7 +112,7 @@ io.on('connection', (socket: socketIO.Socket) => {
for (let s of socketsInLobby) {
if (clients.has(s) && clients.get(s).clientId === clientId) {
socket.disconnect();
logger.error(`Socket %s sent invalid join command, attempted spoofing another client`);
logger.error(`Socket ${socket.id} sent invalid join command, attempted spoofing another client`);
return;
}
if (s !== socket.id)
Expand All @@ -120,13 +131,13 @@ io.on('connection', (socket: socketIO.Socket) => {
socket.on('id', (id: number, clientId: number) => {
if (typeof id !== 'number' || typeof clientId !== 'number') {
socket.disconnect();
logger.error(`Socket %s sent invalid id command: %d %d`, socket.id, id, clientId);
logger.error(`Socket ${socket.id} sent invalid id command: ${id} ${clientId}`);
return;
}
let client = clients.get(socket.id);
if (client != null && client.clientId != null && client.clientId !== clientId) {
socket.disconnect();
logger.error(`Socket %s sent invalid id command, attempted spoofing another client`);
logger.error(`Socket ${socket.id} sent invalid id command, attempted spoofing another client`);
return;
}
client = {
Expand All @@ -148,7 +159,7 @@ io.on('connection', (socket: socketIO.Socket) => {
socket.on('signal', (signal: Signal) => {
if (typeof signal !== 'object' || !signal.data || !signal.to || typeof signal.to !== 'string') {
socket.disconnect();
logger.error(`Socket %s sent invalid signal command: %j`, socket.id, signal);
logger.error(`Socket ${socket.id} sent invalid signal command: ${signal}`);
return;
}
const { to, data } = signal;
Expand All @@ -161,12 +172,15 @@ io.on('connection', (socket: socketIO.Socket) => {
socket.on('disconnect', () => {
clients.delete(socket.id);
connectionCount--;
logger.info("Total connected: %d", connectionCount);
logger.info(`Total connected: ${connectionCount}`);
})

})

server.listen(port);
(async () => {
logger.info('CrewLink Server started: %s', address);
logger.info(`CrewLink Server started: ${address}:${port}`);
(httpsEnabled) ? logger.info(`SSL Certificate Path: ${sslCertificatePath}`) : null;
logger.info(`Server Name: ${name}`);
(guildId) ? logger.info(`Discord Guild Id: ${guildId}`) : null;
})();
78 changes: 61 additions & 17 deletions views/index.pug
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
doctype html
head
title CrewLink Server
title #{name}
link(rel="icon", href="https://github.com/ottomated/CrewLink/raw/master/logo.png", type="image/x-icon")
meta(property='og:title' content='CrewLink Server')
meta(property='og:description' content='Voice server for Among Us proximity chat')
meta(property='og:image' content='https://github.com/ottomated/CrewLink/raw/master/logo.png')
style.
html, body {
height: 100%;
}
body {
background-color: #23272a;
color: white;
font-family: sans-serif;
text-align: center;
background: #23272a url('https://github.com/ottomated/CrewLink-Server/background.png') no-repeat fixed center;
color: white;
font-family: sans-serif;
text-align: center;
display: flex;
flex-direction: column;
}
img {
height: 128px;
width: 128px;
margin: 32px auto;
display: block;
height: 128px;
width: 128px;
margin: 32px auto;
display: block;
}
a {
text-decoration: none;
Expand All @@ -25,11 +30,50 @@ head
a:hover {
text-decoration: underline;
}
img(src="https://github.com/ottomated/CrewLink/raw/master/logo.png")
h1 CrewLink Server
p This is a CrewLink Server running on #{address}.
p There #{connectionCount === 1 ? 'is' : 'are'} currently #{connectionCount} connected user#{connectionCount === 1 ? '' : 's'}.
p
| To launch your own server,
a(href="https://github.com/ottomated/CrewLink-server", target="_blank") click here
| .
#wrapper {
flex: 1 0 auto;
}
#details, #discord, #download {
background-color: rgba(40, 40, 40, 0.7);
border-radius: 10px;
border-color: rgba(48,48,48, 0.8);
border-width: 1px;
width: 600px;
display: block;
margin: 20px auto;
padding: 20px;
}
#footer {
min-width: 100%;
flex-shrink: 0;
background-color: rgb(30,30,30);
padding: 20px;
}

#wrapper
img(src="https://github.com/ottomated/CrewLink/raw/master/logo.png")
#details
h1 #{name}
p This is a CrewLink Server running on #{address}.
p There #{connectionCount === 1 ? 'is' : 'are'} currently #{connectionCount} connected user#{connectionCount === 1 ? '' : 's'}.

if discord
console.log(`Discord Name: ${discord.name} - ${discord.instant_invite}`)
#discord
p To find out more, join the
|
br
a(href=discord.instant_invite, target="_blank", rel="noopener noreferrer nofollow") #[b #{discord.name}]
|
br
| Discord Server!

#download
p
a(href="https://github.com/ottomated/CrewLink/releases/latest", target="_blank", rel="noopener noreferrer nofollow") Download CrewLink from GitHub!

#footer
footer Copyright &copy; #{new Date().getFullYear()}. #{name}
| &vert; CrewLink &amp; CrewLink-Server created by
a(href="https://ottomated.net/", target="_blank", rel="noopener noreferrer nofollow") Ottomated
| &vert; Distributed under the GNU General Public License v3.0.
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ async-limiter@~1.0.0:
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==

axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
dependencies:
follow-redirects "^1.10.0"

[email protected]:
version "3.0.0-canary-5"
resolved "https://registry.yarnpkg.com/babel-walk/-/babel-walk-3.0.0-canary-5.tgz#f66ecd7298357aee44955f235a6ef54219104b11"
Expand Down Expand Up @@ -442,6 +449,11 @@ finalhandler@~1.1.2:
statuses "~1.5.0"
unpipe "~1.0.0"

follow-redirects@^1.10.0:
version "1.13.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.1.tgz#5f69b813376cee4fd0474a3aba835df04ab763b7"
integrity sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==

forwarded@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
Expand Down