| 6 | #include <openssl/sha.h> |
| 7 | #include <openssl/aes.h> |
| 8 | #ifndef OPENSSL_IS_BORINGSSL |
| 9 | #include <openssl/modes.h> |
| 10 | #endif |
| 11 | #include <openssl/rand.h> |
| 12 | } |
| 13 |
| 14 | void tgvoip_openssl_aes_ige_encrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){ |
| 15 | AES_KEY akey; |
| 16 | AES_set_encrypt_key(key, 32*8, &akey); |
| 17 | AES_ige_encrypt(in, out, length, &akey, iv, AES_ENCRYPT); |
| 18 | } |
| 19 |
| 20 | void tgvoip_openssl_aes_ige_decrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){ |
| 21 | AES_KEY akey; |
| 22 | AES_set_decrypt_key(key, 32*8, &akey); |
| 23 | AES_ige_encrypt(in, out, length, &akey, iv, AES_DECRYPT); |
| 24 | } |
| 25 |
| 26 | void tgvoip_openssl_rand_bytes(uint8_t* buffer, size_t len){ |
| 27 | RAND_bytes(buffer, len); |
| 28 | } |
| 29 |
| 30 | void tgvoip_openssl_sha1(uint8_t* msg, size_t len, uint8_t* output){ |
| 31 | SHA1(msg, len, output); |
| 32 | } |
| 33 |
| 34 | void tgvoip_openssl_sha256(uint8_t* msg, size_t len, uint8_t* output){ |
| 35 | SHA256(msg, len, output); |
| 36 | } |
| 37 |
| 38 | void tgvoip_openssl_aes_ctr_encrypt(uint8_t* inout, size_t length, uint8_t* key, uint8_t* iv, uint8_t* ecount, uint32_t* num){ |
| 39 | AES_KEY akey; |
| 40 | AES_set_encrypt_key(key, 32*8, &akey); |
| 41 | #ifdef OPENSSL_IS_BORINGSSL |
| 42 | AES_ctr128_encrypt(inout, inout, length, &akey, iv, ecount, num); |
| 43 | #else |
| 44 | CRYPTO_ctr128_encrypt(inout, inout, length, &akey, iv, ecount, num, (block128_f) AES_encrypt); |
| 45 | #endif |
| 46 | } |
| 47 |
| 48 | void tgvoip_openssl_aes_cbc_encrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){ |
| 49 | AES_KEY akey; |
| 50 | AES_set_encrypt_key(key, 256, &akey); |
| 51 | AES_cbc_encrypt(in, out, length, &akey, iv, AES_ENCRYPT); |
| 52 | } |
| 53 |
| 54 | void tgvoip_openssl_aes_cbc_decrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){ |
| 55 | AES_KEY akey; |
| 56 | AES_set_decrypt_key(key, 256, &akey); |
| 57 | AES_cbc_encrypt(in, out, length, &akey, iv, AES_DECRYPT); |
| 58 | } |
| 59 |
| 60 | tgvoip::CryptoFunctions tgvoip::VoIPController::crypto = { |
| 61 | tgvoip_openssl_rand_bytes, |
| 62 | tgvoip_openssl_sha1, |
| 63 | tgvoip_openssl_sha256, |
| 64 | tgvoip_openssl_aes_ige_encrypt, |
| 65 | tgvoip_openssl_aes_ige_decrypt, |
| 66 | tgvoip_openssl_aes_ctr_encrypt, |
| 67 | tgvoip_openssl_aes_cbc_encrypt, |
| 68 | tgvoip_openssl_aes_cbc_decrypt |
| 69 | }; |
| 70 |
| 71 | namespace tgcalls { |
| 72 |
| 73 | InstanceImplLegacy::InstanceImplLegacy(Descriptor &&descriptor) : |
| 74 | onStateUpdated_(std::move(descriptor.stateUpdated)), |
| 75 | onSignalBarsUpdated_(std::move(descriptor.signalBarsUpdated)) { |
| 76 | controller_ = new tgvoip::VoIPController(); |
| 77 | controller_->implData = this; |
| 78 |
| 79 | controller_->SetPersistentState(descriptor.persistentState.value); |
| 80 |
| 81 | if (const auto proxy = descriptor.proxy.get()) { |
| 82 | controller_->SetProxy(tgvoip::PROXY_SOCKS5, proxy->host, proxy->port, proxy->login, proxy->password); |
| 83 | } |
| 84 |
| 85 | auto callbacks = tgvoip::VoIPController::Callbacks(); |
| 86 | callbacks.connectionStateChanged = &InstanceImplLegacy::ControllerStateCallback; |
| 87 | callbacks.groupCallKeyReceived = nullptr; |
| 88 | callbacks.groupCallKeySent = nullptr; |
| 89 | callbacks.signalBarCountChanged = &InstanceImplLegacy::SignalBarsCallback; |
| 90 | callbacks.upgradeToGroupCallRequested = nullptr; |
| 91 | controller_->SetCallbacks(callbacks); |
| 92 |
| 93 | std::vector<tgvoip::Endpoint> mappedEndpoints; |
| 94 | for (auto endpoint : descriptor.endpoints) { |
| 95 | tgvoip::Endpoint::Type mappedType; |
| 96 | switch (endpoint.type) { |
| 97 | case EndpointType::UdpRelay: |
| 98 | mappedType = tgvoip::Endpoint::Type::UDP_RELAY; |
| 99 | break; |
| 100 | case EndpointType::Lan: |
| 101 | mappedType = tgvoip::Endpoint::Type::UDP_P2P_LAN; |
| 102 | break; |
| 103 | case EndpointType::Inet: |
| 104 | mappedType = tgvoip::Endpoint::Type::UDP_P2P_INET; |
| 105 | break; |
| 106 | case EndpointType::TcpRelay: |
| 107 | mappedType = tgvoip::Endpoint::Type::TCP_RELAY; |
| 108 | break; |
| 109 | default: |
| 110 | mappedType = tgvoip::Endpoint::Type::UDP_RELAY; |
| 111 | break; |
| 112 | } |
| 113 |
| 114 | tgvoip::IPv4Address address(endpoint.host.ipv4); |
| 115 | tgvoip::IPv6Address addressv6(endpoint.host.ipv6); |
| 116 |
| 117 | mappedEndpoints.emplace_back(endpoint.endpointId, endpoint.port, address, addressv6, mappedType, endpoint.peerTag); |
| 118 | } |
| 119 |
| 120 | const auto mappedDataSaving = [&] { |
| 121 | switch (descriptor.config.dataSaving) { |
| 122 | case DataSaving::Mobile: |
| 123 | return tgvoip::DATA_SAVING_MOBILE; |
| 124 | case DataSaving::Always: |
| 125 | return tgvoip::DATA_SAVING_ALWAYS; |
| 126 | default: |
| 127 | return tgvoip::DATA_SAVING_NEVER; |
| 128 | } |
| 129 | }(); |
| 130 |
| 131 | tgvoip::VoIPController::Config mappedConfig( |
| 132 | descriptor.config.initializationTimeout, |
| 133 | descriptor.config.receiveTimeout, |
| 134 | mappedDataSaving, |
| 135 | descriptor.config.enableAEC, |
| 136 | descriptor.config.enableNS, |
| 137 | descriptor.config.enableAGC, |
| 138 | descriptor.config.enableCallUpgrade |
| 139 | ); |
| 140 | mappedConfig.enableVolumeControl = descriptor.config.enableVolumeControl; |
| 141 | mappedConfig.logFilePath = descriptor.config.logPath.data; |
| 142 | mappedConfig.statsDumpFilePath = {}; |
| 143 |
| 144 | controller_->SetConfig(mappedConfig); |
| 145 |
| 146 | setNetworkType(descriptor.initialNetworkType); |
| 147 |
| 148 | controller_->SetEncryptionKey((char *)(descriptor.encryptionKey.value->data()), descriptor.encryptionKey.isOutgoing); |
| 149 | controller_->SetRemoteEndpoints(mappedEndpoints, descriptor.config.enableP2P, descriptor.config.maxApiLayer); |
| 150 |
| 151 | controller_->Start(); |
| 152 |
| 153 | controller_->Connect(); |
| 154 |
| 155 | controller_->SetCurrentAudioInput(descriptor.mediaDevicesConfig.audioInputId); |
| 156 | controller_->SetCurrentAudioOutput(descriptor.mediaDevicesConfig.audioOutputId); |
| 157 | controller_->SetInputVolume(descriptor.mediaDevicesConfig.inputVolume); |
| 158 | controller_->SetOutputVolume(descriptor.mediaDevicesConfig.outputVolume); |
| 159 | } |
| 160 |
| 161 | InstanceImplLegacy::~InstanceImplLegacy() { |
| 162 | if (controller_) { |
| 163 | stop([](FinalState state){}); |
| 164 | } |
| 165 | } |
| 166 |
| 167 | void InstanceImplLegacy::setNetworkType(NetworkType networkType) { |
| 168 | const auto mappedType = [&] { |
| 169 | switch (networkType) { |
| 170 | case NetworkType::Unknown: |
| 171 | return tgvoip::NET_TYPE_UNKNOWN; |
| 172 | case NetworkType::Gprs: |
| 173 | return tgvoip::NET_TYPE_GPRS; |
| 174 | case NetworkType::Edge: |
| 175 | return tgvoip::NET_TYPE_EDGE; |
| 176 | case NetworkType::ThirdGeneration: |
| 177 | return tgvoip::NET_TYPE_3G; |
| 178 | case NetworkType::Hspa: |
| 179 | return tgvoip::NET_TYPE_HSPA; |
| 180 | case NetworkType::Lte: |
| 181 | return tgvoip::NET_TYPE_LTE; |
| 182 | case NetworkType::WiFi: |
| 183 | return tgvoip::NET_TYPE_WIFI; |
| 184 | case NetworkType::Ethernet: |
| 185 | return tgvoip::NET_TYPE_ETHERNET; |
| 186 | case NetworkType::OtherHighSpeed: |
| 187 | return tgvoip::NET_TYPE_OTHER_HIGH_SPEED; |
| 188 | case NetworkType::OtherLowSpeed: |
| 189 | return tgvoip::NET_TYPE_OTHER_LOW_SPEED; |
| 190 | case NetworkType::OtherMobile: |
| 191 | return tgvoip::NET_TYPE_OTHER_MOBILE; |
| 192 | case NetworkType::Dialup: |
| 193 | return tgvoip::NET_TYPE_DIALUP; |
| 194 | default: |
| 195 | return tgvoip::NET_TYPE_UNKNOWN; |
| 196 | } |
| 197 | }(); |
| 198 |
| 199 | controller_->SetNetworkType(mappedType); |
| 200 | } |
| 201 |
| 202 | void InstanceImplLegacy::setMuteMicrophone(bool muteMicrophone) { |
| 203 | controller_->SetMicMute(muteMicrophone); |
| 204 | } |
| 205 |
| 206 | void InstanceImplLegacy::receiveSignalingData(const std::vector<uint8_t> &data) { |
| 207 | } |
| 208 |
| 209 | void InstanceImplLegacy::setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 210 | } |
| 211 |
| 212 | void InstanceImplLegacy::sendVideoDeviceUpdated() { |
| 213 | } |
| 214 |
| 215 | void InstanceImplLegacy::setRequestedVideoAspect(float aspect) { |
| 216 | } |
| 217 |
| 218 | void InstanceImplLegacy::setIncomingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 219 | } |
| 220 |
| 221 | void InstanceImplLegacy::setAudioOutputGainControlEnabled(bool enabled) { |
| 222 | controller_->SetAudioOutputGainControlEnabled(enabled); |
| 223 | } |
| 224 |
| 225 | void InstanceImplLegacy::setEchoCancellationStrength(int strength) { |
| 226 | controller_->SetEchoCancellationStrength(strength); |
| 227 | } |
| 228 |
| 229 | void InstanceImplLegacy::setAudioInputDevice(std::string id) { |
| 230 | controller_->SetCurrentAudioInput(id); |
| 231 | } |
| 232 |
| 233 | void InstanceImplLegacy::setAudioOutputDevice(std::string id) { |
| 234 | controller_->SetCurrentAudioOutput(id); |
| 235 | } |
| 236 |
| 237 | void InstanceImplLegacy::setInputVolume(float level) { |
| 238 | controller_->SetInputVolume(level); |
| 239 | } |
| 240 |
| 241 | void InstanceImplLegacy::setOutputVolume(float level) { |
| 242 | controller_->SetOutputVolume(level); |
| 243 | } |
| 244 |
| 245 | void InstanceImplLegacy::setAudioOutputDuckingEnabled(bool enabled) { |
| 246 | #if defined(__APPLE__) && TARGET_OS_OSX |
| 247 | controller_->SetAudioOutputDuckingEnabled(enabled); |
| 248 | #endif // TARGET_OS_OSX |
| 249 | } |
| 250 |
| 251 | void InstanceImplLegacy::setIsLowBatteryLevel(bool isLowBatteryLevel) { |
| 252 | } |
| 253 |
| 254 | std::string InstanceImplLegacy::getLastError() { |
| 255 | switch (controller_->GetLastError()) { |
| 256 | case tgvoip::ERROR_INCOMPATIBLE: return "ERROR_INCOMPATIBLE"; |
| 257 | case tgvoip::ERROR_TIMEOUT: return "ERROR_TIMEOUT"; |
| 258 | case tgvoip::ERROR_AUDIO_IO: return "ERROR_AUDIO_IO"; |
| 259 | case tgvoip::ERROR_PROXY: return "ERROR_PROXY"; |
| 260 | default: return "ERROR_UNKNOWN"; |
| 261 | } |
| 262 | } |
| 263 |
| 264 | std::string InstanceImplLegacy::getDebugInfo() { |
| 265 | return controller_->GetDebugString(); |
| 266 | } |
| 267 |
| 268 | int64_t InstanceImplLegacy::getPreferredRelayId() { |
| 269 | return controller_->GetPreferredRelayID(); |
| 270 | } |
| 271 |
| 272 | TrafficStats InstanceImplLegacy::getTrafficStats() { |
| 273 | tgvoip::VoIPController::TrafficStats stats; |
| 274 | controller_->GetStats(&stats); |
| 275 | auto result = TrafficStats(); |
| 276 | result.bytesSentWifi = stats.bytesSentWifi; |
| 277 | result.bytesReceivedWifi = stats.bytesRecvdWifi; |
| 278 | result.bytesSentMobile = stats.bytesSentMobile; |
| 279 | result.bytesReceivedMobile = stats.bytesRecvdMobile; |
| 280 | return result; |
| 281 | } |
| 282 |
| 283 | PersistentState InstanceImplLegacy::getPersistentState() { |
| 284 | return {controller_->GetPersistentState()}; |
| 285 | } |
| 286 |
| 287 | void InstanceImplLegacy::stop(std::function<void(FinalState)> completion) { |
| 288 | controller_->Stop(); |
| 289 |
| 290 | auto result = FinalState(); |
| 291 | result.persistentState = getPersistentState(); |
| 292 | result.debugLog = controller_->GetDebugLog(); |
| 293 | result.trafficStats = getTrafficStats(); |
| 294 | result.isRatingSuggested = controller_->NeedRate(); |
| 295 |
| 296 | delete controller_; |
| 297 | controller_ = nullptr; |
| 298 |
| 299 | completion(result); |
| 300 | } |
| 301 |
| 302 | void InstanceImplLegacy::ControllerStateCallback(tgvoip::VoIPController *controller, int state) { |
| 303 | const auto self = static_cast<InstanceImplLegacy*>(controller->implData); |
| 304 | if (self->onStateUpdated_) { |
| 305 | const auto mappedState = [&] { |
| 306 | switch (state) { |
| 307 | case tgvoip::STATE_WAIT_INIT: |
| 308 | return State::WaitInit; |
| 309 | case tgvoip::STATE_WAIT_INIT_ACK: |
| 310 | return State::WaitInitAck; |
| 311 | case tgvoip::STATE_ESTABLISHED: |
| 312 | return State::Established; |
| 313 | case tgvoip::STATE_FAILED: |
| 314 | return State::Failed; |
| 315 | case tgvoip::STATE_RECONNECTING: |
| 316 | return State::Reconnecting; |
| 317 | default: |
| 318 | return State::Established; |
| 319 | } |
| 320 | }(); |
| 321 |
| 322 | self->onStateUpdated_(mappedState); |
| 323 | } |
| 324 | } |
| 325 |
| 326 | void InstanceImplLegacy::SignalBarsCallback(tgvoip::VoIPController *controller, int signalBars) { |
| 327 | const auto self = static_cast<InstanceImplLegacy*>(controller->implData); |
| 328 | if (self->onSignalBarsUpdated_) { |
| 329 | self->onSignalBarsUpdated_(signalBars); |
| 330 | } |
| 331 | } |
| 332 |
| 333 | int InstanceImplLegacy::GetConnectionMaxLayer() { |
| 334 | return tgvoip::VoIPController::GetConnectionMaxLayer(); |
| 335 | } |
| 336 |
| 337 | std::vector<std::string> InstanceImplLegacy::GetVersions() { |
| 338 | std::vector<std::string> result; |
| 339 | result.push_back("2.4.4"); |
| 340 | return result; |
| 341 | } |
| 342 |
| 343 | template <> |
| 344 | bool Register<InstanceImplLegacy>() { |
| 345 | return Meta::RegisterOne<InstanceImplLegacy>(); |
| 346 | } |
| 347 |
| 348 | void SetLegacyGlobalServerConfig(const std::string &serverConfig) { |
| 349 | tgvoip::ServerConfig::GetSharedInstance()->Update(serverConfig); |
| 350 | } |
| 351 |
| 352 | } // namespace tgcalls |
| 353 |