📄 webplugin.java
字号:
}else if (!home_page.startsWith("/" )){
home_page = "/" + home_page;
}
resource_root = param_rootres.getValue().trim();
if ( resource_root.length() == 0 ){
resource_root = null;
}else if ( resource_root.startsWith("/" )){
resource_root = resource_root.substring(1);
}
String root_dir = param_rootdir.getValue().trim();
if ( root_dir.length() == 0 ){
file_root = plugin_interface.getPluginDirectoryName();
if ( file_root == null ){
file_root = SystemProperties.getUserPath() + "web";
}
}else{
// absolute or relative
if ( root_dir.startsWith(File.separator) || root_dir.indexOf(":") != -1 ){
file_root = root_dir;
}else{
file_root = SystemProperties.getUserPath() + "web" + File.separator + root_dir;
}
}
File f_root = new File( file_root );
if ( !f_root.exists()){
String error = "WebPlugin: root dir '" + file_root + "' doesn't exist";
log.log( LoggerChannel.LT_ERROR, error );
throw( new PluginException( error ));
}
if ( !f_root.isDirectory()){
String error = "WebPlugin: root dir '" + file_root + "' isn't a directory";
log.log( LoggerChannel.LT_ERROR, error );
throw( new PluginException( error ));
}
welcome_files = new File[welcome_pages.length];
for (int i=0;i<welcome_pages.length;i++){
welcome_files[i] = new File( file_root + File.separator + welcome_pages[i] );
}
final int port = param_port.getValue();
String protocol_str = param_protocol.getValue().trim();
int protocol = protocol_str.equalsIgnoreCase( "HTTP")?
Tracker.PR_HTTP:Tracker.PR_HTTPS;
log.log( LoggerChannel.LT_INFORMATION, "Initialisation: port = " + port + ", protocol = " + protocol_str + (root_dir.length()==0?"":(", root = " + root_dir )));
String access_str = param_access.getValue().trim();
if ( access_str.length() > 7 && Character.isDigit(access_str.charAt(0))){
ip_range = plugin_interface.getIPFilter().createRange(true);
int sep = access_str.indexOf("-");
if ( sep == -1 ){
ip_range.setStartIP( access_str );
ip_range.setEndIP( access_str );
}else{
ip_range.setStartIP( access_str.substring(0,sep).trim());
ip_range.setEndIP( access_str.substring( sep+1 ).trim());
}
ip_range.checkValid();
if (!ip_range.isValid()){
log.log( LoggerChannel.LT_ERROR, "access parameter '" + access_str + "' is invalid" );
ip_range = null;
}
}else{
if ( access_str.equalsIgnoreCase( "all" )){
ip_range_all = true;
}
}
log.log( LoggerChannel.LT_INFORMATION,
"acceptable IP range = " +
( ip_range==null?
(ip_range_all?"all":"local"):
(ip_range.getStartIP() + " - " + ip_range.getEndIP())));
try{
TrackerWebContext context =
tracker.createWebContext(
plugin_interface.getAzureusName() + " - " + plugin_interface.getPluginName(),
port, protocol );
context.addPageGenerator( this );
context.addAuthenticationListener(
new TrackerAuthenticationAdapter()
{
String last_pw = "";
byte[] last_hash = {};
AEMonitor this_mon = new AEMonitor( "WebPlugin:auth" );
public boolean
authenticate(
URL resource,
String user,
String pw )
{
try{
this_mon.enter();
if ( !pw_enable.getValue()){
return( true );
}
if ( !user.equals(user_name.getValue())){
return( false );
}
byte[] hash = last_hash;
if ( !last_pw.equals( pw )){
hash = plugin_interface.getUtilities().getSecurityManager().calculateSHA1( pw.getBytes());
last_pw = pw;
last_hash = hash;
}
return( Arrays.equals( hash, password.getValue()));
}finally{
this_mon.exit();
}
}
});
}catch( TrackerException e ){
log.log( "Plugin Initialisation Fails", e );
}
plugin_interface.addListener(
new PluginListener()
{
public void
initializationComplete()
{
PluginInterface pi_upnp = plugin_interface.getPluginManager().getPluginInterfaceByClass( UPnPPlugin.class );
if ( pi_upnp == null ){
log.log( "No UPnP plugin available, not attempting port mapping");
}else{
if ( upnp_enable.getValue()){
((UPnPPlugin)pi_upnp.getPlugin()).addMapping( plugin_interface.getPluginName(), true, port, true );
}else{
log.log( "UPnP disabled for the plugin, not attempting port mapping");
}
}
}
public void
closedownInitiated()
{
}
public void
closedownComplete()
{
}
});
}
public boolean
generateSupport(
TrackerWebPageRequest request,
TrackerWebPageResponse response )
throws IOException
{
return( false );
}
public boolean
generate(
TrackerWebPageRequest request,
TrackerWebPageResponse response )
throws IOException
{
if ( !ip_range_all ){
String client = request.getClientAddress();
// System.out.println( "client = " + client );
try{
InetAddress ia = InetAddress.getByName( client );
if ( ip_range == null ){
if ( !ia.isLoopbackAddress()){
log.log( LoggerChannel.LT_ERROR, "Client '" + client + "' is not local, rejecting" );
return( false );
}
}else{
if ( !ip_range.isInRange( ia.getHostAddress())){
log.log( LoggerChannel.LT_ERROR, "Client '" + client + "' (" + ia.getHostAddress() + ") is not in range, rejecting" );
return( false );
}
}
}catch( Throwable e ){
Debug.printStackTrace( e );
return( false );
}
}
if ( request.getURL().toString().endsWith(".class")){
System.out.println( "WebPlugin::generate:" + request.getURL());
}
if ( generateSupport( request, response )){
return(true);
}
OutputStream os = response.getOutputStream();
String url = request.getURL();
if (url.equals("/")){
if (home_page != null ){
url = home_page;
}else{
for (int i=0;i<welcome_files.length;i++){
if ( welcome_files[i].exists()){
url = "/" + welcome_pages[i];
break;
}
}
}
}
// first try file system for data
if ( response.useFile( file_root, url )){
return( true );
}
// now try jars
String resource_name = url;
if (resource_name.startsWith("/")){
resource_name = resource_name.substring(1);
}
int pos = resource_name.lastIndexOf(".");
if ( pos != -1 ){
String type = resource_name.substring( pos+1 );
ClassLoader cl = plugin_interface.getPluginClassLoader();
InputStream is = cl.getResourceAsStream( resource_name );
if ( is == null ){
// failed absolute load, try relative
if ( resource_root != null ){
resource_name = resource_root + "/" + resource_name;
is = cl.getResourceAsStream( resource_name );
}
}
// System.out.println( resource_name + "->" + is + ", url = " + url );
if (is != null ){
try{
response.useStream( type, is );
}finally{
is.close();
}
return( true );
}
}
return( false );
}
protected BasicPluginConfigModel
getConfigModel()
{
return( config_model );
}
protected BasicPluginViewModel getViewModel() {
return this.view_model;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -