简体   繁体   English

如何通过我在 android 中的 vpn 服务应用程序记录/检查网络流量

[英]how to log/check network traffic through my vpn service app in android

I have read https://developer.android.com/guide/topics/connectivity/vpn but i have a few questions about it:我已阅读https://developer.android.com/guide/topics/connectivity/vpn但我有几个问题:

  1. am i creating a vpn client according to the code i have pasted below, if so, where is the vpn server?我是否根据我在下面粘贴的代码创建了一个 vpn 客户端,如果是,vpn 服务器在哪里?
  2. My vpn service is working(as i can see it in the settings of the emulator), how do i know if the network traffic is flowing through my vpn service我的 vpn 服务正在工作(我可以在模拟器的设置中看到它),我如何知道网络流量是否正在流经我的 vpn 服务
  3. how to log details of the network traffic?(destination adrress of the network request etc.)如何记录网络流量的详细信息?(网络请求的目标地址等)

Here is the code:-这是代码: -

public class vpnService extends VpnService {
public vpnService() {
}
private Thread mThread;
private ParcelFileDescriptor mInterface;
Builder builder=new Builder();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mThread=new Thread(new Runnable(){

        @Override
        public void run() {
            try{
                mInterface=builder.setSession("vpnService")
                        .addAddress("192.168.0.1",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());
                DatagramChannel tunnel=DatagramChannel.open();
                tunnel.connect(new InetSocketAddress("127.0.0.1",8087));
                protect(tunnel.socket());

                while(true){
                    Thread.sleep(100);
                }
            }

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

                }
            }
        }
    },"vpnRunnable");
    mThread.start();
    return START_STICKY;

}

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

} }

am i creating a vpn client according to the code i have pasted below, if so, where is the vpn server?我是否根据我在下面粘贴的代码创建了一个 vpn 客户端,如果是,vpn 服务器在哪里?

No, you are just instructing Android to send the network packets of the device to your VpnService不,您只是在指示 Android 将设备的网络数据包发送到您的 VpnService

how do i know if the network traffic is flowing through my vpn service我如何知道网络流量是否流经我的 vpn 服务

You are currently not sending any data.您目前没有发送任何数据。 To send data, you need to read IPv4 packets from the in FileOutputStream and write them to the tunnel channel要发送数据,您需要从in FileOutputStream中读取 IPv4 数据包并将它们写入隧道通道

how to log details of the network traffic如何记录网络流量的详细信息

After you read a packet from the in FileOutputStream , you can the pcap4j library to parse the packet and then log it to the logcat console.in FileOutputStream读取数据包后,您可以使用 pcap4j 库来解析数据包,然后将其记录到 logcat 控制台。 For example:例如:

IpV4Packet pkt = IpV4Packet.newPacket(buf, 0, len);
  Log.d("PKT", (String.format("[%s] %s -> %s [%d B]\n",
            hdr.getProtocol(),
            hdr.getSrcAddr().getHostAddress(), hdr.getDstAddr().getHostAddress(),
            pkt.length())))

You can see find a VPNService example here你可以在这里找到一个 VPNService 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM