gpt4 book ai didi

java - VpnService.java中的 'protect'方法到底是做什么的

转载 作者:行者123 更新时间:2023-11-30 11:05:03 27 4
gpt4 key购买 nike

我正在浏览 ToyVpn 项目中的 VpnService.java。

我最初在运行方法中看到以下几行

 // Create a DatagramChannel as the VPN tunnel.
tunnel = DatagramChannel.open();

// Protect the tunnel before connecting to avoid loopback.
if (!protect(tunnel.socket())) {
throw new IllegalStateException("Cannot protect the tunnel");
}

我知道第一行创建了一个数据报/UDP channel 来连接到远程服务器。但我不明白“保护”方法到底在做什么。

有人可以向我解释尽可能多的技术细节。谢谢。

最佳答案

它是来自 android.net.VpnService library 的函数如您在 the source code 中所见,它是在 ToyVpnService.java 类中导入的

你可以找到它的作用here

public boolean protect (int socket)

Added in API level 14

Protect a socket from VPN connections. After protecting, data sent through this socket will go directly to the underlying network, so its traffic will not be forwarded through the VPN. This method is useful if some connections need to be kept outside of VPN. For example, a VPN tunnel should protect itself if its destination is covered by VPN routes. Otherwise its outgoing packets will be sent back to the VPN interface and cause an infinite loop. This method will fail if the application is not prepared or is revoked.

The socket is NOT closed by this method.

Returns true on success.

关于java - VpnService.java中的 'protect'方法到底是做什么的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com