package com.example.netshareecohub.routing import android.R import android.app.NotificationChannel import android.app.NotificationManager import android.content.Intent import android.content.pm.ServiceInfo import android.net.VpnService import android.os.Build import android.os.ParcelFileDescriptor import androidx.core.app.NotificationCompat import com.example.netshareecohub.data.logging.Logger import com.example.netshareecohub.dhcp.DhcpServer class NetShareVpnService : VpnService() { private val tag = "NetShareVpnService" private var vpnInterface: ParcelFileDescriptor? = null private var tunNatEngine: TunNatEngine? = null override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { val action = intent?.action ?: ACTION_START if (action == ACTION_STOP) { stopVpn() return START_NOT_STICKY } val lanIface = intent?.getStringExtra(EXTRA_LAN_IFACE) ?: "eth0" val upstreamIface = intent?.getStringExtra(EXTRA_UPSTREAM_IFACE) ?: "wlan0" val dnsServer = intent?.getStringExtra(EXTRA_DNS_SERVER) ?: "8.8.8.8" Logger.info(tag, "Starting NetShare VPN Service ($lanIface -> $upstreamIface, DNS: $dnsServer)") startForegroundNotification() try { val builder = Builder() .setMtu(1500) .addAddress("10.0.0.1", 24) .addRoute("0.0.0.0", 0) .addDnsServer(dnsServer) .setSession("NetShareEcoHub Non-Root VPN") val pfd = builder.establish() if (pfd != null) { vpnInterface = pfd val engine = TunNatEngine( fileDescriptor = pfd.fileDescriptor, dnsServer = dnsServer, vpnService = this, dhcpServer = activeDhcpServer, onStatsUpdated = { stats -> latestStats = stats } ) tunNatEngine = engine engine.start() Logger.info(tag, "VPN Interface established successfully") } else { Logger.error(tag, "Failed to establish VPN interface (pfd is null)") stopSelf() } } catch (e: Exception) { Logger.error(tag, "Error establishing VPN Service", e) stopSelf() } return START_STICKY } private fun startForegroundNotification() { val channelId = "netshare_vpn_channel" val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( channelId, "NetShare Non-Root VPN Routing", NotificationManager.IMPORTANCE_LOW ).apply { description = "Active non-root packet routing and NAT service" } notificationManager.createNotificationChannel(channel) } val notification = NotificationCompat.Builder(this, channelId) .setContentTitle("NetShare EcoHub VPN Active") .setContentText("Routing packets in Non-Root mode") .setSmallIcon(R.drawable.stat_sys_download) .setOngoing(true) .setPriority(NotificationCompat.PRIORITY_LOW) .build() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE) } else { startForeground(NOTIFICATION_ID, notification) } } private fun stopVpn() { Logger.info(tag, "Stopping NetShare VPN Service") tunNatEngine?.stop() tunNatEngine = null try { vpnInterface?.close() } catch (e: Exception) { Logger.warn(tag, "Error closing VPN ParcelFileDescriptor: ${e.message}") } vpnInterface = null stopForeground(STOP_FOREGROUND_REMOVE) stopSelf() } override fun onDestroy() { super.onDestroy() stopVpn() } companion object { const val ACTION_START = "com.example.netshareecohub.routing.START_VPN" const val ACTION_STOP = "com.example.netshareecohub.routing.STOP_VPN" const val EXTRA_LAN_IFACE = "extra_lan_iface" const val EXTRA_UPSTREAM_IFACE = "extra_upstream_iface" const val EXTRA_DNS_SERVER = "extra_dns_server" private const val NOTIFICATION_ID = 2001 @Volatile var activeDhcpServer: DhcpServer? = null @Volatile private var latestStats: NatStats = NatStats() fun getMetrics(): NatStats = latestStats } }