gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:31:40 29 4
gpt4 key购买 nike

我已经关注了这个 link并使用 VpnService 创建了 Vpn 接口(interface)。将转发路由添加为“0.0.0.0”后,所有互联网流量都会转发到 Vpn 接口(interface)。我可以读取数据包、访问协议(protocol)、目标 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();
}

最佳答案

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

关于Android VpnService - 如何转发拦截的互联网流量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21399027/

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