Line data Source code
1 : /*
2 : * Copyright (C) 2004-2024 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 <charconv>
21 :
22 : #include "fileutils.h"
23 :
24 : namespace jami {
25 :
26 680 : TransferChannelHandler::TransferChannelHandler(const std::shared_ptr<JamiAccount>& account,
27 680 : dhtnet::ConnectionManager& cm)
28 : : ChannelHandlerInterface()
29 680 : , account_(account)
30 680 : , connectionManager_(cm)
31 : {
32 680 : if (auto acc = account_.lock())
33 680 : idPath_ = fileutils::get_data_dir() / acc->getAccountID();
34 680 : }
35 :
36 1360 : TransferChannelHandler::~TransferChannelHandler() {}
37 :
38 : void
39 0 : TransferChannelHandler::connect(const DeviceId& deviceId,
40 : const std::string& channelName,
41 : ConnectCb&& cb)
42 0 : {}
43 :
44 : bool
45 68 : TransferChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert,
46 : const std::string& name)
47 : {
48 68 : auto acc = account_.lock();
49 68 : if (!acc || !cert || !cert->issuer)
50 0 : return false;
51 68 : auto cm = acc->convModule(true);
52 68 : if (!cm)
53 0 : return false;
54 68 : auto uri = cert->issuer->getId().toString();
55 : // Else, check if it's a profile or file in a conversation.
56 68 : auto idstr = std::string_view(name).substr(DATA_TRANSFER_SCHEME.size());
57 : // Remove arguments for now
58 68 : auto sep = idstr.find_last_of('?');
59 68 : idstr = idstr.substr(0, sep);
60 68 : if (idstr == "profile.vcf") {
61 : // If it's our profile from another device
62 2 : return uri == acc->getUsername();
63 : }
64 66 : sep = idstr.find('/');
65 66 : auto lastSep = idstr.find_last_of('/');
66 66 : auto conversationId = std::string(idstr.substr(0, sep));
67 66 : auto fileHost = idstr.substr(sep + 1, lastSep - sep - 1);
68 66 : auto fileId = idstr.substr(lastSep + 1);
69 66 : if (fileHost == acc->currentDeviceId())
70 0 : return false;
71 :
72 : // Check if peer is member of the conversation
73 132 : if (fileId == fmt::format("{}.vcf", acc->getUsername()) || fileId == "profile.vcf") {
74 : // Or a member from the conversation
75 41 : auto members = cm->getConversationMembers(conversationId);
76 118 : return std::find_if(members.begin(), members.end(), [&](auto m) { return m["uri"] == uri; })
77 82 : != members.end();
78 66 : } else if (fileHost == "profile") {
79 : // If a profile is sent, check if it's from another device
80 12 : return uri == acc->getUsername();
81 : }
82 :
83 13 : return cm->onFileChannelRequest(conversationId,
84 : uri,
85 26 : std::string(fileId),
86 26 : acc->sha3SumVerify());
87 68 : }
88 :
89 : void
90 130 : TransferChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>&,
91 : const std::string& name,
92 : std::shared_ptr<dhtnet::ChannelSocket> channel)
93 : {
94 130 : auto acc = account_.lock();
95 130 : if (!acc)
96 0 : return;
97 :
98 : // Remove scheme
99 130 : auto idstr = name.substr(DATA_TRANSFER_SCHEME.size());
100 : // Parse arguments
101 130 : auto sep = idstr.find_last_of('?');
102 130 : std::string arguments;
103 130 : if (sep != std::string::npos) {
104 14 : arguments = idstr.substr(sep + 1);
105 14 : idstr = idstr.substr(0, sep);
106 : }
107 :
108 130 : auto start = 0u, end = 0u;
109 130 : uint64_t lastModified = 0;
110 130 : std::string sha3Sum;
111 158 : for (const auto arg : split_string(arguments, '&')) {
112 28 : auto keyVal = split_string(arg, '=');
113 28 : if (keyVal.size() == 2) {
114 28 : if (keyVal[0] == "start") {
115 0 : start = to_int<unsigned>(keyVal[1]);
116 28 : } else if (keyVal[0] == "end") {
117 0 : end = to_int<unsigned>(keyVal[1]);
118 28 : } else if (keyVal[0] == "sha3") {
119 14 : sha3Sum = keyVal[1];
120 14 : } else if (keyVal[0] == "modified") {
121 : try {
122 14 : lastModified = jami::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], e.what());
126 0 : }
127 : }
128 : }
129 158 : }
130 :
131 : // Check if profile
132 130 : if (idstr == "profile.vcf") {
133 4 : if (!channel->isInitiator()) {
134 : // Only accept newest profiles
135 0 : if (lastModified == 0
136 4 : || lastModified > fileutils::lastWriteTimeInSeconds(acc->profilePath()))
137 0 : acc->dataTransfer()->onIncomingProfile(channel, sha3Sum);
138 : else
139 0 : channel->shutdown();
140 : } else {
141 : // If it's a profile from sync
142 2 : auto path = idPath_ / "profile.vcf";
143 2 : acc->dataTransfer()->transferFile(channel, idstr, "", path.string());
144 2 : }
145 2 : return;
146 : }
147 :
148 126 : auto splitted_id = split_string(idstr, '/');
149 126 : if (splitted_id.size() < 3) {
150 0 : JAMI_ERR() << "Unsupported ID detected " << name;
151 0 : channel->shutdown();
152 0 : return;
153 : }
154 :
155 : // convId/fileHost/fileId or convId/profile/fileId
156 126 : auto conversationId = std::string(splitted_id[0]);
157 126 : auto fileHost = std::string(splitted_id[1]);
158 126 : auto isContactProfile = splitted_id[1] == "profile";
159 126 : auto fileId = std::string(splitted_id[splitted_id.size() - 1]);
160 126 : if (channel->isInitiator())
161 63 : return;
162 :
163 : // Profile for a member in the conversation
164 126 : if (fileId == fmt::format("{}.vcf", acc->getUsername())) {
165 35 : auto path = idPath_ / "profile.vcf";
166 35 : acc->dataTransfer()->transferFile(channel, fileId, "", path.string());
167 35 : return;
168 63 : } else if (isContactProfile && fileId.find(".vcf") != std::string::npos) {
169 24 : auto path = acc->dataTransfer()->profilePath(fileId.substr(0, fileId.size() - 4));
170 12 : acc->dataTransfer()->transferFile(channel, fileId, "", path.string());
171 12 : return;
172 28 : } else if (fileId == "profile.vcf") {
173 5 : acc->dataTransfer()->onIncomingProfile(channel, sha3Sum);
174 5 : return;
175 : }
176 : // Check if it's a file in a conversation
177 11 : auto dt = acc->dataTransfer(conversationId);
178 11 : sep = fileId.find('_');
179 11 : if (!dt or sep == std::string::npos) {
180 0 : channel->shutdown();
181 0 : return;
182 : }
183 11 : auto interactionId = fileId.substr(0, sep);
184 11 : auto path = dt->path(fileId);
185 11 : dt->transferFile(channel, fileId, interactionId, path.string(), start, end);
186 947 : }
187 :
188 : } // namespace jami
|