Android VpnService - 如何转发拦截的互联网流量?

时间:2014-01-28 07:07:23

标签: android sockets networking tcp vpn

我已关注此link并使用VpnService创建了Vpn接口。在将前向路由添加为“0.0.0.0”时,所有互联网流量都被转发到Vpn接口。我可以读取数据包,访问协议,目标IP和端口。

现在我试图通过tcp套接字将数据包转发到目的地。但是套接字连接失败并且连接超时。

exception: java.net.ConnectException: failed to connect to /74.125.227.114 (port 443): connect failed: ETIMEDOUT (Connection timed out)

注意:当我尝试使用相同的ip和端口连接套接字但没有Vpn时,它会连接。无法弄清楚我哪里出错了?

粘贴以下代码:

public class VpnServiceEx extends VpnService implements Handler.Callback, Runnable {
    private static final String TAG = "VpnServiceEx";

    private Handler mHandler;
    private Thread mThread;

    private ParcelFileDescriptor mInterface;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // The handler is only used to show messages.
        if (mHandler == null) {
            mHandler = new Handler(this);
        }

        // Stop the previous session by interrupting the thread.
        if (mThread != null) {
            mThread.interrupt();
        }

        // Start a new session by creating a new thread.
        mThread = new Thread(this, "TestVpnThread");
        mThread.start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if (mThread != null) {
            mThread.interrupt();
        }
    }

    @Override
    public boolean handleMessage(Message message) {
        if (message != null) {
            Toast.makeText(this, message.what, Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    @Override
    public synchronized void run() {
        Log.i(TAG,"running vpnService");
        try {
            runVpnConnection();
        } catch (Exception e) {
            e.printStackTrace();
            //Log.e(TAG, "Got " + e.toString());
        } finally {
            try {
                mInterface.close();
            } catch (Exception e) {
                // ignore
            }
            mInterface = null;

            mHandler.sendEmptyMessage(R.string.disconnected);
            Log.i(TAG, "Exiting");
        }
    }


    private void runVpnConnection() {

        configure();

        FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());
        FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());

        // Allocate the buffer for a single packet.
        ByteBuffer packet = ByteBuffer.allocate(32767);

        Socket tcpSocket = new Socket();

        boolean ok = true;

        // We keep forwarding packets till something goes wrong.
        while (ok) {
            // Assume that we did not make any progress in this iteration.
            try {
                // Read the outgoing packet from the input stream.
                int length = in.read(packet.array());
                if (length > 0) {

                    Log.i(TAG,"packet received");
                    packet.limit(length);

                    String serverIP = getDestinationIP(packet);
                    int port = getDestinationPort(packet, getHeaderLength(packet));

                    Log.d(TAG, "destination IP: " + serverIP + " port: " + port);

                    InetAddress serverAddr = InetAddress.getByName(serverIP);
                    SocketAddress socketadd= new InetSocketAddress(serverAddr, port);

                    tcpSocket.connect(socketadd);  *****// this fails******

                    OutputStream outBuffer = tcpSocket.getOutputStream();
                    outBuffer.write(packet.array());
                    outBuffer.flush();
                    outBuffer.close();
                    packet.clear();
                    ok = false;
                }

                if (tcpSocket.isConnected()) {
                    InputStream inBuffer = tcpSocket.getInputStream();
                    byte[] bufferOutput = new byte[32767];
                    inBuffer.read(bufferOutput);
                    if (bufferOutput.length > 0) {
                        String recPacketString = new String(bufferOutput,0,bufferOutput.length,"UTF-8");
                        Log.d(TAG , "recPacketString : " + recPacketString);
                        out.write(bufferOutput);
                    }

                    inBuffer.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e(TAG,"exception: " + e.toString());
                ok = false;
            }

        }
    }


    private void configure() {
        // If the old interface has exactly the same parameters, use it!
        if (mInterface != null) {
            Log.i(TAG, "Using the previous interface");
            return;
        }

        // Configure a builder while parsing the parameters.
        Builder builder = new Builder();
        builder.setMtu(1500);
        builder.addAddress(getLocalIpAddress(), 24);
        builder.addRoute("0.0.0.0", 0);  // to intercept packets
        try {
            mInterface.close();
        } catch (Exception e) {
            // ignore
        }

        mInterface = builder.establish();
    }

2 个答案:

答案 0 :(得分:4)

我认为你需要保护套接字不被重新路由VpnService.protect(Socket),如下所述:How to use VPN in Android?

答案 1 :(得分:2)

您从FileInputStream中读取的数据包含IPHeader和TCP / UDP标头,然后将它们放入套接字,这将在其上添加另一个IPHeader和TCP / UDP标头。因此服务器端将获得包含IPHeader和TCP / UDP Header的数据,但它认为数据是Application数据。所以你无法连接服务器。