Reputation: 29
I am working on a wireguard VPN client for android. When I make an HTTP call from within the app using retrofit2, the call does not go through the VPN even when the VPN is working for all other apps.
Here is my code:
fun getPublicIPAddress(onSuccess: (String?) -> Unit) {
val httpClient = OkHttpClient.Builder().build()
httpClient.connectionPool.evictAll()
val retrofit = Retrofit.Builder()
.baseUrl(Constants.IPIFY_API_URI)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build()
val ipifyAPi: IpApi = retrofit.create(IpApi::class.java)
val call = ipifyAPi.getIp()
call.enqueue(object : Callback<IpResponse> {
override fun onResponse(call: Call<IpResponse>, response: Response<IpResponse>) {
if (response.isSuccessful) {
val ipResponse = response.body()
Log.i(LOG_TAG_UI, "Public IP Address: ${ipResponse?.ip}")
onSuccess(ipResponse?.ip)
} else {
Log.e(LOG_TAG_UI, "Failed to get IP address with code: ${response.code()}")
}
}
override fun onFailure(call: Call<IpResponse>, t: Throwable) {
Log.e(LOG_TAG_UI, "Failed to get IP address. Error: $t")
t.printStackTrace()
}
})
}
Device: I am testing on Samsung A31 with Android version 12.
What I have tried:
.proxy(myProxy)
to Retrofit.Builder()
is not the solutionResult: I always get my original IP as the result whether or not the VPN is running. But the same call from browser shows the IP address of the VPN server.
Expected Result: Get the VPN server's IP address as the result just like in the browser.
Upvotes: 0
Views: 122