From 3cf8fa031f0b3596f859bfcf073d6098bb401323 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Tue, 25 Aug 2020 13:21:57 -0600 Subject: [PATCH] Moved to using websockets --- API/decard.pl | 79 ----------------------------------------------- Database/base.sql | 32 ------------------- README.md | 5 ++- Server/server.pl | 71 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 114 deletions(-) delete mode 100644 API/decard.pl delete mode 100644 Database/base.sql create mode 100755 Server/server.pl diff --git a/API/decard.pl b/API/decard.pl deleted file mode 100644 index fd752d6..0000000 --- a/API/decard.pl +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use warnings; -use JSON; -# use database engine - -sub get_request_info { - my $clientIP = ($ENV{"REMOTE_ADDR"} or "0.0.0.0"); - my $proto = ($ENV{"REQUEST_SCHEME"} or "http"); - my $get_query = (split(/\?/,($ENV{"QUERY_STRING"} or "")))[0]; - my $post_query = ""; - if ( $ENV{"CONTENT_LENGTH"} ) { read( , $post_query, $ENV{"CONTENT_LENGTH"}); } - my $referrer = ($ENV{"HTTP_REFERER"} or ""); - return ($proto,$clientIP,$get_query,$post_query,$referrer); -} - -sub http_status { - my ($status,$content,$target) = @_; - my $header = ""; - $header .= "status: ".$status."\r\n"; - $header .= "Location: ".$target."\r\n" if $target; - $header .= "Content-Type: ".$content."\r\n" if $content; - $header .= "\r\n"; - return $header; -} - -sub html_tag { - if ($_[1]) { return "<".($_[0] or "div").($_[2] or "").">".($_[1] or "")."\n"; - } else {return "<".($_[0] or "div").($_[2] or "")." />\n"; - } -} - -sub html_content { - return html_tag("html", html_tag( "head", html_tag( "title", shift ) . shift ) . html_tag( "body", shift ) ); -} - -sub soft_die { - print http_status(500,"text/html; charset=utf-8"); - print html_content("500","",shift); - exit; -} - -sub clean_input { - my $input = shift; - if ($input =~ m!%2F!) { soft_die( "Location/hax\r\n\r\n"; } - $input =~ s!%(..)!chr hex $1!ge; - $input =~ s!\+! !g; - return $input; -} - -my @request = get_request_info(); -my @get_params = split( "!", clean_input($request[2]) ); -# my @post_params -my $directive = shift(@get_params); -if ( "new" eq $directive ) { -# new session, new deck, new location -# if deck/location parent sessionID -# if location public/private -# returns id and key of new object -} elsif ( "shuffle" eq $directive ) { -# shuffle - deckID - full/current - deckKey -# sheffles all cards in deckID's locationID if deckKey matches -} elsif ( "move" eq $directive ) { -# move - cardID - current locationID - current locationKey - destination locationID -# changes ownership of cardID to destination locationID if cardID is owned by current locationID and the current locationKey matchs -} elsif ( "getLocations" eq $directive ) { -# getLocations - sessionID - sessionKey -# return all locationIDs associated with sessionID as long as sessionKey matches -} elsif ( "getCards" eq $directive ) { -# getCards - locationID - locationKey -# returns all cardIDs for locationID as long as its public or the locationKey matches -} elsif ( "getDecks" eq $directive ) { -# getDecks - sessionID - sessionKey -# returns all deckIDs associated with sessionID provided sessionKey matchs. -} else { - print http_status(200,"text/json"); - print '{"error":"No input or incorrect input", "input":"'.$directive.'!'.join("!",@get_params).'"}'; -} diff --git a/Database/base.sql b/Database/base.sql deleted file mode 100644 index c1f1a09..0000000 --- a/Database/base.sql +++ /dev/null @@ -1,32 +0,0 @@ -create tablespace deckard_space - OWNER deckard - LOCATION '\tmp\deckard'; -create database deckard WITH - OWNER=deckard - TABLESPACE=deckard_space; -create table sessions ( - sessionID UUID PRIMARY KEY, - sessionKey VARCHAR (256) NOT NULL, - last_update TIMESTAMP NOT NULL -); -create table locations ( - locationID UUID PRIMARY KEY, - locationKey VARCHAR (256) NOT NULL, - sessionID UUID REFERENCES sessions(sessionID), - locationType text CHECK (locationType = 'deck' or 'hand' or 'discard'), - showPublic boolean NOT NULL -); -create table decks ( - deckID UUID PRIMPARY KEY, - deckKey VARCHAR (256) NOT NULL, - sessionID UUID REFERENCES sessions(sessionID), - locationID UUID REFERENCES locations(locationID) -); -create table cards ( - cardID UUID PRIMARY KEY - sessionID UUID REFERENCES sessions(sessionID), - locationID UUID REFERENCES locations(locationID), - deckID UUID REFERENCES decks(deckID), - cardContent text NOT NULL, - position integer NOT NULL -); diff --git a/README.md b/README.md index 2e4a0d9..72abbb7 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,7 @@ This project is an exansion on my original idea with [Deckard](https://labs.murk ### Goals: -* Implement database backend -* Implement API backend +* Implement websocket server backend * Session creation * Deck upload and sanitization * Multipul Mixed and Separate decks per session @@ -21,4 +20,4 @@ Not yet written ### Original Deckard project -[For developing new decks](https://labs.murkfall.net/bluesaxman/deckard) \ No newline at end of file +[For developing new decks](https://labs.murkfall.net/bluesaxman/deckard) diff --git a/Server/server.pl b/Server/server.pl new file mode 100755 index 0000000..446c532 --- /dev/null +++ b/Server/server.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl -w +use strict; +use warnings; +use Net::WebSocket::Server; +use JSON; + +my $origin = 'ws://localhost'; +my %sessions = (); + +sub generateSession { + my $newSession = shift; + unless (defined $newSession) { $newSession = time; } + if ( grep( /$newSession/, keys %sessions ) ) { + generateSession(); + } else { + print "Adding session ".$newSession." to database\n"; + $sessions{$newSession} = (); + $sessions{$newSession}{"id"} = $newSession; + $sessions{$newSession}{"users"} = []; + $sessions{$newSession}{"decks"} = (); + $sessions{$newSession}{"pools"} = (); + + } + return $newSession; +} + +sub joinSession { + my ($sessionID, $conn) = @_; + print "Connection attempts to use session ".$sessionID."\n"; + unless ( grep( /$sessionID/, keys %sessions ) ) { + print "Session ".$sessionID." does not yet exsist, creating...\n"; + $sessionID = generateSession($sessionID); + } + push(@{$sessions{$sessionID}{"users"}}, $conn->{"initHand"}->res->key); + $conn->{"currentSession"} = $sessions{$sessionID}; + print "A client has connected to session: ".$conn->{"currentSession"}{"id"}."\n"; +} + +Net::WebSocket::Server->new( + listen => 8080, + on_connect => sub { + my ($serv, $conn) = @_; + $conn->on( + handshake => sub { + my ($conn,$handshake) = @_; + $conn->{"initHand"} = $handshake; + }, + ready => sub { + my ($conn) = @_; + #strip the / off of the resource request and then assign just the request to $session + my $session = (split("/", $conn->{"initHand"}->req->resource_name))[1]; + unless ($session) { $conn->send_utf8(generateSession()); $conn->disconnect(); print "Severed client, no session given\n"; } + joinSession($session,$conn); + #Check for user ID, if none $conn->disconnect("noid",generateUser()); + }, + utf8 => sub { + my ($conn, $msg) = @_; + my $sessionID = $conn->{"currentSession"}{"id"}; + my $messageData = ""; + eval { $messageData = from_json($msg) }; + if ($@) { $conn->send_utf8('{"error":1, "message":"ERROR: Invalid json"}'); return 0;} + if ($messageData->{"type"} eq "message") { + print "'".$messageData->{"message"}."' sent in session ".$sessionID."\n"; + for ($conn->server->connections) { + if ($_->{"currentSession"}{"id"} == $sessionID) { $_->send_utf8(to_json($messageData)); } + } + } + }, + ); + }, +)->start;