InetSocketAddress getDestAddress(SocketChannel localChannel) { short portKey = (short)localChannel.Socket().Port; var session = NatSessionManager.getSession(portKey); if (session != null) { return(new InetSocketAddress(localChannel.Socket().InetAddress, session.remotePort & 0xFFFF)); } return(null); }
public static TcpTunnel wrap(SocketChannel channel, Selector selector) { TcpTunnel tunnel = new RawTcpTunnel(channel, selector); var session = NatSessionManager.getSession((short)channel.Socket().Port); if (session != null) { tunnel.isHttpsRequest = session.isHttpsSession; } return(tunnel); }
public void connect(InetSocketAddress destAddress) { if (VpnServiceHelper.protect(mInnerChannel.Socket())) { mDestAddress = destAddress; mInnerChannel.Register(mSelector, Operations.Connect, this); mInnerChannel.Connect(mServerEP); Debug.WriteLine($"Connecting to {mServerEP}"); } else { throw new Exception("VPN protect socket failed."); } }
private void InitializeConnection(Java.Lang.String ipAndPort, InetAddress destinationAddress, int destinationPort, Packet currentPacket, TCPHeader tcpHeader, ByteBuffer responseBuffer) { currentPacket.SwapSourceAndDestination(); if (tcpHeader.isSYN()) { SocketChannel outputChannel = SocketChannel.Open(); outputChannel.ConfigureBlocking(false); vpnService.Protect(outputChannel.Socket()); TCB tcb = new TCB(ipAndPort, random.NextInt(Short.MaxValue + 1), tcpHeader.sequenceNumber, tcpHeader.sequenceNumber + 1, tcpHeader.acknowledgementNumber, outputChannel, currentPacket); TCB.PutTCB(ipAndPort, tcb); try { outputChannel.Connect(new InetSocketAddress(destinationAddress, destinationPort)); if (outputChannel.FinishConnect()) { tcb.status = TCB.TCBStatus.SYN_RECEIVED; // TODO: Set MSS for receiving larger packets from the device currentPacket.updateTCPBuffer(responseBuffer, (byte)(TCPHeader.SYN | TCPHeader.ACK), tcb.mySequenceNum, tcb.myAcknowledgementNum, 0); tcb.mySequenceNum++; // SYN counts as a byte } else { tcb.status = TCB.TCBStatus.SYN_SENT; selector.Wakeup(); tcb.selectionKey = outputChannel.Register(selector, SelectionKey.OpConnect, tcb); return; } } catch (IOException e) { Log.Error(TAG, "Connection error: " + ipAndPort, e); currentPacket.updateTCPBuffer(responseBuffer, (byte)TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0); TCB.CloseTCB(tcb); } } else { currentPacket.updateTCPBuffer(responseBuffer, (byte)TCPHeader.RST, 0, tcpHeader.sequenceNumber + 1, 0); } outputQueue.Offer(responseBuffer); }