blob: 93784b5220978a10c5c62745c80a50328b08ff69 [file] [log] [blame]
刘嘉昕88d3f7d2025-06-04 11:54:09 +08001package com.pt5.pthouduan.config;
2
3import com.pt5.pthouduan.entity.StatsRecorder;
4import com.pt5.pthouduan.entity.TrackerInitializer;
5import com.pt5.pthouduan.entity.TrackeredTorrentWithStats;
6import com.pt5.pthouduan.service.impl.updatePeerStatsService;
7import com.turn.ttorrent.tracker.TorrentsRepository;
8import com.turn.ttorrent.tracker.TrackedTorrent;
9import com.turn.ttorrent.tracker.Tracker;
10import org.springframework.beans.factory.annotation.Autowired;
11import org.springframework.context.annotation.Bean;
12import org.springframework.context.annotation.Configuration;
13
14import java.io.File;
15import java.io.FilenameFilter;
16import java.io.IOException;
17
18import org.springframework.beans.factory.annotation.Value;
19
20import javax.sql.DataSource;
21
22@Configuration
23public 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}