Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 1 | package com.pt.controller; |
| 2 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 3 | import com.pt.service.TorrentMetaService; |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 4 | import com.pt.service.TrackerService; |
| 5 | import jakarta.servlet.http.HttpServletRequest; |
| 6 | import jakarta.servlet.http.HttpServletResponse; |
| 7 | import org.springframework.beans.factory.annotation.Autowired; |
| 8 | import org.springframework.web.bind.annotation.*; |
| 9 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 10 | import java.io.FileInputStream; |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 11 | import java.io.IOException; |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 12 | import java.net.URLDecoder; |
| 13 | import java.nio.charset.StandardCharsets; |
| 14 | import java.security.MessageDigest; |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 15 | import java.util.HashMap; |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 16 | import java.util.Map; |
| 17 | |
| 18 | @RestController |
22301102 | 72bc8d0 | 2025-06-07 10:53:27 +0800 | [diff] [blame] | 19 | @RequestMapping("/api/tracker") |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 20 | @CrossOrigin(origins = "*") |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 21 | public class TrackerController { |
| 22 | |
| 23 | @Autowired |
| 24 | private TrackerService trackerService; |
| 25 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 26 | @Autowired |
| 27 | private TorrentMetaService torrentMetaService; |
| 28 | |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 29 | // tracker相应bt客户端的announce请求 |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 30 | @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, |
22301102 | ca0fb2f | 2025-06-09 18:40:42 +0800 | [diff] [blame] | 42 | HttpServletRequest request, |
| 43 | HttpServletResponse response |
| 44 | ) throws IOException { |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 45 | try { |
Edwardsamaxl | af825a2 | 2025-06-09 21:17:29 +0800 | [diff] [blame] | 46 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 47 | 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); |
Edwardsamaxl | af825a2 | 2025-06-09 21:17:29 +0800 | [diff] [blame] | 52 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 53 | // 转换为字节数组 |
| 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 |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 63 | String ip = request.getRemoteAddr(); |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 64 | System.out.println("Client IP: " + ip); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 65 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 66 | // 将参数封装为 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); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 79 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 80 | byte[] bencodedResponse = trackerService.handleAnnounce(params, ip, event); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 81 | response.setContentType("application/x-bittorrent"); |
| 82 | response.getOutputStream().write(bencodedResponse); |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 83 | |
| 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)); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 90 | } catch (Exception e) { |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 91 | 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)); |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Edwardsamaxl | cba512d | 2025-06-09 21:17:29 +0800 | [diff] [blame^] | 99 | |
Edwardsamaxl | f1bf7ad | 2025-06-03 23:52:16 +0800 | [diff] [blame] | 100 | } |
| 101 | |