📄 webplugin.java
字号:
/*
* File : WebPlugin.java
* Created : 23-Jan-2004
* By : parg
*
* Azureus - a Java Bittorrent client
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details ( see the LICENSE file ).
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.gudy.azureus2.ui.webplugin;
/**
* @author parg
*
*/
import java.io.*;
import java.util.*;
import java.net.*;
import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.plugins.*;
import org.gudy.azureus2.plugins.logging.*;
import org.gudy.azureus2.plugins.ipfilter.*;
import org.gudy.azureus2.plugins.tracker.*;
import org.gudy.azureus2.plugins.tracker.web.*;
import org.gudy.azureus2.plugins.ui.*;
import org.gudy.azureus2.plugins.ui.config.*;
import org.gudy.azureus2.plugins.ui.model.*;
import com.aelitis.azureus.plugins.upnp.UPnPPlugin;
public class
WebPlugin
implements Plugin, TrackerWebPageGenerator
{
public static final String PR_PORT = "Port"; // Integer
public static final String PR_ROOT_RESOURCE = "Root Resource"; // String
public static final String PR_LOG = "DefaultLoggerChannel"; // LoggerChannel
public static final String PR_CONFIG_MODEL = "DefaultConfigModel"; // BasicPluginConfigModel
public static final String PR_VIEW_MODEL = "DefaultViewModel"; // BasicPluginViewModel
public static final String PR_HIDE_RESOURCE_CONFIG = "DefaultHideResourceConfig"; // Boolean
public static final String PROPERTIES_MIGRATED = "Properties Migrated";
public static final String CONFIG_MIGRATED = "Config Migrated";
public static final String CONFIG_PASSWORD_ENABLE = "Password Enable";
public final boolean CONFIG_PASSWORD_ENABLE_DEFAULT = false;
public static final String CONFIG_USER = "User";
public final String CONFIG_USER_DEFAULT = "";
public static final String CONFIG_PASSWORD = "Password";
public final byte[] CONFIG_PASSWORD_DEFAULT = {};
public static final String CONFIG_PORT = PR_PORT;
public int CONFIG_PORT_DEFAULT = 8089;
public static final String CONFIG_PROTOCOL = "Protocol";
public final String CONFIG_PROTOCOL_DEFAULT = "HTTP";
public static final String CONFIG_UPNP_ENABLE = "UPnP Enable";
public final boolean CONFIG_UPNP_ENABLE_DEFAULT = true;
public static final String CONFIG_HOME_PAGE = "Home Page";
public final String CONFIG_HOME_PAGE_DEFAULT = "index.html";
public static final String CONFIG_ROOT_DIR = "Root Dir";
public final String CONFIG_ROOT_DIR_DEFAULT = "";
public static final String CONFIG_ROOT_RESOURCE = PR_ROOT_RESOURCE;
public String CONFIG_ROOT_RESOURCE_DEFAULT = "";
public static final String CONFIG_MODE = "Mode";
public static final String CONFIG_MODE_FULL = "full";
public final String CONFIG_MODE_DEFAULT = CONFIG_MODE_FULL;
public static final String CONFIG_ACCESS = "Access";
public final String CONFIG_ACCESS_DEFAULT = "all";
protected static final String NL = "\r\n";
protected static final String[] welcome_pages = {"index.html", "index.htm", "index.php", "index.tmpl" };
protected static File[] welcome_files;
protected PluginInterface plugin_interface; // unfortunately this is accessed by webui - fix sometime
private LoggerChannel log;
private Tracker tracker;
private BasicPluginViewModel view_model;
private BasicPluginConfigModel config_model;
private String home_page;
private String file_root;
private String resource_root;
private boolean ip_range_all = false;
private IPRange ip_range;
private Properties properties;
public
WebPlugin()
{
properties = new Properties();
}
public
WebPlugin(
Properties defaults )
{
properties = defaults;
}
public void
initialize(
PluginInterface _plugin_interface )
throws PluginException
{
plugin_interface = _plugin_interface;
Integer pr_port = (Integer)properties.get(PR_PORT);
if ( pr_port != null ){
CONFIG_PORT_DEFAULT = pr_port.intValue();
}
String pr_root_resource = (String)properties.get( PR_ROOT_RESOURCE );
if( pr_root_resource != null ){
CONFIG_ROOT_RESOURCE_DEFAULT = pr_root_resource;
}
Boolean pr_hide_resource_config = (Boolean)properties.get( PR_HIDE_RESOURCE_CONFIG );
log = (LoggerChannel)properties.get( PR_LOG );
if ( log == null ){
log = plugin_interface.getLogger().getChannel("WebPlugin");
}
UIManager ui_manager = plugin_interface.getUIManager();
view_model = (BasicPluginViewModel)properties.get( PR_VIEW_MODEL );
if ( view_model == null ){
view_model = ui_manager.createBasicPluginViewModel( plugin_interface.getPluginName());
}
String sConfigSectionID = "plugins." + plugin_interface.getPluginID();
view_model.setConfigSectionID(sConfigSectionID);
view_model.getStatus().setText( "Running" );
view_model.getActivity().setVisible( false );
view_model.getProgress().setVisible( false );
log.addListener(
new LoggerChannelListener()
{
public void
messageLogged(
int type,
String message )
{
view_model.getLogArea().appendText( message+"\n");
}
public void
messageLogged(
String str,
Throwable error )
{
view_model.getLogArea().appendText( str + "\n" );
view_model.getLogArea().appendText( error.toString() + "\n" );
}
});
PluginConfig plugin_config = plugin_interface.getPluginconfig();
config_model = (BasicPluginConfigModel)properties.get( PR_CONFIG_MODEL );
if ( config_model == null ){
config_model = ui_manager.createBasicPluginConfigModel(ConfigSection.SECTION_PLUGINS, sConfigSectionID);
}
boolean save_needed = false;
if ( !plugin_config.getPluginBooleanParameter( CONFIG_MIGRATED, false )){
plugin_config.setPluginParameter( CONFIG_MIGRATED, true );
save_needed = true;
plugin_config.setPluginParameter(
CONFIG_PASSWORD_ENABLE,
plugin_config.getBooleanParameter(
"Tracker Password Enable Web", CONFIG_PASSWORD_ENABLE_DEFAULT ));
plugin_config.setPluginParameter(
CONFIG_USER,
plugin_config.getStringParameter(
"Tracker Username", CONFIG_USER_DEFAULT ));
plugin_config.setPluginParameter(
CONFIG_PASSWORD,
plugin_config.getByteParameter(
"Tracker Password", CONFIG_PASSWORD_DEFAULT ));
}
if ( !plugin_config.getPluginBooleanParameter( PROPERTIES_MIGRATED, false )){
plugin_config.setPluginParameter( PROPERTIES_MIGRATED, true );
Properties props = plugin_interface.getPluginProperties();
// make sure we've got an old properties file too
if ( props.getProperty( "port", "" ).length() > 0 ){
save_needed = true;
String prop_port = props.getProperty( "port", ""+CONFIG_PORT_DEFAULT );
String prop_protocol = props.getProperty( "protocol", CONFIG_PROTOCOL_DEFAULT );
String prop_home = props.getProperty( "homepage", CONFIG_HOME_PAGE_DEFAULT );
String prop_rootdir = props.getProperty( "rootdir", CONFIG_ROOT_DIR_DEFAULT );
String prop_rootres = props.getProperty( "rootresource", CONFIG_ROOT_RESOURCE_DEFAULT );
String prop_mode = props.getProperty( "mode", CONFIG_MODE_DEFAULT );
String prop_access = props.getProperty( "access", CONFIG_ACCESS_DEFAULT );
int prop_port_int = CONFIG_PORT_DEFAULT;
try{
prop_port_int = Integer.parseInt( prop_port );
}catch( Throwable e ){
}
plugin_config.setPluginParameter(CONFIG_PORT, prop_port_int );
plugin_config.setPluginParameter(CONFIG_PROTOCOL, prop_protocol );
plugin_config.setPluginParameter(CONFIG_HOME_PAGE, prop_home );
plugin_config.setPluginParameter(CONFIG_ROOT_DIR, prop_rootdir );
plugin_config.setPluginParameter(CONFIG_ROOT_RESOURCE, prop_rootres );
plugin_config.setPluginParameter(CONFIG_MODE, prop_mode );
plugin_config.setPluginParameter(CONFIG_ACCESS, prop_access );
File props_file = new File( plugin_interface.getPluginDirectoryName(), "plugin.properties" );
PrintWriter pw = null;
try{
File backup = new File( plugin_interface.getPluginDirectoryName(), "plugin.properties.bak" );
props_file.renameTo( backup );
pw = new PrintWriter( new FileWriter( props_file ));
pw.println( "plugin.class=" + props.getProperty( "plugin.class" ));
pw.println( "plugin.name=" + props.getProperty( "plugin.name" ));
pw.println( "plugin.version=" + props.getProperty( "plugin.version" ));
pw.println( "plugin.id=" + props.getProperty( "plugin.id" ));
pw.println( "" );
pw.println( "# configuration has been migrated to plugin config - see view->config->plugins" );
pw.println( "# in the SWT user interface" );
log.logAlert( LoggerChannel.LT_INFORMATION,
plugin_interface.getPluginName() + " - plugin.properties settings migrated to plugin configuration." );
}catch( Throwable e ){
Debug.printStackTrace( e );
log.logAlert( LoggerChannel.LT_ERROR,
plugin_interface.getPluginName() + " - plugin.properties settings migration failed." );
}finally{
if ( pw != null ){
pw.close();
}
}
}
}
if ( save_needed ){
plugin_config.save();
}
config_model.addLabelParameter2( "webui.restart.info" );
IntParameter param_port = config_model.addIntParameter2( CONFIG_PORT, "webui.port", CONFIG_PORT_DEFAULT );
StringListParameter param_protocol =
config_model.addStringListParameter2(
CONFIG_PROTOCOL, "webui.protocol", new String[]{ "http", "https" }, CONFIG_PROTOCOL_DEFAULT );
final BooleanParameter upnp_enable =
config_model.addBooleanParameter2(
CONFIG_UPNP_ENABLE,
"webui.upnpenable",
CONFIG_UPNP_ENABLE_DEFAULT );
StringParameter param_home = config_model.addStringParameter2( CONFIG_HOME_PAGE, "webui.homepage", CONFIG_HOME_PAGE_DEFAULT );
StringParameter param_rootdir = config_model.addStringParameter2( CONFIG_ROOT_DIR, "webui.rootdir", CONFIG_ROOT_DIR_DEFAULT );
StringParameter param_rootres = config_model.addStringParameter2( CONFIG_ROOT_RESOURCE, "webui.rootres", CONFIG_ROOT_RESOURCE_DEFAULT );
if ( pr_hide_resource_config != null && pr_hide_resource_config.booleanValue()){
param_home.setVisible( false );
param_rootdir.setVisible( false );
param_rootres.setVisible( false );
}
config_model.addLabelParameter2( "webui.mode.info" );
StringListParameter param_mode =
config_model.addStringListParameter2(
CONFIG_MODE, "webui.mode", new String[]{ "full", "view" }, CONFIG_MODE_DEFAULT );
config_model.addLabelParameter2( "webui.access.info" );
StringParameter param_access = config_model.addStringParameter2( CONFIG_ACCESS, "webui.access", CONFIG_ACCESS_DEFAULT );
final BooleanParameter pw_enable =
config_model.addBooleanParameter2(
CONFIG_PASSWORD_ENABLE,
"webui.passwordenable",
CONFIG_PASSWORD_ENABLE_DEFAULT );
final StringParameter user_name =
config_model.addStringParameter2(
CONFIG_USER,
"webui.user",
CONFIG_USER_DEFAULT );
final PasswordParameter password =
config_model.addPasswordParameter2(
CONFIG_PASSWORD,
"webui.password",
PasswordParameter.ET_SHA1,
CONFIG_PASSWORD_DEFAULT );
pw_enable.addEnabledOnSelection( user_name );
pw_enable.addEnabledOnSelection( password );
tracker = plugin_interface.getTracker();
home_page = param_home.getValue().trim();
if ( home_page.length() == 0 ){
home_page = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -