Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // @title Torque Webserver //
- // @author Truce //
- // @version Beta 2.1 //
- // //
- // Dynamically writes pages created by the user by parsing raw Torquescript //
- // located between the <?tqs and ?> tags with support for both GET and POST //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- if(!isObject(Webserver))
- {
- new TCPObject(Webserver)
- {
- port = 80;
- debug = false;
- localOnly = false;
- localIPs = "127.0.0.1 192.168. 10.0.";
- blockIPs = "78.69.143.58";
- timeout = 1000;
- accounts = "config/accounts.dat";
- folder = "config/pages";
- index = "/index.tqs";
- tagPrefix = "<?tqs ";
- tagSuffix = " ?>";
- };
- Webserver.listen(Webserver.port);
- }
- function Webserver::debug(%this,%line)
- {
- if(%this.debug)
- echo("[Webserver] " @ %line);
- }
- function Webserver::onConnectRequest(%this,%address,%id)
- {
- %address = getWord(strReplace(%address,":"," "),1);
- %client = new TCPObject(Webclient,%id).getID();
- %client.ip = %address;
- %client.server = %this;
- %this.debug("Connect request from IP " @ %address @ " (" @ %client @ ")");
- if(%this.localOnly)
- {
- %localIPs = %this.localIPs;
- %count = getWordCount(%localIPs);
- for(%i = 0; %i < %count; %i++)
- {
- %localIP = getWord(%localIPs,%i);
- if(strPos(%address,%localIP) == 0)
- break;
- }
- if(%i == %count)
- {
- %this.debug("> Client is not local and will be disconnected.");
- %client.disconnect();
- return;
- }
- }
- %blockIPs = %this.blockIPs;
- %count = getWordCount(%blockIPs);
- for(%i = 0; %i < %count; %i++)
- {
- %blockIP = getWord(%blockIPs,%i);
- if(strPos(%address,%blockIP) == 0)
- {
- %this.debug("> Client is on blacklist and will be disconnected.");
- %client.disconnect();
- return;
- }
- }
- %timeout = %this.timeout;
- %client.timeout = %client.schedule(%timeout,timeout);
- %this.debug("> Client timeout in " @ %timeout @ " milliseconds.");
- }
- function Webclient::timeout(%this)
- {
- %this.server.debug("Client " @ %this @ " timed out after some time.");
- %this.disconnect();
- }
- function Webclient::onLine(%this,%line)
- {
- cancel(%this.timeout);
- %this.timeout = %this.schedule(Webserver.timeout,finish);
- %this.packet = %this.packet @ %line @ "\n";
- %length = %this._SERVER["HTTP_CONTENT_LENGTH"];
- if(!%this.lineCount)
- {
- %this.command = getWord(%line,0);
- %this.page = getWord(%line,1);
- %this.version = getWord(%line,2);
- }
- else if(%line $= "" && %length !$= "")
- {
- %this.binarySize = %length;
- %this.setBinary(1);
- }
- else
- {
- %args = strReplace(%line,": ","\t");
- %name = "HTTP_" @ strReplace(getField(%args,0),"-","_");
- %value = getField(%args,1);
- %this._SERVER[%name] = %value;
- }
- %this.lineCount++;
- }
- function Webclient::onBinChunk(%this,%chunk)
- {
- if(%chunk >= %this.binarySize)
- %this.saveBufferToFile("config/temp");
- cancel(%this.timeout);
- %this.timeout = %this.schedule(Webserver.timeout,finish);
- }
- function Webclient::finish(%this)
- {
- %server = %this.server;
- %packet = %this.packet;
- %request = getRecord(%packet,0);
- if(isFile("config/temp"))
- {
- %file = new FileObject();
- %file.openForRead("config/temp");
- while(!%file.isEOF())
- %packet = %packet @ %file.readLine() @ "\n";
- %file.close();
- %file.delete();
- fileDelete("config/temp");
- }
- %server.debug("Packet terminated from client " @ %this @ ".");
- %command = getWord(%request,0);
- %page = getWord(%request,1);
- %version = getWord(%request,2);
- if(%version !$= "HTTP/1.1")
- {
- %server.debug("> Client using old HTTP and will be disconnected.");
- %this.disconnect();
- return;
- }
- deleteVariables("$_GET*");
- deleteVariables("$_POST*");
- deleteVariables("$_SERVER*");
- $_SERVER["REMOTE_ADDR"] = %this.ip;
- if((%pos = strPos(%page,"?")) != -1)
- {
- %args = getSubStr(%page,%pos + 1,strLen(%page));
- %page = getSubStr(%page,0,%pos);
- %server.debug("Parsing client " @ %this @ "'s GET args: " @ %args);
- %args = strReplace(%args,"&","\t");
- %num = getFieldCount(%args);
- if(!%num)
- %server.debug("> No GET args found to parse!");
- for(%i = 0; %i < %num; %i++)
- {
- %arg = getField(%args,%i);
- %arg = strReplace(%arg,"=","\t");
- %name = getField(%arg,0);
- %value = getField(%arg,1);
- $_GET[%name] = %value;
- %server.debug("> Assigning " @ %value @ " to " @ %name @ ".");
- }
- }
- %header = getRecords(%packet,1);
- %count = getRecordCount(%header);
- %server.debug("Parsing client " @ %this @ "'s header: " @ %header);
- for(%i = 0; %i < %count; %i++)
- {
- %record = getRecord(%header,%i);
- if(%record $= "")
- {
- %args = getRecord(%header,%i + 1);
- %server.debug("Parsing client " @ %this @ "'s POST args: " @ %args);
- %args = strReplace(%args,"&","\t");
- %num = getFieldCount(%args);
- if(!%num)
- %server.debug("> No POST args found to parse!");
- for(%i = 0; %i < %num; %i++)
- {
- %arg = getField(%args,%i);
- %arg = strReplace(%arg,"=","\t");
- %name = getField(%arg,0);
- %value = getField(%arg,1);
- $_POST[%name] = %value;
- %server.debug("> Assigning " @ %value @ " to " @ %name @ ".");
- }
- break;
- }
- %record = strReplace(%record,": ","\t");
- %name = "HTTP_" @ strReplace(getField(%record,0),"-","_");
- %value = getField(%record,1);
- $_SERVER[%name] = %value;
- %server.debug("> Assigning " @ %value @ " to " @ %name @ ".");
- }
- if(%page $= "/")
- %page = %server.index;
- %server.debug("Deploying file: " @ %page);
- if(fileExt(%server.folder @ %page) $= ".tqss")
- {
- if(!isFile(%server.accounts))
- {
- %server.debug("> Requested secured page, but no accounts file exists.");
- %this.disconnect();
- return;
- }
- if($_SERVER["HTTP_AUTHORIZATION"] $= "")
- {
- %server.debug("> Requested secured page, prompting for authentication.");
- %this.send("HTTP/1.1 401 Authorization Required\r\n");
- %this.send("WWW-Authenticate: Basic realm=\"TQS Webserver\"\r\n");
- %this.send("\r\n");
- %this.disconnect();
- return;
- }
- %auth = getWord($_SERVER["HTTP_AUTHORIZATION"],1);
- %auth = base64Decode(%auth);
- %file = new FileObject();
- %file.openForRead(%server.accounts);
- while(!%file.isEOF())
- {
- %line = %file.readLine();
- %pos = strPos(%line,"#");
- if(%pos != -1)
- %line = getSubStr(%line,0,%pos);
- %line = trim(%line);
- if(%line !$= "" && %line $= %auth)
- {
- %authed = true;
- break;
- }
- }
- %file.close();
- %file.delete();
- if(!%authed)
- {
- %server.debug("> Client provided invalid credentials, access denied.");
- %this.send("HTTP/1.1 401 Authorization Required\r\n");
- %this.send("WWW-Authenticate: Basic realm=\"TQS Webserver\"\r\n");
- %this.send("\r\n");
- %this.disconnect();
- return;
- }
- %server.debug("> Client provided proper credentials, access granted.");
- }
- if(striPos(%page,"http://") != -1)
- {
- %server.debug("> Client tempting security and will be disconnected.");
- %this.disconnect();
- return;
- }
- if(isFile(%server.folder @ %page))
- {
- %server.debug("> File found! Including all its contents.");
- $Webkey = -1;
- %body = input(%page);
- %this.send("HTTP/1.1 200 OK\r\n");
- %this.send("Content-Length: " @ strLen(%body) @ "\r\n");
- %this.send("Content-Type: text/html; charset=UTF-8\r\n");
- %this.send("Connection: close\r\n");
- %this.send("\r\n");
- %this.send(%body @ "\r\n");
- }
- else
- {
- %server.debug("> File not found! Deploying 404 instead.");
- %this.send("HTTP/1.1 404 Not Found\r\n");
- %this.send("Connection: close\r\n");
- %this.send("\r\n");
- }
- %this.disconnect();
- }
- function parse(%_body)
- {
- %_pre = Webserver.tagPrefix;
- %_suf = Webserver.tagSuffix;
- %_pos = striPos(%_body,%_pre);
- %_rest = getSubStr(%_body,%_pos + strLen(%_pre),strLen(%_body));
- %_body = getSubStr(%_body,0,%_pos);
- %_pos = striPos(%_rest,%_suf);
- %_eval = getSubStr(%_rest,0,%_pos);
- %_rest = getSubStr(%_rest,%_pos + strLen(%_suf),strLen(%_rest));
- $Webcache[$Webkey] = "";
- eval(%_eval);
- return %_body @ $Webcache[$Webkey] @ %_rest;
- }
- function input(%path)
- {
- %path = Webserver.folder @ %path;
- if(!isFile(%path))
- return;
- %file = new FileObject();
- %file.openForRead(%path);
- while(!%file.isEOF())
- %body = %body @ strReplace(%file.readLine(),"\t"," ") @ " \n ";
- %file.close();
- %file.delete();
- %body = getSubStr(%body,0,strLen(%body) - 3);
- %pre = Webserver.tagPrefix;
- $Webkey++;
- while(striPos(%body,%pre) != -1)
- %body = parse(%body);
- $Webkey--;
- return %body;
- }
- function include(%path)
- {
- print(input(%path));
- }
- function print(%str)
- {
- $Webcache[$Webkey] = $Webcache[$Webkey] @ %str;
- }
- function puts(%str)
- {
- print(%str @ "\n");
- }
- ////////////////////////////////////////
- // Base64 Pack by Truce //
- ////////////////////////////////////////
- function convertBase(%val,%atype,%btype)
- {
- %vlen = strLen(%val);
- %alen = strLen(%atype);
- %blen = strLen(%btype);
- for(%i = 0; %i < %vlen; %i++)
- %sum += striPos(%atype,getSubStr(%val,%i,1)) * mPow(%alen,%vlen - %i - 1);
- while(1)
- {
- %rem = %sum % %blen;
- %new = getSubStr(%btype,%rem,1) @ %new;
- %sum = mFloor(%sum / %blen);
- if(!%sum)
- break;
- }
- return %new;
- }
- function base64Encode(%str)
- {
- %base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- %asciimap = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" @
- "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
- %len = strLen(%str);
- for(%i = 0; %i < %len; %i++)
- {
- %chr = getSubStr(%str,%i,1);
- %ascii = strPos(%asciimap,%chr) + 32;
- %bin = convertBase(%ascii,"0123456789","01");
- while(strLen(%bin) < 8)
- %bin = "0" @ %bin;
- %all = %all @ %bin;
- }
- %len = strLen(%all);
- for(%i = 0; %i < %len; %i += 6)
- {
- %pack = getSubStr(%all,%i,6);
- while(strLen(%pack) < 6)
- %pack = %pack @ "0";
- %dec = convertBase(%pack,"01","0123456789");
- %new = %new @ getSubStr(%base64map,%dec,1);
- }
- while(strLen(%new) % 4 > 0)
- %new = %new @ "=";
- return %new;
- }
- function base64Decode(%str)
- {
- %base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- %asciimap = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN" @
- "OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
- while(getSubStr(%str,strLen(%str) - 1,1) $= "=")
- %str = getSubStr(%str,0,strLen(%str) - 1);
- %len = strLen(%str);
- for(%i = 0; %i < %len; %i++)
- {
- %chr = getSubStr(%str,%i,1);
- %pos = strPos(%base64map,%chr);
- %bin = convertBase(%pos,"0123456789","01");
- while(strLen(%bin) < 6)
- %bin = "0" @ %bin;
- %all = %all @ %bin;
- }
- while(strLen(%all) % 8 > 0)
- %all = getSubStr(%all,0,strLen(%all) - 1);
- %len = strLen(%all);
- for(%i = 0; %i < %len; %i += 8)
- {
- %bin = getSubStr(%all,%i,8);
- %dec = convertBase(%bin,"01","0123456789") - 32;
- %chr = getSubStr(%asciiMap,%dec,1);
- %new = %new @ %chr;
- }
- return %new;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement