root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 1 | package tracker; |
| 2 | |
| 3 | import java.io.InputStream; |
| 4 | import java.io.OutputStream; |
| 5 | import java.net.HttpURLConnection; |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 6 | import java.net.InetSocketAddress; |
| 7 | import java.net.SocketAddress; |
root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 8 | import java.net.URL; |
TRM-coding | d5de51e | 2025-06-08 03:27:01 +0800 | [diff] [blame] | 9 | import tracker.Tracker; |
root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 10 | import org.simpleframework.http.Request; |
| 11 | import org.simpleframework.http.Response; |
| 12 | import org.simpleframework.http.core.Container; |
| 13 | |
| 14 | /** |
| 15 | * 拦截 announce 请求,打印参数后转发给真实 Tracker。 |
| 16 | */ |
| 17 | public class DataCaptureProxy implements Container { |
| 18 | |
| 19 | private final String trackerHost; |
| 20 | private final int trackerPort; |
TRM-coding | cdfe548 | 2025-06-06 17:31:01 +0800 | [diff] [blame] | 21 | private final Tracker tracker; |
root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 22 | |
| 23 | public DataCaptureProxy(String trackerHost, int trackerPort) { |
| 24 | this.trackerHost = trackerHost; |
| 25 | this.trackerPort = trackerPort; |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 26 | this.tracker = new Tracker(); // 初始化 Tracker 实例 |
root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | @Override |
| 30 | public void handle(Request req, Response resp) { |
| 31 | try { |
| 32 | // 提取并打印关键参数 |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 33 | String infoHash = req.getParameter("info_hash"); |
| 34 | String uploaded = req.getParameter("uploaded"); |
| 35 | String downloaded = req.getParameter("downloaded"); |
| 36 | String passkey = req.getParameter("passkey"); |
TRM-coding | d5de51e | 2025-06-08 03:27:01 +0800 | [diff] [blame] | 37 | String port = req.getParameter("port"); // qBittorrent 服务端端口 |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 38 | |
TRM-coding | d5de51e | 2025-06-08 03:27:01 +0800 | [diff] [blame] | 39 | // 获取客户端IP地址和端口 |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 40 | String clientIp; |
TRM-coding | d5de51e | 2025-06-08 03:27:01 +0800 | [diff] [blame] | 41 | int clientPort = -1; |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 42 | // 直接从 TCP 连接(socket 源地址)中读取 |
| 43 | SocketAddress socketAddress = req.getClientAddress(); |
| 44 | if (socketAddress instanceof InetSocketAddress) { |
TRM-coding | d5de51e | 2025-06-08 03:27:01 +0800 | [diff] [blame] | 45 | InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress; |
| 46 | clientIp = inetSocketAddress.getAddress().getHostAddress(); |
| 47 | clientPort = inetSocketAddress.getPort(); |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 48 | } else { |
| 49 | // 兜底写法,将整个 SocketAddress 转为字符串 |
| 50 | clientIp = socketAddress.toString(); |
| 51 | } |
| 52 | |
root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 53 | System.out.println( |
| 54 | "Captured announce → info_hash=" + infoHash + |
| 55 | ", uploaded=" + uploaded + |
| 56 | ", downloaded=" + downloaded + |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 57 | ", passkey=" + passkey + |
TRM-coding | d5de51e | 2025-06-08 03:27:01 +0800 | [diff] [blame] | 58 | ", client_ip=" + clientIp + |
| 59 | ", client_port=" + clientPort + |
| 60 | ", qbt_service_port=" + port |
root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 61 | ); |
| 62 | |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 63 | // 调用 Tracker 方法更新上传和下载数据 |
TRM-coding | d5de51e | 2025-06-08 03:27:01 +0800 | [diff] [blame] | 64 | if (passkey != null && !passkey.isEmpty() && infoHash != null && !infoHash.isEmpty()) { |
TRM-coding | cdfe548 | 2025-06-06 17:31:01 +0800 | [diff] [blame] | 65 | try { |
| 66 | if (uploaded != null && !uploaded.isEmpty()) { |
| 67 | int uploadValue = Integer.parseInt(uploaded); |
| 68 | if (uploadValue > 0) { |
TRM-coding | 508b31f | 2025-06-09 02:07:14 +0800 | [diff] [blame] | 69 | try { |
| 70 | tracker.AddUpLoad(passkey, uploadValue, infoHash); |
| 71 | } catch (javax.persistence.NoResultException e) { |
| 72 | System.out.println("Skipping upload update: info_hash not found in database - " + infoHash); |
| 73 | } |
TRM-coding | cdfe548 | 2025-06-06 17:31:01 +0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | if (downloaded != null && !downloaded.isEmpty()) { |
| 78 | int downloadValue = Integer.parseInt(downloaded); |
| 79 | if (downloadValue > 0) { |
TRM-coding | 508b31f | 2025-06-09 02:07:14 +0800 | [diff] [blame] | 80 | try { |
| 81 | tracker.AddDownload(passkey, downloadValue, infoHash); |
| 82 | } catch (javax.persistence.NoResultException e) { |
| 83 | System.out.println("Skipping download update: info_hash not found in database - " + infoHash); |
| 84 | } |
TRM-coding | cdfe548 | 2025-06-06 17:31:01 +0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } catch (NumberFormatException e) { |
| 88 | System.err.println("Error parsing upload/download values: " + e.getMessage()); |
| 89 | } |
| 90 | } |
| 91 | |
root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 92 | // 构造转发 URL |
TRM-coding | 87c2497 | 2025-06-07 14:05:29 +0800 | [diff] [blame] | 93 | String path = req.getPath().getPath(); |
root | 59a69f8 | 2025-06-05 08:35:22 +0000 | [diff] [blame] | 94 | String query = req.getQuery().toString(); |
| 95 | String targetUrl = "http://" + trackerHost + ":" + trackerPort |
| 96 | + path + "?" + query; |
| 97 | |
| 98 | HttpURLConnection connection = |
| 99 | (HttpURLConnection) new URL(targetUrl).openConnection(); |
| 100 | connection.setRequestMethod("GET"); |
| 101 | |
| 102 | // 转发响应码和类型 |
| 103 | resp.setCode(connection.getResponseCode()); |
| 104 | String ct = connection.getContentType(); |
| 105 | if (ct != null) resp.setValue("Content-Type", ct); |
| 106 | |
| 107 | // 转发响应体 |
| 108 | try (InputStream in = connection.getInputStream(); |
| 109 | OutputStream out = resp.getOutputStream()) { |
| 110 | byte[] buf = new byte[8192]; |
| 111 | int len; |
| 112 | while ((len = in.read(buf)) != -1) { |
| 113 | out.write(buf, 0, len); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } catch (Exception e) { |
| 118 | try { |
| 119 | resp.setCode(500); |
| 120 | resp.close(); |
| 121 | } catch (Exception ignore) {} |
| 122 | e.printStackTrace(); |
| 123 | } |
| 124 | } |
| 125 | } |