summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortejr <tejr@sqt.wtf>2021-01-23 13:54:44 +1300
committertejr <tejr@sqt.wtf>2021-01-23 13:54:44 +1300
commita666416a32043a1082231a7af1a5b46581d8cf5f (patch)
tree3b345c92580dbcf8ad586469e4d61c0e686184fb
parentCreate directories (diff)
parentValidate filenames sent from clients (diff)
downloadsoundboard-a666416a32043a1082231a7af1a5b46581d8cf5f.tar.gz
soundboard-a666416a32043a1082231a7af1a5b46581d8cf5f.zip
Merge branch 'leonardus'
* leonardus: Validate filenames sent from clients Remove hardcoded host from script.js Rewrite soundboard server in Lua 5.1
-rw-r--r--Makefile4
-rw-r--r--config.toml9
-rw-r--r--script.js2
-rwxr-xr-xsoundboard99
-rw-r--r--soundboard-0.1-0.rockspec14
-rw-r--r--soundboard.service1
6 files changed, 90 insertions, 39 deletions
diff --git a/Makefile b/Makefile
index 6204d79..da74b0c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
PREFIX = /usr/local/soundboard
-WEBROOT = /var/www/soundboard.sqt.wtf/public_html
+WEBROOT = /var/www/html
all:
install:
mkdir -p -- $(WEBROOT)
cp -- index.php script.js $(WEBROOT)
mkdir -p -- $(PREFIX)/bin
- cp -- soundboard $(PREFIX)/bin
+ cp -- soundboard config.toml $(PREFIX)/bin
diff --git a/config.toml b/config.toml
new file mode 100644
index 0000000..8d2d2bc
--- /dev/null
+++ b/config.toml
@@ -0,0 +1,9 @@
+[ws_server]
+host = "localhost"
+port = 9000
+webroot = "/var/www/html"
+
+[ws_server.tls]
+enabled = true
+certificate_file = "/run/soundboard/localhost.crt"
+privkey_file = "/run/soundboard/localhost.key" \ No newline at end of file
diff --git a/script.js b/script.js
index fa3c95b..6dc23aa 100644
--- a/script.js
+++ b/script.js
@@ -19,7 +19,7 @@
};
}());
- var socket = new WebSocket("wss://soundboard.sqt.wtf:9000/");
+ var socket = new WebSocket("wss://" + location.host + ":9000/");
socket.onmessage = function (event) {
play(event.data);
};
diff --git a/soundboard b/soundboard
index 4f4f9c7..6eeba7e 100755
--- a/soundboard
+++ b/soundboard
@@ -1,42 +1,71 @@
-#!/usr/bin/env perl
+#!/usr/bin/env lua5.1
-package SQT::Soundboard::Server;
+local cqueues = require("cqueues")
+local http_headers = require("http.headers")
+local http_server = require("http.server")
+local http_tls = require("http.tls")
+local http_websocket = require("http.websocket")
+local lfs = require("lfs")
+local toml = require("toml")
+local ossl_context = require("openssl.ssl.context")
+local ossl_pkey = require("openssl.pkey")
+local ossl_x509 = require("openssl.x509")
-use strict;
-use warnings;
-use utf8;
+local function readfile(filename)
+ local fd = io.open(filename, "r")
+ local contents = fd:read("*a")
+ fd:close()
+ return contents
+end
-use 5.010;
+local config = toml.parse(readfile("config.toml"))
-our $VERSION = 0.1;
+local ctx
+if config.ws_server.tls.enabled == true then
+ ctx = http_tls.new_server_context()
+ ctx:setCertificate(ossl_x509.new(readfile(config.ws_server.tls.certificate_file)))
+ ctx:setPrivateKey(ossl_pkey.new(readfile(config.ws_server.tls.privkey_file)))
+end
-use Carp;
-use English qw(-no_match_vars);
-use IO::Socket::SSL;
-use Net::WebSocket::Server;
+local cqueue = cqueues.new()
+local clients = {}
-my $sock = IO::Socket::SSL->new(
- Listen => 5,
- LocalPort => 9000,
- Proto => 'tcp',
- SSL_cert_file =>
- '/run/soundboard/fullchain.pem',
- SSL_key_file =>
- '/run/soundboard/privkey.pem',
-) or croak( sprintf 'Failed to listen: %s', "$ERRNO" );
+cqueue:wrap(function()
+ local server = http_server.listen{
+ host = config.ws_server.host or "localhost",
+ port = config.ws_server.port or (config.ws_server.tls and 443 or 9000),
+ tls = config.ws_server.tls.enabled or false,
+ ctx = ctx or nil,
+ onstream = function(stream_server, stream)
+ local ws = http_websocket.new_from_stream(stream, stream:get_headers())
+ ws:accept()
+ clients[ws] = ws
+ while true do
+ local frame, opcode = ws:receive()
+ if opcode == "text" then
+ local exists = false
+ for fn in lfs.dir(config.ws_server.webroot .. "/sounds") do
+ if fn:lower() == frame:lower() and fn:gsub("[%s%.]","") ~= "" then
+ exists = true
+ end
+ end
+ if exists then
+ for _, client_sock in pairs(clients) do
+ client_sock:send(frame)
+ end
+ end
+ end
+ if frame == nil then break end
+ end
+ ws:close()
+ clients[ws] = nil
+ end,
+ onerror = function(...)
+ print(...)
+ end
+ }
+ server:listen()
+ server:loop()
+end)
-Net::WebSocket::Server->new(
- listen => $sock,
- on_connect => sub {
- my ( $serv, $conn ) = @_;
- $conn->on(
- utf8 => sub {
- my ( $self, $msg ) = @_;
- say $msg or carp('Failed to write to stdout');
- for ( $self->server->connections ) {
- $_->send_utf8($msg);
- }
- },
- );
- },
-)->start;
+cqueue:loop() \ No newline at end of file
diff --git a/soundboard-0.1-0.rockspec b/soundboard-0.1-0.rockspec
new file mode 100644
index 0000000..ae27c7e
--- /dev/null
+++ b/soundboard-0.1-0.rockspec
@@ -0,0 +1,14 @@
+package = "soundboard"
+version = "0.1-0"
+source = {
+ url = "https://dev.sqt.wtf/cgit/tejr/soundboard.git"
+}
+dependencies = {
+ "lua == 5.1",
+ "cqueues >= 20200726.51-0",
+ "http >= 0.3-0",
+ "luaossl >= 20200709-0",
+ "lua-toml >= 2.0-1",
+ "luafilesystem >= 1.8.0-1"
+}
+build = { type = "none" } \ No newline at end of file
diff --git a/soundboard.service b/soundboard.service
index f40ab55..8c81e1e 100644
--- a/soundboard.service
+++ b/soundboard.service
@@ -4,7 +4,6 @@ Description=Soundboard server
[Service]
RuntimeDirectory=%p
PermissionsStartOnly=true
-Environment=PERL5LIB=/usr/local/soundboard/lib/perl5
ExecStartPre=/bin/cp /etc/letsencrypt/live/soundboard.sqt.wtf/fullchain.pem /etc/letsencrypt/live/soundboard.sqt.wtf/privkey.pem %t/%p
ExecStartPre=/bin/chown soundboard.soundboard %t/%p/fullchain.pem %t/%p/privkey.pem
ExecStart=/usr/local/soundboard/bin/soundboard