| #include <mutex> |
| 6 | #include <cstdarg> |
| 7 |
| 8 | #ifndef TGVOIP_USE_CUSTOM_CRYPTO |
| 9 | extern "C" |
| 10 | { |
| 11 | #include <openssl/aes.h> |
| 12 | //#include <openssl/modes.h> |
| 13 | #include <openssl/rand.h> |
| 14 | #include <openssl/sha.h> |
| 15 | } |
| 16 |
| 17 | void tgvoip_openssl_aes_ige_encrypt(const std::uint8_t* in, std::uint8_t* out, std::size_t length, const std::uint8_t* key, std::uint8_t* iv) |
| 18 | { |
| 19 | AES_KEY akey; |
| 20 | AES_set_encrypt_key(key, 32 * 8, &akey); |
| 21 | AES_ige_encrypt(in, out, length, &akey, iv, AES_ENCRYPT); |
| 22 | } |
| 23 |
| 24 | void tgvoip_openssl_aes_ige_decrypt(const std::uint8_t* in, std::uint8_t* out, std::size_t length, const std::uint8_t* key, std::uint8_t* iv) |
| 25 | { |
| 26 | AES_KEY akey; |
| 27 | AES_set_decrypt_key(key, 32 * 8, &akey); |
| 28 | AES_ige_encrypt(in, out, length, &akey, iv, AES_DECRYPT); |
| 29 | } |
| 30 |
| 31 | void tgvoip_openssl_rand_bytes(std::uint8_t* buffer, std::size_t len) |
| 32 | { |
| 33 | RAND_bytes(buffer, static_cast<int>(len)); |
| 34 | } |
| 35 |
| 36 | void tgvoip_openssl_sha1(const std::uint8_t* msg, std::size_t len, std::uint8_t* output) |
| 37 | { |
| 38 | SHA1(msg, len, output); |
| 39 | } |
| 40 |
| 41 | void tgvoip_openssl_sha256(const std::uint8_t* msg, std::size_t len, std::uint8_t* output) |
| 42 | { |
| 43 | SHA256(msg, len, output); |
| 44 | } |
| 45 |
| 46 | void tgvoip_openssl_aes_ctr_encrypt(std::uint8_t* inout, std::size_t length, const std::uint8_t* key, std::uint8_t* iv, std::uint8_t* ecount, std::uint32_t* num) |
| 47 | { |
| 48 | AES_KEY akey; |
| 49 | AES_set_encrypt_key(key, 32 * 8, &akey); |
| 50 | AES_ctr128_encrypt(inout, inout, length, &akey, iv, ecount, num); |
| 51 | } |
| 52 |
| 53 | void tgvoip_openssl_aes_cbc_encrypt(const std::uint8_t* in, std::uint8_t* out, std::size_t length, const std::uint8_t* key, std::uint8_t* iv) |
| 54 | { |
| 55 | AES_KEY akey; |
| 56 | AES_set_encrypt_key(key, 256, &akey); |
| 57 | AES_cbc_encrypt(in, out, length, &akey, iv, AES_ENCRYPT); |
| 58 | } |
| 59 |
| 60 | void tgvoip_openssl_aes_cbc_decrypt(const std::uint8_t* in, std::uint8_t* out, std::size_t length, const std::uint8_t* key, std::uint8_t* iv) |
| 61 | { |
| 62 | AES_KEY akey; |
| 63 | AES_set_decrypt_key(key, 256, &akey); |
| 64 | AES_cbc_encrypt(in, out, length, &akey, iv, AES_DECRYPT); |
| 65 | } |
| 66 |
| 67 | tgvoip::CryptoFunctions tgvoip::VoIPController::crypto = |
| 68 | { |
| 69 | tgvoip_openssl_rand_bytes, |
| 70 | tgvoip_openssl_sha1, |
| 71 | tgvoip_openssl_sha256, |
| 72 | tgvoip_openssl_aes_ige_encrypt, |
| 73 | tgvoip_openssl_aes_ige_decrypt, |
| 74 | tgvoip_openssl_aes_ctr_encrypt, |
| 75 | tgvoip_openssl_aes_cbc_encrypt, |
| 76 | tgvoip_openssl_aes_cbc_decrypt |
| 77 | }; |
| 78 | #endif |
| 79 |
| 80 | class TgVoipImpl : public TgVoip |
| 81 | { |
| 82 | public: |
| 83 | TgVoipImpl( |
| 84 | std::vector<TgVoipEndpoint> const& endpoints, |
| 85 | TgVoipPersistentState const& persistentState, |
| 86 | std::unique_ptr<TgVoipProxy> const& proxy, |
| 87 | TgVoipConfig const& config, |
| 88 | TgVoipEncryptionKey const& encryptionKey, |
| 89 | TgVoipNetworkType initialNetworkType |
| 90 | #ifdef TGVOIP_USE_CUSTOM_CRYPTO |
| 91 | , |
| 92 | TgVoipCrypto const& crypto |
| 93 | #endif |
| 94 | #ifdef TGVOIP_USE_CALLBACK_AUDIO_IO |
| 95 | , |
| 96 | TgVoipAudioDataCallbacks const& audioDataCallbacks |
| 97 | #endif |
| 98 | ); |
| 99 |
| 100 | ~TgVoipImpl() override; |
| 101 | void setOnStateUpdated(std::function<void(TgVoipState)> onStateUpdated) override; |
| 102 | void setOnSignalBarsUpdated(std::function<void(int)> onSignalBarsUpdated) override; |
| 103 | void setNetworkType(TgVoipNetworkType networkType) final; |
| 104 | void setMuteMicrophone(bool muteMicrophone) override; |
| 105 | void setAudioOutputGainControlEnabled(bool enabled) override; |
| 106 | void setEchoCancellationStrength(int strength) override; |
| 107 | std::string getLastError() override; |
| 108 | std::string getDebugInfo() override; |
| 109 | std::int64_t getPreferredRelayId() override; |
| 110 | TgVoipTrafficStats getTrafficStats() override; |
| 111 | TgVoipPersistentState getPersistentState() override; |
| 112 | TgVoipFinalState stop() override; |
| 113 |
| 114 | static void controllerStateCallback(tgvoip::VoIPController* controller, tgvoip::State state); |
| 115 | static void signalBarsCallback(tgvoip::VoIPController* controller, int signalBars); |
| 116 |
| 117 | private: |
| 118 | tgvoip::VoIPController* m_controller; |
| 119 | std::function<void(TgVoipState)> m_onStateUpdated; |
| 120 | std::function<void(int)> m_onSignalBarsUpdated; |
| 121 | std::mutex m_mutexOnStateUpdated, m_mutexOnSignalBarsUpdated; |
| 122 | }; |
| 123 |
| 124 | TgVoipImpl::TgVoipImpl( |
| 125 | std::vector<TgVoipEndpoint> const& endpoints, |
| 126 | TgVoipPersistentState const& persistentState, |
| 127 | std::unique_ptr<TgVoipProxy> const& proxy, |
| 128 | TgVoipConfig const& config, |
| 129 | TgVoipEncryptionKey const& encryptionKey, |
| 130 | TgVoipNetworkType initialNetworkType |
| 131 | #ifdef TGVOIP_USE_CUSTOM_CRYPTO |
| 132 | , |
| 133 | TgVoipCrypto const& crypto |
| 134 | #endif |
| 135 | #ifdef TGVOIP_USE_CALLBACK_AUDIO_IO |
| 136 | , |
| 137 | TgVoipAudioDataCallbacks const& audioDataCallbacks |
| 138 | #endif |
| 139 | ) |
| 140 | { |
| 141 | #ifdef TGVOIP_USE_CUSTOM_CRYPTO |
| 142 | tgvoip::VoIPController::crypto.sha1 = crypto.sha1; |
| 143 | tgvoip::VoIPController::crypto.sha256 = crypto.sha256; |
| 144 | tgvoip::VoIPController::crypto.rand_bytes = crypto.rand_bytes; |
| 145 | tgvoip::VoIPController::crypto.aes_ige_encrypt = crypto.aes_ige_encrypt; |
| 146 | tgvoip::VoIPController::crypto.aes_ige_decrypt = crypto.aes_ige_decrypt; |
| 147 | tgvoip::VoIPController::crypto.aes_ctr_encrypt = crypto.aes_ctr_encrypt; |
| 148 | #endif |
| 149 |
| 150 | m_controller = new tgvoip::VoIPController(); |
| 151 | m_controller->implData = this; |
| 152 |
| 153 | #ifdef TGVOIP_USE_CALLBACK_AUDIO_IO |
| 154 | m_controller->SetAudioDataCallbacks(audioDataCallbacks.input, audioDataCallbacks.output, audioDataCallbacks.preprocessed); |
| 155 | #endif |
| 156 |
| 157 | m_controller->SetPersistentState(persistentState.value); |
| 158 |
| 159 | if (proxy != nullptr) |
| 160 | { |
| 161 | m_controller->SetProxy(tgvoip::Proxy::SOCKS5, proxy->host, proxy->port, proxy->login, proxy->password); |
| 162 | } |
| 163 |
| 164 | auto callbacks = tgvoip::VoIPController::Callbacks(); |
| 165 | callbacks.connectionStateChanged = &TgVoipImpl::controllerStateCallback; |
| 166 | callbacks.groupCallKeyReceived = nullptr; |
| 167 | callbacks.groupCallKeySent = nullptr; |
| 168 | callbacks.signalBarCountChanged = &TgVoipImpl::signalBarsCallback; |
| 169 | callbacks.upgradeToGroupCallRequested = nullptr; |
| 170 | m_controller->SetCallbacks(callbacks); |
| 171 |
| 172 | std::vector<tgvoip::Endpoint> mappedEndpoints; |
| 173 | for (const TgVoipEndpoint& endpoint : endpoints) |
| 174 | { |
| 175 | tgvoip::Endpoint::Type mappedType; |
| 176 | switch (endpoint.type) |
| 177 | { |
| 178 | case TgVoipEndpointType::UdpRelay: |
| 179 | mappedType = tgvoip::Endpoint::Type::UDP_RELAY; |
| 180 | break; |
| 181 | case TgVoipEndpointType::Lan: |
| 182 | mappedType = tgvoip::Endpoint::Type::UDP_P2P_LAN; |
| 183 | break; |
| 184 | case TgVoipEndpointType::Inet: |
| 185 | mappedType = tgvoip::Endpoint::Type::UDP_P2P_INET; |
| 186 | break; |
| 187 | case TgVoipEndpointType::TcpRelay: |
| 188 | mappedType = tgvoip::Endpoint::Type::TCP_RELAY; |
| 189 | break; |
| 190 | // default: |
| 191 | // mappedType = tgvoip::Endpoint::Type::UDP_RELAY; |
| 192 | // break; |
| 193 | } |
| 194 |
| 195 | tgvoip::IPv4Address address(endpoint.host.ipv4); |
| 196 | tgvoip::IPv6Address addressv6(endpoint.host.ipv6); |
| 197 |
| 198 | mappedEndpoints.emplace_back(endpoint.endpointId, endpoint.port, address, addressv6, mappedType, endpoint.peerTag); |
| 199 | } |
| 200 |
| 201 | tgvoip::DataSaving mappedDataSaving; |
| 202 | switch (config.dataSaving) |
| 203 | { |
| 204 | case TgVoipDataSaving::Mobile: |
| 205 | mappedDataSaving = tgvoip::DataSaving::MOBILE; |
| 206 | break; |
| 207 | case TgVoipDataSaving::Always: |
| 208 | mappedDataSaving = tgvoip::DataSaving::ALWAYS; |
| 209 | break; |
| 210 | case TgVoipDataSaving::Never: |
| 211 | mappedDataSaving = tgvoip::DataSaving::NEVER; |
| 212 | break; |
| 213 | } |
| 214 |
| 215 | tgvoip::VoIPController::Config mappedConfig( |
| 216 | config.initializationTimeout, |
| 217 | config.receiveTimeout, |
| 218 | mappedDataSaving, |
| 219 | config.enableAEC, |
| 220 | config.enableNS, |
| 221 | config.enableAGC, |
| 222 | config.enableCallUpgrade); |
| 223 | mappedConfig.logFilePath = config.logPath; |
| 224 | mappedConfig.statsDumpFilePath = {}; |
| 225 |
| 226 | m_controller->SetConfig(mappedConfig); |
| 227 |
| 228 | setNetworkType(initialNetworkType); |
| 229 |
| 230 | std::vector<std::uint8_t> encryptionKeyValue = encryptionKey.value; |
| 231 | m_controller->SetEncryptionKey(reinterpret_cast<char*>(encryptionKeyValue.data()), encryptionKey.isOutgoing); |
| 232 | m_controller->SetRemoteEndpoints(mappedEndpoints, config.enableP2P, config.maxApiLayer); |
| 233 |
| 234 | m_controller->Start(); |
| 235 |
| 236 | m_controller->Connect(); |
| 237 | } |
| 238 |
| 239 | TgVoipImpl::~TgVoipImpl() = default; |
| 240 |
| 241 | void TgVoipImpl::setOnStateUpdated(std::function<void(TgVoipState)> onStateUpdated) |
| 242 | { |
| 243 | std::lock_guard<std::mutex> lock(m_mutexOnStateUpdated); |
| 244 | m_onStateUpdated = onStateUpdated; |
| 245 | } |
| 246 |
| 247 | void TgVoipImpl::setOnSignalBarsUpdated(std::function<void(int)> onSignalBarsUpdated) |
| 248 | { |
| 249 | std::lock_guard<std::mutex> lock(m_mutexOnSignalBarsUpdated); |
| 250 | m_onSignalBarsUpdated = onSignalBarsUpdated; |
| 251 | } |
| 252 |
| 253 | void TgVoipImpl::setNetworkType(TgVoipNetworkType networkType) |
| 254 | { |
| 255 | tgvoip::NetType mappedType; |
| 256 |
| 257 | switch (networkType) |
| 258 | { |
| 259 | case TgVoipNetworkType::Unknown: |
| 260 | mappedType = tgvoip::NetType::UNKNOWN; |
| 261 | break; |
| 262 | case TgVoipNetworkType::Gprs: |
| 263 | mappedType = tgvoip::NetType::GPRS; |
| 264 | break; |
| 265 | case TgVoipNetworkType::Edge: |
| 266 | mappedType = tgvoip::NetType::EDGE; |
| 267 | break; |
| 268 | case TgVoipNetworkType::ThirdGeneration: |
| 269 | mappedType = tgvoip::NetType::THREE_G; |
| 270 | break; |
| 271 | case TgVoipNetworkType::Hspa: |
| 272 | mappedType = tgvoip::NetType::HSPA; |
| 273 | break; |
| 274 | case TgVoipNetworkType::Lte: |
| 275 | mappedType = tgvoip::NetType::LTE; |
| 276 | break; |
| 277 | case TgVoipNetworkType::WiFi: |
| 278 | mappedType = tgvoip::NetType::WIFI; |
| 279 | break; |
| 280 | case TgVoipNetworkType::Ethernet: |
| 281 | mappedType = tgvoip::NetType::ETHERNET; |
| 282 | break; |
| 283 | case TgVoipNetworkType::OtherHighSpeed: |
| 284 | mappedType = tgvoip::NetType::OTHER_HIGH_SPEED; |
| 285 | break; |
| 286 | case TgVoipNetworkType::OtherLowSpeed: |
| 287 | mappedType = tgvoip::NetType::OTHER_LOW_SPEED; |
| 288 | break; |
| 289 | case TgVoipNetworkType::OtherMobile: |
| 290 | mappedType = tgvoip::NetType::OTHER_MOBILE; |
| 291 | break; |
| 292 | case TgVoipNetworkType::Dialup: |
| 293 | mappedType = tgvoip::NetType::DIALUP; |
| 294 | break; |
| 295 | // default: |
| 296 | // mappedType = tgvoip::NetType::UNKNOWN; |
| 297 | // break; |
| 298 | } |
| 299 |
| 300 | m_controller->SetNetworkType(mappedType); |
| 301 | } |
| 302 |
| 303 | void TgVoipImpl::setMuteMicrophone(bool muteMicrophone) |
| 304 | { |
| 305 | m_controller->SetMicMute(muteMicrophone); |
| 306 | } |
| 307 |
| 308 | void TgVoipImpl::setAudioOutputGainControlEnabled(bool enabled) |
| 309 | { |
| 310 | m_controller->SetAudioOutputGainControlEnabled(enabled); |
| 311 | } |
| 312 |
| 313 | void TgVoipImpl::setEchoCancellationStrength(int strength) |
| 314 | { |
| 315 | m_controller->SetEchoCancellationStrength(strength); |
| 316 | } |
| 317 |
| 318 | std::string TgVoipImpl::getLastError() |
| 319 | { |
| 320 | tgvoip::Error error = m_controller->GetLastError(); |
| 321 | switch (error) |
| 322 | { |
| 323 | case tgvoip::Error::INCOMPATIBLE: |
| 324 | return "Error::INCOMPATIBLE"; |
| 325 | case tgvoip::Error::TIMEOUT: |
| 326 | return "Error::TIMEOUT"; |
| 327 | case tgvoip::Error::AUDIO_IO: |
| 328 | return "Error::AUDIO_IO"; |
| 329 | case tgvoip::Error::PROXY: |
| 330 | return "Error::PROXY"; |
| 331 | case tgvoip::Error::UNKNOWN: |
| 332 | return "Error::UNKNOWN"; |
| 333 | } |
| 334 | throw std::runtime_error("Error " + std::to_string(static_cast<int>(error)) + " is not one of enum values!"); |
| 335 | } |
| 336 |
| 337 | std::string TgVoipImpl::getDebugInfo() |
| 338 | { |
| 339 | return m_controller->GetDebugString(); |
| 340 | } |
| 341 |
| 342 | std::int64_t TgVoipImpl::getPreferredRelayId() |
| 343 | { |
| 344 | return m_controller->GetPreferredRelayID(); |
| 345 | } |
| 346 |
| 347 | TgVoipTrafficStats TgVoipImpl::getTrafficStats() |
| 348 | { |
| 349 | tgvoip::VoIPController::TrafficStats stats; |
| 350 | m_controller->GetStats(&stats); |
| 351 | return TgVoipTrafficStats |
| 352 | { |
| 353 | .bytesSentWifi = stats.bytesSentWifi, |
| 354 | .bytesReceivedWifi = stats.bytesRecvdWifi, |
| 355 | .bytesSentMobile = stats.bytesSentMobile, |
| 356 | .bytesReceivedMobile = stats.bytesRecvdMobile |
| 357 | }; |
| 358 | } |
| 359 |
| 360 | TgVoipPersistentState TgVoipImpl::getPersistentState() |
| 361 | { |
| 362 | return {m_controller->GetPersistentState()}; |
| 363 | } |
| 364 |
| 365 | TgVoipFinalState TgVoipImpl::stop() |
| 366 | { |
| 367 | m_controller->Stop(); |
| 368 |
| 369 | TgVoipFinalState finalState = |
| 370 | { |
| 371 | .persistentState = getPersistentState(), |
| 372 | .debugLog = m_controller->GetDebugLog(), |
| 373 | .trafficStats = getTrafficStats(), |
| 374 | .isRatingSuggested = m_controller->NeedRate() |
| 375 | }; |
| 376 |
| 377 | delete m_controller; |
| 378 | m_controller = nullptr; |
| 379 |
| 380 | return finalState; |
| 381 | } |
| 382 |
| 383 | void TgVoipImpl::controllerStateCallback(tgvoip::VoIPController* controller, tgvoip::State state) |
| 384 | { |
| 385 | TgVoipImpl* self = reinterpret_cast<TgVoipImpl*>(controller->implData); |
| 386 | std::lock_guard<std::mutex> lock(self->m_mutexOnStateUpdated); |
| 387 |
| 388 | if (self->m_onStateUpdated) |
| 389 | { |
| 390 | TgVoipState mappedState; |
| 391 | switch (state) |
| 392 | { |
| 393 | case tgvoip::State::WAIT_INIT: |
| 394 | mappedState = TgVoipState::WaitInit; |
| 395 | break; |
| 396 | case tgvoip::State::WAIT_INIT_ACK: |
| 397 | mappedState = TgVoipState::WaitInitAck; |
| 398 | break; |
| 399 | case tgvoip::State::ESTABLISHED: |
| 400 | mappedState = TgVoipState::Estabilished; |
| 401 | break; |
| 402 | case tgvoip::State::FAILED: |
| 403 | mappedState = TgVoipState::Failed; |
| 404 | break; |
| 405 | case tgvoip::State::RECONNECTING: |
| 406 | mappedState = TgVoipState::Reconnecting; |
| 407 | break; |
| 408 | // default: |
| 409 | // mappedState = TgVoipState::Estabilished; |
| 410 | // break; |
| 411 | } |
| 412 |
| 413 | self->m_onStateUpdated(mappedState); |
| 414 | } |
| 415 | } |
| 416 |
| 417 | void TgVoipImpl::signalBarsCallback(tgvoip::VoIPController* controller, int signalBars) |
| 418 | { |
| 419 | TgVoipImpl* self = reinterpret_cast<TgVoipImpl*>(controller->implData); |
| 420 | std::lock_guard<std::mutex> lock(self->m_mutexOnSignalBarsUpdated); |
| 421 |
| 422 | if (self->m_onSignalBarsUpdated) |
| 423 | { |
| 424 | self->m_onSignalBarsUpdated(signalBars); |
| 425 | } |
| 426 | } |
| 427 |
| 428 | static std::function<void(std::string const&)> globalLoggingFunction; |
| 429 |
| 430 | void __tgvoip_call_tglog(const char* format, ...) |
| 431 | { |
| 432 | va_list vaArgs; |
| 433 | va_start(vaArgs, format); |
| 434 |
| 435 | va_list vaCopy; |
| 436 | va_copy(vaCopy, vaArgs); |
| 437 | const int length = std::vsnprintf(nullptr, 0, format, vaCopy); |
| 438 | va_end(vaCopy); |
| 439 |
| 440 | std::vector<char> zc(static_cast<std::size_t>(length) + 1); |
| 441 | std::vsnprintf(zc.data(), zc.size(), format, vaArgs); |
| 442 | va_end(vaArgs); |
| 443 |
| 444 | if (globalLoggingFunction != nullptr) |
| 445 | { |
| 446 | globalLoggingFunction(std::string(zc.data(), zc.size())); |
| 447 | } |
| 448 | } |
| 449 |
| 450 | void TgVoip::setLoggingFunction(std::function<void(std::string const&)> loggingFunction) |
| 451 | { |
| 452 | globalLoggingFunction = std::move(loggingFunction); |
| 453 | } |
| 454 |
| 455 | void TgVoip::setGlobalServerConfig(const std::string& serverConfig) |
| 456 | { |
| 457 | tgvoip::ServerConfig::GetSharedInstance()->Update(serverConfig); |
| 458 | } |
| 459 |
| 460 | int TgVoip::getConnectionMaxLayer() |
| 461 | { |
| 462 | return tgvoip::VoIPController::GetConnectionMaxLayer(); |
| 463 | } |
| 464 |
| 465 | std::string TgVoip::getVersion() |
| 466 | { |
| 467 | return tgvoip::VoIPController::GetVersion(); |
| 468 | } |
| 469 |
| 470 | TgVoip* TgVoip::makeInstance( |
| 471 | TgVoipConfig const& config, |
| 472 | TgVoipPersistentState const& persistentState, |
| 473 | std::vector<TgVoipEndpoint> const& endpoints, |
| 474 | std::unique_ptr<TgVoipProxy> const& proxy, |
| 475 | TgVoipNetworkType initialNetworkType, |
| 476 | TgVoipEncryptionKey const& encryptionKey |
| 477 | #ifdef TGVOIP_USE_CUSTOM_CRYPTO |
| 478 | , |
| 479 | TgVoipCrypto const& crypto |
| 480 | #endif |
| 481 | #ifdef TGVOIP_USE_CALLBACK_AUDIO_IO |
| 482 | , |
| 483 | TgVoipAudioDataCallbacks const& audioDataCallbacks |
| 484 | #endif |
| 485 | ) |
| 486 | { |
| 487 | return new TgVoipImpl( |
| 488 | endpoints, |
| 489 | persistentState, |
| 490 | proxy, |
| 491 | config, |
| 492 | encryptionKey, |
| 493 | initialNetworkType |
| 494 | #ifdef TGVOIP_USE_CUSTOM_CRYPTO |
| 495 | , |
| 496 | crypto |
| 497 | #endif |
| 498 | #ifdef TGVOIP_USE_CALLBACK_AUDIO_IO |
| 499 | , |
| 500 | audioDataCallbacks |
| 501 | #endif |
| 502 | ); |
| 503 | } |
| 504 |
| 505 | TgVoip::~TgVoip() = default; |
| 506 |