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,253 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "socket.h"
|
|
4
|
+
#include "logger.h"
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <vector>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <functional>
|
|
9
|
+
#include <thread>
|
|
10
|
+
#include <atomic>
|
|
11
|
+
#include <mutex>
|
|
12
|
+
#include <chrono>
|
|
13
|
+
#include <map>
|
|
14
|
+
#include <cstdint>
|
|
15
|
+
#include <condition_variable>
|
|
16
|
+
|
|
17
|
+
namespace librats {
|
|
18
|
+
|
|
19
|
+
#define LOG_MDNS_DEBUG(message) LOG_DEBUG("mdns", message)
|
|
20
|
+
#define LOG_MDNS_INFO(message) LOG_INFO("mdns", message)
|
|
21
|
+
#define LOG_MDNS_WARN(message) LOG_WARN("mdns", message)
|
|
22
|
+
#define LOG_MDNS_ERROR(message) LOG_ERROR("mdns", message)
|
|
23
|
+
|
|
24
|
+
// mDNS protocol constants
|
|
25
|
+
const uint16_t MDNS_PORT = 5353;
|
|
26
|
+
const std::string MDNS_MULTICAST_IPv4 = "224.0.0.251";
|
|
27
|
+
const std::string MDNS_MULTICAST_IPv6 = "ff02::fb";
|
|
28
|
+
const std::string LIBRATS_SERVICE_TYPE = "_librats._tcp.local.";
|
|
29
|
+
const std::string LIBRATS_SERVICE_INSTANCE_SUFFIX = ".local.";
|
|
30
|
+
|
|
31
|
+
// DNS record types
|
|
32
|
+
enum class DnsRecordType : uint16_t {
|
|
33
|
+
A = 1,
|
|
34
|
+
PTR = 12,
|
|
35
|
+
TXT = 16,
|
|
36
|
+
AAAA = 28,
|
|
37
|
+
SRV = 33
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// DNS record classes
|
|
41
|
+
enum class DnsRecordClass : uint16_t {
|
|
42
|
+
CLASS_IN = 1,
|
|
43
|
+
CLASS_IN_FLUSH = 0x8001 // Cache flush bit set
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// mDNS message flags
|
|
47
|
+
enum class MdnsFlags : uint16_t {
|
|
48
|
+
QUERY = 0x0000,
|
|
49
|
+
RESPONSE = 0x8000,
|
|
50
|
+
AUTHORITATIVE = 0x8400
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Discovered service structure
|
|
54
|
+
struct MdnsService {
|
|
55
|
+
std::string service_name; // e.g., "rats-node-abc123._librats._tcp.local."
|
|
56
|
+
std::string host_name; // e.g., "MyComputer.local."
|
|
57
|
+
std::string ip_address; // IPv4 or IPv6 address
|
|
58
|
+
uint16_t port; // Service port
|
|
59
|
+
std::map<std::string, std::string> txt_records; // TXT record key-value pairs
|
|
60
|
+
std::chrono::steady_clock::time_point last_seen;
|
|
61
|
+
|
|
62
|
+
MdnsService() : port(0) {}
|
|
63
|
+
|
|
64
|
+
MdnsService(const std::string& name, const std::string& host,
|
|
65
|
+
const std::string& ip, uint16_t p)
|
|
66
|
+
: service_name(name), host_name(host), ip_address(ip), port(p),
|
|
67
|
+
last_seen(std::chrono::steady_clock::now()) {}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// DNS message header
|
|
71
|
+
struct DnsHeader {
|
|
72
|
+
uint16_t transaction_id;
|
|
73
|
+
uint16_t flags;
|
|
74
|
+
uint16_t question_count;
|
|
75
|
+
uint16_t answer_count;
|
|
76
|
+
uint16_t authority_count;
|
|
77
|
+
uint16_t additional_count;
|
|
78
|
+
|
|
79
|
+
DnsHeader() : transaction_id(0), flags(0), question_count(0),
|
|
80
|
+
answer_count(0), authority_count(0), additional_count(0) {}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// DNS question structure
|
|
84
|
+
struct DnsQuestion {
|
|
85
|
+
std::string name;
|
|
86
|
+
DnsRecordType type;
|
|
87
|
+
DnsRecordClass record_class;
|
|
88
|
+
|
|
89
|
+
DnsQuestion() : type(DnsRecordType::PTR), record_class(DnsRecordClass::CLASS_IN) {}
|
|
90
|
+
DnsQuestion(const std::string& n, DnsRecordType t, DnsRecordClass c)
|
|
91
|
+
: name(n), type(t), record_class(c) {}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// DNS resource record structure
|
|
95
|
+
struct DnsResourceRecord {
|
|
96
|
+
std::string name;
|
|
97
|
+
DnsRecordType type;
|
|
98
|
+
DnsRecordClass record_class;
|
|
99
|
+
uint32_t ttl;
|
|
100
|
+
std::vector<uint8_t> data;
|
|
101
|
+
|
|
102
|
+
DnsResourceRecord() : type(DnsRecordType::PTR), record_class(DnsRecordClass::CLASS_IN), ttl(120) {}
|
|
103
|
+
DnsResourceRecord(const std::string& n, DnsRecordType t, DnsRecordClass c, uint32_t ttl_val)
|
|
104
|
+
: name(n), type(t), record_class(c), ttl(ttl_val) {}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Complete DNS message structure
|
|
108
|
+
struct DnsMessage {
|
|
109
|
+
DnsHeader header;
|
|
110
|
+
std::vector<DnsQuestion> questions;
|
|
111
|
+
std::vector<DnsResourceRecord> answers;
|
|
112
|
+
std::vector<DnsResourceRecord> authorities;
|
|
113
|
+
std::vector<DnsResourceRecord> additionals;
|
|
114
|
+
|
|
115
|
+
DnsMessage() = default;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// mDNS service discovery callback
|
|
119
|
+
using MdnsServiceCallback = std::function<void(const MdnsService& service, bool is_new)>;
|
|
120
|
+
|
|
121
|
+
class MdnsClient {
|
|
122
|
+
public:
|
|
123
|
+
explicit MdnsClient(const std::string& service_instance_name = "", uint16_t service_port = 0);
|
|
124
|
+
~MdnsClient();
|
|
125
|
+
|
|
126
|
+
// Core functionality
|
|
127
|
+
bool start();
|
|
128
|
+
void stop();
|
|
129
|
+
void shutdown_immediate();
|
|
130
|
+
bool is_running() const;
|
|
131
|
+
|
|
132
|
+
// Service announcement
|
|
133
|
+
bool announce_service(const std::string& instance_name, uint16_t port,
|
|
134
|
+
const std::map<std::string, std::string>& txt_records = {});
|
|
135
|
+
void stop_announcing();
|
|
136
|
+
bool is_announcing() const;
|
|
137
|
+
|
|
138
|
+
// Service discovery
|
|
139
|
+
void set_service_callback(MdnsServiceCallback callback);
|
|
140
|
+
bool start_discovery();
|
|
141
|
+
void stop_discovery();
|
|
142
|
+
bool is_discovering() const;
|
|
143
|
+
|
|
144
|
+
// Query for specific services
|
|
145
|
+
bool query_services();
|
|
146
|
+
|
|
147
|
+
// Get discovered services
|
|
148
|
+
std::vector<MdnsService> get_discovered_services() const;
|
|
149
|
+
std::vector<MdnsService> get_recent_services(std::chrono::seconds max_age = std::chrono::seconds(300)) const;
|
|
150
|
+
void clear_old_services(std::chrono::seconds max_age = std::chrono::seconds(600));
|
|
151
|
+
|
|
152
|
+
// Configuration
|
|
153
|
+
void set_announcement_interval(std::chrono::seconds interval);
|
|
154
|
+
void set_query_interval(std::chrono::seconds interval);
|
|
155
|
+
|
|
156
|
+
private:
|
|
157
|
+
// Core properties
|
|
158
|
+
std::string service_instance_name_;
|
|
159
|
+
uint16_t service_port_;
|
|
160
|
+
std::map<std::string, std::string> txt_records_;
|
|
161
|
+
|
|
162
|
+
// Network properties
|
|
163
|
+
socket_t multicast_socket_;
|
|
164
|
+
std::string local_hostname_;
|
|
165
|
+
std::string local_ip_address_;
|
|
166
|
+
|
|
167
|
+
// Threading and state
|
|
168
|
+
std::atomic<bool> running_;
|
|
169
|
+
std::atomic<bool> announcing_;
|
|
170
|
+
std::atomic<bool> discovering_;
|
|
171
|
+
std::thread receiver_thread_;
|
|
172
|
+
std::thread announcer_thread_;
|
|
173
|
+
std::thread querier_thread_;
|
|
174
|
+
|
|
175
|
+
// Conditional variables for immediate shutdown
|
|
176
|
+
std::condition_variable shutdown_cv_;
|
|
177
|
+
std::mutex shutdown_mutex_;
|
|
178
|
+
|
|
179
|
+
// Discovery state
|
|
180
|
+
mutable std::mutex services_mutex_;
|
|
181
|
+
std::map<std::string, MdnsService> discovered_services_;
|
|
182
|
+
MdnsServiceCallback service_callback_;
|
|
183
|
+
|
|
184
|
+
// Timing configuration
|
|
185
|
+
std::chrono::seconds announcement_interval_;
|
|
186
|
+
std::chrono::seconds query_interval_;
|
|
187
|
+
|
|
188
|
+
// Socket operations
|
|
189
|
+
bool create_multicast_socket();
|
|
190
|
+
bool join_multicast_group();
|
|
191
|
+
bool leave_multicast_group();
|
|
192
|
+
void close_multicast_socket();
|
|
193
|
+
|
|
194
|
+
// Message handling threads
|
|
195
|
+
void receiver_loop();
|
|
196
|
+
void announcer_loop();
|
|
197
|
+
void querier_loop();
|
|
198
|
+
|
|
199
|
+
// Packet processing
|
|
200
|
+
void handle_received_packet(const std::vector<uint8_t>& packet, const std::string& sender_ip);
|
|
201
|
+
void process_mdns_message(const DnsMessage& message, const std::string& sender_ip);
|
|
202
|
+
void process_query(const DnsMessage& query, const std::string& sender_ip);
|
|
203
|
+
void process_response(const DnsMessage& response, const std::string& sender_ip);
|
|
204
|
+
|
|
205
|
+
// Service processing
|
|
206
|
+
void extract_service_from_response(const DnsMessage& response, const std::string& sender_ip);
|
|
207
|
+
bool is_librats_service(const std::string& service_name) const;
|
|
208
|
+
void add_or_update_service(const MdnsService& service);
|
|
209
|
+
|
|
210
|
+
// Message creation
|
|
211
|
+
DnsMessage create_query_message();
|
|
212
|
+
DnsMessage create_announcement_message();
|
|
213
|
+
DnsMessage create_response_message(const DnsQuestion& question);
|
|
214
|
+
|
|
215
|
+
// DNS record creation
|
|
216
|
+
DnsResourceRecord create_ptr_record(const std::string& service_type, const std::string& instance_name, uint32_t ttl = 120);
|
|
217
|
+
DnsResourceRecord create_srv_record(const std::string& instance_name, const std::string& hostname, uint16_t port, uint32_t ttl = 120);
|
|
218
|
+
DnsResourceRecord create_txt_record(const std::string& instance_name, const std::map<std::string, std::string>& txt_data, uint32_t ttl = 120);
|
|
219
|
+
DnsResourceRecord create_a_record(const std::string& hostname, const std::string& ip_address, uint32_t ttl = 120);
|
|
220
|
+
|
|
221
|
+
// DNS serialization/deserialization
|
|
222
|
+
std::vector<uint8_t> serialize_dns_message(const DnsMessage& message);
|
|
223
|
+
bool deserialize_dns_message(const std::vector<uint8_t>& data, DnsMessage& message);
|
|
224
|
+
|
|
225
|
+
// DNS name compression helpers
|
|
226
|
+
void write_dns_name(std::vector<uint8_t>& buffer, const std::string& name);
|
|
227
|
+
std::string read_dns_name(const std::vector<uint8_t>& buffer, size_t& offset);
|
|
228
|
+
void write_uint16(std::vector<uint8_t>& buffer, uint16_t value);
|
|
229
|
+
void write_uint32(std::vector<uint8_t>& buffer, uint32_t value);
|
|
230
|
+
uint16_t read_uint16(const std::vector<uint8_t>& buffer, size_t& offset);
|
|
231
|
+
uint32_t read_uint32(const std::vector<uint8_t>& buffer, size_t& offset);
|
|
232
|
+
|
|
233
|
+
// TXT record helpers
|
|
234
|
+
std::vector<uint8_t> encode_txt_record(const std::map<std::string, std::string>& txt_data);
|
|
235
|
+
std::map<std::string, std::string> decode_txt_record(const std::vector<uint8_t>& txt_data);
|
|
236
|
+
|
|
237
|
+
// SRV record helpers
|
|
238
|
+
std::vector<uint8_t> encode_srv_record(uint16_t priority, uint16_t weight, uint16_t port, const std::string& target);
|
|
239
|
+
bool decode_srv_record(const std::vector<uint8_t>& srv_data, uint16_t& priority, uint16_t& weight, uint16_t& port, std::string& target);
|
|
240
|
+
|
|
241
|
+
// Utility functions
|
|
242
|
+
std::string get_local_hostname();
|
|
243
|
+
std::string get_local_ip_address();
|
|
244
|
+
std::string create_service_instance_name(const std::string& instance_name);
|
|
245
|
+
std::string extract_instance_name_from_service(const std::string& service_name);
|
|
246
|
+
bool send_multicast_packet(const std::vector<uint8_t>& packet);
|
|
247
|
+
|
|
248
|
+
// Name validation
|
|
249
|
+
bool is_valid_dns_name(const std::string& name) const;
|
|
250
|
+
std::string normalize_dns_name(const std::string& name) const;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
} // namespace librats
|