| 1 | <%@page import="java.util.ArrayList" %><%@page import="java.util.List" %><%@page import="java.util.Map" %><%@page import="java.util.HashMap" %><%@page import="net.i2p.i2pcontrol.*" %><%@page import="org.klomp.snark.bencode.BEncoder" %><% |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | * Above one-liner is so there is no whitespace -> IllegalStateException |
|---|
| 5 | * |
|---|
| 6 | * Copyright 2010 zzz (zzz@mail.i2p) |
|---|
| 7 | * |
|---|
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 9 | * you may not use this file except in compliance with the License. |
|---|
| 10 | * You may obtain a copy of the License at |
|---|
| 11 | * |
|---|
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 13 | * |
|---|
| 14 | * Unless required by applicable law or agreed to in writing, software |
|---|
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 17 | * See the License for the specific language governing permissions and |
|---|
| 18 | * limitations under the License. |
|---|
| 19 | * |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | /* |
|---|
| 23 | * USE CAUTION WHEN EDITING |
|---|
| 24 | * Trailing whitespace OR NEWLINE on the last line will cause |
|---|
| 25 | * IllegalStateExceptions !!! |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | // so the chars will turn into bytes correctly |
|---|
| 29 | request.setCharacterEncoding("ISO-8859-1"); |
|---|
| 30 | java.io.OutputStream cout = response.getOutputStream(); |
|---|
| 31 | response.setCharacterEncoding("ISO-8859-1"); |
|---|
| 32 | response.setContentType("text/plain"); |
|---|
| 33 | response.setHeader("Pragma", "no-cache"); |
|---|
| 34 | String info_hash = request.getParameter("info_hash"); |
|---|
| 35 | String xff = request.getHeader("X-Forwarded-For"); |
|---|
| 36 | String xfs = request.getHeader("X-Forwarded-Server"); |
|---|
| 37 | |
|---|
| 38 | boolean fail = false; |
|---|
| 39 | String msg = "bad"; |
|---|
| 40 | |
|---|
| 41 | if (xff != null || xfs != null) { |
|---|
| 42 | fail = true; |
|---|
| 43 | msg = "Non-I2P access denied"; |
|---|
| 44 | response.setStatus(403, msg); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | boolean all = info_hash == null; |
|---|
| 48 | |
|---|
| 49 | InfoHash ih = null; |
|---|
| 50 | if ((!all) && !fail) { |
|---|
| 51 | try { |
|---|
| 52 | ih = new InfoHash(info_hash); |
|---|
| 53 | } catch (Exception e) { |
|---|
| 54 | fail = true; |
|---|
| 55 | msg = "bad infohash " + e; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | Torrents torrents = I2PControlController.getTorrents(); |
|---|
| 60 | |
|---|
| 61 | // build 3-level dictionary |
|---|
| 62 | Map<String, Object> m = new HashMap(); |
|---|
| 63 | if (fail) { |
|---|
| 64 | m.put("failure reason", msg); |
|---|
| 65 | } else { |
|---|
| 66 | List<InfoHash> ihList = new ArrayList(); |
|---|
| 67 | if (all) |
|---|
| 68 | ihList.addAll(torrents.keySet()); |
|---|
| 69 | else |
|---|
| 70 | ihList.add(ih); |
|---|
| 71 | Map<String, Map> files = new HashMap(); |
|---|
| 72 | for (InfoHash ihash : ihList) { |
|---|
| 73 | Peers peers = torrents.get(ihash); |
|---|
| 74 | if (peers == null) |
|---|
| 75 | continue; |
|---|
| 76 | Map<String, Object> dict = new HashMap(); |
|---|
| 77 | int size = peers.size(); |
|---|
| 78 | int seeds = peers.countSeeds(); |
|---|
| 79 | dict.put("complete", Integer.valueOf(seeds)); |
|---|
| 80 | dict.put("incomplete", Integer.valueOf(size - seeds)); |
|---|
| 81 | dict.put("downloaded", Integer.valueOf(0)); |
|---|
| 82 | files.put(new String(ihash.getData(), "ISO-8859-1"), dict); |
|---|
| 83 | } |
|---|
| 84 | m.put("files", files); |
|---|
| 85 | } |
|---|
| 86 | BEncoder.bencode(m, cout); |
|---|
| 87 | |
|---|
| 88 | /* |
|---|
| 89 | * Remove the newline on the last line or |
|---|
| 90 | * it will generate an IllegalStateException |
|---|
| 91 | * |
|---|
| 92 | */ |
|---|
| 93 | %> |
|---|