刘嘉昕 | 88d3f7d | 2025-06-04 11:54:09 +0800 | [diff] [blame] | 1 | package com.pt5.pthouduan.config; |
| 2 | |
| 3 | import com.pt5.pthouduan.entity.StatsRecorder; |
| 4 | import com.pt5.pthouduan.entity.TrackerInitializer; |
| 5 | import com.pt5.pthouduan.entity.TrackeredTorrentWithStats; |
| 6 | import com.pt5.pthouduan.service.impl.updatePeerStatsService; |
| 7 | import com.turn.ttorrent.tracker.TorrentsRepository; |
| 8 | import com.turn.ttorrent.tracker.TrackedTorrent; |
| 9 | import com.turn.ttorrent.tracker.Tracker; |
| 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | import org.springframework.context.annotation.Bean; |
| 12 | import org.springframework.context.annotation.Configuration; |
| 13 | |
| 14 | import java.io.File; |
| 15 | import java.io.FilenameFilter; |
| 16 | import java.io.IOException; |
| 17 | |
| 18 | import org.springframework.beans.factory.annotation.Value; |
| 19 | |
| 20 | import javax.sql.DataSource; |
| 21 | |
| 22 | @Configuration |
| 23 | public class TrackerConfig { |
| 24 | |
| 25 | @Value("${tracker.port:6969}") |
| 26 | private int trackerPort; |
| 27 | |
| 28 | @Value("${tracker.accept.foreign:false}") |
| 29 | private boolean acceptForeignTorrents; |
| 30 | |
| 31 | @Autowired |
| 32 | private DataSource dataSource; |
| 33 | @Autowired |
| 34 | private TrackerInitializer trackerInitializer; |
| 35 | @Autowired |
| 36 | private updatePeerStatsService statsService; |
| 37 | |
| 38 | @Bean |
| 39 | public Tracker tracker() throws IOException { |
| 40 | // 1. 创建 Tracker 实例 |
| 41 | //Tracker tracker = new Tracker(6969); |
| 42 | //Tracker tracker = new Tracker(6969,"127.0.0.1"); |
| 43 | Tracker tracker = trackerInitializer.createTracker(); |
| 44 | // 4. 启动 Tracker(true 表示作为守护线程运行) |
| 45 | tracker.start(true); |
| 46 | |
| 47 | // 2. 自动加载目录下的所有种子文件 |
| 48 | File torrentsDirectory = new File("D:\\torrenttest\\111111"); |
| 49 | if (torrentsDirectory.exists() && torrentsDirectory.isDirectory()) { |
| 50 | FilenameFilter filter = (dir, name) -> name.endsWith(".torrent"); |
| 51 | |
| 52 | for (File torrentFile : torrentsDirectory.listFiles(filter)) { |
| 53 | try { |
| 54 | TrackedTorrent baseTorrent = TrackedTorrent.load(torrentFile); |
| 55 | //获取HashInfo |
| 56 | byte[] infoHash = baseTorrent.getInfoHash(); |
| 57 | System.out.println("Registered torrent hash: " + infoHash); |
| 58 | System.out.println("Registered torrent hash: " + baseTorrent.getHexInfoHash()); |
| 59 | TrackeredTorrentWithStats trackedTorrentWithStats = new TrackeredTorrentWithStats(infoHash,dataSource,"",statsService); |
| 60 | tracker.announce(trackedTorrentWithStats); |
| 61 | //tracker.announce(TrackedTorrent.load(torrentFile)); |
| 62 | System.out.println("Registered torrent: " + torrentFile.getName()); |
| 63 | } catch (IOException e) { |
| 64 | System.err.println("Failed to load torrent: " + torrentFile.getName()); |
| 65 | e.printStackTrace(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // 3. 设置是否接受未知种子 |
| 71 | tracker.setAcceptForeignTorrents(true); |
| 72 | |
| 73 | |
| 74 | |
| 75 | System.out.println("Tracker started on port " + trackerPort + |
| 76 | ", accepting foreign torrents: " + acceptForeignTorrents); |
| 77 | |
| 78 | // 添加JVM关闭钩子确保Tracker正常停止 |
| 79 | Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 80 | tracker.stop(); |
| 81 | System.out.println("Tracker stopped gracefully"); |
| 82 | })); |
| 83 | |
| 84 | System.out.println("All registered torrents:"); |
| 85 | for (TrackedTorrent t : tracker.getTrackedTorrents()) { |
| 86 | System.out.println(t.getHexInfoHash()); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | return tracker; |
| 91 | } |
| 92 | |
| 93 | } |