Skip to content

Commit

Permalink
PIA-XXXX: Update connectivity logic to use tcp sockets (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-juan-docal authored Jun 26, 2024
1 parent 3d3ac58 commit 60e5a32
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.kape.vpnservicemanager.data.externals

import com.kape.vpnservicemanager.presenters.VPNServiceManagerError
import com.kape.vpnservicemanager.presenters.VPNServiceManagerErrorCode
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Socket

/*
* Copyright (c) 2022 Private Internet Access, Inc.
Expand All @@ -27,17 +26,17 @@ internal class Connectivity : IConnectivity {

companion object {
private const val PING_TIMEOUT = 3000
private const val PING_PORT = 443
}

// region IConnectivity
override suspend fun isNetworkReachable(host: String): Result<Unit> {
try {
val isReachable = InetAddress.getByName(host).isReachable(PING_TIMEOUT)
return if (isReachable) {
Result.success(Unit)
} else {
Result.failure(VPNServiceManagerError(code = VPNServiceManagerErrorCode.NETWORK_UNREACHABLE))
}
val socket = Socket()
socket.tcpNoDelay = true
socket.connect(InetSocketAddress(host, PING_PORT), PING_TIMEOUT)
socket.close()
return Result.success(Unit)
} catch (exception: IOException) {
return Result.failure(exception)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class ConnectivityTest {
fun `local host is reachable`() = runTest {
val connectivity = Connectivity()

val result = connectivity.isNetworkReachable("127.0.0.1")
val result = connectivity.isNetworkReachable("1.1.1.1")

assert(result.isSuccess)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

package com.kape.vpnprotocol.data.externals.common

import com.kape.vpnprotocol.presenters.VPNProtocolError
import com.kape.vpnprotocol.presenters.VPNProtocolErrorCode
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Socket

/*
* Copyright (c) 2022 Private Internet Access, Inc.
Expand All @@ -47,17 +46,17 @@ internal class Connectivity : IConnectivity {

companion object {
private const val PING_TIMEOUT = 3000
private const val PING_PORT = 443
}

// region IConnectivity
override suspend fun isNetworkReachable(host: String): Result<Unit> {
try {
val isReachable = InetAddress.getByName(host).isReachable(PING_TIMEOUT)
return if (isReachable) {
Result.success(Unit)
} else {
Result.failure(VPNProtocolError(code = VPNProtocolErrorCode.NETWORK_NOT_REACHABLE))
}
val socket = Socket()
socket.tcpNoDelay = true
socket.connect(InetSocketAddress(host, PING_PORT), PING_TIMEOUT)
socket.close()
return Result.success(Unit)
} catch (exception: IOException) {
return Result.failure(exception)
}
Expand Down

0 comments on commit 60e5a32

Please sign in to comment.