Search in sources :

Example 1 with TrackerTorrent

use of com.biglybt.pif.tracker.TrackerTorrent in project BiglyBT by BiglySoftware.

the class RPTracker method _process.

@Override
public RPReply _process(RPRequest request) {
    String method = request.getMethod();
    Object[] params = request.getParams();
    if (method.equals("host[Torrent,boolean]")) {
        try {
            Torrent torrent = params[0] == null ? null : (Torrent) ((RPTorrent) params[0])._setLocal();
            if (torrent == null) {
                throw (new RPException("Invalid torrent"));
            }
            TrackerTorrent tt = delegate.host(torrent, ((Boolean) params[1]).booleanValue());
            RPTrackerTorrent res = RPTrackerTorrent.create(tt);
            return (new RPReply(res));
        } catch (TrackerException e) {
            return (new RPReply(e));
        }
    } else if (method.equals("getTorrents")) {
        TrackerTorrent[] torrents = delegate.getTorrents();
        RPTrackerTorrent[] res = new RPTrackerTorrent[torrents.length];
        for (int i = 0; i < res.length; i++) {
            res[i] = RPTrackerTorrent.create(torrents[i]);
        }
        return (new RPReply(res));
    }
    throw (new RPException("Unknown method: " + method));
}
Also used : TrackerException(com.biglybt.pif.tracker.TrackerException) RPTorrent(com.biglybt.pifimpl.remote.torrent.RPTorrent) Torrent(com.biglybt.pif.torrent.Torrent) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) RPException(com.biglybt.pifimpl.remote.RPException) RPTorrent(com.biglybt.pifimpl.remote.torrent.RPTorrent) RPReply(com.biglybt.pifimpl.remote.RPReply) RPObject(com.biglybt.pifimpl.remote.RPObject)

Example 2 with TrackerTorrent

use of com.biglybt.pif.tracker.TrackerTorrent in project BiglyBT by BiglySoftware.

the class ShareHosterPlugin method resourceAdded.

@Override
public void resourceAdded(final ShareResource resource) {
    log.log(LoggerChannel.LT_INFORMATION, "Resource added:".concat(resource.getName()));
    try {
        resource.addDeletionListener(new ShareResourceWillBeDeletedListener() {

            @Override
            public void resourceWillBeDeleted(ShareResource resource) throws ShareResourceDeletionVetoException {
                canResourceBeDeleted(resource);
            }
        });
        Download new_download = null;
        int type = resource.getType();
        if (type == ShareResource.ST_FILE) {
            ShareResourceFile file_resource = (ShareResourceFile) resource;
            ShareItem item = file_resource.getItem();
            Torrent torrent = item.getTorrent();
            Download download = download_manager.getDownload(torrent);
            if (download == null) {
                new_download = addDownload(resource, torrent, item.getTorrentFile(), file_resource.getFile());
            }
        } else if (type == ShareResource.ST_DIR) {
            ShareResourceDir dir_resource = (ShareResourceDir) resource;
            ShareItem item = dir_resource.getItem();
            Torrent torrent = item.getTorrent();
            Download download = download_manager.getDownload(torrent);
            if (download == null) {
                new_download = addDownload(resource, torrent, item.getTorrentFile(), dir_resource.getDir());
            }
        }
        if (new_download != null) {
            final Download f_new_download = new_download;
            resource_dl_map.put(resource, new_download);
            resource.addChangeListener(new ShareResourceListener() {

                @Override
                public void shareResourceChanged(ShareResource resource, ShareResourceEvent event) {
                    if (event.getType() == ShareResourceEvent.ET_ATTRIBUTE_CHANGED) {
                        TorrentAttribute attribute = (TorrentAttribute) event.getData();
                        // System.out.println( "sh: res -> ds: " + attribute.getName() + "/" + resource.getAttribute( attribute ));
                        f_new_download.setAttribute(attribute, resource.getAttribute(attribute));
                    }
                }
            });
            TorrentAttribute[] attributes = resource.getAttributes();
            for (int i = 0; i < attributes.length; i++) {
                TorrentAttribute ta = attributes[i];
                new_download.setAttribute(ta, resource.getAttribute(ta));
            }
            new_download.addAttributeListener(new DownloadAttributeListener() {

                @Override
                public void attributeEventOccurred(Download d, TorrentAttribute attr, int event_type) {
                    resource.setAttribute(attr, d.getAttribute(attr));
                }
            }, plugin_interface.getTorrentManager().getAttribute(TorrentAttribute.TA_CATEGORY), DownloadAttributeListener.WRITTEN);
            boolean persistent = resource.isPersistent();
            Torrent dl_torrent = new_download.getTorrent();
            if (dl_torrent != null) {
                TrackerTorrent tt = tracker.host(dl_torrent, persistent);
                if (!persistent) {
                    tt.addRemovalListener(new TrackerTorrentWillBeRemovedListener() {

                        @Override
                        public void torrentWillBeRemoved(TrackerTorrent tt) throws TrackerTorrentRemovalVetoException {
                            if (tt != torrent_being_removed) {
                                throw (new TrackerTorrentRemovalVetoException(MessageText.getString("plugin.sharing.torrent.remove.veto")));
                            }
                        }
                    });
                }
                resource_tt_map.put(resource, tt);
            }
            if (!persistent) {
                new_download.addDownloadWillBeRemovedListener(new DownloadWillBeRemovedListener() {

                    @Override
                    public void downloadWillBeRemoved(Download dl) throws DownloadRemovalVetoException {
                        if (dl != download_being_removed) {
                            throw (new DownloadRemovalVetoException(MessageText.getString("plugin.sharing.download.remove.veto")));
                        }
                    }
                });
            }
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) TrackerTorrentWillBeRemovedListener(com.biglybt.pif.tracker.TrackerTorrentWillBeRemovedListener) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) TorrentAttribute(com.biglybt.pif.torrent.TorrentAttribute) TrackerTorrentRemovalVetoException(com.biglybt.pif.tracker.TrackerTorrentRemovalVetoException)

Example 3 with TrackerTorrent

use of com.biglybt.pif.tracker.TrackerTorrent in project BiglyBT by BiglySoftware.

the class ShareHosterPlugin method resourceDeleted.

@Override
public void resourceDeleted(ShareResource resource) {
    log.log(LoggerChannel.LT_INFORMATION, "Resource deleted:".concat(resource.getName()));
    Download dl = (Download) resource_dl_map.get(resource);
    if (dl != null) {
        try {
            download_being_removed = dl;
            try {
                dl.stop();
            } catch (Throwable e) {
            // ignore this as it might already be stopped
            }
            dl.remove();
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        } finally {
            download_being_removed = null;
        }
        resource_dl_map.remove(resource);
    }
    TrackerTorrent tt = (TrackerTorrent) resource_tt_map.get(resource);
    if (tt != null) {
        try {
            torrent_being_removed = tt;
            tt.remove();
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        } finally {
            torrent_being_removed = null;
        }
        resource_tt_map.remove(resource);
    }
}
Also used : TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent)

Example 4 with TrackerTorrent

use of com.biglybt.pif.tracker.TrackerTorrent in project BiglyBT by BiglySoftware.

the class MySharesView method startStopSelectedShares.

private void startStopSelectedShares(boolean do_stop) {
    List items = getSelectedItems();
    if (items.size() == 0) {
        return;
    }
    PluginInterface pi = PluginInitializer.getDefaultInterface();
    com.biglybt.pif.download.DownloadManager dm = pi.getDownloadManager();
    Tracker tracker = pi.getTracker();
    for (int i = 0; i < items.size(); i++) {
        ShareItem item = (ShareItem) items.get(i);
        try {
            Torrent t = item.getTorrent();
            TrackerTorrent tracker_torrent = tracker.getTorrent(t);
            Download download = dm.getDownload(t);
            if (tracker_torrent == null || download == null) {
                continue;
            }
            int dl_state = download.getState();
            if (dl_state == Download.ST_ERROR) {
            } else if (dl_state != Download.ST_STOPPED) {
                if (do_stop) {
                    try {
                        download.stop();
                    } catch (Throwable e) {
                    }
                    try {
                        tracker_torrent.stop();
                    } catch (Throwable e) {
                    }
                }
            } else {
                if (!do_stop) {
                    try {
                        download.restart();
                    } catch (Throwable e) {
                    }
                    try {
                        tracker_torrent.start();
                    } catch (Throwable e) {
                    }
                }
            }
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        }
    }
}
Also used : Tracker(com.biglybt.pif.tracker.Tracker) Torrent(com.biglybt.pif.torrent.Torrent) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) PluginInterface(com.biglybt.pif.PluginInterface) List(java.util.List) Download(com.biglybt.pif.download.Download)

Example 5 with TrackerTorrent

use of com.biglybt.pif.tracker.TrackerTorrent in project BiglyBT by BiglySoftware.

the class WebPlugin method handleTunnelRequest.

private byte[] handleTunnelRequest(final InetAddress originator, String endpoint_url, final byte[] request_bytes) throws IOException {
    int q_pos = endpoint_url.indexOf('?');
    boolean raw = true;
    if (q_pos != -1) {
        String params = endpoint_url.substring(q_pos + 1);
        String[] args = params.split("&");
        String new_endpoint = endpoint_url.substring(0, q_pos);
        String sep = "?";
        for (String arg : args) {
            if (arg.startsWith("tunnel_format=")) {
                String temp = arg.substring(14);
                if (temp.startsWith("h")) {
                    raw = false;
                }
            } else {
                new_endpoint += sep + arg;
                sep = "&";
            }
        }
        endpoint_url = new_endpoint;
    }
    final String f_endpoint_url = endpoint_url;
    final JSONObject request_headers = new JSONObject();
    final int data_start;
    if (raw) {
        data_start = 0;
    } else {
        int request_header_len = ((request_bytes[0] << 8) & 0x0000ff00) | (request_bytes[1] & 0x000000ff);
        String reply_json_str = new String(request_bytes, 2, request_header_len, "UTF-8");
        request_headers.putAll(JSONUtils.decodeJSON(reply_json_str));
        data_start = request_header_len + 2;
    }
    TrackerWebPageRequest request = new TrackerWebPageRequest() {

        @Override
        public Tracker getTracker() {
            return (null);
        }

        @Override
        public String getClientAddress() {
            return (originator.getHostAddress());
        }

        @Override
        public InetSocketAddress getClientAddress2() {
            return (new InetSocketAddress(originator, 0));
        }

        @Override
        public InetSocketAddress getLocalAddress() {
            return (new InetSocketAddress("127.0.0.1", 0));
        }

        @Override
        public String getUser() {
            return (null);
        }

        @Override
        public String getURL() {
            String url = (String) request_headers.get("HTTP-URL");
            if (url != null) {
                return (url);
            }
            return (f_endpoint_url);
        }

        @Override
        public String getHeader() {
            return ("");
        }

        @Override
        public Map getHeaders() {
            return (request_headers);
        }

        @Override
        public InputStream getInputStream() {
            return (new ByteArrayInputStream(request_bytes, data_start, request_bytes.length - data_start));
        }

        @Override
        public URL getAbsoluteURL() {
            try {
                return (new URL("http://127.0.0.1" + getURL()));
            } catch (Throwable e) {
                return (null);
            }
        }

        @Override
        public TrackerWebContext getContext() {
            return (null);
        }
    };
    final ByteArrayOutputStream[] baos = { new ByteArrayOutputStream() };
    final Map reply_headers = new HashMap();
    TrackerWebPageResponse response = new TrackerWebPageResponse() {

        @Override
        public OutputStream getOutputStream() {
            return (baos[0]);
        }

        @Override
        public void setOutputStream(ByteArrayOutputStream os) {
            baos[0] = os;
        }

        @Override
        public void setReplyStatus(int status) {
            reply_headers.put("HTTP-Status", String.valueOf(status));
        }

        @Override
        public void setContentType(String type) {
            reply_headers.put("Content-Type", type);
        }

        public String getContentType() {
            return ((String) reply_headers.get("Content-Type"));
        }

        @Override
        public void setLastModified(long time) {
        }

        @Override
        public void setExpires(long time) {
        }

        @Override
        public void setHeader(String name, String value) {
            reply_headers.put(name, value);
        }

        @Override
        public void setGZIP(boolean gzip) {
        }

        @Override
        public boolean useFile(String root_dir, String relative_url) throws IOException {
            Debug.out("Not supported");
            return (false);
        }

        @Override
        public void useStream(String file_type, InputStream stream) throws IOException {
            Debug.out("Not supported");
        }

        @Override
        public void writeTorrent(TrackerTorrent torrent) throws IOException {
            Debug.out("Not supported");
        }

        @Override
        public void setAsynchronous(boolean async) throws IOException {
            Debug.out("Not supported");
        }

        @Override
        public boolean getAsynchronous() {
            return (false);
        }

        @Override
        public OutputStream getRawOutputStream() throws IOException {
            Debug.out("Not supported");
            throw (new IOException("Not supported"));
        }

        @Override
        public boolean isActive() {
            return (true);
        }
    };
    try {
        byte[] bytes;
        if (generate2(request, response, true)) {
            bytes = baos[0].toByteArray();
        } else {
            Debug.out("Tunnelled request not handled: " + request.getURL());
            response.setReplyStatus(404);
            bytes = new byte[0];
        }
        if (raw) {
            return (bytes);
        } else {
            String accept_encoding = (String) request_headers.get("Accept-Encoding");
            if (accept_encoding != null && accept_encoding.contains("gzip")) {
                reply_headers.put("Content-Encoding", "gzip");
                ByteArrayOutputStream temp = new ByteArrayOutputStream(bytes.length + 512);
                GZIPOutputStream gos = new GZIPOutputStream(temp);
                gos.write(bytes);
                gos.close();
                bytes = temp.toByteArray();
            }
            ByteArrayOutputStream baos2 = new ByteArrayOutputStream(bytes.length + 512);
            String header_json = JSONUtils.encodeToJSON(reply_headers);
            byte[] header_bytes = header_json.getBytes("UTF-8");
            int header_len = header_bytes.length;
            byte[] header_len_bytes = new byte[] { (byte) (header_len >> 8), (byte) header_len };
            baos2.write(header_len_bytes);
            baos2.write(header_bytes);
            baos2.write(bytes);
            return (baos2.toByteArray());
        }
    } catch (Throwable e) {
        Debug.out(e);
        return (new byte[0]);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL) TrackerTorrent(com.biglybt.pif.tracker.TrackerTorrent) JSONObject(org.json.simple.JSONObject) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Aggregations

TrackerTorrent (com.biglybt.pif.tracker.TrackerTorrent)7 Torrent (com.biglybt.pif.torrent.Torrent)3 PluginInterface (com.biglybt.pif.PluginInterface)2 Tracker (com.biglybt.pif.tracker.Tracker)2 TrackerTorrentRemovalVetoException (com.biglybt.pif.tracker.TrackerTorrentRemovalVetoException)2 GlobalManagerDownloadRemovalVetoException (com.biglybt.core.global.GlobalManagerDownloadRemovalVetoException)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TRHostException (com.biglybt.core.tracker.host.TRHostException)1 PluginException (com.biglybt.pif.PluginException)1 Download (com.biglybt.pif.download.Download)1 PlatformManagerException (com.biglybt.pif.platform.PlatformManagerException)1 TorrentAttribute (com.biglybt.pif.torrent.TorrentAttribute)1 TrackerException (com.biglybt.pif.tracker.TrackerException)1 TrackerTorrentWillBeRemovedListener (com.biglybt.pif.tracker.TrackerTorrentWillBeRemovedListener)1 RPException (com.biglybt.pifimpl.remote.RPException)1 RPObject (com.biglybt.pifimpl.remote.RPObject)1 RPReply (com.biglybt.pifimpl.remote.RPReply)1 RPTorrent (com.biglybt.pifimpl.remote.torrent.RPTorrent)1 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1