proxy over vpn in flutter

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 textdroid.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).

New contributor

ByteSoft Lab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật