-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
105 lines (86 loc) · 2.6 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2020-2022 Russell Clarey. All rights reserved. MIT license.
import { Metainfo } from "./metainfo.ts";
import {
endReceiveHandshake,
sendHandshake,
startReceiveHandshake,
} from "./protocol.ts";
import { fileStorage, Storage, StorageMethod } from "./storage.ts";
import { getIpAddrsAndMapPort } from "./upnp.ts";
import { Torrent } from "./torrent.ts";
export interface ClientConfig {
storage?: StorageMethod;
port?: number;
peerId?: string;
}
export const defaultClientConfig: Required<ClientConfig> = {
storage: fileStorage,
port: 0,
peerId: "-DT0000-",
};
function peerIdFromPrefix(prefix: string) {
const peerId = new Uint8Array(20);
const encoded = new TextEncoder().encode(prefix);
peerId.set(encoded);
crypto.getRandomValues(peerId.subarray(encoded.length));
return peerId;
}
export class Client {
listener!: Deno.Listener;
torrents: Map<string, Torrent> = new Map();
peerId: Uint8Array;
internalIp!: string;
externalIp!: string;
config: Required<ClientConfig>;
constructor(config: ClientConfig = {}) {
this.config = Object.assign(defaultClientConfig, config);
this.peerId = peerIdFromPrefix(this.config.peerId);
this.run();
}
add(metainfo: Metainfo, dir: string): void {
const hashStr = metainfo.infoHash.toString();
if (!this.torrents.has(hashStr)) {
this.torrents.set(
hashStr,
new Torrent({
ip: this.externalIp,
metainfo,
peerId: this.peerId,
port: this.config.port,
storage: new Storage(this.config.storage, metainfo.info, dir),
}),
);
}
}
async run() {
this.listener = Deno.listen({ port: this.config.port });
const addr = this.listener.addr as Deno.NetAddr;
// if config.port was 0, then we get a random free port so we should set
// that to config.port so we can reference it later
if (addr.port !== this.config.port) {
this.config.port = addr.port;
}
[this.internalIp, this.externalIp] = await getIpAddrsAndMapPort(
this.config.port,
);
this.acceptConnections();
}
private async acceptConnections() {
for await (const conn of this.listener) {
try {
const infoHash = await startReceiveHandshake(conn);
const torrent = this.torrents.get(infoHash.toString());
if (!torrent) {
conn.close();
continue;
}
await sendHandshake(conn, infoHash, this.peerId);
const peerId = await endReceiveHandshake(conn);
torrent.addPeer(peerId, conn);
} catch (e) {
console.error(e);
conn.close();
}
}
}
}