📄 upnpplugin.java
字号:
protected void
closeDown(
boolean end_of_day )
{
for (int i=0;i<mappings.size();i++){
UPnPMapping mapping = (UPnPMapping)mappings.get(i);
if ( !mapping.isEnabled()){
continue;
}
for (int j=0;j<services.size();j++){
UPnPPluginService service = (UPnPPluginService)services.get(j);
service.removeMapping( log, mapping, end_of_day );
}
}
}
public boolean
deviceDiscovered(
String USN,
URL location )
{
if ( !ignore_bad_devices.getValue()){
return( true );
}
incrementDeviceStats( USN, STATS_DISCOVER );
boolean ok = checkDeviceStats( USN, location );
String stats = "";
for (int i=0;i<STATS_KEYS.length;i++){
stats += (i==0?"":",")+STATS_KEYS[i] + "=" + getDeviceStats( USN, STATS_KEYS[i] );
}
if ( !ok ){
log.log( "Device '" + location + "' is being ignored: " + stats );
}else{
log.log( "Device '" + location +"' is ok: " + stats );
}
return( ok );
}
public void
rootDeviceFound(
UPnPRootDevice device )
{
incrementDeviceStats( device.getUSN(), "found" );
checkDeviceStats( device );
try{
processDevice( device.getDevice() );
try{
this_mon.enter();
root_info_map.put( device.getLocation(), device.getInfo());
Iterator it = root_info_map.values().iterator();
String all_info = "";
List reported_info = new ArrayList();
while( it.hasNext()){
String info = (String)it.next();
if ( info != null && !reported_info.contains( info )){
reported_info.add( info );
all_info += (all_info.length()==0?"":",") + info;
}
}
if ( all_info.length() > 0 ){
plugin_interface.getPluginconfig().setPluginParameter( "plugin.info", all_info );
}
}finally{
this_mon.exit();
}
}catch( Throwable e ){
log.log( "Root device processing fails", e );
}
}
protected boolean
checkDeviceStats(
UPnPRootDevice root )
{
return( checkDeviceStats( root.getUSN(), root.getLocation()));
}
protected boolean
checkDeviceStats(
String USN,
URL location )
{
long discovers = getDeviceStats( USN, STATS_DISCOVER );
long founds = getDeviceStats( USN, STATS_FOUND );
if ( discovers > 3 && founds == 0 ){
// discovered but never found - something went wrong with the device
// construction process
ignoreDevice( USN, location );
return( false );
}else if ( founds > 0 ){
// found ok before, reset details in case now its screwed
setDeviceStats( USN, STATS_DISCOVER, 0 );
setDeviceStats( USN, STATS_FOUND, 0 );
}
long map_ok = getDeviceStats( USN, STATS_MAP_OK );
long map_bad = getDeviceStats( USN, STATS_MAP_BAD );
if ( map_bad > 5 && map_ok == 0 ){
ignoreDevice( USN, location );
return( false );
}else if ( map_ok > 0 ){
setDeviceStats( USN, STATS_MAP_OK, 0 );
setDeviceStats( USN, STATS_MAP_BAD, 0 );
}
return( true );
}
protected long
incrementDeviceStats(
String USN,
String stat_key )
{
String key = "upnp.device.stats." + stat_key;
PluginConfig pc = plugin_interface.getPluginconfig();
Map counts = pc.getPluginMapParameter( key, new HashMap());
Long count = (Long)counts.get( USN );
if ( count == null ){
count = new Long(1);
}else{
count = new Long( count.longValue() + 1 );
}
counts.put( USN, count );
pc.getPluginMapParameter( key, counts );
return( count.longValue());
}
protected long
getDeviceStats(
String USN,
String stat_key )
{
String key = "upnp.device.stats." + stat_key;
PluginConfig pc = plugin_interface.getPluginconfig();
Map counts = pc.getPluginMapParameter( key, new HashMap());
Long count = (Long)counts.get( USN );
if ( count == null ){
return( 0 );
}
return( count.longValue());
}
protected void
setDeviceStats(
String USN,
String stat_key,
long value )
{
String key = "upnp.device.stats." + stat_key;
PluginConfig pc = plugin_interface.getPluginconfig();
Map counts = pc.getPluginMapParameter( key, new HashMap());
counts.put( USN, new Long( value ));
pc.getPluginMapParameter( key, counts );
}
public void
mappingResult(
UPnPWANConnection connection,
boolean ok )
{
UPnPRootDevice root = connection.getGenericService().getDevice().getRootDevice();
incrementDeviceStats( root.getUSN(), ok?STATS_MAP_OK:STATS_MAP_BAD );
checkDeviceStats( root );
}
public void
mappingsReadResult(
UPnPWANConnection connection,
boolean ok )
{
UPnPRootDevice root = connection.getGenericService().getDevice().getRootDevice();
incrementDeviceStats( root.getUSN(), ok?STATS_READ_OK:STATS_READ_BAD );
}
protected String[]
getSelectedInterfaces()
{
String si = selected_interfaces_param.getValue().trim();
StringTokenizer tok = new StringTokenizer( si, ";" );
List res = new ArrayList();
while( tok.hasMoreTokens()){
String s = tok.nextToken().trim();
if ( s.length() > 0 ){
res.add( s );
}
}
return( (String[])res.toArray( new String[res.size()]));
}
protected void
processDevice(
UPnPDevice device )
throws UPnPException
{
processServices( device, device.getServices());
UPnPDevice[] kids = device.getSubDevices();
for (int i=0;i<kids.length;i++){
processDevice( kids[i] );
}
}
protected void
processServices(
UPnPDevice device,
UPnPService[] device_services )
throws UPnPException
{
for (int i=0;i<device_services.length;i++){
UPnPService s = device_services[i];
String service_type = s.getServiceType();
if ( service_type.equalsIgnoreCase( "urn:schemas-upnp-org:service:WANIPConnection:1") ||
service_type.equalsIgnoreCase( "urn:schemas-upnp-org:service:WANPPPConnection:1")){
final UPnPWANConnection wan_service = (UPnPWANConnection)s.getSpecificService();
device.getRootDevice().addListener(
new UPnPRootDeviceListener()
{
public void
lost(
UPnPRootDevice root,
boolean replaced )
{
removeService( wan_service, replaced );
}
});
addService( wan_service );
}else if ( service_type.equalsIgnoreCase( "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1")){
/* useless stats
try{
UPnPWANCommonInterfaceConfig config = (UPnPWANCommonInterfaceConfig)s.getSpecificService();
long[] speeds = config.getCommonLinkProperties();
if ( speeds[0] > 0 && speeds[1] > 0 ){
log.log( "Device speed: down=" +
plugin_interface.getUtilities().getFormatters().formatByteCountToKiBEtcPerSec(speeds[0]/8) + ", up=" +
plugin_interface.getUtilities().getFormatters().formatByteCountToKiBEtcPerSec(speeds[1]/8));
}
}catch( Throwable e ){
log.log(e);
}
*/
}
}
}
protected void
addService(
UPnPWANConnection wan_service )
throws UPnPException
{
wan_service.addListener( this );
mapping_manager.serviceFound( wan_service );
try{
this_mon.enter();
log.log( " Found " + ( wan_service.getGenericService().getServiceType().indexOf("PPP") == -1? "WANIPConnection":"WANPPPConnection" ));
UPnPWANConnectionPortMapping[] ports;
String usn = wan_service.getGenericService().getDevice().getRootDevice().getUSN();
if ( getDeviceStats( usn, STATS_READ_OK ) == 0 && getDeviceStats( usn, STATS_READ_BAD ) > 2 ){
ports = new UPnPWANConnectionPortMapping[0];
wan_service.periodicallyRecheckMappings( false );
log.log( " Not reading port mappings from device due to previous failures" );
}else{
ports = wan_service.getPortMappings();
}
for (int j=0;j<ports.length;j++){
log.log( " mapping [" + j + "] " + ports[j].getExternalPort() + "/" +
(ports[j].isTCP()?"TCP":"UDP" ) + " [" + ports[j].getDescription() + "] -> " + ports[j].getInternalHost());
}
services.add(new UPnPPluginService( wan_service, ports, alert_success_param, grab_ports_param, alert_other_port_param, release_mappings_param ));
checkState();
}finally{
this_mon.exit();
}
}
protected void
removeService(
UPnPWANConnection wan_service,
boolean replaced )
{
try{
this_mon.enter();
String name = wan_service.getGenericService().getServiceType().indexOf("PPP") == -1? "WANIPConnection":"WANPPPConnection";
String text =
MessageText.getString(
"upnp.alert.lostdevice",
new String[]{ name, wan_service.getGenericService().getDevice().getRootDevice().getLocation().getHost()});
log.log( text );
if ( (!replaced) && alert_device_probs_param.getValue()){
log.logAlertRepeatable( LoggerChannel.LT_WARNING, text );
}
for (int i=0;i<services.size();i++){
UPnPPluginService ps = (UPnPPluginService)services.get(i);
if ( ps.getService() == wan_service ){
services.remove(i);
break;
}
}
}finally{
this_mon.exit();
}
}
protected void
addMapping(
UPnPMapping mapping )
{
try{
this_mon.enter();
mappings.add( mapping );
log.log( "Mapping request: " + mapping.getString() + ", enabled = " + mapping.isEnabled());
mapping.addListener( this );
checkState();
}finally{
this_mon.exit();
}
}
public void
mappingChanged(
UPnPMapping mapping )
{
checkState();
}
public void
mappingDestroyed(
UPnPMapping mapping )
{
try{
this_mon.enter();
mappings.remove( mapping );
log.log( "Mapping request removed: " + mapping.getString());
for (int j=0;j<services.size();j++){
UPnPPluginService service = (UPnPPluginService)services.get(j);
service.removeMapping( log, mapping, false );
}
}finally{
this_mon.exit();
}
}
protected void
checkState()
{
try{
this_mon.enter();
for (int i=0;i<mappings.size();i++){
UPnPMapping mapping = (UPnPMapping)mappings.get(i);
for (int j=0;j<services.size();j++){
UPnPPluginService service = (UPnPPluginService)services.get(j);
service.checkMapping( log, mapping );
}
}
}finally{
this_mon.exit();
}
}
public String[]
getExternalIPAddresses()
{
List res = new ArrayList();
try{
this_mon.enter();
for (int j=0;j<services.size();j++){
UPnPPluginService service = (UPnPPluginService)services.get(j);
try{
String address = service.getService().getExternalIPAddress();
if ( address != null ){
res.add( address );
}
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
}finally{
this_mon.exit();
}
return((String[])res.toArray( new String[res.size()]));
}
public UPnPPluginService[]
getServices()
{
try{
this_mon.enter();
return((UPnPPluginService[])services.toArray( new UPnPPluginService[services.size()] ));
}finally{
this_mon.exit();
}
}
// for external use, e.g. webui
public UPnPMapping
addMapping(
String desc_resource,
boolean tcp,
int port,
boolean enabled )
{
return( mapping_manager.addMapping( desc_resource, tcp, port, enabled ));
}
public UPnPMapping
getMapping(
boolean tcp,
int port )
{
return( mapping_manager.getMapping( tcp, port ));
}
protected void
setNATPMPEnableState()
{
boolean enabled = natpmp_enable_param.getValue() && upnp_enable_param.getValue();
try{
if ( enabled ){
if ( nat_pmp_upnp == null ){
nat_pmp_upnp =
NatPMPUPnPFactory.create(
upnp,
NatPMPDeviceFactory.getSingleton(
new NATPMPDeviceAdapter()
{
public String
getRouterAddress()
{
return( nat_pmp_router.getValue());
}
public void
log(
String str )
{
log.log( "NAT-PMP: " + str );
}
}));
nat_pmp_upnp.addListener( this );
}
nat_pmp_upnp.setEnabled( true );
}else{
if ( nat_pmp_upnp != null ){
nat_pmp_upnp.setEnabled( false );
}
}
}catch( Throwable e ){
log.log( "Failed to initialise NAT-PMP subsystem", e );
}
}
protected void
logAlert(
int type,
String resource,
String[] params )
{
String text =
plugin_interface.getUtilities().getLocaleUtilities().getLocalisedMessageText(
resource, params );
log.logAlertRepeatable( type, text );
}
/**
* Provided for use by other plugins.
*/
public void refreshMappings() {
this.upnp.reset();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -