@@ -34,6 +34,31 @@ public class WfbNgVpnService extends VpnService {
3434
3535 @ Override
3636 public int onStartCommand (Intent intent , int flags , int startId ) {
37+ if (intent != null && "STOP_SERVICE" .equals (intent .getAction ())) {
38+ Log .i (TAG , "VPN Service stopping" );
39+ // Stop threads
40+ isRunning = false ;
41+
42+ if (udpToVpnThread != null ) {
43+ udpToVpnThread .interrupt ();
44+ }
45+ if (vpnToUdpThread != null ) {
46+ vpnToUdpThread .interrupt ();
47+ }
48+
49+ // Close the interface
50+ if (vpnInterface != null ) {
51+ try {
52+ vpnInterface .close ();
53+ } catch (IOException e ) {
54+ Log .e (TAG , "Failed to close VPN interface" , e );
55+ }
56+ vpnInterface = null ;
57+ }
58+ stopSelf ();
59+ return START_NOT_STICKY ;
60+ }
61+
3762 Log .i (TAG , "VPN Service started" );
3863
3964 // If already running, don't start again
@@ -98,13 +123,14 @@ private void startVpnThreads(final ParcelFileDescriptor vpnInterfacePfd) {
98123 udpToVpnThread = new Thread (new Runnable () {
99124 @ Override
100125 public void run () {
101- Log .i (TAG , "UDP → VPN thread started" );
126+ Log .i (TAG , "UDP (WFB) → VPN thread started" );
102127 byte [] buffer = new byte [4024 ];
103128
104- try (DatagramSocket socket = new DatagramSocket (new InetSocketAddress ( 8000 ) )) {
105- // Bind to local UDP port 8000 on all interfaces
129+ try (DatagramSocket socket = new DatagramSocket (null )) {
130+ // Set reuse address before binding
106131 socket .setReuseAddress (true );
107- //socket.bind(new InetSocketAddress(8000));
132+ // Bind to local UDP port 8000 on all interfaces
133+ socket .bind (new InetSocketAddress (8000 ));
108134
109135 while (isRunning ) {
110136 // Read data from UDP into buffer
@@ -134,7 +160,7 @@ public void run() {
134160 vpnToUdpThread = new Thread (new Runnable () {
135161 @ Override
136162 public void run () {
137- Log .i (TAG , "VPN → UDP thread started" );
163+ Log .i (TAG , "VPN → UDP (WFB) thread started" );
138164 byte [] buffer = new byte [1024 ];
139165
140166 try (DatagramSocket socket = new DatagramSocket ()) {
0 commit comments