I am writing a VPN client for Android. There is a configured server on the hosting that has the firewall disabled. I connect to the server through the browser via the 3x-ui panel. And when you try to connect through my client, this error is displayed. The official documentation does not provide any clues. Can you tell me how to find the necessary information to correctly connect my application to my VPN server?
Error message
java.SocketTimeoutException: failed to connect to /95.181.175.221 (port 47644) from /192.168.232.2 (port 53116) after 3000ms
Code
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!")
}