I want to route all network traffic from android device to internal vpn service and to my desired ip and port.
i want to implement logic as same as “oxy proxy manager app” where user enters ip and port information and when pressed connect vpn profile is created in device setting->Connection->More connection settings->VPN and user gets connected to proxy(Please use Oxy Proxy Manager App for more details). I want to implement same feature.
below is my code
package com.bytesoftlab.true_vpn
import android.content.Intent
import android.net.VpnService
import android.os.ParcelFileDescriptor
import android.util.Log
import java.io.*
import java.net.InetSocketAddress
import java.net.Proxy
import java.net.Socket
class MyVpnService : VpnService() {
private var vpnInterface: ParcelFileDescriptor? = null
private val TAG = "MyVpnService"
override fun onCreate() {
super.onCreate()
Log.d(TAG, "VPN Service Created")
}
override fun onDestroy() {
super.onDestroy()
vpnInterface?.close()
vpnInterface = null
Log.d(TAG, "VPN Service Destroyed")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val proxyIp = "81.95.232.128"
val proxyPort = 3128
Log.d(TAG, "Starting VPN with proxy IP: $proxyIp and port: $proxyPort")
startVpn(proxyIp, proxyPort)
return START_STICKY
}
private fun startVpn(proxyIp: String, proxyPort: Int) {
Log.d(TAG, "Setting up VPN interface")
if (vpnInterface != null) {
Log.d(TAG, "VPN interface already exists")
return
}
val builder = Builder().apply {
addAddress("10.0.0.2", 24)
addRoute("0.0.0.0", 0)
addDnsServer("8.8.8.8")
addDnsServer("8.8.4.4")
setSession("InternalVPN")
setMtu(1500)
}
vpnInterface = builder.establish()
if (vpnInterface == null) {
Log.e(TAG, "Failed to establish VPN interface")
return
}
Log.d(TAG, "VPN Interface Established successfully")
routeTrafficThroughHttpProxy(proxyIp, proxyPort)
}
private fun routeTrafficThroughHttpProxy(proxyIp: String, proxyPort: Int) {
try {
Log.d(TAG, "Connecting to HTTP proxy $proxyIp:$proxyPort")
val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(proxyIp, proxyPort))
val socket = Socket(proxy)
socket.connect(InetSocketAddress("8.8.8.8", 80), 10000) // Test connection
Log.d(TAG, "Proxy connection established successfully")
val vpnInput: InputStream = FileInputStream(vpnInterface!!.fileDescriptor)
val vpnOutput: OutputStream = FileOutputStream(vpnInterface!!.fileDescriptor)
val proxyOutput = socket.getOutputStream()
val proxyInput = socket.getInputStream()
vpnInput.use { vpnIn ->
proxyOutput.use { proxyOut ->
val buffer = ByteArray(32767)
while (true) {
val bytesRead = vpnIn.read(buffer)
if (bytesRead == -1) break
proxyOut.write(buffer, 0, bytesRead)
}
}
}
proxyInput.use { proxyIn ->
vpnOutput.use { vpnOut ->
val buffer = ByteArray(32767)
while (true) {
val bytesRead = proxyIn.read(buffer)
if (bytesRead == -1) break
vpnOut.write(buffer, 0, bytesRead)
}
}
}
} catch (e: Exception) {
Log.e(TAG, "Error routing traffic through proxy: ${e.message}", e)
}
}
}
**and the error is **
**D/MyVpnService(13485): VPN Service Created
D/MyVpnService(13485): Starting VPN with proxy IP: 81.95.232.128 and port: 3128
D/MyVpnService(13485): Setting up VPN interface
D/MyVpnService(13485): VPN Interface Established successfully
D/MyVpnService(13485): Connecting to HTTP proxy 81.95.232.128:3128
E/MyVpnService(13485): Error routing traffic through proxy: Invalid Proxy
E/MyVpnService(13485): java.lang.IllegalArgumentException: Invalid Proxy
E/MyVpnService(13485): at java.net.Socket.(Socket.java:160)
E/MyVpnService(13485): at **com.bytesoftlab.true_vpn.MyVpnService.routeTrafficThroughHttpProxy(MyVpnService.kt:72)
E/MyVpnService(13485): at com.bytesoftlab.true_vpn.MyVpnService.startVpn(MyVpnService.kt:64)
E/MyVpnService(13485): at com.bytesoftlab.true_vpn.MyVpnService.onStartCommand(MyVpnService.kt:34)
E/MyVpnService(13485): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5268)
E/MyVpnService(13485): at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)
E/MyVpnService(13485): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2531)
E/MyVpnService(13485): at android.os.Handler.dispatchMessage(Handler.java:106)
E/MyVpnService(13485): at android.os.Looper.loopOnce(Looper.java:230)
E/MyVpnService(13485): at anyour text
droid.os.Looper.loop(Looper.java:319)
E/MyVpnService(13485): at android.app.ActivityThread.main(ActivityThread.java:8918)
E/MyVpnService(13485): at java.lang.reflect.Method.invoke(Native Method)
E/MyVpnService(13485): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
E/MyVpnService(13485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
i want to create internal vpn server on android device and route traffic through my proxy ip and port(which i bought and has ip authentication in it).
ByteSoft Lab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.