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,237 @@
1
+ #pragma once
2
+
3
+ #include "rats_export.h"
4
+ #include <stdint.h>
5
+ #include <stddef.h>
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+
11
+ // Opaque handle to a RatsClient instance
12
+ typedef void* rats_client_t;
13
+
14
+ // Memory helpers for returned strings
15
+ RATS_API void rats_string_free(const char* str);
16
+
17
+ // Version / ABI
18
+ RATS_API const char* rats_get_version_string(void);
19
+ RATS_API void rats_get_version(int* major, int* minor, int* patch, int* build);
20
+ RATS_API const char* rats_get_git_describe(void);
21
+ RATS_API uint32_t rats_get_abi(void);
22
+
23
+ // Client lifecycle
24
+ RATS_API rats_client_t rats_create(int listen_port);
25
+ RATS_API void rats_destroy(rats_client_t client);
26
+ RATS_API int rats_start(rats_client_t client);
27
+ RATS_API void rats_stop(rats_client_t client);
28
+
29
+ // Basic operations
30
+ RATS_API int rats_connect(rats_client_t client, const char* host, int port);
31
+ RATS_API int rats_get_listen_port(rats_client_t client);
32
+ RATS_API int rats_broadcast_string(rats_client_t client, const char* message);
33
+ RATS_API int rats_send_string(rats_client_t client, const char* peer_id, const char* message);
34
+
35
+ // Info
36
+ RATS_API int rats_get_peer_count(rats_client_t client);
37
+ RATS_API char* rats_get_our_peer_id(rats_client_t client); // caller must free with rats_string_free
38
+ RATS_API char* rats_get_connection_statistics_json(rats_client_t client); // caller must free with rats_string_free
39
+ RATS_API char** rats_get_validated_peer_ids(rats_client_t client, int* count); // caller must free array and strings
40
+ RATS_API char** rats_get_peer_ids(rats_client_t client, int* count); // caller must free array and strings
41
+ RATS_API char* rats_get_peer_info_json(rats_client_t client, const char* peer_id); // caller must free
42
+
43
+ // Logging controls (optional helpers)
44
+ RATS_API void rats_set_logging_enabled(int enabled);
45
+ RATS_API void rats_set_log_level(const char* level_str); // "DEBUG", "INFO", "WARN", "ERROR"
46
+ RATS_API void rats_set_log_file_path(rats_client_t client, const char* file_path);
47
+ RATS_API char* rats_get_log_file_path(rats_client_t client); // caller must free
48
+ RATS_API void rats_set_log_colors_enabled(rats_client_t client, int enabled);
49
+ RATS_API int rats_is_log_colors_enabled(rats_client_t client);
50
+ RATS_API void rats_set_log_timestamps_enabled(rats_client_t client, int enabled);
51
+ RATS_API int rats_is_log_timestamps_enabled(rats_client_t client);
52
+ RATS_API void rats_set_log_rotation_size(rats_client_t client, size_t max_size_bytes);
53
+ RATS_API void rats_set_log_retention_count(rats_client_t client, int count);
54
+ RATS_API void rats_clear_log_file(rats_client_t client);
55
+
56
+ // Error codes
57
+ typedef enum {
58
+ RATS_SUCCESS = 0,
59
+ RATS_ERROR_INVALID_HANDLE = -1,
60
+ RATS_ERROR_INVALID_PARAMETER = -2,
61
+ RATS_ERROR_NOT_RUNNING = -3,
62
+ RATS_ERROR_OPERATION_FAILED = -4,
63
+ RATS_ERROR_PEER_NOT_FOUND = -5,
64
+ RATS_ERROR_MEMORY_ALLOCATION = -6,
65
+ RATS_ERROR_JSON_PARSE = -7
66
+ } rats_error_t;
67
+
68
+ // Connection strategy enum
69
+ typedef enum {
70
+ RATS_STRATEGY_DIRECT_ONLY = 0,
71
+ RATS_STRATEGY_STUN_ASSISTED = 1,
72
+ RATS_STRATEGY_ICE_FULL = 2,
73
+ RATS_STRATEGY_TURN_RELAY = 3,
74
+ RATS_STRATEGY_AUTO_ADAPTIVE = 4
75
+ } rats_connection_strategy_t;
76
+
77
+ // C callbacks
78
+ typedef void (*rats_connection_cb)(void* user_data, const char* peer_id);
79
+ typedef void (*rats_string_cb)(void* user_data, const char* peer_id, const char* message);
80
+ typedef void (*rats_binary_cb)(void* user_data, const char* peer_id, const void* data, size_t size);
81
+ typedef void (*rats_json_cb)(void* user_data, const char* peer_id, const char* json_str);
82
+ typedef void (*rats_disconnect_cb)(void* user_data, const char* peer_id);
83
+ typedef void (*rats_peer_discovered_cb)(void* user_data, const char* host, int port, const char* service_name);
84
+ typedef void (*rats_message_cb)(void* user_data, const char* peer_id, const char* message_data);
85
+
86
+ // Peer configuration
87
+ RATS_API rats_error_t rats_set_max_peers(rats_client_t client, int max_peers);
88
+ RATS_API int rats_get_max_peers(rats_client_t client);
89
+ RATS_API int rats_is_peer_limit_reached(rats_client_t client);
90
+
91
+ // Advanced connection methods
92
+ RATS_API rats_error_t rats_connect_with_strategy(rats_client_t client, const char* host, int port,
93
+ rats_connection_strategy_t strategy);
94
+ RATS_API rats_error_t rats_disconnect_peer_by_id(rats_client_t client, const char* peer_id);
95
+
96
+ // Binary data operations
97
+ RATS_API rats_error_t rats_send_binary(rats_client_t client, const char* peer_id,
98
+ const void* data, size_t size);
99
+ RATS_API int rats_broadcast_binary(rats_client_t client, const void* data, size_t size);
100
+
101
+ // JSON operations
102
+ RATS_API rats_error_t rats_send_json(rats_client_t client, const char* peer_id, const char* json_str);
103
+ RATS_API int rats_broadcast_json(rats_client_t client, const char* json_str);
104
+
105
+ // DHT Discovery
106
+ RATS_API rats_error_t rats_start_dht_discovery(rats_client_t client, int dht_port);
107
+ RATS_API void rats_stop_dht_discovery(rats_client_t client);
108
+ RATS_API int rats_is_dht_running(rats_client_t client);
109
+ RATS_API rats_error_t rats_announce_for_hash(rats_client_t client, const char* content_hash, int port);
110
+ RATS_API size_t rats_get_dht_routing_table_size(rats_client_t client);
111
+
112
+ // Automatic discovery
113
+ RATS_API void rats_start_automatic_peer_discovery(rats_client_t client);
114
+ RATS_API void rats_stop_automatic_peer_discovery(rats_client_t client);
115
+ RATS_API int rats_is_automatic_discovery_running(rats_client_t client);
116
+ RATS_API char* rats_get_discovery_hash(rats_client_t client); // caller must free
117
+ RATS_API char* rats_get_rats_peer_discovery_hash(void); // caller must free - static method
118
+
119
+ // mDNS Discovery
120
+ RATS_API rats_error_t rats_start_mdns_discovery(rats_client_t client, const char* service_name);
121
+ RATS_API void rats_stop_mdns_discovery(rats_client_t client);
122
+ RATS_API int rats_is_mdns_running(rats_client_t client);
123
+ RATS_API rats_error_t rats_query_mdns_services(rats_client_t client);
124
+
125
+ // Encryption
126
+ RATS_API rats_error_t rats_set_encryption_enabled(rats_client_t client, int enabled);
127
+ RATS_API int rats_is_encryption_enabled(rats_client_t client);
128
+ RATS_API char* rats_get_encryption_key(rats_client_t client); // caller must free
129
+ RATS_API rats_error_t rats_set_encryption_key(rats_client_t client, const char* key_hex);
130
+ RATS_API char* rats_generate_encryption_key(rats_client_t client); // caller must free
131
+
132
+ // Protocol configuration
133
+ RATS_API rats_error_t rats_set_protocol_name(rats_client_t client, const char* protocol_name);
134
+ RATS_API rats_error_t rats_set_protocol_version(rats_client_t client, const char* protocol_version);
135
+ RATS_API char* rats_get_protocol_name(rats_client_t client); // caller must free
136
+ RATS_API char* rats_get_protocol_version(rats_client_t client); // caller must free
137
+
138
+ // Message Exchange API
139
+ RATS_API rats_error_t rats_on_message(rats_client_t client, const char* message_type,
140
+ rats_message_cb callback, void* user_data);
141
+ RATS_API rats_error_t rats_send_message(rats_client_t client, const char* peer_id,
142
+ const char* message_type, const char* data);
143
+ RATS_API rats_error_t rats_broadcast_message(rats_client_t client, const char* message_type,
144
+ const char* data);
145
+
146
+ // File Transfer
147
+ RATS_API char* rats_send_file(rats_client_t client, const char* peer_id,
148
+ const char* file_path, const char* remote_filename); // returns transfer_id, caller must free
149
+ RATS_API char* rats_send_directory(rats_client_t client, const char* peer_id, const char* directory_path, const char* remote_directory_name, int recursive); // returns transfer_id, caller must free
150
+ RATS_API char* rats_request_file(rats_client_t client, const char* peer_id, const char* remote_file_path, const char* local_path); // returns transfer_id, caller must free
151
+ RATS_API char* rats_request_directory(rats_client_t client, const char* peer_id, const char* remote_directory_path, const char* local_directory_path, int recursive); // returns transfer_id, caller must free
152
+ RATS_API rats_error_t rats_accept_file_transfer(rats_client_t client, const char* transfer_id,
153
+ const char* local_path);
154
+ RATS_API rats_error_t rats_reject_file_transfer(rats_client_t client, const char* transfer_id,
155
+ const char* reason);
156
+ RATS_API rats_error_t rats_cancel_file_transfer(rats_client_t client, const char* transfer_id);
157
+ RATS_API rats_error_t rats_accept_directory_transfer(rats_client_t client, const char* transfer_id, const char* local_path);
158
+ RATS_API rats_error_t rats_reject_directory_transfer(rats_client_t client, const char* transfer_id, const char* reason);
159
+ RATS_API rats_error_t rats_pause_file_transfer(rats_client_t client, const char* transfer_id);
160
+ RATS_API rats_error_t rats_resume_file_transfer(rats_client_t client, const char* transfer_id);
161
+ RATS_API char* rats_get_file_transfer_progress_json(rats_client_t client, const char* transfer_id); // caller must free
162
+ RATS_API char* rats_get_file_transfer_statistics_json(rats_client_t client); // caller must free
163
+
164
+ // File transfer callbacks
165
+ typedef void (*rats_file_request_cb)(void* user_data, const char* peer_id, const char* transfer_id, const char* remote_path, const char* filename);
166
+ typedef void (*rats_directory_request_cb)(void* user_data, const char* peer_id, const char* transfer_id, const char* remote_path, const char* directory_name);
167
+ typedef void (*rats_directory_progress_cb)(void* user_data, const char* transfer_id, int files_completed, int total_files, const char* current_file);
168
+ typedef void (*rats_file_progress_cb)(void* user_data, const char* transfer_id, int progress_percent, const char* status);
169
+
170
+ // Additional file transfer callbacks
171
+ RATS_API void rats_set_file_request_callback(rats_client_t client, rats_file_request_cb cb, void* user_data);
172
+ RATS_API void rats_set_directory_request_callback(rats_client_t client, rats_directory_request_cb cb, void* user_data);
173
+ RATS_API void rats_set_file_progress_callback(rats_client_t client, rats_file_progress_cb cb, void* user_data);
174
+ RATS_API void rats_set_directory_progress_callback(rats_client_t client, rats_directory_progress_cb cb, void* user_data);
175
+
176
+ // Enhanced callbacks
177
+ RATS_API void rats_set_connection_callback(rats_client_t client, rats_connection_cb cb, void* user_data);
178
+ RATS_API void rats_set_string_callback(rats_client_t client, rats_string_cb cb, void* user_data);
179
+ RATS_API void rats_set_binary_callback(rats_client_t client, rats_binary_cb cb, void* user_data);
180
+ RATS_API void rats_set_json_callback(rats_client_t client, rats_json_cb cb, void* user_data);
181
+ RATS_API void rats_set_disconnect_callback(rats_client_t client, rats_disconnect_cb cb, void* user_data);
182
+ RATS_API void rats_set_peer_discovered_callback(rats_client_t client, rats_peer_discovered_cb cb, void* user_data);
183
+
184
+ // GossipSub functionality
185
+ RATS_API int rats_is_gossipsub_available(rats_client_t client);
186
+ RATS_API int rats_is_gossipsub_running(rats_client_t client);
187
+ RATS_API rats_error_t rats_subscribe_to_topic(rats_client_t client, const char* topic);
188
+ RATS_API rats_error_t rats_unsubscribe_from_topic(rats_client_t client, const char* topic);
189
+ RATS_API int rats_is_subscribed_to_topic(rats_client_t client, const char* topic);
190
+ RATS_API char** rats_get_subscribed_topics(rats_client_t client, int* count); // caller must free array and strings
191
+ RATS_API rats_error_t rats_publish_to_topic(rats_client_t client, const char* topic, const char* message);
192
+ RATS_API rats_error_t rats_publish_json_to_topic(rats_client_t client, const char* topic, const char* json_str);
193
+ RATS_API char** rats_get_topic_peers(rats_client_t client, const char* topic, int* count); // caller must free array and strings
194
+ RATS_API char** rats_get_topic_mesh_peers(rats_client_t client, const char* topic, int* count); // caller must free array and strings
195
+ RATS_API char* rats_get_gossipsub_statistics_json(rats_client_t client); // caller must free
196
+
197
+ // GossipSub callbacks
198
+ typedef void (*rats_topic_message_cb)(void* user_data, const char* peer_id, const char* topic, const char* message);
199
+ typedef void (*rats_topic_json_message_cb)(void* user_data, const char* peer_id, const char* topic, const char* json_str);
200
+ typedef void (*rats_topic_peer_joined_cb)(void* user_data, const char* peer_id, const char* topic);
201
+ typedef void (*rats_topic_peer_left_cb)(void* user_data, const char* peer_id, const char* topic);
202
+
203
+ RATS_API void rats_set_topic_message_callback(rats_client_t client, const char* topic, rats_topic_message_cb cb, void* user_data);
204
+ RATS_API void rats_set_topic_json_message_callback(rats_client_t client, const char* topic, rats_topic_json_message_cb cb, void* user_data);
205
+ RATS_API void rats_set_topic_peer_joined_callback(rats_client_t client, const char* topic, rats_topic_peer_joined_cb cb, void* user_data);
206
+ RATS_API void rats_set_topic_peer_left_callback(rats_client_t client, const char* topic, rats_topic_peer_left_cb cb, void* user_data);
207
+ RATS_API void rats_clear_topic_callbacks(rats_client_t client, const char* topic);
208
+
209
+ // NAT Traversal and STUN
210
+ RATS_API rats_error_t rats_discover_and_ignore_public_ip(rats_client_t client, const char* stun_server, int stun_port);
211
+ RATS_API char* rats_get_public_ip(rats_client_t client); // caller must free
212
+ RATS_API int rats_detect_nat_type(rats_client_t client); // Returns NAT type as integer
213
+ RATS_API char* rats_get_nat_characteristics_json(rats_client_t client); // caller must free
214
+ RATS_API void rats_add_ignored_address(rats_client_t client, const char* ip_address);
215
+ RATS_API char* rats_get_nat_traversal_statistics_json(rats_client_t client); // caller must free
216
+
217
+ // ICE coordination
218
+ RATS_API char* rats_create_ice_offer(rats_client_t client, const char* peer_id); // caller must free JSON string
219
+ RATS_API rats_error_t rats_connect_with_ice(rats_client_t client, const char* peer_id, const char* ice_offer_json);
220
+ RATS_API rats_error_t rats_handle_ice_answer(rats_client_t client, const char* peer_id, const char* ice_answer_json);
221
+
222
+ // Configuration persistence
223
+ RATS_API rats_error_t rats_load_configuration(rats_client_t client);
224
+ RATS_API rats_error_t rats_save_configuration(rats_client_t client);
225
+ RATS_API rats_error_t rats_set_data_directory(rats_client_t client, const char* directory_path);
226
+ RATS_API char* rats_get_data_directory(rats_client_t client); // caller must free
227
+ RATS_API int rats_load_and_reconnect_peers(rats_client_t client);
228
+ RATS_API int rats_load_historical_peers(rats_client_t client);
229
+ RATS_API int rats_save_historical_peers(rats_client_t client);
230
+ RATS_API void rats_clear_historical_peers(rats_client_t client);
231
+ RATS_API char** rats_get_historical_peer_ids(rats_client_t client, int* count); // caller must free array and strings
232
+
233
+ #ifdef __cplusplus
234
+ } // extern "C"
235
+ #endif
236
+
237
+
@@ -0,0 +1,123 @@
1
+ #include "librats.h"
2
+ #include "encrypted_socket.h"
3
+ #include <iostream>
4
+ #include <algorithm>
5
+ #include <chrono>
6
+ #include <memory>
7
+ #include <stdexcept>
8
+
9
+ #ifdef TESTING
10
+ #define LOG_CLIENT_DEBUG(message) LOG_DEBUG("client", "[pointer: " << this << "] " << message)
11
+ #define LOG_CLIENT_INFO(message) LOG_INFO("client", "[pointer: " << this << "] " << message)
12
+ #define LOG_CLIENT_WARN(message) LOG_WARN("client", "[pointer: " << this << "] " << message)
13
+ #define LOG_CLIENT_ERROR(message) LOG_ERROR("client", "[pointer: " << this << "] " << message)
14
+ #else
15
+ #define LOG_CLIENT_DEBUG(message) LOG_DEBUG("client", message)
16
+ #define LOG_CLIENT_INFO(message) LOG_INFO("client", message)
17
+ #define LOG_CLIENT_WARN(message) LOG_WARN("client", message)
18
+ #define LOG_CLIENT_ERROR(message) LOG_ERROR("client", message)
19
+ #endif
20
+
21
+ namespace librats {
22
+
23
+ //=============================================================================
24
+ // Encryption Methods Implementation
25
+ //=============================================================================
26
+
27
+ bool RatsClient::initialize_encryption(bool enable) {
28
+ std::lock_guard<std::mutex> lock(encryption_mutex_);
29
+
30
+ encryption_enabled_ = enable;
31
+
32
+ if (enable) {
33
+ // Initialize the encryption system with our static key
34
+ bool success = encrypted_communication::initialize_encryption(static_encryption_key_);
35
+ if (!success) {
36
+ LOG_CLIENT_ERROR("Failed to initialize encryption system");
37
+ encryption_enabled_ = false;
38
+ return false;
39
+ }
40
+
41
+ LOG_CLIENT_INFO("Encryption initialized and enabled");
42
+ } else {
43
+ encrypted_communication::set_encryption_enabled(false);
44
+ LOG_CLIENT_INFO("Encryption disabled");
45
+ }
46
+
47
+ return true;
48
+ }
49
+
50
+ void RatsClient::set_encryption_enabled(bool enabled) {
51
+ std::lock_guard<std::mutex> lock(encryption_mutex_);
52
+ encryption_enabled_ = enabled;
53
+ encrypted_communication::set_encryption_enabled(enabled);
54
+
55
+ LOG_CLIENT_INFO("Encryption " << (enabled ? "enabled" : "disabled"));
56
+ }
57
+
58
+ bool RatsClient::is_encryption_enabled() const {
59
+ std::lock_guard<std::mutex> lock(encryption_mutex_);
60
+ return encryption_enabled_;
61
+ }
62
+
63
+ std::string RatsClient::get_encryption_key() const {
64
+ std::lock_guard<std::mutex> lock(encryption_mutex_);
65
+ return EncryptedSocket::key_to_string(static_encryption_key_);
66
+ }
67
+
68
+ bool RatsClient::set_encryption_key(const std::string& key_hex) {
69
+ std::lock_guard<std::mutex> lock(encryption_mutex_);
70
+
71
+ NoiseKey new_key = EncryptedSocket::string_to_key(key_hex);
72
+
73
+ // Validate key (check if it's not all zeros)
74
+ bool is_valid = false;
75
+ for (uint8_t byte : new_key) {
76
+ if (byte != 0) {
77
+ is_valid = true;
78
+ break;
79
+ }
80
+ }
81
+
82
+ if (!is_valid) {
83
+ LOG_CLIENT_ERROR("Invalid encryption key provided");
84
+ return false;
85
+ }
86
+
87
+ static_encryption_key_ = new_key;
88
+
89
+ // Reinitialize encryption system if it's enabled
90
+ if (encryption_enabled_) {
91
+ encrypted_communication::initialize_encryption(static_encryption_key_);
92
+ }
93
+
94
+ LOG_CLIENT_INFO("Updated encryption key");
95
+ return true;
96
+ }
97
+
98
+ std::string RatsClient::generate_new_encryption_key() {
99
+ std::lock_guard<std::mutex> lock(encryption_mutex_);
100
+
101
+ static_encryption_key_ = encrypted_communication::generate_node_key();
102
+
103
+ // Reinitialize encryption system if it's enabled
104
+ if (encryption_enabled_) {
105
+ encrypted_communication::initialize_encryption(static_encryption_key_);
106
+ }
107
+
108
+ std::string key_hex = EncryptedSocket::key_to_string(static_encryption_key_);
109
+ LOG_CLIENT_INFO("Generated new encryption key: " << key_hex);
110
+
111
+ return key_hex;
112
+ }
113
+
114
+ bool RatsClient::is_peer_encrypted(const std::string& peer_id) const {
115
+ std::lock_guard<std::mutex> lock(peers_mutex_);
116
+ auto it = peers_.find(peer_id);
117
+ if (it != peers_.end()) {
118
+ return it->second.encryption_enabled && it->second.noise_handshake_completed;
119
+ }
120
+ return false;
121
+ }
122
+
123
+ } // namespace librats
@@ -0,0 +1,226 @@
1
+ #include "librats.h"
2
+ // Logging macros for RatsClient
3
+ #define LOG_CLIENT_DEBUG(message) LOG_DEBUG("client", message)
4
+ #define LOG_CLIENT_INFO(message) LOG_INFO("client", message)
5
+ #define LOG_CLIENT_WARN(message) LOG_WARN("client", message)
6
+ #define LOG_CLIENT_ERROR(message) LOG_ERROR("client", message)
7
+
8
+ namespace librats {
9
+
10
+ //=============================================================================
11
+ // File Transfer API Implementation
12
+ //=============================================================================
13
+
14
+ FileTransferManager& RatsClient::get_file_transfer_manager() {
15
+ if (!file_transfer_manager_) {
16
+ throw std::runtime_error("File transfer manager not initialized");
17
+ }
18
+ return *file_transfer_manager_;
19
+ }
20
+
21
+ bool RatsClient::is_file_transfer_available() const {
22
+ return file_transfer_manager_ != nullptr;
23
+ }
24
+
25
+ std::string RatsClient::send_file(const std::string& peer_id, const std::string& file_path,
26
+ const std::string& remote_filename) {
27
+ if (!is_file_transfer_available()) {
28
+ LOG_CLIENT_ERROR("File transfer manager not available");
29
+ return "";
30
+ }
31
+
32
+ return file_transfer_manager_->send_file(peer_id, file_path, remote_filename);
33
+ }
34
+
35
+ std::string RatsClient::send_directory(const std::string& peer_id, const std::string& directory_path,
36
+ const std::string& remote_directory_name, bool recursive) {
37
+ if (!is_file_transfer_available()) {
38
+ LOG_CLIENT_ERROR("File transfer manager not available");
39
+ return "";
40
+ }
41
+
42
+ return file_transfer_manager_->send_directory(peer_id, directory_path, remote_directory_name, recursive);
43
+ }
44
+
45
+ bool RatsClient::accept_file_transfer(const std::string& transfer_id, const std::string& local_path) {
46
+ if (!is_file_transfer_available()) {
47
+ LOG_CLIENT_ERROR("File transfer manager not available");
48
+ return false;
49
+ }
50
+
51
+ return file_transfer_manager_->accept_file_transfer(transfer_id, local_path);
52
+ }
53
+
54
+ bool RatsClient::reject_file_transfer(const std::string& transfer_id, const std::string& reason) {
55
+ if (!is_file_transfer_available()) {
56
+ LOG_CLIENT_ERROR("File transfer manager not available");
57
+ return false;
58
+ }
59
+
60
+ return file_transfer_manager_->reject_file_transfer(transfer_id, reason);
61
+ }
62
+
63
+ bool RatsClient::accept_directory_transfer(const std::string& transfer_id, const std::string& local_path) {
64
+ if (!is_file_transfer_available()) {
65
+ LOG_CLIENT_ERROR("File transfer manager not available");
66
+ return false;
67
+ }
68
+
69
+ return file_transfer_manager_->accept_directory_transfer(transfer_id, local_path);
70
+ }
71
+
72
+ bool RatsClient::reject_directory_transfer(const std::string& transfer_id, const std::string& reason) {
73
+ if (!is_file_transfer_available()) {
74
+ LOG_CLIENT_ERROR("File transfer manager not available");
75
+ return false;
76
+ }
77
+
78
+ return file_transfer_manager_->reject_directory_transfer(transfer_id, reason);
79
+ }
80
+
81
+ bool RatsClient::pause_file_transfer(const std::string& transfer_id) {
82
+ if (!is_file_transfer_available()) {
83
+ LOG_CLIENT_ERROR("File transfer manager not available");
84
+ return false;
85
+ }
86
+
87
+ return file_transfer_manager_->pause_transfer(transfer_id);
88
+ }
89
+
90
+ bool RatsClient::resume_file_transfer(const std::string& transfer_id) {
91
+ if (!is_file_transfer_available()) {
92
+ LOG_CLIENT_ERROR("File transfer manager not available");
93
+ return false;
94
+ }
95
+
96
+ return file_transfer_manager_->resume_transfer(transfer_id);
97
+ }
98
+
99
+ bool RatsClient::cancel_file_transfer(const std::string& transfer_id) {
100
+ if (!is_file_transfer_available()) {
101
+ LOG_CLIENT_ERROR("File transfer manager not available");
102
+ return false;
103
+ }
104
+
105
+ return file_transfer_manager_->cancel_transfer(transfer_id);
106
+ }
107
+
108
+ std::shared_ptr<FileTransferProgress> RatsClient::get_file_transfer_progress(const std::string& transfer_id) const {
109
+ if (!is_file_transfer_available()) {
110
+ LOG_CLIENT_ERROR("File transfer manager not available");
111
+ return nullptr;
112
+ }
113
+
114
+ return file_transfer_manager_->get_transfer_progress(transfer_id);
115
+ }
116
+
117
+ std::vector<std::shared_ptr<FileTransferProgress>> RatsClient::get_active_file_transfers() const {
118
+ if (!is_file_transfer_available()) {
119
+ LOG_CLIENT_ERROR("File transfer manager not available");
120
+ return {};
121
+ }
122
+
123
+ return file_transfer_manager_->get_active_transfers();
124
+ }
125
+
126
+ nlohmann::json RatsClient::get_file_transfer_statistics() const {
127
+ if (!is_file_transfer_available()) {
128
+ LOG_CLIENT_ERROR("File transfer manager not available");
129
+ return nlohmann::json::object();
130
+ }
131
+
132
+ return file_transfer_manager_->get_transfer_statistics();
133
+ }
134
+
135
+ void RatsClient::set_file_transfer_config(const FileTransferConfig& config) {
136
+ if (!is_file_transfer_available()) {
137
+ LOG_CLIENT_ERROR("File transfer manager not available");
138
+ return;
139
+ }
140
+
141
+ file_transfer_manager_->set_config(config);
142
+ }
143
+
144
+ const FileTransferConfig& RatsClient::get_file_transfer_config() const {
145
+ if (!is_file_transfer_available()) {
146
+ throw std::runtime_error("File transfer manager not available");
147
+ }
148
+
149
+ return file_transfer_manager_->get_config();
150
+ }
151
+
152
+ void RatsClient::on_file_transfer_progress(FileTransferProgressCallback callback) {
153
+ if (!is_file_transfer_available()) {
154
+ LOG_CLIENT_ERROR("File transfer manager not available");
155
+ return;
156
+ }
157
+
158
+ file_transfer_manager_->set_progress_callback(callback);
159
+ }
160
+
161
+ void RatsClient::on_file_transfer_completed(FileTransferCompletedCallback callback) {
162
+ if (!is_file_transfer_available()) {
163
+ LOG_CLIENT_ERROR("File transfer manager not available");
164
+ return;
165
+ }
166
+
167
+ file_transfer_manager_->set_completion_callback(callback);
168
+ }
169
+
170
+ void RatsClient::on_file_transfer_request(FileTransferRequestCallback callback) {
171
+ if (!is_file_transfer_available()) {
172
+ LOG_CLIENT_ERROR("File transfer manager not available");
173
+ return;
174
+ }
175
+
176
+ file_transfer_manager_->set_request_callback(callback);
177
+ }
178
+
179
+ void RatsClient::on_directory_transfer_progress(DirectoryTransferProgressCallback callback) {
180
+ if (!is_file_transfer_available()) {
181
+ LOG_CLIENT_ERROR("File transfer manager not available");
182
+ return;
183
+ }
184
+
185
+ file_transfer_manager_->set_directory_progress_callback(callback);
186
+ }
187
+
188
+ std::string RatsClient::request_file(const std::string& peer_id, const std::string& remote_file_path,
189
+ const std::string& local_path) {
190
+ if (!is_file_transfer_available()) {
191
+ LOG_CLIENT_ERROR("File transfer manager not available");
192
+ return "";
193
+ }
194
+
195
+ return file_transfer_manager_->request_file(peer_id, remote_file_path, local_path);
196
+ }
197
+
198
+ std::string RatsClient::request_directory(const std::string& peer_id, const std::string& remote_directory_path,
199
+ const std::string& local_directory_path, bool recursive) {
200
+ if (!is_file_transfer_available()) {
201
+ LOG_CLIENT_ERROR("File transfer manager not available");
202
+ return "";
203
+ }
204
+
205
+ return file_transfer_manager_->request_directory(peer_id, remote_directory_path, local_directory_path, recursive);
206
+ }
207
+
208
+ void RatsClient::on_file_request(FileRequestCallback callback) {
209
+ if (!is_file_transfer_available()) {
210
+ LOG_CLIENT_ERROR("File transfer manager not available");
211
+ return;
212
+ }
213
+
214
+ file_transfer_manager_->set_file_request_callback(callback);
215
+ }
216
+
217
+ void RatsClient::on_directory_request(DirectoryRequestCallback callback) {
218
+ if (!is_file_transfer_available()) {
219
+ LOG_CLIENT_ERROR("File transfer manager not available");
220
+ return;
221
+ }
222
+
223
+ file_transfer_manager_->set_directory_request_callback(callback);
224
+ }
225
+
226
+ }