Line data Source code
1 : /*
2 : * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "jamidht/transfer_channel_handler.h"
19 :
20 : #include <opendht/thread_pool.h>
21 : #include <charconv>
22 :
23 : #include "fileutils.h"
24 :
25 : namespace jami {
26 :
27 569 : TransferChannelHandler::TransferChannelHandler(const std::shared_ptr<JamiAccount>& account,
28 569 : dhtnet::ConnectionManager& cm)
29 : : ChannelHandlerInterface()
30 569 : , account_(account)
31 569 : , connectionManager_(cm)
32 : {
33 569 : if (auto acc = account_.lock())
34 569 : idPath_ = fileutils::get_data_dir() / acc->getAccountID();
35 569 : }
36 :
37 1138 : TransferChannelHandler::~TransferChannelHandler() {}
38 :
39 : void
40 0 : TransferChannelHandler::connect(const DeviceId& deviceId,
41 : const std::string& channelName,
42 : ConnectCb&& cb,
43 : const std::string& connectionType,
44 : bool forceNewConnection)
45 0 : {}
46 :
47 : bool
48 54 : TransferChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert,
49 : const std::string& name)
50 : {
51 54 : auto acc = account_.lock();
52 54 : if (!acc || !cert || !cert->issuer)
53 0 : return false;
54 54 : auto cm = acc->convModule(true);
55 54 : if (!cm)
56 0 : return false;
57 54 : auto uri = cert->issuer->getId().toString();
58 : // Else, check if it's a profile or file in a conversation.
59 54 : auto idstr = std::string_view(name).substr(DATA_TRANSFER_SCHEME.size());
60 : // Remove arguments for now
61 54 : auto sep = idstr.find_last_of('?');
62 54 : idstr = idstr.substr(0, sep);
63 54 : if (idstr == "profile.vcf") {
64 : // If it's our profile from another device
65 2 : return uri == acc->getUsername();
66 : }
67 52 : sep = idstr.find('/');
68 52 : auto lastSep = idstr.find_last_of('/');
69 52 : auto conversationId = std::string(idstr.substr(0, sep));
70 52 : auto fileHost = idstr.substr(sep + 1, lastSep - sep - 1);
71 52 : auto fileId = idstr.substr(lastSep + 1);
72 52 : if (fileHost == acc->currentDeviceId())
73 0 : return false;
74 :
75 : // Check if peer is member of the conversation
76 104 : if (fileId == fmt::format("{}.vcf", acc->getUsername()) || fileId == "profile.vcf") {
77 : // Or a member from the conversation
78 34 : auto members = cm->getConversationMembers(conversationId);
79 98 : return std::find_if(members.begin(), members.end(), [&](auto m) { return m["uri"] == uri; })
80 68 : != members.end();
81 52 : } else if (fileHost == "profile") {
82 : // If a profile is sent, check if it's from another device
83 5 : return uri == acc->getUsername();
84 : }
85 :
86 13 : return cm->onFileChannelRequest(conversationId, uri, std::string(fileId), acc->sha3SumVerify());
87 54 : }
88 :
89 : void
90 106 : TransferChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>&,
91 : const std::string& name,
92 : std::shared_ptr<dhtnet::ChannelSocket> channel)
93 : {
94 106 : auto acc = account_.lock();
95 106 : if (!acc)
96 0 : return;
97 :
98 : // Remove scheme
99 106 : auto idstr = name.substr(DATA_TRANSFER_SCHEME.size());
100 : // Parse arguments
101 106 : auto sep = idstr.find_last_of('?');
102 106 : std::string arguments;
103 106 : if (sep != std::string::npos) {
104 20 : arguments = idstr.substr(sep + 1);
105 20 : idstr = idstr.substr(0, sep);
106 : }
107 :
108 106 : auto start = 0u, end = 0u;
109 106 : uint64_t lastModified = 0;
110 106 : std::string sha3Sum;
111 146 : for (const auto arg : split_string(arguments, '&')) {
112 40 : auto keyVal = split_string(arg, '=');
113 40 : if (keyVal.size() == 2) {
114 40 : if (keyVal[0] == "start") {
115 4 : start = to_int<unsigned>(keyVal[1]);
116 36 : } else if (keyVal[0] == "end") {
117 4 : end = to_int<unsigned>(keyVal[1]);
118 32 : } else if (keyVal[0] == "sha3") {
119 16 : sha3Sum = keyVal[1];
120 16 : } else if (keyVal[0] == "modified") {
121 : try {
122 16 : lastModified = to_int<uint64_t>(keyVal[1]);
123 0 : } catch (const std::exception& e) {
124 0 : JAMI_WARNING("TransferChannel: Unable to parse modified date: {}: {}",
125 : keyVal[1],
126 : e.what());
127 0 : }
128 : }
129 : }
130 146 : }
131 :
132 : // Check if profile
133 106 : if (idstr == "profile.vcf") {
134 4 : dht::ThreadPool::io().run([wacc = acc->weak(), path = idPath_ / "profile.vcf", channel, idstr, lastModified, sha3Sum] {
135 4 : if (auto acc = wacc.lock()) {
136 4 : if (!channel->isInitiator()) {
137 : // Only accept newest profiles
138 2 : if (lastModified == 0
139 2 : || lastModified > fileutils::lastWriteTimeInSeconds(acc->profilePath()))
140 2 : acc->dataTransfer()->onIncomingProfile(channel, sha3Sum);
141 : else
142 0 : channel->shutdown();
143 : } else {
144 : // If it's a profile from sync
145 2 : acc->dataTransfer()->transferFile(channel, idstr, "", path.string());
146 : }
147 4 : }
148 4 : });
149 4 : return;
150 : }
151 :
152 102 : auto splitted_id = split_string(idstr, '/');
153 102 : if (splitted_id.size() < 3) {
154 0 : JAMI_ERROR("Unsupported ID detected {}", name);
155 0 : channel->shutdown();
156 0 : return;
157 : }
158 :
159 : // convId/fileHost/fileId or convId/profile/fileId
160 102 : auto conversationId = std::string(splitted_id[0]);
161 102 : auto fileHost = std::string(splitted_id[1]);
162 102 : auto isContactProfile = splitted_id[1] == "profile";
163 102 : auto fileId = std::string(splitted_id[splitted_id.size() - 1]);
164 102 : if (channel->isInitiator())
165 51 : return;
166 :
167 : // Profile for a member in the conversation
168 51 : dht::ThreadPool::io().run([wacc = acc->weak(), profilePath = idPath_ / "profile.vcf", channel, conversationId, fileId, isContactProfile, idstr, start, end, sha3Sum] {
169 51 : if (auto acc = wacc.lock()) {
170 102 : if (fileId == fmt::format("{}.vcf", acc->getUsername())) {
171 28 : acc->dataTransfer()->transferFile(channel, fileId, "", profilePath.string());
172 39 : return;
173 23 : } else if (isContactProfile && fileId.find(".vcf") != std::string::npos) {
174 10 : auto path = acc->dataTransfer()->profilePath(fileId.substr(0, fileId.size() - 4));
175 5 : acc->dataTransfer()->transferFile(channel, fileId, "", path.string());
176 5 : return;
177 23 : } else if (fileId == "profile.vcf") {
178 6 : acc->dataTransfer()->onIncomingProfile(channel, sha3Sum);
179 6 : return;
180 : }
181 : // Check if it's a file in a conversation
182 12 : auto dt = acc->dataTransfer(conversationId);
183 12 : auto sep = fileId.find('_');
184 12 : if (!dt or sep == std::string::npos) {
185 0 : channel->shutdown();
186 0 : return;
187 : }
188 12 : auto interactionId = fileId.substr(0, sep);
189 12 : auto path = dt->path(fileId);
190 12 : dt->transferFile(channel, fileId, interactionId, path.string(), start, end);
191 63 : }
192 : });
193 475 : }
194 :
195 : } // namespace jami
|