From bec496715edd64c7ea5b1f00d6dbaa709f665e5a Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Fri, 26 Aug 2016 17:40:14 +0300 Subject: [PATCH] 1.5 ? :D --- .gitignore | 6 ++ .vscode/tasks.json | 7 +++ 403.html | 5 -- 404.html | 5 -- config.yml | 13 ++-- resources/config.yml | 16 +++-- resources/handler.php | 68 ++++++++++++++++++--- router.php | 68 ++++++++++++++++++--- src/Ad5001/Online/Main.php | 37 ++++++++--- src/Ad5001/Online/OnlineTask.php | 98 ------------------------------ src/Ad5001/Online/isOnlineTask.php | 31 ---------- 11 files changed, 181 insertions(+), 173 deletions(-) create mode 100644 .vscode/tasks.json delete mode 100644 403.html delete mode 100644 404.html delete mode 100644 src/Ad5001/Online/OnlineTask.php delete mode 100644 src/Ad5001/Online/isOnlineTask.php diff --git a/.gitignore b/.gitignore index cd2946a..005e60a 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,9 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk + + +# Github files, Junk files +.github +.router.php +.config.yml \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..be046c1 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,7 @@ +{ + "version": "0.1.0", + "command": "start", + "isShellCommand": true, + "args": ["cmd", "/C", "C:\\Program Files\\Git\\commit-signed.cmd"], + "showOutput": "always" +} \ No newline at end of file diff --git a/403.html b/403.html deleted file mode 100644 index 87140c9..0000000 --- a/403.html +++ /dev/null @@ -1,5 +0,0 @@ - - -403 FORBIDDEN - -Error. You don't have the peremission to view this page or to perform this action ! \ No newline at end of file diff --git a/404.html b/404.html deleted file mode 100644 index 8778804..0000000 --- a/404.html +++ /dev/null @@ -1,5 +0,0 @@ - - -404 NOT FOUND - -Error. File not found ! \ No newline at end of file diff --git a/config.yml b/config.yml index b8dc36c..362909b 100644 --- a/config.yml +++ b/config.yml @@ -10,10 +10,15 @@ index: index.html 403: 403.html # Here you can config pages that user will not be able to see ! -denied-pages: - - /config.yml - - /router.php +denied-pages: [] + +# Your domains (one per time). To redirect a domain here, put one per line then go to your registar CPanel and add a CName record that redirects to it ! +Domains: [] + +#Parked domains: Domains that redirects to one of your domains. Same setup as shown above. +Parked domains: [] + # Here you can choose if the server automaticly disable itself on server stop. -# DO NOT DISABLE THIS OPTION WHEN YOU'RE USING AN HOSTER ! OR YOU WON4T BE ABLE TO STOP IT ! +# DO NOT DISABLE THIS OPTION WHEN YOU'RE USING AN HOSTER ! OR YOU WON'T BE ABLE TO STOP IT ! KillOnShutdown: true \ No newline at end of file diff --git a/resources/config.yml b/resources/config.yml index b8dc36c..bf5d5c7 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -10,10 +10,18 @@ index: index.html 403: 403.html # Here you can config pages that user will not be able to see ! -denied-pages: - - /config.yml - - /router.php +denied-pages: [] + +# Your domains (one per time). To redirect a domain here, put one per line then go to your registar CPanel and add a CName record that redirects to it ! +Domains: + - localhost + +#Parked domains: Domains that redirects to one of your domains. Same setup as shown above. +Parked domains: + 127.0.0.1: localhost + 0.0.0.0: localhost + # Here you can choose if the server automaticly disable itself on server stop. -# DO NOT DISABLE THIS OPTION WHEN YOU'RE USING AN HOSTER ! OR YOU WON4T BE ABLE TO STOP IT ! +# DO NOT DISABLE THIS OPTION WHEN YOU'RE USING AN HOSTER ! OR YOU WON'T BE ABLE TO STOP IT ! KillOnShutdown: true \ No newline at end of file diff --git a/resources/handler.php b/resources/handler.php index cd71567..3bf4235 100644 --- a/resources/handler.php +++ b/resources/handler.php @@ -3,14 +3,66 @@ Copyright (C) Ad5001 2016 All rights reserved. @link http://ad5001.ga Do not attemped to modify this file if you're not sure on how it works. -This file process 404, 403 requests and custom index. +This file process 404, 403 requests, custom index and some other stuff. */ -if(!file_exists(__DIR__ . $_SERVER["REQUEST_URI"])) { - echo file_get_contents(__DIR__ . "/" . yaml_parse(file_get_contents(__DIR__ . "/config.yml"))[404]); -} elseif($_SERVER["REQUEST_URI"] == "/") { - echo file_get_contents(__DIR__ . "/" . yaml_parse(file_get_contents(__DIR__ . "/config.yml"))["index"]); -} elseif(in_array($_SERVER["REQUEST_URI"], yaml_parse(file_get_contents(__DIR__ . "/config.yml"))["denied-pages"])) { - echo file_get_contents(__DIR__ . "/" . yaml_parse(file_get_contents(__DIR__ . "/config.yml"))[403]); +$cfg = yaml_parse(file_get_contents(__DIR__ . "/config.yml")); +$host = $_SERVER["HTTP_HOST"]; +if(isset($cfg["Parked domains"][$host])) { + $host = $cfg["Parked domains"][$host]; +} +if(in_array($host, $cfg["Domains"])) { + $_SERVER["REQUEST_URI"] = $host . $_SERVER["REQUEST_URI"]; } else { - return false; + unallowedDomain(); + return true; +} +$uri = $_SERVER["REQUEST_URI"]; +if(strpos($uri, "?") !== false) { + $uri = explode("?", $uri)[0]; +} +if(!file_exists(__DIR__ . "/" . $uri)) { + echo file_get_contents(__DIR__ . "/" . $host . "/" . $cfg[404]); +} elseif(in_array($uri, yaml_parse(file_get_contents(__DIR__ . "/config.yml"))["denied-pages"])) { + echo file_get_contents(__DIR__ . "/" . $host . "/" . $cfg[403]); +} elseif(is_dir(__DIR__ . "/" .$uri)) { + if(file_exists(__DIR__ . "/" . $uri . "index.html")) { + include(__DIR__ . "/" . $uri . "index.html"); + } elseif(file_exists(__DIR__ . "/" . $uri . "index.php")) { + include(__DIR__ . "/" . $uri . "index.php"); + } +} else { + include(__DIR__ . "/" . $uri); +} + + + +function unallowedDomain() { + echo << + + + + + +
+

Unallowed domain


+

This IP does not have any domain on this machine. Please refer to your server administartor if you think it's an error.

+

Online 1.5 - Eclipse edition

+ + +A; } \ No newline at end of file diff --git a/router.php b/router.php index cd71567..3bf4235 100644 --- a/router.php +++ b/router.php @@ -3,14 +3,66 @@ Copyright (C) Ad5001 2016 All rights reserved. @link http://ad5001.ga Do not attemped to modify this file if you're not sure on how it works. -This file process 404, 403 requests and custom index. +This file process 404, 403 requests, custom index and some other stuff. */ -if(!file_exists(__DIR__ . $_SERVER["REQUEST_URI"])) { - echo file_get_contents(__DIR__ . "/" . yaml_parse(file_get_contents(__DIR__ . "/config.yml"))[404]); -} elseif($_SERVER["REQUEST_URI"] == "/") { - echo file_get_contents(__DIR__ . "/" . yaml_parse(file_get_contents(__DIR__ . "/config.yml"))["index"]); -} elseif(in_array($_SERVER["REQUEST_URI"], yaml_parse(file_get_contents(__DIR__ . "/config.yml"))["denied-pages"])) { - echo file_get_contents(__DIR__ . "/" . yaml_parse(file_get_contents(__DIR__ . "/config.yml"))[403]); +$cfg = yaml_parse(file_get_contents(__DIR__ . "/config.yml")); +$host = $_SERVER["HTTP_HOST"]; +if(isset($cfg["Parked domains"][$host])) { + $host = $cfg["Parked domains"][$host]; +} +if(in_array($host, $cfg["Domains"])) { + $_SERVER["REQUEST_URI"] = $host . $_SERVER["REQUEST_URI"]; } else { - return false; + unallowedDomain(); + return true; +} +$uri = $_SERVER["REQUEST_URI"]; +if(strpos($uri, "?") !== false) { + $uri = explode("?", $uri)[0]; +} +if(!file_exists(__DIR__ . "/" . $uri)) { + echo file_get_contents(__DIR__ . "/" . $host . "/" . $cfg[404]); +} elseif(in_array($uri, yaml_parse(file_get_contents(__DIR__ . "/config.yml"))["denied-pages"])) { + echo file_get_contents(__DIR__ . "/" . $host . "/" . $cfg[403]); +} elseif(is_dir(__DIR__ . "/" .$uri)) { + if(file_exists(__DIR__ . "/" . $uri . "index.html")) { + include(__DIR__ . "/" . $uri . "index.html"); + } elseif(file_exists(__DIR__ . "/" . $uri . "index.php")) { + include(__DIR__ . "/" . $uri . "index.php"); + } +} else { + include(__DIR__ . "/" . $uri); +} + + + +function unallowedDomain() { + echo << + + + + + +
+

Unallowed domain


+

This IP does not have any domain on this machine. Please refer to your server administartor if you think it's an error.

+

Online 1.5 - Eclipse edition

+ + +A; } \ No newline at end of file diff --git a/src/Ad5001/Online/Main.php b/src/Ad5001/Online/Main.php index bf5c1ff..70c74d6 100644 --- a/src/Ad5001/Online/Main.php +++ b/src/Ad5001/Online/Main.php @@ -12,34 +12,51 @@ use pocketmine\Player; class Main extends PluginBase{ public function onEnable(){ $this->saveDefaultConfig(); -if(!file_exists($this->getDataFolder() . "index.html")) { - file_put_contents($this->getDataFolder() . "index.html", $this->getResource("index.html")); -} + if(!stream_resolve_include_path("router.php")) { file_put_contents($this->getDataFolder() . "router.php", $this->getResource("handler.php")); } -if(!file_exists($this->getDataFolder() . "404.html")) { - file_put_contents($this->getDataFolder() . "404.html", $this->getResource("404.html")); -} -if(!file_exists($this->getDataFolder() . "403.html")) { - file_put_contents($this->getDataFolder() . "403.html", $this->getResource("403.html")); +foreach($this->getConfig()->get("Domains") as $d) { + @mkdir($this->getDataFolder() . $d); + if(!file_exists($this->getDataFolder() . $d . "/index.html") and !file_exists($this->getDataFolder() . $d . "/index.php")) { + file_put_contents($this->getDataFolder() .$d. "/index.html", $this->getResource("index.html")); + } + if(!file_exists($this->getDataFolder() .$d. "/404.html")) { + file_put_contents($this->getDataFolder() . $d . "/404.html", $this->getResource("404.html")); + } + if(!file_exists($this->getDataFolder() . $d . "/403.html")) { + file_put_contents($this->getDataFolder() .$d . "/403.html", $this->getResource("403.html")); + } } + +register_shutdown_function("Ad5001\\Online\\Main::shutdown"); + set_time_limit(0); $this->port = $this->getConfig()->get("port"); +if(!UPnP::PortForward($this->port)) {// Beta for Windows + $this->getLogger()->info("Not able to port forward!"); +} + $this->getServer()->getScheduler()->scheduleAsyncTask(new execTask($this->getServer()->getFilePath())); -// UPnP::PortForward($port); \\\\ Beta for Windows +} + +public static function shutdown() { + echo "Shutdowned !"; } public function onDisable() { if($this->getConfig()->get("KillOnShutdown") !== "false") { + $this->getLogger()->info("Shutdowning....."); switch(true) { case stristr(PHP_OS, "WIN"): - exec('FOR /F "tokens=4 delims= " %P IN (\'netstat -a -n -o ^| findstr :'. $this->port .'\') DO @ECHO TaskKill.exe /PID %P'); + shell_exec('FOR /F "tokens=5" %P IN (\'netstat -a -n -o ^| findstr 0.0.0.0:'. $this->port .'\') DO TaskKill.exe /F /PID %P'); + $this->getLogger()->info("Shutdowned on Windows !"); break; case stristr(PHP_OS, "DAR") or stristr(PHP_OS, "LINUX"): shell_exec("kill -kill `lsof -t -i tcp:$this->port`"); + $this->getLogger()->info("Shutdowned on Linux or MAC !"); break; } } diff --git a/src/Ad5001/Online/OnlineTask.php b/src/Ad5001/Online/OnlineTask.php deleted file mode 100644 index 928dc19..0000000 --- a/src/Ad5001/Online/OnlineTask.php +++ /dev/null @@ -1,98 +0,0 @@ -sock = $sock; - $this->datapath = $datapath; - $this->isRunning = true; - $this->cfg = new Config($datapath . "config.yml", Config::YAML); - } - public function close() { - $this->isRunning = false; - } - public function onRun() { - $sock = $this->sock; - $client = socket_accept($sock); - $input = socket_read($client, 1024); - $incoming = explode("\r\n", $input); - $fetchArray = explode(" ", $incoming[0]); - if($fetchArray[1] == "/"){ - $file = $this->cfg->get("index"); - $fetchArray[1] = $this->cfg->get("index"); - } else { - $filearray = []; - $filearray = explode("/", $fetchArray[1]); - $file = $fetchArray[1]; - } - $output = ""; - $Header = "HTTP/1.1 200 OK \r\n" . - "Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" . - "Content-Type: text/html \r\n\r\n"; - $file = ltrim($file, '/'); - if(strpos($file, "?")) { - $exe = explode("?", $file); - $file = $exe[0]; - $exe = explode("&", $exe[1]); - } - - if(file_exists($this->datapath . $file)) { - if(pathinfo($this->datapath . $file)['extension'] === "php") { - if(isset($exe[0])) { - $GET = []; - foreach($exe as $exes) { - $ex = explode("=", $exes); - array_push($GET, "\"{$ex[0]}\" => \"{$ex[1]}\""); - } - $current = '' . file_get_contents($this->datapath . $file); - $current = str_ireplace('$_GET', '$GET', $current); - file_put_contents($this->datapath . "current.php", $current); - $file = "current.php"; - } - ob_start(); - include $this->datapath . $file ; - $Content = ob_get_contents(); - ob_end_clean(); - } else { - $Content = file_get_contents($this->datapath . $file); - } - $Header = "HTTP/1.1 200 OK \r\n" . -"Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" . -"Content-Type: text/html \r\n\r\n"; - } else { - $Header = "HTTP/1.1 404 NOT FOUND \r\n" . -"Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" . -"Content-Type: text/html \r\n\r\n"; - $Content = file_get_contents($this->datapath . $this->cfg->get("404")); - } - foreach($this->cfg->get("denied-pages") as $dp) { - if($dp === $file) { - $Header = "HTTP/1.1 403 FORBIDDEN \r\n" . -"Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n" . -"Content-Type: text/html \r\n\r\n"; - $Content = file_get_contents($this->datapath . $this->cfg->get("403")); - } - } - $output = $Header . $Content; - socket_write($client,$output,strlen($output)); - } - } \ No newline at end of file diff --git a/src/Ad5001/Online/isOnlineTask.php b/src/Ad5001/Online/isOnlineTask.php deleted file mode 100644 index e7e1acd..0000000 --- a/src/Ad5001/Online/isOnlineTask.php +++ /dev/null @@ -1,31 +0,0 @@ -pl = $plugin; - $this->sock = $sock; - $this->datapath = $datapath; - $this->isRunning = true; - } - public function close() { - $this->isRunning = false; - } - public function onRun($tick) { - if($this->isRunning) { - socket_listen($this->sock); - $this->pl->getServer()->getScheduler()->scheduleAsyncTask(new OnlineTask($this->pl, $this->sock, $this->datapath)); - } - } - } \ No newline at end of file