import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
@SuppressWarnings("all")
public class NIOProxy extends Thread {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
Selector selector;
InetSocketAddress remote;
public static void main(String[] args) throws IOException {
String remoteAddr = "192.1.1.1";
int remotePort = 80;
int localPort = 801;
new NIOProxy(localPort, remoteAddr, remotePort).start();
}
@Override
public void run() {
while (true) {
try {
selector.select();
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
keys.remove();
if (key.isValid()) {
if (key.isAcceptable()) {
Acceptable(key);
}
if (key.isReadable()) {
ReadData(key);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public NIOProxy(int localPort, String remoteAddr, int remotePort) {
System.out.println("代理为本地端口:" + localPort + "代理" + remoteAddr + "地址的" + remotePort + "端口");
try {
selector = Selector.open();
remote = new InetSocketAddress(remoteAddr, remotePort);
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
SocketAddress sockerAddress = new InetSocketAddress(localPort);
serverSocketChannel.bind(sockerAddress);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (IOException e) {
e.printStackTrace();
}
}
private void Acceptable(SelectionKey key) {
try {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel clientChannel = serverSocketChannel.accept();
clientChannel.configureBlocking(false);
SocketChannel serverChannel = SocketChannel.open();
serverChannel.configureBlocking(false);
serverChannel.connect(remote);
serverChannel.finishConnect();
clientChannel.register(selector, SelectionKey.OP_READ, serverChannel);
serverChannel.register(selector, SelectionKey.OP_READ, clientChannel);
} catch (Exception e) {
System.out.println("连接异常");
}
}
private void ReadData(SelectionKey key) throws IOException {
SocketChannel otherChannel = (SocketChannel) key.attachment();
try {
SocketChannel socketChannel = (SocketChannel) key.channel();
int resetSocket = 0;
while (true) {
if (socketChannel.isConnected() || resetSocket > 10) {
break;
}
System.out.println("请求完成连接");
socketChannel.finishConnect();
Thread.sleep(10);
}
int reset = 0;
while (true) {
if (otherChannel.isConnected() || reset > 10) {
break;
}
System.out.println("请求完成连接");
otherChannel.finishConnect();
Thread.sleep(10);
}
readBuffer.clear();
int read = socketChannel.read(readBuffer);
if (read == -1) {
key.channel().close();
key.cancel();
otherChannel.close();
return;
}
readBuffer.flip();
otherChannel.write(readBuffer);
socketChannel.register(selector, SelectionKey.OP_READ, otherChannel);
} catch (Exception e) {
key.channel().close();
key.cancel();
otherChannel.close();
e.printStackTrace();
}
}
}