我想创建app,重新路由数据包。我使用vpnservice(因为不需要root手机),使用toyVpn和这段代码 http://www.thegeekstuff.com/2014/06/Android-vpn-service/ 但问题出在这一行DatagramChannel tunnel = DatagramChannel.open(); 这行抛出异常。 这是我的代码
public class MyVpnService extends VpnService { public static String CONNECTION = "StartService"; public static String DISCONNECTION = "StopService"; public static boolean work = false; private Thread mThread; private ParcelFileDescriptor mInterface; Builder builder = new Builder(); // Services interface @Override public int onStartCommand(Intent intent, int flags, int startId) { // Start a new session by creating a new thread. if (intent != null && DISCONNECTION.equals(intent.getAction())) { work = false; stopForeground(true); mThread.interrupt(); stopSelf(); return START_NOT_STICKY; } work = true; mThread = new Thread(new Runnable() { @Override public void run() { try { Log.d("VPNservice", "0"); mInterface = builder.setSession("MyVPNService") .addAddress("192.168.1.3", 24) .addDnsServer("8.8.8.8") .addRoute("0.0.0.0", 0).establish(); FileInputStream in = new FileInputStream( mInterface.getFileDescriptor()); FileOutputStream out = new FileOutputStream( mInterface.getFileDescriptor()); Log.d("VPNservice", "1 " + (mInterface != null)); DatagramChannel tunnel = DatagramChannel.open(); Log.d("VPNservice", "2 " + (tunnel != null)); Log.d("VPNservice", "3 " + tunnel.connect(new InetSocketAddress("127.0.0.1", 8087))); //tunnel.connect(new InetSocketAddress("127.0.0.1", 8087)); tunnel.configureBlocking(false); protect(tunnel.socket()); //e. Use a loop to pass packets. ByteBuffer packet = ByteBuffer.allocate(Short.MAX_VALUE); Log.d("VPNservice", "4"); while (!Thread.interrupted()) { Log.d("VPNservice", "5"); int length = in.read(packet.array()); if (length > 0) { packet.limit(length); tunnel.write(packet); // There might be more outgoing packets. Log.d("VPNservice", "6"); } Log.d("VPNservice", "7"); length = tunnel.read(packet); Log.d("VPNservice", "8"); if (length > 0) { if (packet.get(0) != 0) { out.write(packet.array(), 0, length); Log.d("VPNservice", "9"); } packet.clear(); Log.d("VPNservice", "10"); } Thread.sleep(100); Log.d("VPNservice", "11"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (mInterface != null) { mInterface.close(); mInterface = null; } } catch (Exception e) { } } } }, "MyVpnRunnable"); //start the service mThread.start(); return START_STICKY; } @Override public void onDestroy() { // TODO Auto-generated method stub if (mThread != null) { mThread.interrupt(); } super.onDestroy(); } }
这是日志:
07-02 15:13:09.271 8853-8853/app.my.myvpnservice D/ActivityThread: SVC-Calling onStartCommand: app.my.myvpnservice.MyVpnService@424f59f0, flags=0, startId=1
07-02 15:13:09.272 8853-8853/app.my.myvpnservice D/dalvikvm: create interp thread : stack size=128KB
07-02 15:13:09.272 8853-8853/app.my.myvpnservice D/dalvikvm: create new thread
07-02 15:13:09.274 8853-8853/app.my.myvpnservice D/dalvikvm: new thread created
07-02 15:13:09.274 8853-8853/app.my.myvpnservice D/dalvikvm: update thread list
07-02 15:13:09.274 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: interp stack at 0x7d83d000
07-02 15:13:09.274 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: created from interp
07-02 15:13:09.274 8853-8853/app.my.myvpnservice D/dalvikvm: start new thread
07-02 15:13:09.274 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: notify debugger
07-02 15:13:09.274 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11 (MyVpnRunnable): calling run()
07-02 15:13:09.275 8853-8853/app.my.myvpnservice D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@424ef338 startId=1 args=Intent { act=StartService cmp=app.my.myvpnservice/.MyVpnService }}
07-02 15:13:09.275 8853-9304/app.my.myvpnservice D/VPNservice: 0
07-02 15:13:09.306 8853-8853/app.my.myvpnservice D/ActivityThread: SVC-BIND_SERVICE handled : 0 / BindServiceData{token=android.os.BinderProxy@424ef338 intent=Intent { act=android.net.VpnService cmp=app.my.myvpnservice/.MyVpnService }}
07-02 15:13:09.447 8853-9304/app.my.myvpnservice D/VPNservice: 1 true
07-02 15:13:09.471 8853-9304/app.my.myvpnservice W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err: at libcore.io.IoBridge.socket(IoBridge.java:622)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err: at java.nio.DatagramChannelImpl.<init>(DatagramChannelImpl.java:75)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err: at java.nio.SelectorProviderImpl.openDatagramChannel(SelectorProviderImpl.java:33)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err: at java.nio.channels.DatagramChannel.open(DatagramChannel.java:66)
07-02 15:13:09.481 8853-9304/app.my.myvpnservice W/System.err: at app.my.myvpnservice.MyVpnService$1.run(MyVpnService.java:50)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err: at java.lang.Thread.run(Thread.java:848)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err: Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err: at libcore.io.Posix.socket(Native Method)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err: at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err: at libcore.io.IoBridge.socket(IoBridge.java:607)
07-02 15:13:09.482 8853-9304/app.my.myvpnservice W/System.err: ... 5 more
07-02 15:13:09.536 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: exiting
07-02 15:13:09.536 8853-9304/app.my.myvpnservice D/dalvikvm: threadid=11: bye!
07-02 15:13:09.583 8853-8853/app.my.myvpnservice D/ActivityThread: SVC-UNBIND_SERVICE handled : 0 / BindServiceData{token=android.os.BinderProxy@424ef338 intent=Intent { act=android.net.VpnService cmp=app.my.myvpnservice/.MyVpnService }}
&#13;
我还有一个问题。我是否必须使用vpn服务器,或者我可以使用默认方式(隧道)支持数据包而不使用此服务吗?
答案 0 :(得分:1)
这个问题已经很老了,但是...
在您的堆栈跟踪中有以下行:
SocketException:套接字失败:EACCES(权限被拒绝)
这似乎是一个权限问题。您可以通过在您的 AndroidManifest.xml 中添加 INTERNET 权限来解决该问题(如果您已经拥有此权限并且仍然遇到此异常,只需添加像这样的 ACCESS 权限):
<manifest
..... >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
....
答案 1 :(得分:-1)
已经对您的AndroidManifest .xml文件设置了权限?像这样一个:
<service
android:name=".MyVpnService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter>
<action android:name="android.net.VpnService"/>
</intent-filter>
</service>