📄 upnpplugin.java
字号:
/*
* Created on 14-Jun-2004
* Created by Paul Gardner
* Copyright (C) 2004, 2005, 2006 Aelitis, All Rights Reserved.
*
* 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, or (at your option) any later version.
* 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.
* 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.
*
* AELITIS, SAS au capital de 46,603.30 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*
*/
package com.aelitis.azureus.plugins.upnp;
/**
* @author parg
*
*/
import java.util.*;
import java.net.URL;
import org.gudy.azureus2.plugins.*;
import org.gudy.azureus2.plugins.logging.*;
import org.gudy.azureus2.plugins.ui.*;
import org.gudy.azureus2.plugins.ui.model.*;
import org.gudy.azureus2.plugins.ui.config.*;
import org.gudy.azureus2.plugins.utils.UTTimer;
import org.gudy.azureus2.plugins.utils.resourcedownloader.ResourceDownloaderFactory;
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocument;
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentException;
import org.gudy.azureus2.core3.internat.MessageText;
import org.gudy.azureus2.core3.util.AEMonitor;
import org.gudy.azureus2.core3.util.Debug;
import com.aelitis.net.natpmp.NATPMPDeviceAdapter;
import com.aelitis.net.natpmp.NatPMPDeviceFactory;
import com.aelitis.net.natpmp.upnp.NatPMPUPnP;
import com.aelitis.net.natpmp.upnp.NatPMPUPnPFactory;
import com.aelitis.net.upnp.*;
import com.aelitis.net.upnp.services.*;
public class
UPnPPlugin
implements Plugin, UPnPListener, UPnPMappingListener, UPnPWANConnectionListener
{
private static final String UPNP_PLUGIN_CONFIGSECTION_ID = "UPnP";
private static final String NATPMP_PLUGIN_CONFIGSECTION_ID = "NATPMP";
private static final String STATS_DISCOVER = "discover";
private static final String STATS_FOUND = "found";
private static final String STATS_READ_OK = "read_ok";
private static final String STATS_READ_BAD = "read_bad";
private static final String STATS_MAP_OK = "map_ok";
private static final String STATS_MAP_BAD = "map_bad";
private static final String[] STATS_KEYS = { STATS_DISCOVER, STATS_FOUND, STATS_READ_OK, STATS_READ_BAD, STATS_MAP_OK, STATS_MAP_BAD };
private PluginInterface plugin_interface;
private LoggerChannel log;
private UPnPMappingManager mapping_manager = UPnPMappingManager.getSingleton( this );
private UPnP upnp;
private NatPMPUPnP nat_pmp_upnp;
private BooleanParameter natpmp_enable_param;
private StringParameter nat_pmp_router;
private BooleanParameter upnp_enable_param;
private BooleanParameter alert_success_param;
private BooleanParameter grab_ports_param;
private BooleanParameter alert_other_port_param;
private BooleanParameter alert_device_probs_param;
private BooleanParameter release_mappings_param;
private StringParameter selected_interfaces_param;
private BooleanParameter ignore_bad_devices;
private LabelParameter ignored_devices_list;
private List mappings = new ArrayList();
private List services = new ArrayList();
private Map root_info_map = new HashMap();
protected AEMonitor this_mon = new AEMonitor( "UPnPPlugin" );
public void
initialize(
PluginInterface _plugin_interface )
{
plugin_interface = _plugin_interface;
plugin_interface.getPluginProperties().setProperty( "plugin.version", "1.0" );
plugin_interface.getPluginProperties().setProperty( "plugin.name", "Universal Plug and Play (UPnP)" );
log = plugin_interface.getLogger().getTimeStampedChannel("UPnP");
UIManager ui_manager = plugin_interface.getUIManager();
final BasicPluginViewModel model =
ui_manager.createBasicPluginViewModel(
"UPnP");
model.setConfigSectionID(UPNP_PLUGIN_CONFIGSECTION_ID);
BasicPluginConfigModel upnp_config = ui_manager.createBasicPluginConfigModel(ConfigSection.SECTION_PLUGINS, UPNP_PLUGIN_CONFIGSECTION_ID );
// NATPMP
BasicPluginConfigModel natpmp_config = ui_manager.createBasicPluginConfigModel( UPNP_PLUGIN_CONFIGSECTION_ID, NATPMP_PLUGIN_CONFIGSECTION_ID );
natpmp_config.addLabelParameter2( "natpmp.info" );
ActionParameter natpmp_wiki = natpmp_config.addActionParameter2( "Utils.link.visit", "MainWindow.about.internet.wiki" );
natpmp_wiki.setStyle( ActionParameter.STYLE_LINK );
natpmp_wiki.addListener(
new ParameterListener()
{
public void
parameterChanged(
Parameter param )
{
try{
plugin_interface.getUIManager().openURL( new URL( "http://azureus.aelitis.com/wiki/index.php/NATPMP" ));
}catch( Throwable e ){
e.printStackTrace();
}
}
});
natpmp_enable_param =
natpmp_config.addBooleanParameter2( "natpmp.enable", "natpmp.enable", false );
nat_pmp_router = natpmp_config.addStringParameter2( "natpmp.routeraddress", "natpmp.routeraddress", "" );
natpmp_enable_param.addListener(
new ParameterListener()
{
public void
parameterChanged(
Parameter param )
{
setNATPMPEnableState();
}
});
natpmp_enable_param.addEnabledOnSelection( nat_pmp_router );
// UPNP
upnp_config.addLabelParameter2( "upnp.info" );
ActionParameter upnp_wiki = upnp_config.addActionParameter2( "Utils.link.visit", "MainWindow.about.internet.wiki" );
upnp_wiki.setStyle( ActionParameter.STYLE_LINK );
upnp_wiki.addListener(
new ParameterListener()
{
public void
parameterChanged(
Parameter param )
{
try{
plugin_interface.getUIManager().openURL( new URL( "http://azureus.aelitis.com/wiki/index.php/UPnP" ));
}catch( Throwable e ){
e.printStackTrace();
}
}
});
upnp_enable_param =
upnp_config.addBooleanParameter2( "upnp.enable", "upnp.enable", true );
grab_ports_param = upnp_config.addBooleanParameter2( "upnp.grabports", "upnp.grabports", false );
release_mappings_param = upnp_config.addBooleanParameter2( "upnp.releasemappings", "upnp.releasemappings", true );
ActionParameter refresh_param = upnp_config.addActionParameter2( "upnp.refresh.label", "upnp.refresh.button" );
refresh_param.addListener(
new ParameterListener()
{
public void
parameterChanged(
Parameter param )
{
UPnPPlugin.this.refreshMappings();
}
});
upnp_config.addLabelParameter2( "blank.resource" );
alert_success_param = upnp_config.addBooleanParameter2( "upnp.alertsuccess", "upnp.alertsuccess", false );
alert_other_port_param = upnp_config.addBooleanParameter2( "upnp.alertothermappings", "upnp.alertothermappings", true );
alert_device_probs_param = upnp_config.addBooleanParameter2( "upnp.alertdeviceproblems", "upnp.alertdeviceproblems", true );
selected_interfaces_param = upnp_config.addStringParameter2( "upnp.selectedinterfaces", "upnp.selectedinterfaces", "" );
ignore_bad_devices = upnp_config.addBooleanParameter2( "upnp.ignorebaddevices", "upnp.ignorebaddevices", true );
ignored_devices_list = upnp_config.addLabelParameter2( "upnp.ignorebaddevices.info" );
ActionParameter reset_param = upnp_config.addActionParameter2( "upnp.ignorebaddevices.reset", "upnp.ignorebaddevices.reset.action" );
reset_param.addListener(
new ParameterListener()
{
public void
parameterChanged(
Parameter param )
{
PluginConfig pc = plugin_interface.getPluginconfig();
for (int i=0;i<STATS_KEYS.length;i++){
String key = "upnp.device.stats." + STATS_KEYS[i];
pc.setPluginMapParameter( key, new HashMap());
}
pc.setPluginMapParameter( "upnp.device.ignorelist", new HashMap());
updateIgnoreList();
}
});
upnp_enable_param.addEnabledOnSelection( alert_success_param );
upnp_enable_param.addEnabledOnSelection( grab_ports_param );
upnp_enable_param.addEnabledOnSelection( refresh_param );
upnp_enable_param.addEnabledOnSelection( alert_other_port_param );
upnp_enable_param.addEnabledOnSelection( alert_device_probs_param );
upnp_enable_param.addEnabledOnSelection( release_mappings_param );
upnp_enable_param.addEnabledOnSelection( selected_interfaces_param );
upnp_enable_param.addEnabledOnSelection( ignore_bad_devices );
upnp_enable_param.addEnabledOnSelection( ignored_devices_list );
upnp_enable_param.addEnabledOnSelection( reset_param );
final boolean enabled = upnp_enable_param.getValue();
natpmp_enable_param.setEnabled( enabled );
model.getStatus().setText( enabled?"Running":"Disabled" );
upnp_enable_param.addListener(
new ParameterListener()
{
public void
parameterChanged(
Parameter p )
{
boolean e = upnp_enable_param.getValue();
natpmp_enable_param.setEnabled( e );
model.getStatus().setText( e?"Running":"Disabled" );
if ( e ){
startUp();
}else{
closeDown( true );
}
setNATPMPEnableState();
}
});
model.getActivity().setVisible( false );
model.getProgress().setVisible( false );
log.addListener(
new LoggerChannelListener()
{
public void
messageLogged(
int type,
String message )
{
model.getLogArea().appendText( message+"\n");
}
public void
messageLogged(
String str,
Throwable error )
{
model.getLogArea().appendText( error.toString()+"\n");
}
});
plugin_interface.addListener(
new PluginListener()
{
public void
initializationComplete()
{
if ( enabled ){
updateIgnoreList();
startUp();
}
}
public void
closedownInitiated()
{
if ( services.size() == 0 ){
plugin_interface.getPluginconfig().setPluginParameter( "plugin.info", "" );
}
}
public void
closedownComplete()
{
closeDown( true );
}
});
}
protected void
updateIgnoreList()
{
try{
String param = "";
if ( ignore_bad_devices.getValue()){
PluginConfig pc = plugin_interface.getPluginconfig();
Map ignored = pc.getPluginMapParameter( "upnp.device.ignorelist", new HashMap());
Iterator it = ignored.entrySet().iterator();
while( it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
Map value = (Map)entry.getValue();
param += "\n " + entry.getKey() + ": " + new String((byte[])value.get( "Location" ));
}
if ( ignored.size() > 0 ){
log.log( "Devices currently being ignored: " + param );
}
}
String text =
plugin_interface.getUtilities().getLocaleUtilities().getLocalisedMessageText(
"upnp.ignorebaddevices.info",
new String[]{ param });
ignored_devices_list.setLabelText( text );
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
protected void
ignoreDevice(
String USN,
URL location )
{
// only take note of this if enabled to do so
if ( ignore_bad_devices.getValue()){
try{
PluginConfig pc = plugin_interface.getPluginconfig();
Map ignored = pc.getPluginMapParameter( "upnp.device.ignorelist", new HashMap());
Map entry = (Map)ignored.get( USN );
if ( entry == null ){
entry = new HashMap();
entry.put( "Location", location.toString().getBytes());
ignored.put( USN, entry );
pc.setPluginMapParameter( "upnp.device.ignorelist", ignored );
updateIgnoreList();
String text =
plugin_interface.getUtilities().getLocaleUtilities().getLocalisedMessageText(
"upnp.ignorebaddevices.alert",
new String[]{ location.toString() });
log.logAlertRepeatable( LoggerChannel.LT_WARNING, text );
}
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
}
protected void
startUp()
{
if ( upnp != null ){
// already started up, must have been re-enabled
refreshMappings();
return;
}
final LoggerChannel core_log = plugin_interface.getLogger().getChannel("UPnP Core");
try{
upnp = UPnPFactory.getSingleton(
new UPnPAdapter()
{
Set exception_traces = new HashSet();
public SimpleXMLParserDocument
parseXML(
String data )
throws SimpleXMLParserDocumentException
{
return( plugin_interface.getUtilities().getSimpleXMLParserDocumentFactory().create( data ));
}
public ResourceDownloaderFactory
getResourceDownloaderFactory()
{
return( plugin_interface.getUtilities().getResourceDownloaderFactory());
}
public UTTimer
createTimer(
String name )
{
return( plugin_interface.getUtilities().createTimer( name, true ));
}
public void
createThread(
String name,
Runnable runnable )
{
plugin_interface.getUtilities().createThread( name, runnable );
}
public Comparator
getAlphanumericComparator()
{
return( plugin_interface.getUtilities().getFormatters().getAlphanumericComparator( true ));
}
public void
trace(
String str )
{
core_log.log( str );
}
public void
log(
Throwable e )
{
String nested = Debug.getNestedExceptionMessage(e);
if ( !exception_traces.contains( nested )){
exception_traces.add( nested );
if ( exception_traces.size() > 128 ){
exception_traces.clear();
}
core_log.log( e );
}else{
core_log.log( nested );
}
}
public void
log(
String str )
{
log.log( str );
}
public String
getTraceDir()
{
return( plugin_interface.getUtilities().getAzureusUserDir());
}
},
getSelectedInterfaces());
upnp.addRootDeviceListener( this );
upnp.addLogListener(
new UPnPLogListener()
{
public void
log(
String str )
{
log.log( str );
}
public void
logAlert(
String str,
boolean error,
int type )
{
boolean logged = false;
if ( alert_device_probs_param.getValue()){
if ( type == UPnPLogListener.TYPE_ALWAYS ){
log.logAlertRepeatable(
error?LoggerChannel.LT_ERROR:LoggerChannel.LT_WARNING,
str );
logged = true;
}else{
boolean do_it = false;
if ( type == UPnPLogListener.TYPE_ONCE_EVER ){
byte[] fp =
plugin_interface.getUtilities().getSecurityManager().calculateSHA1(
str.getBytes());
String key = "upnp.alert.fp." + plugin_interface.getUtilities().getFormatters().encodeBytesToString( fp );
PluginConfig pc = plugin_interface.getPluginconfig();
if ( !pc.getPluginBooleanParameter( key, false )){
pc.setPluginParameter( key, true );
do_it = true;
}
}else{
do_it = true;
}
if ( do_it ){
log.logAlert(
error?LoggerChannel.LT_ERROR:LoggerChannel.LT_WARNING,
str );
logged = true;
}
}
}
if ( !logged ){
log.log( str );
}
}
});
mapping_manager.addListener(
new UPnPMappingManagerListener()
{
public void
mappingAdded(
UPnPMapping mapping )
{
addMapping( mapping );
}
});
UPnPMapping[] upnp_mappings = mapping_manager.getMappings();
for (int i=0;i<upnp_mappings.length;i++){
addMapping( upnp_mappings[i] );
}
setNATPMPEnableState();
}catch( Throwable e ){
log.log( e );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -