2015-10-19 17:43:46 +00:00
package me.totalfreedom.totalfreedommod.httpd ;
2013-08-27 01:48:04 +00:00
2013-09-03 19:20:28 +00:00
import java.io.File ;
import java.io.FileInputStream ;
2013-08-27 01:48:04 +00:00
import java.io.IOException ;
2016-05-12 19:40:39 +00:00
import java.util.HashMap ;
import java.util.Map ;
2013-09-03 19:20:28 +00:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2016-03-01 16:47:01 +00:00
import me.totalfreedom.totalfreedommod.FreedomService ;
2015-10-19 17:43:46 +00:00
import me.totalfreedom.totalfreedommod.config.ConfigEntry ;
import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD.Response ;
2018-07-31 07:05:28 +00:00
import me.totalfreedom.totalfreedommod.httpd.module.HTTPDModule ;
2019-07-11 02:13:57 +00:00
import me.totalfreedom.totalfreedommod.httpd.module.Module_activitylog ;
2020-12-25 19:46:43 +00:00
import me.totalfreedom.totalfreedommod.httpd.module.Module_admins ;
2018-07-31 07:05:28 +00:00
import me.totalfreedom.totalfreedommod.httpd.module.Module_bans ;
import me.totalfreedom.totalfreedommod.httpd.module.Module_file ;
import me.totalfreedom.totalfreedommod.httpd.module.Module_help ;
2020-08-15 22:58:48 +00:00
import me.totalfreedom.totalfreedommod.httpd.module.Module_indefbans ;
2020-08-17 22:17:45 +00:00
import me.totalfreedom.totalfreedommod.httpd.module.Module_index ;
2018-07-31 07:05:28 +00:00
import me.totalfreedom.totalfreedommod.httpd.module.Module_list ;
import me.totalfreedom.totalfreedommod.httpd.module.Module_logfile ;
import me.totalfreedom.totalfreedommod.httpd.module.Module_players ;
import me.totalfreedom.totalfreedommod.httpd.module.Module_punishments ;
import me.totalfreedom.totalfreedommod.httpd.module.Module_schematic ;
2015-10-19 17:43:46 +00:00
import me.totalfreedom.totalfreedommod.util.FLog ;
2018-07-31 07:01:29 +00:00
import org.apache.commons.io.FilenameUtils ;
2019-07-28 06:04:16 +00:00
import org.apache.commons.lang.StringUtils ;
2019-07-31 16:19:23 +00:00
import org.apache.commons.lang.exception.ExceptionUtils ;
2013-08-27 01:48:04 +00:00
2016-03-01 16:47:01 +00:00
public class HTTPDaemon extends FreedomService
2013-08-27 01:48:04 +00:00
{
2015-11-15 23:32:04 +00:00
2020-12-25 19:46:43 +00:00
private static final Pattern EXT_REGEX = Pattern . compile ( " \\ .([^. \\ s]+)$ " ) ;
2015-10-19 17:43:46 +00:00
public static String MIME_DEFAULT_BINARY = " application/octet-stream " ;
2013-09-03 19:20:28 +00:00
//
2016-05-12 19:40:39 +00:00
public int port ;
public Map < String , ModuleExecutable > modules = new HashMap < > ( ) ;
2020-12-25 19:46:43 +00:00
private HTTPD httpd ;
public static Response serveFileBasic ( File file )
{
Response response = null ;
if ( file ! = null & & file . exists ( ) )
{
try
{
String mimetype = null ;
Matcher matcher = EXT_REGEX . matcher ( file . getCanonicalPath ( ) ) ;
if ( matcher . find ( ) )
{
mimetype = Module_file . MIME_TYPES . get ( matcher . group ( 1 ) ) ;
}
if ( mimetype = = null | | mimetype . trim ( ) . isEmpty ( ) )
{
mimetype = MIME_DEFAULT_BINARY ;
}
// Some browsers like firefox download the file for text/yaml mime types
if ( FilenameUtils . getExtension ( file . getName ( ) ) . equals ( " yml " ) )
{
mimetype = NanoHTTPD . MIME_PLAINTEXT ;
}
response = new Response ( Response . Status . OK , mimetype , new FileInputStream ( file ) ) ;
response . addHeader ( " Content-Length " , " " + file . length ( ) ) ;
}
catch ( IOException ex )
{
FLog . severe ( ex ) ;
}
}
return response ;
}
2013-08-27 01:48:04 +00:00
2015-10-19 17:43:46 +00:00
@Override
public void onStart ( )
2014-05-19 17:32:25 +00:00
{
2015-10-19 17:43:46 +00:00
if ( ! ConfigEntry . HTTPD_ENABLED . getBoolean ( ) )
2013-08-28 00:20:11 +00:00
{
return ;
}
2018-07-31 06:41:56 +00:00
port = ConfigEntry . HTTPD_PORT . getInteger ( ) ;
2016-05-12 19:40:39 +00:00
httpd = new HTTPD ( port ) ;
// Modules
modules . clear ( ) ;
2019-07-11 02:13:57 +00:00
module ( " activitylog " , Module_activitylog . class , true ) ;
2020-12-04 00:28:53 +00:00
module ( " admins " , Module_admins . class , true ) ;
2018-01-02 02:46:35 +00:00
module ( " bans " , Module_bans . class , true ) ;
2016-05-12 19:40:39 +00:00
module ( " help " , Module_help . class , false ) ;
module ( " list " , Module_list . class , false ) ;
2018-01-02 02:46:35 +00:00
module ( " logfile " , Module_logfile . class , true ) ;
2020-08-10 00:49:52 +00:00
module ( " indefbans " , Module_indefbans . class , true ) ;
2016-05-12 19:40:39 +00:00
module ( " players " , Module_players . class , false ) ;
2018-03-03 04:29:08 +00:00
module ( " punishments " , Module_punishments . class , true ) ;
2016-05-12 19:40:39 +00:00
module ( " schematic " , Module_schematic . class , true ) ;
2020-08-17 22:17:45 +00:00
module ( " index " , Module_index . class , false ) ;
2016-05-12 19:40:39 +00:00
2013-08-27 01:48:04 +00:00
try
{
2016-05-12 19:40:39 +00:00
httpd . start ( ) ;
2013-08-27 16:39:28 +00:00
2016-05-12 19:40:39 +00:00
if ( httpd . isAlive ( ) )
2013-08-27 16:39:28 +00:00
{
2016-05-12 19:40:39 +00:00
FLog . info ( " TFM HTTPd started. Listening on port: " + httpd . getListeningPort ( ) ) ;
2013-08-27 16:39:28 +00:00
}
else
{
2015-10-19 17:43:46 +00:00
FLog . info ( " Error starting TFM HTTPd. " ) ;
2013-08-27 16:39:28 +00:00
}
2013-08-27 01:48:04 +00:00
}
catch ( IOException ex )
{
2015-10-19 17:43:46 +00:00
FLog . severe ( ex ) ;
2013-08-27 01:48:04 +00:00
}
}
2015-10-19 17:43:46 +00:00
@Override
public void onStop ( )
2013-08-27 01:48:04 +00:00
{
2015-10-19 17:43:46 +00:00
if ( ! ConfigEntry . HTTPD_ENABLED . getBoolean ( ) )
2013-08-28 00:20:11 +00:00
{
return ;
}
2016-05-12 19:40:39 +00:00
httpd . stop ( ) ;
2013-08-27 16:39:28 +00:00
2015-10-19 17:43:46 +00:00
FLog . info ( " TFM HTTPd stopped. " ) ;
2013-08-27 01:48:04 +00:00
}
2016-05-12 19:40:39 +00:00
private void module ( String name , Class < ? extends HTTPDModule > clazz , boolean async )
2013-08-28 00:20:11 +00:00
{
2021-01-08 23:32:54 +00:00
modules . put ( name , ModuleExecutable . forClass ( clazz , async ) ) ;
2013-08-28 00:20:11 +00:00
}
2016-05-12 19:40:39 +00:00
private class HTTPD extends NanoHTTPD
2013-08-27 01:48:04 +00:00
{
2016-05-12 19:40:39 +00:00
private HTTPD ( int port )
2013-08-27 01:48:04 +00:00
{
super ( port ) ;
}
2021-01-08 23:32:54 +00:00
2013-08-27 01:48:04 +00:00
@Override
2013-09-18 01:31:46 +00:00
public Response serve ( HTTPSession session )
2013-08-27 01:48:04 +00:00
{
2016-05-12 19:40:39 +00:00
final String [ ] args = StringUtils . split ( session . getUri ( ) , " / " ) ;
2013-08-27 01:48:04 +00:00
2016-05-12 19:40:39 +00:00
ModuleExecutable mex = modules . get ( " file " ) ;
if ( args . length > = 1 )
2013-08-27 01:48:04 +00:00
{
2016-05-12 19:40:39 +00:00
mex = modules . get ( args [ 0 ] . toLowerCase ( ) ) ;
2013-08-27 01:48:04 +00:00
}
2016-05-12 19:40:39 +00:00
if ( mex = = null )
2013-08-28 00:20:11 +00:00
{
2016-05-12 19:40:39 +00:00
return new Response ( Response . Status . NOT_FOUND , MIME_PLAINTEXT , " Error 404: Not Found - The requested resource was not found on this server. " ) ;
2013-08-28 00:20:11 +00:00
}
2016-05-12 19:40:39 +00:00
try
2013-08-27 01:48:04 +00:00
{
2016-05-12 19:40:39 +00:00
return mex . execute ( session ) ;
}
catch ( Exception ex )
{
FLog . severe ( ex ) ;
return new Response ( Response . Status . INTERNAL_ERROR , MIME_PLAINTEXT , " Error 500: Internal Server Error \ r \ n " + ex . getMessage ( ) + " \ r \ n " + ExceptionUtils . getStackTrace ( ex ) ) ;
2013-08-27 01:48:04 +00:00
}
}
}
2020-08-17 22:17:45 +00:00
}