Skip to content

Commit c7936de

Browse files
authored
Stop VPN service when minimized (#70)
1 parent 46c19d9 commit c7936de

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

app/src/main/java/com/openipc/pixelpilot/VideoActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,9 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
11101110
Log.e(TAG, "VPN permission was not granted by the user.");
11111111
}
11121112
}
1113+
else {
1114+
Log.w(TAG, "onActivityResult: unknown request code " + requestCode);
1115+
}
11131116
}
11141117

11151118
public void setDefaultGsKey() {
@@ -1193,6 +1196,12 @@ protected void onPause() {
11931196
videoPlayer.stop();
11941197
videoPlayer.stopAudio();
11951198
wfbLinkManager.stopAdapters();
1199+
1200+
// Stop VPN service
1201+
Log.w(TAG, "onPause: stopping service");
1202+
Intent intent = new Intent(this, WfbNgVpnService.class);
1203+
intent.setAction("STOP_SERVICE");
1204+
startService(intent);
11961205
}
11971206

11981207
@Override
@@ -1222,6 +1231,7 @@ protected void onResume() {
12221231

12231232
osdManager.restoreOSDConfig();
12241233

1234+
startVpnService();
12251235

12261236
super.onResume();
12271237
}

app/src/main/java/com/openipc/pixelpilot/WfbNgVpnService.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)