Сообщение об ошибке
Код: Выделить всё
java.net.SocketTimeoutException: failed to connect to /95.181.175.221 (port 47644) from /192.168.232.2 (port 53116) after 3000ms
Код: Выделить всё
private val serverHost = "95.181.175.221" // IP или домен VLESS сервера
private val serverPort = 47644 // Порт VLESS сервера
private fun establishVpnTunnel() {
// Настройка интерфейса VPN
val builder = Builder()
builder.setSession("XrayVPN")
.addAddress(getLocalIpAddress() ?: "10.0.0.2", 24) // Пример внутреннего IP адреса
.addRoute("0.0.0.0", 0) // Маршрут для всего трафика
vpnInterface = builder.establish()
// Установление TCP-соединения с сервером VLESS
CoroutineScope(Dispatchers.IO).launch {
socket = Socket()
socket?.connect(InetSocketAddress(serverHost, serverPort), 3000) // Таймаут 3 секунды
// Удостоверимся, что соединение установлено
if (socket?.channel?.isConnected == true) {
Log.d(TAG, "Successfully connected to server")
} else {
Log.d(TAG, "Error connected to server")
}
socket?.channel?.configureBlocking(true)
socket?.let { socket -> authenticate(socket) }
}
}
private fun authenticate(socket: Socket) {
val outputStream: OutputStream = socket.getOutputStream()
// Проверка корректности UUID
val uuidBytes = try {
UUID.fromString(userUuid).toString().replace("-", "").chunked(2).map { it.toInt(16).toByte() }.toByteArray()
} catch (e: IllegalArgumentException) {
throw IllegalArgumentException("Invalid UUID format")
}
// Формируем стартовый пакет VLESS
val buffer = ByteBuffer.allocate(24) // Стартовый пакет VLESS: 16 байт UUID + 8 байт
buffer.put(uuidBytes) // UUID пользователя
buffer.put(0x01) // Версия протокола (обычно 1)
buffer.put(ByteArray(7)) // Зарезервировано (заполняется нулями)
// Отправляем пакет на сервер
Log.d(TAG, "$buffer")
outputStream.write(buffer.array())
outputStream.flush()
// Ожидание ответа от сервера
val inputStream = socket.getInputStream()
val response = ByteArray(1)
if (inputStream.read(response) != 1 || response[0].toInt() != 0) {
Log.e(TAG, "Authentication failed or server rejected connection")
throw IllegalStateException("Authentication failed or server rejected connection")
}
Log.d(TAG, "Authentication successful!")
}

Подробнее здесь: https://stackoverflow.com/questions/793 ... vpn-server