Search in sources :

Example 1 with IPCInterfaceImpl

use of com.biglybt.pifimpl.local.ipc.IPCInterfaceImpl in project BiglyBT by BiglySoftware.

the class DeviceManagerUPnPImpl method addListener.

protected void addListener(PluginInterface pi) {
    try {
        IPCInterface my_ipc = new IPCInterfaceImpl(new Object() {

            public Map<String, Object> browseReceived(TrackerWebPageRequest request, Map<String, Object> browser_args) {
                Map headers = request.getHeaders();
                String user_agent = (String) headers.get("user-agent");
                String client_info = (String) headers.get("x-av-client-info");
                InetSocketAddress client_address = request.getClientAddress2();
                DeviceMediaRenderer explicit_renderer = null;
                boolean handled = false;
                if (user_agent != null) {
                    String lc_agent = user_agent.toLowerCase();
                    if (lc_agent.contains("playstation 3")) {
                        handlePS3(client_address);
                        handled = true;
                    } else if (lc_agent.contains("xbox")) {
                        handleXBox(client_address);
                        handled = true;
                    } else if (lc_agent.contains("nintendo wii")) {
                        handleWii(client_address);
                        handled = true;
                    }
                }
                if (client_info != null) {
                    String lc_info = client_info.toLowerCase();
                    if (lc_info.contains("playstation 3")) {
                        handlePS3(client_address);
                        handled = true;
                    } else if (lc_info.contains("azureus") || lc_info.contains("vuze") || lc_info.contains("biglybt")) {
                        explicit_renderer = handleVuzeMSBrowser(client_address, client_info);
                        handled = true;
                    }
                }
                if (!handled) {
                    handled = manager.browseReceived(request, browser_args);
                }
                if (!handled) {
                    String source = (String) browser_args.get("source");
                    if (source != null && source.equalsIgnoreCase("http")) {
                        handleBrowser(client_address);
                        handled = true;
                    }
                }
                /**
                 *							System.out.println(
                 *									"Received browse: " + request.getClientAddress() +
                 *									", agent=" + user_agent +
                 *									", info=" + client_info +
                 *									", handled=" + handled + ", " + request.getURL());
                 *									System.out.println("\n\n");
                 *								/*
                 */
                DeviceImpl[] devices = manager.getDevices();
                final List<DeviceMediaRendererImpl> browse_devices = new ArrayList<>();
                boolean restrict_access = false;
                for (DeviceImpl device : devices) {
                    if (device instanceof DeviceMediaRendererImpl) {
                        DeviceMediaRendererImpl renderer = (DeviceMediaRendererImpl) device;
                        if (explicit_renderer != null) {
                            if (renderer != explicit_renderer) {
                                continue;
                            }
                        }
                        InetAddress device_address = renderer.getAddress();
                        try {
                            if (device_address != null) {
                                if (device_address.equals(client_address.getAddress())) {
                                    if (renderer.canFilterFilesView()) {
                                        boolean skip = false;
                                        if (renderer.canRestrictAccess()) {
                                            String restriction = renderer.getAccessRestriction().trim();
                                            if (restriction.length() > 0) {
                                                String x = client_address.getAddress().getHostAddress();
                                                skip = true;
                                                String[] ips = restriction.split(",");
                                                for (String ip : ips) {
                                                    if (ip.startsWith("-")) {
                                                        ip = ip.substring(1);
                                                        if (ip.equals(x)) {
                                                            break;
                                                        }
                                                    } else {
                                                        if (ip.startsWith("+")) {
                                                            ip = ip.substring(1);
                                                        }
                                                        if (ip.equals(x)) {
                                                            skip = false;
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        if (skip) {
                                            restrict_access = true;
                                            String host = client_address.getAddress().getHostAddress();
                                            synchronized (access_logs) {
                                                if (!access_logs.contains(host)) {
                                                    access_logs.add(host);
                                                    manager.log("Ignoring browse from " + host + " due to access restriction for '" + renderer.getName() + "'");
                                                }
                                            }
                                        }
                                        browse_devices.add(renderer);
                                        renderer.browseReceived();
                                    }
                                }
                            }
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                    }
                }
                Map<String, Object> result = new HashMap<>();
                if (browse_devices.size() > 0) {
                    synchronized (unassociated_devices) {
                        unassociated_devices.remove(client_address.getAddress());
                    }
                    final boolean f_restrict_access = restrict_access;
                    result.put("filter", new ContentFilter() {

                        @Override
                        public boolean isVisible(ContentDownload download, Map<String, Object> browse_args) {
                            if (f_restrict_access) {
                                return (false);
                            }
                            boolean visible = false;
                            for (DeviceUPnPImpl device : browse_devices) {
                                if (device.isVisible(download)) {
                                    visible = true;
                                }
                            }
                            return (visible);
                        }

                        @Override
                        public boolean isVisible(ContentFile file, Map<String, Object> browse_args) {
                            if (f_restrict_access) {
                                return (false);
                            }
                            boolean visible = false;
                            for (DeviceUPnPImpl device : browse_devices) {
                                if (device.isVisible(file)) {
                                    visible = true;
                                }
                            }
                            return (visible);
                        }
                    });
                } else {
                    if (request.getHeader().substring(0, 4).equalsIgnoreCase("POST")) {
                        synchronized (unassociated_devices) {
                            unassociated_devices.put(client_address.getAddress(), user_agent);
                        }
                    }
                }
                return (result);
            }
        });
        if (upnpav_ipc.canInvoke("addBrowseListener", new Object[] { my_ipc })) {
            DeviceImpl[] devices = manager.getDevices();
            for (DeviceImpl device : devices) {
                if (device instanceof DeviceUPnPImpl) {
                    DeviceUPnPImpl u_d = (DeviceUPnPImpl) device;
                    u_d.resetUPNPAV();
                }
            }
            upnpav_ipc.invoke("addBrowseListener", new Object[] { my_ipc });
        } else {
            manager.log("UPnPAV plugin needs upgrading");
        }
    } catch (Throwable e) {
        manager.log("Failed to hook into UPnPAV", e);
    }
}
Also used : ContentDownload(com.biglybt.core.content.ContentDownload) ContentFile(com.biglybt.core.content.ContentFile) TrackerWebPageRequest(com.biglybt.pif.tracker.web.TrackerWebPageRequest) InetSocketAddress(java.net.InetSocketAddress) IPCInterfaceImpl(com.biglybt.pifimpl.local.ipc.IPCInterfaceImpl) ContentFilter(com.biglybt.core.content.ContentFilter) DeviceMediaRenderer(com.biglybt.core.devices.DeviceMediaRenderer) IPCInterface(com.biglybt.pif.ipc.IPCInterface) InetAddress(java.net.InetAddress)

Aggregations

ContentDownload (com.biglybt.core.content.ContentDownload)1 ContentFile (com.biglybt.core.content.ContentFile)1 ContentFilter (com.biglybt.core.content.ContentFilter)1 DeviceMediaRenderer (com.biglybt.core.devices.DeviceMediaRenderer)1 IPCInterface (com.biglybt.pif.ipc.IPCInterface)1 TrackerWebPageRequest (com.biglybt.pif.tracker.web.TrackerWebPageRequest)1 IPCInterfaceImpl (com.biglybt.pifimpl.local.ipc.IPCInterfaceImpl)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1