blob: b670f3f46552ae05eda605fc2028e68cf3080f45 [file] [log] [blame]
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +08001package com.pt.controller;
2
Edwardsamaxlcba512d2025-06-09 21:17:29 +08003import com.pt.service.TorrentMetaService;
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +08004import com.pt.service.TrackerService;
5import jakarta.servlet.http.HttpServletRequest;
6import jakarta.servlet.http.HttpServletResponse;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.web.bind.annotation.*;
9
Edwardsamaxlcba512d2025-06-09 21:17:29 +080010import java.io.FileInputStream;
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080011import java.io.IOException;
Edwardsamaxlcba512d2025-06-09 21:17:29 +080012import java.net.URLDecoder;
13import java.nio.charset.StandardCharsets;
14import java.security.MessageDigest;
22301102ca0fb2f2025-06-09 18:40:42 +080015import java.util.HashMap;
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080016import java.util.Map;
17
18@RestController
2230110272bc8d02025-06-07 10:53:27 +080019@RequestMapping("/api/tracker")
22301102ca0fb2f2025-06-09 18:40:42 +080020@CrossOrigin(origins = "*")
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080021public class TrackerController {
22
23 @Autowired
24 private TrackerService trackerService;
25
Edwardsamaxlcba512d2025-06-09 21:17:29 +080026 @Autowired
27 private TorrentMetaService torrentMetaService;
28
22301102ca0fb2f2025-06-09 18:40:42 +080029 // tracker相应bt客户端的announce请求
Edwardsamaxlcba512d2025-06-09 21:17:29 +080030 @GetMapping("/announce")
31 public void announce(
32 @RequestParam(value = "passkey", required = false) String passkey,
33 @RequestParam(value = "info_hash") String encodedInfoHash,
34 @RequestParam(value = "peer_id") String encodedPeerId,
35 @RequestParam(value = "port") int port,
36 @RequestParam(value = "uploaded") long uploaded,
37 @RequestParam(value = "downloaded") long downloaded,
38 @RequestParam(value = "left") long left,
39 @RequestParam(value = "event", defaultValue = "") String event,
40 @RequestParam(value = "compact", defaultValue = "1") int compact,
41 @RequestParam(value = "numwant", defaultValue = "50") int numwant,
22301102ca0fb2f2025-06-09 18:40:42 +080042 HttpServletRequest request,
43 HttpServletResponse response
44 ) throws IOException {
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080045 try {
Edwardsamaxlaf825a22025-06-09 21:17:29 +080046
Edwardsamaxlcba512d2025-06-09 21:17:29 +080047 System.out.println("TrackerController------------------------announce");
48 // 解码 info_hash 和 peer_id(从URL编码的字符串转为字节数组)
49 System.out.println("TrackerController------------------------announce: info_hash=" + encodedInfoHash + ", peer_id=" + encodedPeerId);
50 String decodedInfoHash = URLDecoder.decode(encodedInfoHash, StandardCharsets.ISO_8859_1);
51 String decodedPeerId = URLDecoder.decode(encodedPeerId, StandardCharsets.ISO_8859_1);
Edwardsamaxlaf825a22025-06-09 21:17:29 +080052
Edwardsamaxlcba512d2025-06-09 21:17:29 +080053 // 转换为字节数组
54 byte[] infoHashBytes = decodedInfoHash.getBytes(StandardCharsets.ISO_8859_1);
55 byte[] peerIdBytes = decodedPeerId.getBytes(StandardCharsets.ISO_8859_1);
56
57 // 验证 info_hash 长度
58// if (infoHashBytes.length != 20) {
59// throw new IllegalArgumentException("Invalid info_hash length");
60// }
61
62 // 获取客户端IP
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080063 String ip = request.getRemoteAddr();
Edwardsamaxlcba512d2025-06-09 21:17:29 +080064 System.out.println("Client IP: " + ip);
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080065
Edwardsamaxlcba512d2025-06-09 21:17:29 +080066 // 将参数封装为 Map 传给服务层
67 Map<String, Object> params = new HashMap<>();
68 params.put("info_hash", decodedInfoHash);
69 params.put("peer_id", decodedPeerId);
70 params.put("port", port);
71 params.put("uploaded", uploaded);
72 params.put("downloaded", downloaded);
73 params.put("left", left);
74 params.put("event", event);
75 params.put("compact", compact);
76 params.put("numwant", numwant);
77 params.put("ip", ip);
78 params.put("passkey", passkey);
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080079
Edwardsamaxlcba512d2025-06-09 21:17:29 +080080 byte[] bencodedResponse = trackerService.handleAnnounce(params, ip, event);
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080081 response.setContentType("application/x-bittorrent");
82 response.getOutputStream().write(bencodedResponse);
Edwardsamaxlcba512d2025-06-09 21:17:29 +080083
84 } catch (IllegalArgumentException e) {
85 System.out.println("TrackerService-----------------------Error processing announce request111: " + e.getMessage());
86 e.printStackTrace();
87 String errorResponse = "d14:failure reason" + e.getMessage().length() + ":" + e.getMessage() + "e";
88 response.setContentType("application/x-bittorrent");
89 response.getOutputStream().write(errorResponse.getBytes(StandardCharsets.ISO_8859_1));
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080090 } catch (Exception e) {
Edwardsamaxlcba512d2025-06-09 21:17:29 +080091 e.printStackTrace();
92 System.out.println("TrackerService-----------------------Error processing announce request222: " + e.getMessage());
93 String errorResponse = "d14:failure reason20:Internal server errore";
94 response.setContentType("application/x-bittorrent");
95 response.getOutputStream().write(errorResponse.getBytes(StandardCharsets.ISO_8859_1));
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +080096 }
97 }
98
Edwardsamaxlcba512d2025-06-09 21:17:29 +080099
Edwardsamaxlf1bf7ad2025-06-03 23:52:16 +0800100}
101