📄 upnpplugin.java
字号:
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] );
}
}catch( Throwable e ){
log.log( e );
}
}
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 );
}
}
}
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")){
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
{
try{
this_mon.enter();
log.log( " Found " + ( wan_service.getGenericService().getServiceType().indexOf("PPP") == -1? "WANIPConnection":"WANPPPConnection" ));
UPnPWANConnectionPortMapping[] 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 );
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();
}
}
// for external use, e.g. webui
public UPnPMapping
addMapping(
String desc_resource,
boolean tcp,
int port,
boolean enabled )
{
return( UPnPMappingManager.getSingleton().addMapping( desc_resource, tcp, port, enabled ));
}
public UPnPMapping
getMapping(
boolean tcp,
int port )
{
return( UPnPMappingManager.getSingleton().getMapping( tcp, port ));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -