Search in sources :

Example 41 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class ResourceDownloaderFactoryImpl method getSuffixBasedDownloader.

@Override
public ResourceDownloader getSuffixBasedDownloader(ResourceDownloader _downloader) {
    ResourceDownloaderBaseImpl dl = (ResourceDownloaderBaseImpl) _downloader;
    URL target = null;
    while (true) {
        List kids = dl.getChildren();
        if (kids.size() == 0) {
            target = ((ResourceDownloaderURLImpl) dl).getURL();
            break;
        }
        dl = (ResourceDownloaderBaseImpl) kids.get(0);
    }
    ResourceDownloader result;
    if (target == null) {
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "ResourceDownloader: suffix " + "based downloader failed to find leaf"));
        result = _downloader;
    } else {
        if (target.getPath().toLowerCase().endsWith(".torrent")) {
            result = getTorrentDownloader(_downloader, true);
        } else {
            result = _downloader;
        }
    }
    if (COConfigurationManager.getBooleanParameter("update.anonymous")) {
        try {
            ((ResourceDownloaderBaseImpl) result).setPropertyRecursive(ResourceDownloader.PR_BOOLEAN_ANONYMOUS, true);
        } catch (Throwable e) {
            Debug.out(e);
        }
    }
    return (result);
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) List(java.util.List) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) URL(java.net.URL)

Example 42 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class SESTSConnectionImpl method rateLimit.

protected static void rateLimit(InetSocketAddress originator) throws Exception {
    synchronized (SESTSConnectionImpl.class) {
        int hit_count = generate_bloom.add(AddressUtils.getAddressBytes(originator));
        long now = SystemTime.getCurrentTime();
        if (generate_bloom.getSize() / generate_bloom.getEntryCount() < 10) {
            generate_bloom = BloomFilterFactory.createAddRemove4Bit(generate_bloom.getSize() + BLOOM_INCREASE);
            generate_bloom_create_time = now;
            Logger.log(new LogEvent(LOGID, "STS bloom: size increased to " + generate_bloom.getSize()));
        } else if (now < generate_bloom_create_time || now - generate_bloom_create_time > BLOOM_RECREATE) {
            generate_bloom = BloomFilterFactory.createAddRemove4Bit(generate_bloom.getSize());
            generate_bloom_create_time = now;
        }
        if (hit_count >= 15) {
            Logger.log(new LogEvent(LOGID, "STS bloom: too many recent connection attempts from " + originator));
            Debug.out("STS: too many recent connection attempts from " + originator);
            throw (new IOException("Too many recent connection attempts (sts)"));
        }
        long since_last = now - last_incoming_sts_create;
        long delay = 100 - since_last;
        if (delay > 0 && delay < 100) {
            try {
                Logger.log(new LogEvent(LOGID, "STS: too many recent connection attempts, delaying " + delay));
                Thread.sleep(delay);
            } catch (Throwable e) {
            }
        }
        last_incoming_sts_create = now;
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) IOException(java.io.IOException) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint)

Example 43 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class CorePatchChecker method patch.

protected void patch(UpdateCheckInstance instance, Update updater_update, PluginInterface updater_plugin) {
    try {
        // use the update plugin to log stuff....
        ResourceDownloader rd_log = updater_update.getDownloaders()[0];
        File[] files = new File(updater_plugin.getPluginDirectoryName()).listFiles();
        if (files == null) {
            if (Logger.isEnabled())
                Logger.log(new LogEvent(LOGID, "Core Patcher: no files in plugin dir!!!"));
            return;
        }
        String patch_prefix = "BiglyBT_" + Constants.getBaseVersion() + "_P";
        int highest_p = -1;
        File highest_p_file = null;
        for (int i = 0; i < files.length; i++) {
            String name = files[i].getName();
            if (name.startsWith(patch_prefix) && name.endsWith(".pat")) {
                if (Logger.isEnabled())
                    Logger.log(new LogEvent(LOGID, "Core Patcher: found patch file '" + name + "'"));
                try {
                    int this_p = Integer.parseInt(name.substring(patch_prefix.length(), name.indexOf(".pat")));
                    if (this_p > highest_p) {
                        highest_p = this_p;
                        highest_p_file = files[i];
                    }
                } catch (Throwable e) {
                    Debug.printStackTrace(e);
                }
            }
        }
        if (CorePatchLevel.getCurrentPatchLevel() >= highest_p) {
            if (Logger.isEnabled())
                Logger.log(new LogEvent(LOGID, "Core Patcher: no applicable patch found (highest = " + highest_p + ")"));
            if (updater_update.getRestartRequired() == Update.RESTART_REQUIRED_MAYBE) {
                updater_update.setRestartRequired(Update.RESTART_REQUIRED_NO);
            }
        } else {
            rd_log.reportActivity("Applying patch '" + highest_p_file.getName() + "'");
            if (Logger.isEnabled())
                Logger.log(new LogEvent(LOGID, "Core Patcher: applying patch '" + highest_p_file.toString() + "'"));
            InputStream pis = new FileInputStream(highest_p_file);
            try {
                patchBiglyBT(instance, pis, "P" + highest_p, plugin_interface.getLogger().getChannel("CorePatcher"));
                Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_INFORMATION, "Patch " + highest_p_file.getName() + " ready to be applied"));
                String done_file = highest_p_file.toString();
                done_file = done_file.substring(0, done_file.length() - 1) + "x";
                highest_p_file.renameTo(new File(done_file));
                // flip the original update over to 'restart required'
                updater_update.setRestartRequired(Update.RESTART_REQUIRED_YES);
            } finally {
                try {
                    pis.close();
                } catch (Throwable e) {
                }
            }
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Core Patcher failed", e));
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) LogAlert(com.biglybt.core.logging.LogAlert)

Example 44 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class CoreUpdateChecker method launchUpdate.

protected void launchUpdate(File file, String[] args) {
    try {
        if (file.getName().endsWith(".exe")) {
            try {
                AEWin32Access accessor = AEWin32Manager.getAccessor(true);
                // accessor.createProcess( , false );
                String s_args = null;
                if (args.length > 0) {
                    s_args = "";
                    for (String s : args) {
                        s_args += (s_args.length() == 0 ? "" : " ") + s;
                    }
                }
                accessor.shellExecute(null, file.getAbsolutePath(), s_args, SystemProperties.getApplicationPath(), AEWin32Access.SW_NORMAL);
            } catch (Throwable e) {
                Logger.log(new LogEvent(LogIDs.LOGGER, "AEWin32Access failed", e));
                if (args.length > 0) {
                    String[] s_args = new String[args.length + 1];
                    s_args[0] = file.getAbsolutePath();
                    System.arraycopy(args, 0, s_args, 1, args.length);
                    Runtime.getRuntime().exec(s_args);
                } else {
                    Runtime.getRuntime().exec(new String[] { file.getAbsolutePath() });
                }
            }
        } else {
            // osx, need to unzip .app and launch
            File dir = file.getParentFile();
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
            Throwable unzip_error = null;
            String chmod_command = findCommand("chmod");
            try {
                while (true) {
                    ZipEntry entry = zis.getNextEntry();
                    if (entry == null) {
                        break;
                    }
                    if (entry.isDirectory()) {
                        continue;
                    }
                    String name = entry.getName();
                    FileOutputStream entry_os = null;
                    File entry_file = null;
                    if (!name.endsWith("/")) {
                        entry_file = new File(dir, name.replace('/', File.separatorChar));
                        entry_file.getParentFile().mkdirs();
                        entry_os = new FileOutputStream(entry_file);
                    }
                    try {
                        byte[] buffer = new byte[65536];
                        while (true) {
                            int len = zis.read(buffer);
                            if (len <= 0) {
                                break;
                            }
                            if (entry_os != null) {
                                entry_os.write(buffer, 0, len);
                            }
                        }
                    } finally {
                        if (entry_os != null) {
                            entry_os.close();
                            if (name.endsWith(".jnilib") || name.endsWith("JavaApplicationStub")) {
                                try {
                                    String[] to_run = { chmod_command, "a+x", entry_file.getAbsolutePath() };
                                    runCommand(to_run, true);
                                } catch (Throwable e) {
                                    unzip_error = e;
                                }
                            }
                        }
                    }
                }
            } finally {
                zis.close();
            }
            if (unzip_error != null) {
                throw (unzip_error);
            }
            File[] files = dir.listFiles();
            boolean launched = false;
            for (File f : files) {
                if (f.getName().endsWith(".app")) {
                    String[] to_run;
                    if (args.length == 0 || !Constants.isOSX) {
                        to_run = new String[] { "/bin/sh", "-c", "open \"" + f.getAbsolutePath() + "\"" };
                    } else {
                        to_run = new String[3 + args.length];
                        to_run[0] = findCommand("open");
                        to_run[1] = f.getAbsolutePath();
                        to_run[2] = "--args";
                        System.arraycopy(args, 0, to_run, 3, args.length);
                    }
                    runCommand(to_run, false);
                    launched = true;
                }
            }
            if (!launched) {
                throw (new Exception("No .app files found in '" + dir + "'"));
            }
        }
    } catch (Throwable e) {
        Logger.log(new LogEvent(LogIDs.LOGGER, "Failed to launch update '" + file + "'", e));
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) ZipEntry(java.util.zip.ZipEntry) ZipInputStream(java.util.zip.ZipInputStream) AEWin32Access(com.biglybt.platform.win32.access.AEWin32Access)

Example 45 with LogEvent

use of com.biglybt.core.logging.LogEvent in project BiglyBT by BiglySoftware.

the class CoreUpdateChecker method displayUserMessage.

/**
 * Log and display a user message if contained within reply.
 * @param reply from server
 */
private void displayUserMessage(Map reply) {
    try {
        Iterator it = reply.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            if (key.startsWith("message_sig") || !key.startsWith("message")) {
                continue;
            }
            byte[] message_bytes = (byte[]) reply.get(key);
            if (message_bytes != null && message_bytes.length > 0) {
                String message;
                try {
                    message = new String(message_bytes, "UTF-8");
                } catch (Throwable e) {
                    message = new String(message_bytes);
                }
                String sig_key;
                int pos = key.indexOf('_');
                if (pos == -1) {
                    sig_key = "message_sig";
                } else {
                    sig_key = "message_sig" + key.substring(pos);
                }
                String last_message_key = "CoreUpdateChecker.last" + key;
                String last = COConfigurationManager.getStringParameter(last_message_key, "");
                if (!message.equals(last)) {
                    boolean repeatable = false;
                    byte[] signature = (byte[]) reply.get(sig_key);
                    if (signature == null) {
                        Logger.log(new LogEvent(LogIDs.LOGGER, "Signature missing from message"));
                        return;
                    }
                    try {
                        AEVerifier.verifyData(message, signature);
                    } catch (Throwable e) {
                        Logger.log(new LogEvent(LogIDs.LOGGER, "Message signature check failed", e));
                        return;
                    }
                    boolean completed = false;
                    if (message.startsWith("x:") || message.startsWith("y:")) {
                        // emergency patch application
                        repeatable = message.startsWith("y:");
                        try {
                            URL jar_url = new URL(message.substring(2));
                            if (!repeatable) {
                                Logger.log(new LogEvent(LogIDs.LOGGER, "Patch application requsted: url=" + jar_url));
                            }
                            File temp_dir = AETemporaryFileHandler.createTempDir();
                            File jar_file = new File(temp_dir, "patch.jar");
                            InputStream is = rdf.create(jar_url).download();
                            try {
                                FileUtil.copyFile(is, jar_file);
                                is = null;
                                AEVerifier.verifyData(jar_file);
                                ClassLoader cl = CoreUpdateChecker.class.getClassLoader();
                                if (cl instanceof URLClassLoader) {
                                    URL[] old = ((URLClassLoader) cl).getURLs();
                                    URL[] new_urls = new URL[old.length + 1];
                                    System.arraycopy(old, 0, new_urls, 1, old.length);
                                    new_urls[0] = jar_file.toURL();
                                    cl = new URLClassLoader(new_urls, cl);
                                } else {
                                    cl = new URLClassLoader(new URL[] { jar_file.toURL() }, cl);
                                }
                                Class cla = cl.loadClass("com.biglybt.update.version.Patch");
                                cla.newInstance();
                                completed = true;
                            } finally {
                                if (is != null) {
                                    is.close();
                                }
                                jar_file.delete();
                                temp_dir.delete();
                            }
                        } catch (Throwable e) {
                            if (!repeatable) {
                                Logger.log(new LogEvent(LogIDs.LOGGER, "Patch application failed", e));
                            }
                        }
                    } else if (message.startsWith("u:") && message.length() > 4) {
                        try {
                            String type = message.substring(2, 3);
                            String url = message.substring(4);
                            UIFunctions uif = UIFunctionsManager.getUIFunctions();
                            if (uif != null) {
                                uif.viewURL(url, null, 0.9, 0.9, true, type.equals("1"));
                            }
                        } catch (Throwable t) {
                            Logger.log(new LogEvent(LogIDs.LOGGER, "URL message failed", t));
                        }
                        // mark as complete even if errored
                        completed = true;
                    } else {
                        int alert_type = LogAlert.AT_WARNING;
                        String alert_text = message;
                        boolean force = false;
                        if (alert_text.startsWith("f:")) {
                            force = true;
                            alert_text = alert_text.substring(2);
                        }
                        if (alert_text.startsWith("i:")) {
                            alert_type = LogAlert.AT_INFORMATION;
                            alert_text = alert_text.substring(2);
                        }
                        plugin_interface.getPluginProperties().setProperty(MESSAGE_PROPERTY, alert_text);
                        if (force) {
                            UIFunctions uif = UIFunctionsManager.getUIFunctions();
                            if (uif != null) {
                                try {
                                    uif.forceNotify(UIFunctions.STATUSICON_NONE, null, alert_text, null, null, 0);
                                    completed = true;
                                } catch (Throwable e) {
                                }
                            }
                        }
                        if (!completed) {
                            Logger.log(new LogAlert(LogAlert.UNREPEATABLE, alert_type, alert_text, 0));
                        }
                        completed = true;
                    }
                    if (completed) {
                        if (!repeatable) {
                            COConfigurationManager.setParameter(last_message_key, message);
                            COConfigurationManager.save();
                        }
                    }
                }
            }
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) ZipInputStream(java.util.zip.ZipInputStream) URL(java.net.URL) LogAlert(com.biglybt.core.logging.LogAlert) UIFunctions(com.biglybt.ui.UIFunctions) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader)

Aggregations

LogEvent (com.biglybt.core.logging.LogEvent)172 LogAlert (com.biglybt.core.logging.LogAlert)20 IOException (java.io.IOException)14 File (java.io.File)11 URL (java.net.URL)11 ArrayList (java.util.ArrayList)9 InetSocketAddress (java.net.InetSocketAddress)8 InputStream (java.io.InputStream)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 ZipInputStream (java.util.zip.ZipInputStream)7 CacheFileManagerException (com.biglybt.core.diskmanager.cache.CacheFileManagerException)6 TOTorrent (com.biglybt.core.torrent.TOTorrent)6 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)6 ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)6 UIFunctions (com.biglybt.ui.UIFunctions)6 SocketChannel (java.nio.channels.SocketChannel)6 Iterator (java.util.Iterator)6 ConnectionEndpoint (com.biglybt.core.networkmanager.ConnectionEndpoint)5 ClientIDException (com.biglybt.pif.clientid.ClientIDException)5 ParameterListener (com.biglybt.core.config.ParameterListener)4