Search in sources :

Example 1 with NetworkAdminASN

use of com.biglybt.core.networkmanager.admin.NetworkAdminASN in project BiglyBT by BiglySoftware.

the class VersionCheckClient method preProcessReply.

protected void preProcessReply(Map reply, final boolean v6) {
    NetworkAdmin admin = NetworkAdmin.getSingleton();
    try {
        byte[] address = (byte[]) reply.get("source_ip_address");
        if (address != null) {
            InetAddress my_ip = InetAddress.getByName(new String(address));
            NetworkAdminASN old_asn = admin.getCurrentASN();
            NetworkAdminASN new_asn = admin.lookupCurrentASN(my_ip);
            if (!new_asn.sameAs(old_asn)) {
                if (!secondary_check_done) {
                    secondary_check_done = true;
                    new AEThread("Secondary version check", true) {

                        @Override
                        public void runSupport() {
                            getVersionCheckInfoSupport(REASON_SECONDARY_CHECK, false, true, v6);
                        }
                    }.start();
                }
            }
        }
    } catch (Throwable e) {
        if (!Debug.containsException(e, UnknownHostException.class)) {
            Debug.printStackTrace(e);
        }
    }
    Long as_advice = (Long) reply.get("as_advice");
    if (as_advice != null) {
        NetworkAdminASN current_asn = admin.getCurrentASN();
        String asn = current_asn.getASName();
        if (asn != null) {
            long advice = as_advice.longValue();
            if (advice != 0) {
                // require crypto
                String done_asn = COConfigurationManager.getStringParameter("ASN Advice Followed", "");
                if (!done_asn.equals(asn)) {
                    COConfigurationManager.setParameter("ASN Advice Followed", asn);
                    boolean change = advice == 1 || advice == 2;
                    boolean alert = advice == 1 || advice == 3;
                    if (!COConfigurationManager.getBooleanParameter("network.transport.encrypted.require")) {
                        if (change) {
                            COConfigurationManager.setParameter("network.transport.encrypted.require", true);
                        }
                        if (alert) {
                            String msg = MessageText.getString("crypto.alert.as.warning", new String[] { asn });
                            Logger.log(new LogAlert(false, LogAlert.AT_WARNING, msg));
                        }
                    }
                }
            }
        }
    }
    // set ui.toolbar.uiswitcher based on instructions from tracker
    // Really shouldn't be in VersionCheck client, but instead have some
    // listener and have the code elsewhere.  Simply calling
    // getVersionCheckInfo from "code elsewhere" (to get the cached result)
    // caused a deadlock at startup.
    Long lEnabledUISwitcher = (Long) reply.get("ui.toolbar.uiswitcher");
    if (lEnabledUISwitcher != null) {
        COConfigurationManager.setBooleanDefault("ui.toolbar.uiswitcher", lEnabledUISwitcher.longValue() == 1);
    }
}
Also used : NetworkAdmin(com.biglybt.core.networkmanager.admin.NetworkAdmin) NetworkAdminASN(com.biglybt.core.networkmanager.admin.NetworkAdminASN) LogAlert(com.biglybt.core.logging.LogAlert)

Example 2 with NetworkAdminASN

use of com.biglybt.core.networkmanager.admin.NetworkAdminASN 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)

Example 3 with NetworkAdminASN

use of com.biglybt.core.networkmanager.admin.NetworkAdminASN in project BiglyBT by BiglySoftware.

the class VersionCheckClient method constructVersionCheckMessage.

/**
 * Construct the default version check message.
 * @return message to send
 */
public static Map<String, Object> constructVersionCheckMessage(String reason) {
    // only send if anonymous-check flag is not set
    boolean send_info = COConfigurationManager.getBooleanParameter("Send Version Info");
    Map<String, Object> message = new HashMap<>();
    // always send
    message.put("appid", SystemProperties.getApplicationIdentifier());
    message.put("appname", SystemProperties.getApplicationName());
    message.put("version", Constants.AZUREUS_VERSION);
    message.put("first_version", COConfigurationManager.getStringParameter("First Recorded Version", ""));
    String sub_ver = Constants.AZUREUS_SUBVER;
    if (sub_ver.length() > 0) {
        message.put("subver", sub_ver);
    }
    if (COConfigurationManager.getBooleanParameter("Beta Programme Enabled")) {
        message.put("beta_prog", "true");
    }
    message.put("ui", COConfigurationManager.getStringParameter("ui", "unknown"));
    message.put("os", Constants.OSName);
    message.put("os_version", System.getProperty("os.version"));
    // see http://lopica.sourceforge.net/os.html
    message.put("os_arch", System.getProperty("os.arch"));
    // might be needed to openjdk on osx
    message.put("os_arch_dm", System.getProperty("sun.arch.data.model"));
    boolean using_phe = COConfigurationManager.getBooleanParameter("network.transport.encrypted.require");
    message.put("using_phe", using_phe ? new Long(1) : new Long(0));
    message.put("imode", COConfigurationManager.getStringParameter("installer.mode", ""));
    // swt stuff
    try {
        Class c = Class.forName("org.eclipse.swt.SWT");
        String swt_platform = (String) c.getMethod("getPlatform", new Class[] {}).invoke(null, new Object[] {});
        message.put("swt_platform", swt_platform);
        Integer swt_version = (Integer) c.getMethod("getVersion", new Class[] {}).invoke(null, new Object[] {});
        message.put("swt_version", new Long(swt_version.longValue()));
    } catch (ClassNotFoundException e) {
    /* ignore */
    } catch (NoClassDefFoundError er) {
    /* ignore */
    } catch (InvocationTargetException err) {
    /* ignore */
    } catch (Throwable t) {
        t.printStackTrace();
    }
    int last_send_time = COConfigurationManager.getIntParameter("Send Version Info Last Time", -1);
    int current_send_time = (int) (SystemTime.getCurrentTime() / 1000);
    COConfigurationManager.setParameter("Send Version Info Last Time", current_send_time);
    String id = COConfigurationManager.getStringParameter("ID", null);
    if (id != null && send_info) {
        message.put("id", id);
        try {
            byte[] id2 = CryptoManagerFactory.getSingleton().getSecureID();
            message.put("id2", id2);
        } catch (Throwable e) {
        }
        if (last_send_time != -1 && last_send_time < current_send_time) {
            // time since last
            message.put("tsl", new Long(current_send_time - last_send_time));
        }
        message.put("reason", reason);
        String java_version = Constants.JAVA_VERSION;
        if (java_version == null) {
            java_version = "unknown";
        }
        message.put("java", java_version);
        String java_vendor = System.getProperty("java.vm.vendor");
        if (java_vendor == null) {
            java_vendor = "unknown";
        }
        message.put("javavendor", java_vendor);
        int api_level = Constants.API_LEVEL;
        if (api_level > 0) {
            message.put("api_level", api_level);
        }
        long max_mem = Runtime.getRuntime().maxMemory() / (1024 * 1024);
        message.put("javamx", new Long(max_mem));
        String java_rt_name = System.getProperty("java.runtime.name");
        if (java_rt_name != null) {
            message.put("java_rt_name", java_rt_name);
        }
        String java_rt_version = System.getProperty("java.runtime.version");
        if (java_rt_version != null) {
            message.put("java_rt_version", java_rt_version);
        }
        OverallStats stats = StatsFactory.getStats();
        if (stats != null) {
            // long total_bytes_downloaded 	= stats.getDownloadedBytes();
            // long total_bytes_uploaded		= stats.getUploadedBytes();
            long total_uptime = stats.getTotalUpTime();
            // removed due to complaints about anonymous stats collection
            // message.put( "total_bytes_downloaded", new Long( total_bytes_downloaded ) );
            // message.put( "total_bytes_uploaded", new Long( total_bytes_uploaded ) );
            message.put("total_uptime", new Long(total_uptime));
        // message.put( "dlstats", stats.getDownloadStats());
        }
        try {
            int port = UDPNetworkManager.getSingleton().getUDPNonDataListeningPortNumber();
            message.put("dht", port);
        } catch (Throwable e) {
            Debug.out(e);
        }
        try {
            NetworkAdminASN current_asn = NetworkAdmin.getSingleton().getCurrentASN();
            message.put("ip_as", current_asn.getAS());
            String asn = current_asn.getASName();
            if (asn.length() > 64) {
                asn = asn.substring(0, 64);
            }
            message.put("ip_asn", asn);
        } catch (Throwable e) {
            Debug.out(e);
        }
        // send locale, so we can determine which languages need attention
        message.put("locale", Locale.getDefault().toString());
        String originalLocale = System.getProperty("user.language") + "_" + System.getProperty("user.country");
        String variant = System.getProperty("user.variant");
        if (variant != null && variant.length() > 0) {
            originalLocale += "_" + variant;
        }
        message.put("orig_locale", originalLocale);
        // We may want to reply differently if the user is in Beginner mode vs Advanced
        message.put("user_mode", COConfigurationManager.getIntParameter("User Mode", -1));
        try {
            if (CoreFactory.isCoreAvailable() && CoreFactory.getSingleton().getPluginManager().isInitialized()) {
                // installed plugin IDs
                PluginInterface[] plugins = CoreFactory.getSingleton().getPluginManager().getPluginInterfaces();
                List pids = new ArrayList();
                List vs_data = new ArrayList();
                for (int i = 0; i < plugins.length; i++) {
                    PluginInterface plugin = plugins[i];
                    String pid = plugin.getPluginID();
                    String info = plugin.getPluginconfig().getPluginStringParameter("plugin.info");
                    // filter out built-in and core ones
                    if ((info != null && info.length() > 0) || (!pid.startsWith("<") && !pid.startsWith("azbp") && !pid.startsWith("azupdater") && !pid.startsWith("azplatform") && !pids.contains(pid))) {
                        if (info != null && info.length() > 0) {
                            if (info.length() < 256) {
                                pid += ":" + info;
                            } else {
                                Debug.out("Plugin '" + pid + "' reported excessive info string '" + info + "'");
                            }
                        }
                        pids.add(pid);
                    }
                    Map data = plugin.getPluginconfig().getPluginMapParameter("plugin.versionserver.data", null);
                    if (data != null) {
                        Map payload = new HashMap();
                        byte[] data_bytes = BEncoder.encode(data);
                        if (data_bytes.length > 16 * 1024) {
                            Debug.out("Plugin '" + pid + "' reported excessive version server data (length=" + data_bytes.length + ")");
                            payload.put("error", "data too long: " + data_bytes.length);
                        } else {
                            payload.put("data", data_bytes);
                        }
                        payload.put("id", pid);
                        payload.put("version", plugin.getPluginVersion());
                        vs_data.add(payload);
                    }
                }
                message.put("plugins", pids);
                if (vs_data.size() > 0) {
                    message.put("plugin_data", vs_data);
                }
            }
        } catch (Throwable e) {
            Debug.out(e);
        }
    }
    return message;
}
Also used : OverallStats(com.biglybt.core.stats.transfer.OverallStats) NetworkAdminASN(com.biglybt.core.networkmanager.admin.NetworkAdminASN) PluginInterface(com.biglybt.pif.PluginInterface) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

NetworkAdminASN (com.biglybt.core.networkmanager.admin.NetworkAdminASN)3 PluginInterface (com.biglybt.pif.PluginInterface)2 LogAlert (com.biglybt.core.logging.LogAlert)1 NetworkAdmin (com.biglybt.core.networkmanager.admin.NetworkAdmin)1 NetworkAdminException (com.biglybt.core.networkmanager.admin.NetworkAdminException)1 OverallStats (com.biglybt.core.stats.transfer.OverallStats)1 PRUDPPacketHandler (com.biglybt.net.udp.uc.PRUDPPacketHandler)1 PRUDPReleasablePacketHandler (com.biglybt.net.udp.uc.PRUDPReleasablePacketHandler)1 UPnPPlugin (com.biglybt.plugin.upnp.UPnPPlugin)1 UPnPPluginService (com.biglybt.plugin.upnp.UPnPPluginService)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1