librats 0.5.0 → 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.
- package/binding.gyp +1 -0
- 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 +2342 -0
- package/native-src/src/dht.h +501 -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 +2715 -0
- package/native-src/src/librats.h +1729 -0
- package/native-src/src/librats_bittorrent.cpp +167 -0
- package/native-src/src/librats_c.cpp +1317 -0
- package/native-src/src/librats_c.h +237 -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
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
#include "librats.h"
|
|
2
|
+
#include "mdns.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
|
+
// ===== mDNS DISCOVERY METHODS IMPLEMENTATION =====
|
|
24
|
+
|
|
25
|
+
bool RatsClient::start_mdns_discovery(const std::string& service_instance_name,
|
|
26
|
+
const std::map<std::string, std::string>& txt_records) {
|
|
27
|
+
if (!running_.load()) {
|
|
28
|
+
LOG_CLIENT_ERROR("RatsClient is not running, cannot start mDNS discovery");
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (mdns_client_ && mdns_client_->is_running()) {
|
|
33
|
+
LOG_CLIENT_WARN("mDNS discovery is already running");
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
LOG_CLIENT_INFO("Starting mDNS service discovery and announcement");
|
|
38
|
+
|
|
39
|
+
// Create service instance name from our peer ID if not provided
|
|
40
|
+
std::string instance_name = service_instance_name;
|
|
41
|
+
if (instance_name.empty()) {
|
|
42
|
+
instance_name = "librats-" + get_our_peer_id().substr(0, 8);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Create mDNS client
|
|
46
|
+
mdns_client_ = std::make_unique<MdnsClient>(instance_name, listen_port_);
|
|
47
|
+
|
|
48
|
+
// Set service discovery callback
|
|
49
|
+
mdns_client_->set_service_callback([this](const MdnsService& service, bool is_new) {
|
|
50
|
+
handle_mdns_service_discovery(service, is_new);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Start mDNS client
|
|
54
|
+
if (!mdns_client_->start()) {
|
|
55
|
+
LOG_CLIENT_ERROR("Failed to start mDNS client");
|
|
56
|
+
mdns_client_.reset();
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Start service announcement
|
|
61
|
+
if (!mdns_client_->announce_service(instance_name, listen_port_, txt_records)) {
|
|
62
|
+
LOG_CLIENT_WARN("Failed to start mDNS service announcement");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Start service discovery
|
|
66
|
+
if (!mdns_client_->start_discovery()) {
|
|
67
|
+
LOG_CLIENT_WARN("Failed to start mDNS service discovery");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
LOG_CLIENT_INFO("mDNS discovery started successfully for service: " << instance_name);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
void RatsClient::stop_mdns_discovery() {
|
|
75
|
+
if (!mdns_client_) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
LOG_CLIENT_INFO("Stopping mDNS discovery");
|
|
80
|
+
|
|
81
|
+
mdns_client_->stop();
|
|
82
|
+
mdns_client_.reset();
|
|
83
|
+
|
|
84
|
+
LOG_CLIENT_INFO("mDNS discovery stopped");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
bool RatsClient::is_mdns_running() const {
|
|
88
|
+
return mdns_client_ && mdns_client_->is_running();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
void RatsClient::set_mdns_callback(std::function<void(const std::string&, int, const std::string&)> callback) {
|
|
92
|
+
mdns_callback_ = callback;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
std::vector<MdnsService> RatsClient::get_mdns_services() const {
|
|
96
|
+
if (!mdns_client_) {
|
|
97
|
+
return {};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return mdns_client_->get_recent_services(std::chrono::seconds(300)); // Services seen in last 5 minutes
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
bool RatsClient::query_mdns_services() {
|
|
104
|
+
if (!mdns_client_) {
|
|
105
|
+
LOG_CLIENT_ERROR("mDNS client not initialized");
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return mdns_client_->query_services();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
void RatsClient::handle_mdns_service_discovery(const MdnsService& service, bool is_new) {
|
|
113
|
+
LOG_CLIENT_INFO("mDNS discovered " << (is_new ? "new" : "updated") << " librats service: "
|
|
114
|
+
<< service.service_name << " at " << service.ip_address << ":" << service.port);
|
|
115
|
+
|
|
116
|
+
// Extract instance name from service name for logging
|
|
117
|
+
std::string instance_name = service.service_name;
|
|
118
|
+
size_t pos = instance_name.find("._librats._tcp.local.");
|
|
119
|
+
if (pos != std::string::npos) {
|
|
120
|
+
instance_name = instance_name.substr(0, pos);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Call user callback if registered
|
|
124
|
+
if (mdns_callback_) {
|
|
125
|
+
try {
|
|
126
|
+
mdns_callback_(service.ip_address, service.port, instance_name);
|
|
127
|
+
} catch (const std::exception& e) {
|
|
128
|
+
LOG_CLIENT_ERROR("Exception in mDNS callback: " << e.what());
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Auto-connect to discovered services if they're new
|
|
133
|
+
if (is_new) {
|
|
134
|
+
// Check if this peer should be ignored (local interface)
|
|
135
|
+
if (should_ignore_peer(service.ip_address, service.port)) {
|
|
136
|
+
LOG_CLIENT_DEBUG("Ignoring mDNS discovered peer " << service.ip_address << ":"
|
|
137
|
+
<< service.port << " - local interface address");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Check if we're already connected to this peer
|
|
142
|
+
std::string normalized_peer_address = normalize_peer_address(service.ip_address, service.port);
|
|
143
|
+
if (is_already_connected_to_address(normalized_peer_address)) {
|
|
144
|
+
LOG_CLIENT_DEBUG("Already connected to mDNS discovered peer: " << normalized_peer_address);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Check if peer limit is reached
|
|
149
|
+
if (is_peer_limit_reached()) {
|
|
150
|
+
LOG_CLIENT_DEBUG("Peer limit reached, not connecting to mDNS discovered peer "
|
|
151
|
+
<< service.ip_address << ":" << service.port);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
LOG_CLIENT_INFO("Attempting to connect to mDNS discovered peer: "
|
|
156
|
+
<< service.ip_address << ":" << service.port);
|
|
157
|
+
|
|
158
|
+
// Try to connect to the discovered peer (non-blocking)
|
|
159
|
+
std::thread([this, service]() {
|
|
160
|
+
if (connect_to_peer(service.ip_address, service.port)) {
|
|
161
|
+
LOG_CLIENT_INFO("Successfully connected to mDNS discovered peer: "
|
|
162
|
+
<< service.ip_address << ":" << service.port);
|
|
163
|
+
} else {
|
|
164
|
+
LOG_CLIENT_DEBUG("Failed to connect to mDNS discovered peer: "
|
|
165
|
+
<< service.ip_address << ":" << service.port);
|
|
166
|
+
}
|
|
167
|
+
}).detach();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
} // namespace librats
|