Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for vpn not stopping #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

private PacketsTransfer packetsTransfer;

private VpnService.Builder builder;

/* package */ NetBareThread(VpnService vpnService, NetBareConfig config) {
super("NetBare");
this.mVpnService = vpnService;
Expand All @@ -70,6 +72,13 @@ public void interrupt() {
NetBareUtils.closeQuietly(vpnDescriptor);
NetBareUtils.closeQuietly(input);
NetBareUtils.closeQuietly(output);
try {
builder.establish().close();
} catch (IOException e) {
NetBareLog.wtf(e);
}
// Notify NetBareListener that the service is stopped now.
NetBare.get().notifyServiceStopped();
}

@Override
Expand All @@ -88,10 +97,6 @@ public void run() {
// Establish VPN, it runs a while loop unless failed.
establishVpn(packetsTransfer);
}

// Notify NetBareListener that the service is stopped now.
NetBare.get().notifyServiceStopped();

}

private void establishVpn(PacketsTransfer packetsTransfer) {
Expand Down Expand Up @@ -125,6 +130,7 @@ private void establishVpn(PacketsTransfer packetsTransfer) {
} catch (PackageManager.NameNotFoundException e) {
NetBareLog.wtf(e);
}
this.builder = builder;
vpnDescriptor = builder.establish();
if (vpnDescriptor == null) {
return;
Expand Down Expand Up @@ -194,7 +200,7 @@ private void transfer(InputStream input, OutputStream output) throws IOException
transfer(buffer, input.read(buffer), output);
}

private void transfer(byte[] packet, int len, OutputStream output) {
private synchronized void transfer(byte[] packet, int len, OutputStream output) {
if (len < IpHeader.MIN_HEADER_LENGTH) {
NetBareLog.w("Ip header length < " + IpHeader.MIN_HEADER_LENGTH);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class MainActivity : AppCompatActivity(), NetBareListener {
}
// 启动NetBare服务
mNetBare.start(NetBareConfig.defaultHttpConfig(App.getInstance().getJSK(),
interceptorFactories()))
interceptorFactories()).newBuilder().addAllowedApplication("none").build())
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Expand Down