数据包已发送但无法接收到数据包

2024-01-22

我一直在编辑安卓toyvpn https://android.googlesource.com/platform/development/+/master/samples/ToyVpnVPN 的示例项目,我为我的示例应用程序找到了这个

我知道我的代码有问题/缺失,因为当我通过 android 设置手动设置 VPN 时,有数据包接收,这就是为什么 我一直在寻找如何接收数据包,但我不知道如何让它工作。

这是我的源代码扩展 VpnService 的 VCL

import android.app.PendingIntent;
import android.net.VpnService;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

/**
 * Created by Jameshwart Lopez on 8/18/15.
 */
public class VCL extends VpnService {

    private static final String TAG = "VpnClientLibrary";
    private Thread mThread;
    private ParcelFileDescriptor mInterface;

    private String mServerAddress;
    private String mServerPort;
    private PendingIntent mConfigureIntent;
    private String mParameters;

    //a. Configure a builder for the interface.
    Builder builder = new Builder();

    public  void vclRun(){
        try {
            //a. Configure the TUN and get the interface.
            mInterface = builder.setSession("thesessionname")
                    .addAddress("192.168.0.1",24)
                    .addDnsServer("8.8.8.8")
                    .addRoute("0.0.0.0", 0).establish();
            //b. Packets to be sent are queued in this input stream.
            FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());

            //b. Packets received need to be written to this output stream.
            FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());

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

            //c. The UDP channel can be used to pass/get ip package to/from server
            DatagramChannel tunnel = DatagramChannel.open();

            // Connect to the server, localhost is used for demonstration only.
            mServerAddress="";//some of the vpn ip address here
            mServerPort="1723";
            InetSocketAddress server = new InetSocketAddress(mServerAddress, Integer.parseInt(mServerPort) );
            tunnel.connect(server);

            // For simplicity, we use the same thread for both reading and
            // writing. Here we put the tunnel into non-blocking mode.
            tunnel.configureBlocking(false);


            // Authenticate and configure the virtual network interface.
            handshake(tunnel);

            //d. Protect this socket, so package send by it will not be feedback to the vpn service.
            protect(tunnel.socket());

            int timer = 0;
            //e. Use a loop to pass packets.
            while (true) {
                //get packet with in
                //put packet to tunnel
                //get packet form tunnel
                //return packet with out
                //sleep is a must

                // Assume that we did not make any progress in this iteration.
                boolean idle = true;

                // Read the outgoing packet from the input stream.
                int length = in.read(packet.array());
                if (length > 0) {
                    // Write the outgoing packet to the tunnel.
                    packet.limit(length);
                    tunnel.write(packet);
                    packet.clear();

                    // There might be more outgoing packets.
                    idle = false;

                    // If we were receiving, switch to sending.
                    if (timer < 1) {
                        timer = 1;
                    }
                }

                // Read the incoming packet from the tunnel.
                length = tunnel.read(packet);
                if (length > 0) {
                    // Ignore control messages, which start with zero.
                    if (packet.get(0) != 0) {
                        // Write the incoming packet to the output stream.
                        out.write(packet.array(), 0, length);
                    }
                    packet.clear();

                    // There might be more incoming packets.
                    idle = false;

                    // If we were sending, switch to receiving.
                    if (timer > 0) {
                        timer = 0;
                    }
                }

                // If we are idle or waiting for the network, sleep for a
                // fraction of time to avoid busy looping.
                if (idle) {
                    Thread.sleep(100);

                    // Increase the timer. This is inaccurate but good enough,
                    // since everything is operated in non-blocking mode.
                    timer += (timer > 0) ? 100 : -100;

                    // We are receiving for a long time but not sending.
                    if (timer < -15000) {
                        // Send empty control messages.
                        packet.put((byte) 0).limit(1);
                        for (int i = 0; i < 3; ++i) {
                            packet.position(0);
                            tunnel.write(packet);
                        }
                        packet.clear();

                        // Switch to sending.
                        timer = 1;
                    }

                    // We are sending for a long time but not receiving.
                    //if (timer > 20000) {
                    //    throw new IllegalStateException("Timed out");
                    //}
                }


            }
        } catch (Exception e) {
            // Catch any exception
            e.printStackTrace();
        } finally {
            try {
                if (mInterface != null) {
                    mInterface.close();
                    mInterface = null;
                }
            } catch (Exception e) {
            }
        }
    }

    private void handshake(DatagramChannel tunnel) throws Exception {
        // To build a secured tunnel, we should perform mutual authentication
        // and exchange session keys for encryption. To keep things simple in
        // this demo, we just send the shared secret in plaintext and wait
        // for the server to send the parameters.
        // Allocate the buffer for handshaking.
        ByteBuffer packet = ByteBuffer.allocate(1024);

        // Control messages always start with zero.
        String password = "";//vpn password here
        packet.put((byte) 0).put(password.getBytes()).flip();

        // Send the secret several times in case of packet loss.
        for (int i = 0; i < 3; ++i) {
            Log.e("packetsdata", packet.toString());
            packet.position(0);
            tunnel.write(packet);
        }
        packet.clear();

        // Wait for the parameters within a limited time.
        for (int i = 0; i < 50; ++i) {
            Thread.sleep(100);

            // Normally we should not receive random packets.
            int length = tunnel.read(packet);
            if (length > 0 && packet.get(0) == 0) {
                configure(new String(packet.array(), 1, length - 1).trim());
                return;
            }
        }
        //throw new IllegalStateException("Timed out");
    }
    private void configure(String parameters) throws Exception {
        // 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();
        for (String parameter : parameters.split(" ")) {
            String[] fields = parameter.split(",");
            try {
                switch (fields[0].charAt(0)) {
                    case 'm':
                        builder.setMtu(Short.parseShort(fields[1]));
                        break;
                    case 'a':
                        builder.addAddress(fields[1], Integer.parseInt(fields[2]));
                        break;
                    case 'r':
                        builder.addRoute(fields[1], Integer.parseInt(fields[2]));
                        break;
                    case 'd':
                        builder.addDnsServer(fields[1]);
                        break;
                    case 's':
                        builder.addSearchDomain(fields[1]);
                        break;
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("Bad parameter: " + parameter);
            }
        }

        // Close the old interface since the parameters have been changed.
        try {
            mInterface.close();
        } catch (Exception e) {
            // ignore
        }

        // Create a new interface using the builder and save the parameters.
        mInterface = builder.setSession(mServerAddress)
                .setConfigureIntent(mConfigureIntent)
                .establish();
        mParameters = parameters;
        Log.i(TAG, "New interface: " + parameters);
    }
}

这就是我使用上面的类的方式

private Thread mThread;

    /*
    * Services interface
    * */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Start a new session by creating a new thread.
        mThread = new Thread(this, "VpnRunnable");
        //start the service
        mThread.start();
        /*
         *service is left "started" and will later be restarted by the system
         * http://android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html
         */
        return START_STICKY;
    }
    @Override
    public void onDestroy() {

        if (mThread != null) {
            mThread.interrupt();
        }
        super.onDestroy();
    }


    @Override
    public synchronized void run() {
        /*
        * to run the vpn interface call the vclRun method inside VCL class
        * */
       this.vclRun();
    }

首先,检查是否有字节发送到您的 Android 设备。因为如果没有任何内容可接收,它就不会读取任何内容。

然后看看这个,因为它可能会扰乱你的连接。

您需要将其包含在onStartCommand:

// 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();
}
// Extract information from the intent.
String prefix = getPackageName();
mServerAddress = intent.getStringExtra(prefix + ".ADDRESS");
mServerPort = intent.getStringExtra(prefix + ".PORT");
mSharedSecret = intent.getStringExtra(prefix + ".SECRET").getBytes();
// Start a new session by creating a new thread.
mThread = new Thread(this, "ToyVpnThread");
mThread.start();
return START_STICKY;

还有详细信息(如下所示)sychronized void.

@Override
public synchronized void run() {
    try {
        Log.i(TAG, "Starting");


        // If anything needs to be obtained using the network, get it now.
        // This greatly reduces the complexity of seamless handover, which
        // tries to recreate the tunnel without shutting down everything.
        // In this demo, all we need to know is the server address.


        InetSocketAddress server = new InetSocketAddress(
                mServerAddress, Integer.parseInt(mServerPort));
        // We try to create the tunnel for several times. The better way
        // is to work with ConnectivityManager, such as trying only when
        // the network is avaiable. Here we just use a counter to keep
        // things simple.
        for (int attempt = 0; attempt < 10; ++attempt) {
            mHandler.sendEmptyMessage(R.string.connecting);
            // Reset the counter if we were connected.

            // See BELOW 
            if (run(server)) { 
                attempt = 0;
            }

            // Sleep for a while. This also checks if we got interrupted.
            Thread.sleep(3000);
        } /..../

您没有很好地管理线程操作。建议在尝试运行之前接收需要接收的任何字节。不这样做可能会导致问题。
我会回顾一下你的代码,然后把你取出的东西放进去。 我还建议您在这里更改代码:

packet.put((byte) 0).put(password.getBytes()).flip();

尝试使用显式编码:

packet.put((byte) 0).put(password.getBytes("UTF-8")).flip();

因为如果没有它,数据可能会丢失。看这个答案:
https://stackoverflow.com/a/7947911/3956566 https://stackoverflow.com/a/7947911/3956566

我已经检查过,您的项目正在使用“UTF-8”。

如果这没有帮助,请告诉我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数据包已发送但无法接收到数据包 的相关文章

  • 增加android中的网格间距

    我有一个网格视图 其中三列中有很多项目 我想增加它们之间的间距 我怎样才能在安卓中做到这一点 您可以使用android verticalSpacing and android horizontalSpacing在 GridView 标记中并
  • 安卓卸载通知

    当用户卸载应用程序时有什么方法可以发送通知 如果用户卸载该应用程序 应用程序所有者应该收到以下用户已卸载该应用程序的通知 是否可以 应用程序本身不可能 除非有来自同一作者的其他应用程序可以检查 但我认为有一个可用但复杂的解决方法 应用程序可
  • 前台服务不持续运行

    在我的应用程序中 我使用必须不断运行的前台服务 有时前台服务会停止 在什么情况下操作系统会终止我的服务 即使有足够的内存 电池已充满 手机正在充电也会发生 到目前为止 我的代码是这样的 public class ServiceTest ex
  • 使用 BroadcastListener 通话结束后从 Calllog 中获取通话持续时间

    我需要尝试在通话结束后获取通话持续时间 我有一个广播侦听器 它使用电话管理器跟踪电话状态 即 摘机 空闲 等 使用此接收器 我可以了解拨出呼叫何时完成 通话完成后 我启动一项服务来获取上次通话的通话持续时间 但是 我得到的值是前一个调用的值
  • 动态添加导航抽屉中的项目

    我创建了抽屉 但是我想动态设置抽屉的项目列表 意味着从数据库获取数据并设置为抽屉列表 是否可以 是的 比如何 我也知道静态抽屉 尝试这个 final Menu menu navigationView getMenu for int i 1
  • Java-Android 上的 MulticastSocket 问题

    我开始使用 MulticastSocket 进行编码 尝试制作一个带有客户端和服务器的简单应用程序来发送消息 我的服务器代码 import java io IOException import java net DatagramPacket
  • 如何在android中播放音频文件

    我的 Android 手机中有一个 mp3 文件 让它在我的 SD 卡中的某个位置成为 xyz mp3 如何通过我的应用程序播放它 只需您就可以使用MediaPlayer并播放音频文件 查看这个很好的例子 http www helloand
  • 为什么线性布局 maxHeight 不起作用?

    这是我的布局 我在线性布局上设置了最大和最小高度 但最大高度似乎不起作用 事实上如果TextView R id testo有很多文本 不会被修剪 如果我设置固定高度 则不会发生这种情况 但我不想设置固定高度 以便在选择溢出屏幕模式时正确调整
  • 尝试将 AndroidX 迁移到 Android AppCompat

    从 AndroidX 迁移到 Appcompat 时出现错误 这是日志构建 Manifest merger failed Attribute application appComponentFactory value android sup
  • 了解 Android 上的默认键盘

    我想知道 Android 中用户选择的默认键盘 我知道我可以使用以下命令访问启用的输入法列表InputMethodManager 但我想知道用户当前使用的是哪一个 到目前为止 我已经尝试获取当前的输入法子类型 InputMethodMana
  • 将 .cer 格式的证书添加到 .bks 密钥库中

    我需要将 cer 格式的证书添加到 BKS 密钥存储中 SO 上对此进行了描述 如何将 cer 转换为 BKS https stackoverflow com questions 21284466 how to convert cer to
  • 浮动操作按钮未显示在 recyclerview 上(位于 DrawerLayout 内)

    我正在尝试通过 recyclerview 获取 FAB 在我的情况下 它将覆盖整个屏幕 即使 recyclerview 为空 FAB 也不会显示 以下是我的 xml 代码
  • Android 中的 Sqlite 全文搜索对非英语字符的 Unicode 支持

    滚动到末尾以跳过说明 背景 在我的 Android 应用程序中 我想使用非英语 Unicode 文本字符串来搜索存储在 SQLite 数据库中的文本文档 字段中的匹配项 我了解到 所以我认为 我需要做的是实施一个使用 fts3 fts4 进
  • 如何从github项目获取jar? [复制]

    这个问题在这里已经有答案了 我想使用官方网站上的 kSoap2 android 库http simpligility github io ksoap2 android index html http simpligility github
  • indexoutofboundException :setSpan (2...2) 结束长度超出长度 1

    I ve a MultiAutoCompleteTextView当用户按空格键时 我在其中创建芯片文本的自定义控件 我不希望用户在文本框为空时最初输入空格 所以我放了一个inputFilter以防止用户最初放置空格 这是过滤器代码 priv
  • 共享 Google 地图或拍摄 Android 手机屏幕截图

    我正在使用 android google map api v2 开发 android 应用程序 到目前为止我已经取得了以下成绩 打开谷歌地图显示用户的位置 当他行走时 他可以在地图上添加标记 标记他经过的地方 他可以删除标记或拖动它们 我现
  • 从 Android 应用程序调用 Google 地图应用程序以获取行车方向

    我需要使用外部谷歌地图应用程序显示行车方向我找到了这个链接http developer android com guide appendix g app intents html http developer android com gui
  • 如何展开和折叠列表视图中的项目

    我对安卓还很陌生 我想实现一个列表视图 它包含一些列表项 当单击它们时 它们应该展开以显示更多信息 但我找不到办法做到这一点 这是我的activity main xml
  • 总小时数无法从 Android 插入 MySQL

    我使用以下公式获得总小时数 public void updateTotalHours int a SplitTime objMyCustomBaseAdapter getFistTime int b SplitTime objMyCusto
  • 作业调度 API android L

    我正在制作一个使用 jobscheduler API 的应用程序 我想定期以及在设备充电时运行服务 这是代码 JobInfo Builder builder new JobInfo Builder kJobId mServiceCompon

随机推荐