Search in sources :

Example 1 with UPnPPluginService

use of com.biglybt.plugin.upnp.UPnPPluginService in project BiglyBT by BiglySoftware.

the class NetworkAdminImpl method getNATDevices.

@Override
public NetworkAdminNATDevice[] getNATDevices(Core core) {
    List<NetworkAdminNATDeviceImpl> devices = new ArrayList<>();
    try {
        PluginInterface upnp_pi = core.getPluginManager().getPluginInterfaceByClass(UPnPPlugin.class);
        if (upnp_pi != null) {
            UPnPPlugin upnp = (UPnPPlugin) upnp_pi.getPlugin();
            UPnPPluginService[] services = upnp.getServices();
            for (UPnPPluginService service : services) {
                NetworkAdminNATDeviceImpl dev = new NetworkAdminNATDeviceImpl(service);
                boolean same = false;
                for (NetworkAdminNATDeviceImpl d : devices) {
                    if (d.sameAs(dev)) {
                        same = true;
                        break;
                    }
                }
                if (!same) {
                    devices.add(dev);
                }
            }
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
    }
    return ((NetworkAdminNATDevice[]) devices.toArray(new NetworkAdminNATDevice[devices.size()]));
}
Also used : UPnPPluginService(com.biglybt.plugin.upnp.UPnPPluginService) UPnPPlugin(com.biglybt.plugin.upnp.UPnPPlugin) PluginInterface(com.biglybt.pif.PluginInterface)

Example 2 with UPnPPluginService

use of com.biglybt.plugin.upnp.UPnPPluginService in project BiglyBT by BiglySoftware.

the class PairingManagerImpl method doUpdate.

protected void doUpdate() {
    long now = SystemTime.getMonotonousTime();
    synchronized (this) {
        if (deferred_update_event != null) {
            return;
        }
        long time_since_last_update = now - last_update_time;
        if (last_update_time > 0 && time_since_last_update < min_update_period) {
            deferUpdate(min_update_period - time_since_last_update);
            return;
        }
        update_in_progress = true;
    }
    try {
        Map<String, Object> payload = new HashMap<>();
        boolean is_enabled = param_enable.getValue();
        boolean has_services = false;
        synchronized (this) {
            List<Map<String, String>> list = new ArrayList<>();
            payload.put("s", list);
            if (services.size() > 0 && is_enabled) {
                if (global_update_event == null) {
                    global_update_event = SimpleTimer.addPeriodicEvent("PM:updater", GLOBAL_UPDATE_PERIOD, new TimerEventPerformer() {

                        private int tick_count;

                        @Override
                        public void perform(TimerEvent event) {
                            tick_count++;
                            updateGlobals(false);
                            if (tick_count % CD_REFRESH_TICKS == 0) {
                                updateNeeded();
                            }
                        }
                    });
                    updateGlobals(true);
                }
                boolean enable_nets = param_net_enable.getValue();
                for (PairedServiceImpl service : services.values()) {
                    list.add(service.toMap(enable_nets));
                }
                has_services = list.size() > 0;
            } else {
                if (global_update_event == null) {
                    if (consec_update_fails == 0 && !must_update_once) {
                        update_in_progress = false;
                        setStatus(MessageText.getString(is_enabled ? "pairing.status.noservices" : "label.disabled"));
                        return;
                    }
                } else {
                    global_update_event.cancel();
                    global_update_event = null;
                }
            }
            last_update_time = now;
        }
        // we need a valid access code here!
        String ac = readAccessCode();
        if (ac.length() == 0) {
            ac = allocateAccessCode(true);
        }
        payload.put("ac", ac);
        String gc = getGroup();
        if (gc != null && gc.length() > 0) {
            payload.put("gc", gc);
        }
        if (is_enabled && has_services && param_srp_enable.getValue()) {
            tunnel_handler.setActive(true);
            tunnel_handler.updateRegistrationData(payload);
        } else {
            tunnel_handler.setActive(false);
        }
        synchronized (this) {
            if (current_v4 != null) {
                payload.put("c_v4", current_v4.getHostAddress());
            }
            if (current_v6 != null) {
                payload.put("c_v6", current_v6.getHostAddress());
            }
            if (local_v4.length() > 0) {
                payload.put("l_v4", local_v4);
            }
            if (local_v6.length() > 0) {
                payload.put("l_v6", local_v6);
            }
            if (param_e_enable.getValue()) {
                String host = param_host.getValue().trim();
                if (host.length() > 0) {
                    payload.put("e_h", host);
                }
                String v4 = param_public_ipv4.getValue().trim();
                if (v4.length() > 0) {
                    payload.put("e_v4", v4);
                }
                String v6 = param_public_ipv6.getValue().trim();
                if (v6.length() > 0) {
                    payload.put("e_v6", v6);
                }
                String l_v4 = param_local_ipv4.getValue().trim();
                if (l_v4.length() > 0) {
                    payload.put("e_l_v4", l_v4);
                }
                String l_v6 = param_local_ipv6.getValue().trim();
                if (l_v6.length() > 0) {
                    payload.put("e_l_v6", l_v6);
                }
            }
            try {
                PluginInterface pi_upnp = core.getPluginManager().getPluginInterfaceByClass(UPnPPlugin.class);
                if (pi_upnp != null) {
                    UPnPPlugin upnp = (UPnPPlugin) pi_upnp.getPlugin();
                    if (upnp.isEnabled()) {
                        List<Map<String, String>> upnp_list = new ArrayList<>();
                        payload.put("upnp", upnp_list);
                        UPnPPluginService[] services = upnp.getServices();
                        Set<UPnPRootDevice> devices = new HashSet<>();
                        for (UPnPPluginService service : services) {
                            if (upnp_list.size() > 10) {
                                break;
                            }
                            UPnPRootDevice root_device = service.getService().getGenericService().getDevice().getRootDevice();
                            if (!devices.contains(root_device)) {
                                devices.add(root_device);
                                Map<String, String> map = new HashMap<>();
                                upnp_list.add(map);
                                map.put("i", root_device.getInfo());
                            }
                        }
                    }
                }
            } catch (Throwable e) {
            }
            try {
                NetworkAdmin admin = NetworkAdmin.getSingleton();
                NetworkAdminHTTPProxy http_proxy = admin.getHTTPProxy();
                if (http_proxy != null) {
                    payload.put("hp", http_proxy.getName());
                }
                NetworkAdminSocksProxy[] socks_proxies = admin.getSocksProxies();
                if (socks_proxies.length > 0) {
                    payload.put("sp", socks_proxies[0].getName());
                }
            } catch (Throwable e) {
            }
            payload.put("_enabled", is_enabled ? 1L : 0L);
        }
        if (DEBUG) {
            System.out.println("PS: doUpdate: " + payload);
        }
        sendRequest("update", payload);
        synchronized (this) {
            consec_update_fails = 0;
            must_update_once = false;
            if (deferred_update_event == null) {
                COConfigurationManager.setParameter("pairing.updateoutstanding", false);
            }
            update_in_progress = false;
            if (global_update_event == null) {
                setStatus(MessageText.getString(is_enabled ? "pairing.status.noservices" : "label.disabled"));
            } else {
                setStatus(MessageText.getString("pairing.status.registered", new String[] { new SimpleDateFormat().format(new Date(SystemTime.getCurrentTime())) }));
            }
        }
    } catch (Throwable e) {
        synchronized (this) {
            try {
                consec_update_fails++;
                long back_off = min_update_period;
                for (int i = 0; i < consec_update_fails; i++) {
                    back_off *= 2;
                    if (back_off > max_update_period) {
                        back_off = max_update_period;
                        break;
                    }
                }
                deferUpdate(back_off);
            } finally {
                update_in_progress = false;
            }
        }
    } finally {
        synchronized (this) {
            if (update_in_progress) {
                Debug.out("Something didn't clear update_in_progress!!!!");
                update_in_progress = false;
            }
        }
    }
}
Also used : UPnPPluginService(com.biglybt.plugin.upnp.UPnPPluginService) PluginInterface(com.biglybt.pif.PluginInterface) UPnPRootDevice(com.biglybt.net.upnp.UPnPRootDevice) UPnPPlugin(com.biglybt.plugin.upnp.UPnPPlugin) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with UPnPPluginService

use of com.biglybt.plugin.upnp.UPnPPluginService in project BiglyBT by BiglySoftware.

the class DeviceInternetGatewayImpl method getDisplayProperties.

@Override
protected void getDisplayProperties(List<String[]> dp) {
    super.getDisplayProperties(dp);
    addDP(dp, "device.router.is_mapping", mapper_enabled);
    UPnPPluginService[] services = current_services;
    String req_map_str = "";
    Set<mapping> required = getRequiredMappings();
    for (mapping m : required) {
        req_map_str += (req_map_str.length() == 0 ? "" : ",") + m.getString();
    }
    addDP(dp, "device.router.req_map", req_map_str);
    if (services != null) {
        for (UPnPPluginService service : services) {
            Set<mapping> actual = getActualMappings(service);
            String act_map_str = "";
            for (mapping m : actual) {
                if (required.contains(m)) {
                    act_map_str += (act_map_str.length() == 0 ? "" : ",") + m.getString();
                }
            }
            String service_name = MessageText.getString("device.router.con_type", new String[] { service.getService().getConnectionType() });
            addDP(dp, "!    " + service_name + "!", act_map_str);
        }
    }
}
Also used : UPnPPluginService(com.biglybt.plugin.upnp.UPnPPluginService)

Example 4 with UPnPPluginService

use of com.biglybt.plugin.upnp.UPnPPluginService in project BiglyBT by BiglySoftware.

the class NetworkAdminUDPTester method testInbound.

@Override
public InetAddress testInbound(InetAddress bind_ip, int bind_port) throws NetworkAdminException {
    PRUDPReleasablePacketHandler handler = PRUDPPacketHandlerFactory.getReleasableHandler(bind_port);
    PRUDPPacketHandler packet_handler = handler.getHandler();
    HashMap data_to_send = new HashMap();
    PluginInterface pi_upnp = core.getPluginManager().getPluginInterfaceByClass(UPnPPlugin.class);
    String upnp_str = null;
    if (pi_upnp != null) {
        UPnPPlugin upnp = (UPnPPlugin) pi_upnp.getPlugin();
        /*
			UPnPMapping mapping = upnp.getMapping( true, port );

			if ( mapping == null ) {

				new_mapping = mapping = upnp.addMapping( "NAT Tester", true, port, true );

				// give UPnP a chance to work

				try {
					Thread.sleep( 500 );

				}
				catch (Throwable e) {

					Debug.printStackTrace( e );
				}
			}
			*/
        UPnPPluginService[] services = upnp.getServices();
        if (services.length > 0) {
            upnp_str = "";
            for (int i = 0; i < services.length; i++) {
                UPnPPluginService service = services[i];
                upnp_str += (i == 0 ? "" : ",") + service.getInfo();
            }
        }
    }
    if (upnp_str != null) {
        data_to_send.put("upnp", upnp_str);
    }
    NetworkAdminASN net_asn = NetworkAdmin.getSingleton().getCurrentASN();
    String as = net_asn.getAS();
    String asn = net_asn.getASName();
    if (as.length() > 0) {
        data_to_send.put("as", as);
    }
    if (asn.length() > 0) {
        data_to_send.put("asn", asn);
    }
    data_to_send.put("locale", MessageText.getCurrentLocale().toString());
    Random random = new Random();
    data_to_send.put("id", new Long(random.nextLong()));
    try {
        packet_handler.setExplicitBindAddress(bind_ip);
        Throwable last_error = null;
        long timeout = 5000;
        long timeout_inc = 5000;
        try {
            for (int i = 0; i < 3; i++) {
                data_to_send.put("seq", new Long(i));
                try {
                    // connection ids for requests must always have their msb set...
                    // apart from the original darn udp tracker spec....
                    long connection_id = 0x8000000000000000L | random.nextLong();
                    NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest(connection_id);
                    request_packet.setPayload(data_to_send);
                    if (listener != null) {
                        listener.reportProgress("Sending outbound packet and waiting for reply probe (timeout=" + timeout + ")");
                    }
                    NetworkAdminNATUDPReply reply_packet = (NetworkAdminNATUDPReply) packet_handler.sendAndReceive(null, request_packet, new InetSocketAddress(UDP_SERVER_ADDRESS, UDP_SERVER_PORT), timeout, PRUDPPacketHandler.PRIORITY_IMMEDIATE);
                    Map reply = reply_packet.getPayload();
                    byte[] ip_bytes = (byte[]) reply.get("ip_address");
                    if (ip_bytes == null) {
                        throw (new NetworkAdminException("IP address missing in reply"));
                    }
                    byte[] reason = (byte[]) reply.get("reason");
                    if (reason != null) {
                        throw (new NetworkAdminException(new String(reason, "UTF8")));
                    }
                    return (InetAddress.getByAddress(ip_bytes));
                } catch (Throwable e) {
                    last_error = e;
                    timeout += timeout_inc;
                }
            }
            if (last_error != null) {
                throw (last_error);
            }
            throw (new NetworkAdminException("Timeout"));
        } finally {
            try {
                data_to_send.put("seq", new Long(99));
                long connection_id = 0x8000000000000000L | random.nextLong();
                NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest(connection_id);
                request_packet.setPayload(data_to_send);
                if (listener != null) {
                    listener.reportProgress("Sending completion event");
                }
                packet_handler.send(request_packet, new InetSocketAddress(UDP_SERVER_ADDRESS, UDP_SERVER_PORT));
            } catch (Throwable e) {
            }
        }
    } catch (NetworkAdminException e) {
        throw (e);
    } catch (Throwable e) {
        throw (new NetworkAdminException("Inbound test failed", e));
    } finally {
        packet_handler.setExplicitBindAddress(null);
        handler.release();
    }
}
Also used : NetworkAdminException(com.biglybt.core.networkmanager.admin.NetworkAdminException) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) PluginInterface(com.biglybt.pif.PluginInterface) NetworkAdminASN(com.biglybt.core.networkmanager.admin.NetworkAdminASN) UPnPPluginService(com.biglybt.plugin.upnp.UPnPPluginService) PRUDPReleasablePacketHandler(com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler) Random(java.util.Random) PRUDPPacketHandler(com.biglybt.net.udp.uc.PRUDPPacketHandler) UPnPPlugin(com.biglybt.plugin.upnp.UPnPPlugin) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UPnPPluginService (com.biglybt.plugin.upnp.UPnPPluginService)4 PluginInterface (com.biglybt.pif.PluginInterface)3 UPnPPlugin (com.biglybt.plugin.upnp.UPnPPlugin)3 NetworkAdminASN (com.biglybt.core.networkmanager.admin.NetworkAdminASN)1 NetworkAdminException (com.biglybt.core.networkmanager.admin.NetworkAdminException)1 PRUDPPacketHandler (com.biglybt.net.udp.uc.PRUDPPacketHandler)1 PRUDPReleasablePacketHandler (com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler)1 UPnPRootDevice (com.biglybt.net.upnp.UPnPRootDevice)1 InetSocketAddress (java.net.InetSocketAddress)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1