librats 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/binding.gyp +1 -0
- package/lib/index.d.ts +2 -1
- package/native-src/3rdparty/android/ifaddrs-android.c +600 -0
- package/native-src/3rdparty/android/ifaddrs-android.h +54 -0
- package/native-src/CMakeLists.txt +360 -0
- package/native-src/LICENSE +21 -0
- package/native-src/src/bencode.cpp +485 -0
- package/native-src/src/bencode.h +145 -0
- package/native-src/src/bittorrent.cpp +3682 -0
- package/native-src/src/bittorrent.h +731 -0
- package/native-src/src/dht.cpp +2460 -0
- package/native-src/src/dht.h +508 -0
- package/native-src/src/encrypted_socket.cpp +817 -0
- package/native-src/src/encrypted_socket.h +239 -0
- package/native-src/src/file_transfer.cpp +1808 -0
- package/native-src/src/file_transfer.h +567 -0
- package/native-src/src/fs.cpp +639 -0
- package/native-src/src/fs.h +108 -0
- package/native-src/src/gossipsub.cpp +1137 -0
- package/native-src/src/gossipsub.h +403 -0
- package/native-src/src/ice.cpp +1386 -0
- package/native-src/src/ice.h +328 -0
- package/native-src/src/json.hpp +25526 -0
- package/native-src/src/krpc.cpp +558 -0
- package/native-src/src/krpc.h +145 -0
- package/native-src/src/librats.cpp +2735 -0
- package/native-src/src/librats.h +1732 -0
- package/native-src/src/librats_bittorrent.cpp +167 -0
- package/native-src/src/librats_c.cpp +1333 -0
- package/native-src/src/librats_c.h +239 -0
- package/native-src/src/librats_encryption.cpp +123 -0
- package/native-src/src/librats_file_transfer.cpp +226 -0
- package/native-src/src/librats_gossipsub.cpp +293 -0
- package/native-src/src/librats_ice.cpp +515 -0
- package/native-src/src/librats_logging.cpp +158 -0
- package/native-src/src/librats_mdns.cpp +171 -0
- package/native-src/src/librats_nat.cpp +571 -0
- package/native-src/src/librats_persistence.cpp +815 -0
- package/native-src/src/logger.h +412 -0
- package/native-src/src/mdns.cpp +1178 -0
- package/native-src/src/mdns.h +253 -0
- package/native-src/src/network_utils.cpp +598 -0
- package/native-src/src/network_utils.h +162 -0
- package/native-src/src/noise.cpp +981 -0
- package/native-src/src/noise.h +227 -0
- package/native-src/src/os.cpp +371 -0
- package/native-src/src/os.h +40 -0
- package/native-src/src/rats_export.h +17 -0
- package/native-src/src/sha1.cpp +163 -0
- package/native-src/src/sha1.h +42 -0
- package/native-src/src/socket.cpp +1376 -0
- package/native-src/src/socket.h +309 -0
- package/native-src/src/stun.cpp +484 -0
- package/native-src/src/stun.h +349 -0
- package/native-src/src/threadmanager.cpp +105 -0
- package/native-src/src/threadmanager.h +53 -0
- package/native-src/src/tracker.cpp +1110 -0
- package/native-src/src/tracker.h +268 -0
- package/native-src/src/version.cpp +24 -0
- package/native-src/src/version.h.in +45 -0
- package/native-src/version.rc.in +31 -0
- package/package.json +2 -8
- package/scripts/build-librats.js +59 -12
- package/scripts/prepare-package.js +133 -37
- package/src/librats_node.cpp +46 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
#include "sha1.h"
|
|
2
|
+
#include <iomanip>
|
|
3
|
+
#include <sstream>
|
|
4
|
+
#include <cstring>
|
|
5
|
+
|
|
6
|
+
namespace librats {
|
|
7
|
+
|
|
8
|
+
// SHA1 constants
|
|
9
|
+
static const uint32_t K[] = {
|
|
10
|
+
0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// Left rotate function
|
|
14
|
+
static uint32_t left_rotate(uint32_t value, int amount) {
|
|
15
|
+
return (value << amount) | (value >> (32 - amount));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
SHA1::SHA1() {
|
|
19
|
+
reset();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
void SHA1::reset() {
|
|
23
|
+
// SHA1 initialization constants
|
|
24
|
+
h0 = 0x67452301;
|
|
25
|
+
h1 = 0xEFCDAB89;
|
|
26
|
+
h2 = 0x98BADCFE;
|
|
27
|
+
h3 = 0x10325476;
|
|
28
|
+
h4 = 0xC3D2E1F0;
|
|
29
|
+
|
|
30
|
+
buffer_length = 0;
|
|
31
|
+
total_length = 0;
|
|
32
|
+
finalized = false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
void SHA1::update(uint8_t byte) {
|
|
36
|
+
if (finalized) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
buffer[buffer_length++] = byte;
|
|
41
|
+
total_length++;
|
|
42
|
+
|
|
43
|
+
if (buffer_length == 64) {
|
|
44
|
+
process_block();
|
|
45
|
+
buffer_length = 0;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
void SHA1::update(const uint8_t* data, size_t length) {
|
|
50
|
+
for (size_t i = 0; i < length; i++) {
|
|
51
|
+
update(data[i]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void SHA1::update(const std::string& str) {
|
|
56
|
+
update(reinterpret_cast<const uint8_t*>(str.c_str()), str.length());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void SHA1::process_block() {
|
|
60
|
+
uint32_t w[80];
|
|
61
|
+
|
|
62
|
+
// Break chunk into sixteen 32-bit big-endian words
|
|
63
|
+
for (int i = 0; i < 16; i++) {
|
|
64
|
+
w[i] = (buffer[i * 4] << 24) |
|
|
65
|
+
(buffer[i * 4 + 1] << 16) |
|
|
66
|
+
(buffer[i * 4 + 2] << 8) |
|
|
67
|
+
(buffer[i * 4 + 3]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Extend the sixteen 32-bit words into eighty 32-bit words
|
|
71
|
+
for (int i = 16; i < 80; i++) {
|
|
72
|
+
w[i] = left_rotate(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Initialize hash value for this chunk
|
|
76
|
+
uint32_t a = h0;
|
|
77
|
+
uint32_t b = h1;
|
|
78
|
+
uint32_t c = h2;
|
|
79
|
+
uint32_t d = h3;
|
|
80
|
+
uint32_t e = h4;
|
|
81
|
+
|
|
82
|
+
// Main loop
|
|
83
|
+
for (int i = 0; i < 80; i++) {
|
|
84
|
+
uint32_t f, k;
|
|
85
|
+
|
|
86
|
+
if (i < 20) {
|
|
87
|
+
f = (b & c) | (~b & d);
|
|
88
|
+
k = K[0];
|
|
89
|
+
} else if (i < 40) {
|
|
90
|
+
f = b ^ c ^ d;
|
|
91
|
+
k = K[1];
|
|
92
|
+
} else if (i < 60) {
|
|
93
|
+
f = (b & c) | (b & d) | (c & d);
|
|
94
|
+
k = K[2];
|
|
95
|
+
} else {
|
|
96
|
+
f = b ^ c ^ d;
|
|
97
|
+
k = K[3];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
uint32_t temp = left_rotate(a, 5) + f + e + k + w[i];
|
|
101
|
+
e = d;
|
|
102
|
+
d = c;
|
|
103
|
+
c = left_rotate(b, 30);
|
|
104
|
+
b = a;
|
|
105
|
+
a = temp;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Add this chunk's hash to result so far
|
|
109
|
+
h0 += a;
|
|
110
|
+
h1 += b;
|
|
111
|
+
h2 += c;
|
|
112
|
+
h3 += d;
|
|
113
|
+
h4 += e;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
std::string SHA1::finalize() {
|
|
117
|
+
if (finalized) {
|
|
118
|
+
// Return empty string for subsequent calls
|
|
119
|
+
return "";
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Pre-processing: adding padding bits
|
|
123
|
+
uint64_t bit_length = total_length * 8;
|
|
124
|
+
|
|
125
|
+
// Append the '1' bit
|
|
126
|
+
update(0x80);
|
|
127
|
+
|
|
128
|
+
// Append zeros until message length ≡ 448 (mod 512)
|
|
129
|
+
while (buffer_length != 56) {
|
|
130
|
+
update(0x00);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Append length in bits as 64-bit big-endian integer
|
|
134
|
+
for (int i = 7; i >= 0; i--) {
|
|
135
|
+
update(static_cast<uint8_t>(bit_length >> (i * 8)));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Produce the final hash value as a 160-bit number (hex string)
|
|
139
|
+
std::ostringstream result;
|
|
140
|
+
result << std::hex << std::setfill('0');
|
|
141
|
+
result << std::setw(8) << h0;
|
|
142
|
+
result << std::setw(8) << h1;
|
|
143
|
+
result << std::setw(8) << h2;
|
|
144
|
+
result << std::setw(8) << h3;
|
|
145
|
+
result << std::setw(8) << h4;
|
|
146
|
+
|
|
147
|
+
finalized = true;
|
|
148
|
+
return result.str();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
std::string SHA1::hash(const std::string& input) {
|
|
152
|
+
SHA1 hasher;
|
|
153
|
+
hasher.update(input);
|
|
154
|
+
return hasher.finalize();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
std::string SHA1::hash_bytes(const std::vector<uint8_t>& input) {
|
|
158
|
+
SHA1 hasher;
|
|
159
|
+
hasher.update(input.data(), input.size());
|
|
160
|
+
return hasher.finalize();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
} // namespace librats
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <vector>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
|
|
7
|
+
namespace librats {
|
|
8
|
+
|
|
9
|
+
class SHA1 {
|
|
10
|
+
public:
|
|
11
|
+
SHA1();
|
|
12
|
+
|
|
13
|
+
// Process a single byte
|
|
14
|
+
void update(uint8_t byte);
|
|
15
|
+
|
|
16
|
+
// Process a buffer
|
|
17
|
+
void update(const uint8_t* data, size_t length);
|
|
18
|
+
|
|
19
|
+
// Process a string
|
|
20
|
+
void update(const std::string& str);
|
|
21
|
+
|
|
22
|
+
// Get the final hash as a hex string
|
|
23
|
+
std::string finalize();
|
|
24
|
+
|
|
25
|
+
// Convenience function to hash a string directly
|
|
26
|
+
static std::string hash(const std::string& input);
|
|
27
|
+
|
|
28
|
+
// Convenience function to hash a vector of bytes directly
|
|
29
|
+
static std::string hash_bytes(const std::vector<uint8_t>& input);
|
|
30
|
+
|
|
31
|
+
private:
|
|
32
|
+
void process_block();
|
|
33
|
+
void reset();
|
|
34
|
+
|
|
35
|
+
uint32_t h0, h1, h2, h3, h4;
|
|
36
|
+
uint8_t buffer[64];
|
|
37
|
+
size_t buffer_length;
|
|
38
|
+
uint64_t total_length;
|
|
39
|
+
bool finalized;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
} // namespace librats
|