Frontend: Add support for adding servers

This commit is contained in:
mrkubax10 2023-09-12 10:19:46 +02:00
parent 5830236ea5
commit f36cd5085f
4 changed files with 118 additions and 10 deletions

View File

@ -297,6 +297,31 @@ sub sendBadRequest {
$aClient->send($response);
}
sub sendForbidden {
my $aClient = $_[0];
my $aMessage = $_[1];
my $content = "<h1>403 Forbidden</h1><h6>irclogger_web</h6>Error: $aMessage";
my $response = getBaseResponse(403, "Forbidden");
$response.="Content-Type: text/html, charset=utf-8\r\n";
$response.="Content-Length: ".length($content)."\r\n\r\n";
$response.=$content;
$aClient->send($response);
}
sub sendConflict {
my $aClient = $_[0];
my $aMessage = $_[1];
my $content = "<h1>409 Conflict</h1><h6>irclogger_web</h6>Error: $aMessage";
my $response = getBaseResponse(409, "Conflict");
$response.="Content-Type: text/html, charset=utf-8\r\n";
$response.="Content-Length: ".length($content)."\r\n\r\n";
$response.=$content;
$aClient->send($response);
}
sub redirect {
my $aClient = $_[0];
my $aLocation = $_[1];

View File

@ -18,6 +18,7 @@ package frontend_routes;
use lib ".";
use frontend_session;
use logger;
use Digest::SHA;
@ -186,10 +187,10 @@ sub handlePath {
my $manageServers = "";
if($privileges==2) {
$manageServers.="<h3>Manage servers</h3>";
$manageServers.="<form action=\"add_server_action\" method=\"POST\">";
$manageServers.="<form action=\"manage_server_action\" method=\"POST\">";
$manageServers.="<input name=\"name\" type=\"text\" placeholder=\"Server name\" /><br />";
$manageServers.="<input name=\"address\" type=\"text\" placeholder=\"Server address\" /> ";
$manageServers.="<input name=\"port\" type=\"number\" placeholder=\"Server port\" /><br />";
$manageServers.="<input name=\"port\" type=\"number\" placeholder=\"Server port (optional)\" /><br />";
$manageServers.="<input type=\"submit\" value=\"Add\" />";
$manageServers.="</form>";
}
@ -197,7 +198,7 @@ sub handlePath {
my $manageChannels = "";
if($privileges==2) {
$manageChannels.="<h3>Manage channels</h3>";
$manageChannels.="<form action=\"add_channel_action\" method=\"POST\">";
$manageChannels.="<form action=\"manage_channel_action\" method=\"POST\">";
$manageChannels.="<input name=\"channel\" type=\"text\" placeholder=\"Channel\" /> at ";
$manageChannels.="<select name=\"server\">";
$query = $aConnection->prepare(qq(select id, name from servers;));
@ -271,6 +272,65 @@ sub handlePath {
return 1;
}
when("/manage_server_action") {
if(!defined($aRequest->{"cookies"}{"session"}) || !frontend_session::isValidSession($aRequest->{"cookies"}{"session"})) {
frontend::redirect($aClient, "/");
return 1;
}
if(defined($aRequest->{"headers"}{"Content-Type"}) && $aRequest->{"headers"}{"Content-Type"} ne "application/x-www-form-urlencoded") {
frontend::sendBadRequest($aClient, "Unsupported form Content-Type (application/x-www-form-urlencoded required)");
return 1;
}
if(!defined($aRequest->{"content"})) {
frontend::sendBadRequest($aClient, "Request content required");
return 1;
}
my $session = $frontend_session::sessions{$aRequest->{"cookies"}{"session"}};
my $query = $aConnection->prepare(qq(select privileges from users where name=?;));
$query->execute($session->{"username"});
my @row = $query->fetchrow_array();
if($row[0]<2) {
frontend::sendForbidden($aClient, "Insufficient permissions to perform this operation");
return 1;
}
my %parameters = frontend::parsePathParameters($aRequest->{"content"});
if(!defined($parameters{"name"})) {
frontend::sendBadRequest($aClient, "Server name required");
return 1;
}
if(!defined($parameters{"address"})) {
frontend::sendBadRequest($aClient, "Server address required");
return 1;
}
my $port = 6667;
if(defined($parameters{"port"}) && length($parameters{"port"})>0) {
$port = $parameters{"port"};
}
$query = $aConnection->prepare(qq(select id from servers where name=?;));
$query->execute($parameters{"name"});
@row = $query->fetchrow_array();
if(scalar(@row)>0) {
frontend::sendConflict($aClient, "Server with name $parameters{'name'} already exists");
return 1;
}
$query = $aConnection->prepare(qq(select id from servers order by rowid desc limit 1;));
$query->execute();
@row = $query->fetchrow_array();
my $lastID = 0;
if(scalar(@row)>0) {
$lastID = $row[0]+1;
}
$query = $aConnection->prepare(qq(insert into servers values($lastID, ?, ?, ?);));
$query->execute($parameters{"name"}, $parameters{"address"}, $port);
frontend::redirect($aClient, "/server_added.html");
logger::createLogger($parameters{"name"}, $parameters{"address"}, $port, ());
return 1;
}
when("/view_logs") {
my $channelID = $aRequest->{"path"}{"parameters"}{"channel"};
if(!defined($channelID)) {

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
package logger;
use IO::Socket;
use List::Util;
use Time::Piece;
@ -260,10 +262,11 @@ sub connectionWorker {
my $aPort = $_[1];
my $aServerName = $_[2];
my $aChannels = $_[3];
my $aConnection = $_[4];
my %logFiles;
my $stream = connectToServer($aHost, $aPort, $aServerName);
while(!eof($stream)) {
while(!eof($stream) && $aConnection->[1]) {
my $line = readline($stream);
my @command = parseIRCCommand($line);
printf(":: Server -> %s", $line);
@ -280,6 +283,19 @@ sub connectionWorker {
close($stream);
}
our @connections;
sub createLogger {
my $aName = $_[0];
my $aHost = $_[1];
my $aPort = $_[2];
my $aChannels = $_[3];
my @connection = ($aName, 1);
push(@connection, threads->create("connectionWorker", $aHost, $aPort, $aName, $aChannels, \@connection));
push(@connections, @connection);
}
my $db = DBI->connect("DBI:SQLite:dbname=$configuration::database", "", "", {RaiseError=>1});
my $query = $db->prepare(qq(select * from servers;));
$query->execute();
@ -296,10 +312,7 @@ while(my @row = $query->fetchrow_array()) {
my $name = $channelsRow[0];
push(@channels, $name);
}
threads->create("connectionWorker", $host, $port, $name, \@channels);
}
foreach my $thread (threads->list(threads::running)) {
$thread->join();
createLogger($name, $host, $port, \@channels);
}
$db->disconnect();
1;

10
static/server_added.html Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Server added</title>
</head>
<body>
<p>Server successfully added</p>
<a href="/panel">Return to user panel</a>
</body>
</html>