librats 0.3.1 → 0.5.1

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.
Files changed (69) hide show
  1. package/README.md +405 -405
  2. package/binding.gyp +96 -95
  3. package/lib/index.d.ts +522 -522
  4. package/lib/index.js +82 -82
  5. package/native-src/3rdparty/android/ifaddrs-android.c +600 -0
  6. package/native-src/3rdparty/android/ifaddrs-android.h +54 -0
  7. package/native-src/CMakeLists.txt +360 -0
  8. package/native-src/LICENSE +21 -0
  9. package/native-src/src/bencode.cpp +485 -0
  10. package/native-src/src/bencode.h +145 -0
  11. package/native-src/src/bittorrent.cpp +3682 -0
  12. package/native-src/src/bittorrent.h +731 -0
  13. package/native-src/src/dht.cpp +2342 -0
  14. package/native-src/src/dht.h +501 -0
  15. package/native-src/src/encrypted_socket.cpp +817 -0
  16. package/native-src/src/encrypted_socket.h +239 -0
  17. package/native-src/src/file_transfer.cpp +1808 -0
  18. package/native-src/src/file_transfer.h +567 -0
  19. package/native-src/src/fs.cpp +639 -0
  20. package/native-src/src/fs.h +108 -0
  21. package/native-src/src/gossipsub.cpp +1137 -0
  22. package/native-src/src/gossipsub.h +403 -0
  23. package/native-src/src/ice.cpp +1386 -0
  24. package/native-src/src/ice.h +328 -0
  25. package/native-src/src/json.hpp +25526 -0
  26. package/native-src/src/krpc.cpp +558 -0
  27. package/native-src/src/krpc.h +145 -0
  28. package/native-src/src/librats.cpp +2715 -0
  29. package/native-src/src/librats.h +1729 -0
  30. package/native-src/src/librats_bittorrent.cpp +167 -0
  31. package/native-src/src/librats_c.cpp +1317 -0
  32. package/native-src/src/librats_c.h +237 -0
  33. package/native-src/src/librats_encryption.cpp +123 -0
  34. package/native-src/src/librats_file_transfer.cpp +226 -0
  35. package/native-src/src/librats_gossipsub.cpp +293 -0
  36. package/native-src/src/librats_ice.cpp +515 -0
  37. package/native-src/src/librats_logging.cpp +158 -0
  38. package/native-src/src/librats_mdns.cpp +171 -0
  39. package/native-src/src/librats_nat.cpp +571 -0
  40. package/native-src/src/librats_persistence.cpp +815 -0
  41. package/native-src/src/logger.h +412 -0
  42. package/native-src/src/mdns.cpp +1178 -0
  43. package/native-src/src/mdns.h +253 -0
  44. package/native-src/src/network_utils.cpp +598 -0
  45. package/native-src/src/network_utils.h +162 -0
  46. package/native-src/src/noise.cpp +981 -0
  47. package/native-src/src/noise.h +227 -0
  48. package/native-src/src/os.cpp +371 -0
  49. package/native-src/src/os.h +40 -0
  50. package/native-src/src/rats_export.h +17 -0
  51. package/native-src/src/sha1.cpp +163 -0
  52. package/native-src/src/sha1.h +42 -0
  53. package/native-src/src/socket.cpp +1376 -0
  54. package/native-src/src/socket.h +309 -0
  55. package/native-src/src/stun.cpp +484 -0
  56. package/native-src/src/stun.h +349 -0
  57. package/native-src/src/threadmanager.cpp +105 -0
  58. package/native-src/src/threadmanager.h +53 -0
  59. package/native-src/src/tracker.cpp +1110 -0
  60. package/native-src/src/tracker.h +268 -0
  61. package/native-src/src/version.cpp +24 -0
  62. package/native-src/src/version.h.in +45 -0
  63. package/native-src/version.rc.in +31 -0
  64. package/package.json +62 -68
  65. package/scripts/build-librats.js +241 -194
  66. package/scripts/postinstall.js +52 -52
  67. package/scripts/prepare-package.js +187 -91
  68. package/scripts/verify-installation.js +119 -119
  69. package/src/librats_node.cpp +1174 -1174
@@ -0,0 +1,239 @@
1
+ #pragma once
2
+
3
+ #include "socket.h"
4
+ #include "noise.h"
5
+ #include <string>
6
+ #include <memory>
7
+ #include <vector>
8
+ #include <mutex>
9
+ #include <unordered_map>
10
+
11
+ namespace librats {
12
+
13
+ /**
14
+ * Encrypted socket wrapper that provides Noise Protocol encryption
15
+ * for all socket communications in librats
16
+ */
17
+ class EncryptedSocket {
18
+ public:
19
+ EncryptedSocket();
20
+ ~EncryptedSocket();
21
+
22
+ // Initialize encryption for a socket
23
+ bool initialize_as_initiator(socket_t socket, const NoiseKey& static_private_key);
24
+ bool initialize_as_responder(socket_t socket, const NoiseKey& static_private_key);
25
+
26
+ // Check encryption status
27
+ bool is_encrypted(socket_t socket) const;
28
+ bool is_handshake_completed(socket_t socket) const;
29
+ bool has_handshake_failed(socket_t socket) const;
30
+
31
+ // Handshake operations
32
+ bool send_handshake_message(socket_t socket, const std::vector<uint8_t>& payload = {});
33
+ std::vector<uint8_t> receive_handshake_message(socket_t socket);
34
+
35
+ // Encrypted communication (available after handshake completion) - binary primary
36
+ bool send_encrypted_data(socket_t socket, const std::vector<uint8_t>& data);
37
+ std::vector<uint8_t> receive_encrypted_data(socket_t socket);
38
+
39
+ // Encrypted communication - string convenience wrappers
40
+ bool send_encrypted_data(socket_t socket, const std::string& data);
41
+ std::string receive_encrypted_data_string(socket_t socket);
42
+
43
+ // Fallback to unencrypted communication - binary primary
44
+ bool send_unencrypted_data(socket_t socket, const std::vector<uint8_t>& data);
45
+ std::vector<uint8_t> receive_unencrypted_data(socket_t socket);
46
+
47
+ // Fallback to unencrypted communication - string convenience wrappers
48
+ bool send_unencrypted_data(socket_t socket, const std::string& data);
49
+ std::string receive_unencrypted_data_string(socket_t socket);
50
+
51
+ // Socket management
52
+ void remove_socket(socket_t socket);
53
+ void clear_all_sockets();
54
+
55
+ // Key management
56
+ static NoiseKey generate_static_key();
57
+ static std::string key_to_string(const NoiseKey& key);
58
+ static NoiseKey string_to_key(const std::string& key_str);
59
+
60
+ // Utility functions
61
+ NoiseRole get_socket_role(socket_t socket) const;
62
+ const NoiseKey& get_remote_static_key(socket_t socket) const;
63
+
64
+ private:
65
+ struct SocketSession {
66
+ std::unique_ptr<NoiseSession> session;
67
+ socket_t socket;
68
+ bool is_encrypted;
69
+
70
+ SocketSession(socket_t sock) : socket(sock), is_encrypted(false) {
71
+ session = std::make_unique<NoiseSession>();
72
+ }
73
+ };
74
+
75
+ std::unordered_map<socket_t, std::unique_ptr<SocketSession>> sessions_;
76
+ mutable std::mutex sessions_mutex_;
77
+
78
+ public:
79
+ // Helper methods
80
+ SocketSession* get_session(socket_t socket);
81
+ const SocketSession* get_session(socket_t socket) const;
82
+
83
+ private:
84
+
85
+ // Helper to read exact number of bytes from socket
86
+ std::string receive_exact_bytes(socket_t socket, size_t byte_count);
87
+
88
+ // Message framing for transport
89
+ static std::vector<uint8_t> frame_message(const std::vector<uint8_t>& message);
90
+ static std::vector<uint8_t> unframe_message(const std::vector<uint8_t>& framed_message);
91
+ static bool is_noise_handshake_message(const std::vector<uint8_t>& data);
92
+ };
93
+
94
+ /**
95
+ * Encrypted socket manager - singleton for managing all encrypted sockets
96
+ */
97
+ class EncryptedSocketManager {
98
+ public:
99
+ static EncryptedSocketManager& getInstance();
100
+
101
+ // Socket initialization
102
+ bool initialize_socket_as_initiator(socket_t socket, const NoiseKey& static_private_key);
103
+ bool initialize_socket_as_responder(socket_t socket, const NoiseKey& static_private_key);
104
+
105
+ // Communication methods (binary - primary)
106
+ bool send_data(socket_t socket, const std::vector<uint8_t>& data);
107
+ std::vector<uint8_t> receive_data(socket_t socket);
108
+
109
+ // Communication methods (string - convenience wrappers)
110
+ bool send_data(socket_t socket, const std::string& data);
111
+ std::string receive_data_string(socket_t socket);
112
+
113
+ // Handshake management
114
+ bool perform_handshake_step(socket_t socket, const std::vector<uint8_t>& received_data = {});
115
+ bool is_handshake_completed(socket_t socket) const;
116
+ bool has_handshake_failed(socket_t socket) const;
117
+
118
+ // Socket lifecycle
119
+ void remove_socket(socket_t socket);
120
+ void cleanup_all_sockets();
121
+
122
+ // Key management
123
+ void set_static_key(const NoiseKey& key) { static_key_ = key; }
124
+ const NoiseKey& get_static_key() const { return static_key_; }
125
+
126
+ // Configuration
127
+ void set_encryption_enabled(bool enabled) { encryption_enabled_ = enabled; }
128
+ bool is_encryption_enabled() const { return encryption_enabled_; }
129
+
130
+ private:
131
+ EncryptedSocketManager();
132
+ ~EncryptedSocketManager();
133
+
134
+ // Prevent copying
135
+ EncryptedSocketManager(const EncryptedSocketManager&) = delete;
136
+ EncryptedSocketManager& operator=(const EncryptedSocketManager&) = delete;
137
+
138
+ EncryptedSocket encrypted_socket_;
139
+ NoiseKey static_key_;
140
+ bool encryption_enabled_;
141
+ mutable std::mutex key_mutex_;
142
+ };
143
+
144
+ // High-level encrypted communication functions that replace the standard socket functions
145
+ namespace encrypted_communication {
146
+
147
+ /**
148
+ * Initialize encryption for librats with a static key
149
+ * @param static_key The static private key for this node
150
+ * @return true if successful, false otherwise
151
+ */
152
+ bool initialize_encryption(const NoiseKey& static_key);
153
+
154
+ /**
155
+ * Generate a new static key pair for this node
156
+ * @return The generated static private key
157
+ */
158
+ NoiseKey generate_node_key();
159
+
160
+ /**
161
+ * Enable or disable encryption globally
162
+ * @param enabled Whether encryption should be enabled
163
+ */
164
+ void set_encryption_enabled(bool enabled);
165
+
166
+ /**
167
+ * Check if encryption is enabled
168
+ * @return true if encryption is enabled, false otherwise
169
+ */
170
+ bool is_encryption_enabled();
171
+
172
+ /**
173
+ * Send binary data through an encrypted socket (primary method)
174
+ * @param socket The socket handle
175
+ * @param data The binary data to send
176
+ * @return Number of bytes sent, or -1 on error
177
+ */
178
+ int send_tcp_data_encrypted(socket_t socket, const std::vector<uint8_t>& data);
179
+
180
+ /**
181
+ * Send data through an encrypted socket (convenience wrapper for strings)
182
+ * @param socket The socket handle
183
+ * @param data The data to send
184
+ * @return Number of bytes sent, or -1 on error
185
+ */
186
+ int send_tcp_data_encrypted(socket_t socket, const std::string& data);
187
+
188
+ /**
189
+ * Receive binary data from an encrypted socket (primary method)
190
+ * @param socket The socket handle
191
+ * @param buffer_size Maximum number of bytes to receive
192
+ * @return Received binary data, empty vector on error
193
+ */
194
+ std::vector<uint8_t> receive_tcp_data_encrypted(socket_t socket, size_t buffer_size = 1024);
195
+
196
+ /**
197
+ * Receive data from an encrypted socket (convenience wrapper for strings)
198
+ * @param socket The socket handle
199
+ * @param buffer_size Maximum number of bytes to receive
200
+ * @return Received data as string, empty string on error
201
+ */
202
+ std::string receive_tcp_data_encrypted_string(socket_t socket, size_t buffer_size = 1024);
203
+
204
+ /**
205
+ * Initialize encryption for an outgoing connection
206
+ * @param socket The socket handle for the outgoing connection
207
+ * @return true if successful, false otherwise
208
+ */
209
+ bool initialize_outgoing_connection(socket_t socket);
210
+
211
+ /**
212
+ * Initialize encryption for an incoming connection
213
+ * @param socket The socket handle for the incoming connection
214
+ * @return true if successful, false otherwise
215
+ */
216
+ bool initialize_incoming_connection(socket_t socket);
217
+
218
+ /**
219
+ * Perform handshake step for a socket
220
+ * @param socket The socket handle
221
+ * @return true if handshake is progressing, false if failed
222
+ */
223
+ bool perform_handshake(socket_t socket);
224
+
225
+ /**
226
+ * Check if handshake is completed for a socket
227
+ * @param socket The socket handle
228
+ * @return true if handshake is completed, false otherwise
229
+ */
230
+ bool is_handshake_completed(socket_t socket);
231
+
232
+ /**
233
+ * Clean up encryption state for a socket
234
+ * @param socket The socket handle
235
+ */
236
+ void cleanup_socket(socket_t socket);
237
+ }
238
+
239
+ } // namespace librats