FoxiGram/TMessagesProj/jni/tgnet/Connection.h
instant992 8e79f2ee9c FoxiGram: Telegram client with built-in Xray VLESS proxy
Based on Nekogram. Key additions:
- Rebrand to FoxiGram (app name, APK name, applicationId com.foxigram.app)
- Embedded Xray (VLESS+Reality) proxy client via JNI libxray.so
- Bundled hidden one-tap proxies (LTE + WiFi), read-only in UI
- Auto-restore proxy on restart, rebind to active network (LTE/WiFi)
- Server credentials externalized to git-ignored XrayServers.java (+ template)
- libxray Go source included; compiled .so, keystore, google-services.json ignored
2026-06-08 16:41:07 +04:00

127 lines
3.4 KiB
C++

/*
* This is the source code of tgnet library v. 1.1
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2015-2018.
*/
#ifndef CONNECTION_H
#define CONNECTION_H
#include <pthread.h>
#include <vector>
#include <string>
#include <openssl/aes.h>
#include "ConnectionSession.h"
#include "ConnectionSocket.h"
#include "Defines.h"
#include "ChaffScheduler.h"
class Datacenter;
class Timer;
class ByteStream;
class ByteArray;
class Connection : public ConnectionSession, public ConnectionSocket {
public:
Connection(Datacenter *datacenter, ConnectionType type, int8_t num);
~Connection();
void connect();
void suspendConnection();
void suspendConnection(bool idle);
void sendData(NativeByteBuffer *buffer, bool reportAck, bool encrypted);
bool hasUsefullData();
void setHasUsefullData();
bool allowsCustomPadding();
uint32_t getConnectionToken();
ConnectionType getConnectionType();
int8_t getConnectionNum();
Datacenter *getDatacenter();
bool isSuspended();
static bool isMediaConnectionType(ConnectionType type);
protected:
void onReceivedData(NativeByteBuffer *buffer) override;
void onDisconnected(int32_t reason, int32_t error) override;
void onConnected() override;
bool hasPendingRequests() override;
void reconnect();
private:
enum TcpConnectionState {
TcpConnectionStageIdle,
TcpConnectionStageConnecting,
TcpConnectionStageReconnecting,
TcpConnectionStageConnected,
TcpConnectionStageSuspended
};
enum ProtocolType {
ProtocolTypeEF,
ProtocolTypeEE,
ProtocolTypeDD,
ProtocolTypeTLS
};
inline void encryptKeyWithSecret(uint8_t *array, uint8_t secretType);
inline std::string *getCurrentSecret(uint8_t secretType);
void onDisconnectedInternal(int32_t reason, int32_t error);
void scheduleChaffTimer();
ProtocolType currentProtocolType = ProtocolTypeEE;
TcpConnectionState connectionState = TcpConnectionStageIdle;
uint32_t connectionToken = 0;
std::string hostAddress;
std::string secret;
uint16_t hostPort;
uint16_t failedConnectionCount;
Datacenter *currentDatacenter;
uint32_t currentAddressFlags;
ConnectionType connectionType;
int8_t connectionNum;
bool firstPacketSent = false;
NativeByteBuffer *restOfTheData = nullptr;
uint32_t lastPacketLength = 0;
bool hasSomeDataSinceLastConnect = false;
bool isTryingNextPort = false;
bool wasConnected = false;
uint32_t willRetryConnectCount = 5;
Timer *reconnectTimer;
Timer *chaffTimer;
stealth::IptController chaffIpt_;
stealth::ChaffScheduler chaffScheduler_;
bool chaffEnabled_{false};
bool usefullData = false;
bool forceNextPort = false;
bool isMediaConnection = false;
bool waitForReconnectTimer = false;
bool connectionInProcess = false;
uint32_t lastReconnectTimeout = 100;
int64_t usefullDataReceiveTime;
uint32_t currentTimeout = 4;
uint32_t receivedDataAmount = 0;
uint32_t generation = 0;
uint8_t temp[64];
AES_KEY encryptKey;
uint8_t encryptIv[16];
uint32_t encryptNum;
uint8_t encryptCount[16];
AES_KEY decryptKey;
uint8_t decryptIv[16];
uint32_t decryptNum;
uint8_t decryptCount[16];
friend class ConnectionsManager;
};
#endif