forked from KNEMC/KLALB
KLALB2.3
This commit is contained in:
@@ -5,13 +5,18 @@ import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.Map;
|
||||
|
||||
public class SocketToSocketProxy extends Proxy {
|
||||
private SocketListener sl;
|
||||
private SocketChannelListener scl;
|
||||
private DatagramSocketListener dsl;
|
||||
private MultipurposeSocketAddress listen, cbind, defaultConnect;
|
||||
private Map<String, MultipurposeSocketAddress> detectedConnect;
|
||||
private SocketBridgeFactory defaultFactory;
|
||||
private SocketChannelBridgeFactory defaultChannelFactory;
|
||||
private Map<String, SocketBridgeFactory> detectedFactory;
|
||||
|
||||
public SocketToSocketProxy(MultipurposeSocketAddress listen, MultipurposeSocketAddress connect) throws IOException {
|
||||
this(listen, new MultipurposeSocketAddress(new InetSocketAddress(0)), connect);
|
||||
@@ -24,10 +29,28 @@ public class SocketToSocketProxy extends Proxy {
|
||||
public SocketToSocketProxy(MultipurposeSocketAddress listen, MultipurposeSocketAddress cbind,
|
||||
MultipurposeSocketAddress defaultConnect, Map<String, MultipurposeSocketAddress> detectedConnect)
|
||||
throws IOException {
|
||||
this(listen,cbind,defaultConnect,detectedConnect,new DefaultSocketBridgeFactory(),null);
|
||||
}
|
||||
|
||||
public SocketToSocketProxy(MultipurposeSocketAddress listen, MultipurposeSocketAddress cbind,
|
||||
MultipurposeSocketAddress defaultConnect, Map<String, MultipurposeSocketAddress> detectedConnect,SocketBridgeFactory defaultFactory,Map<String, SocketBridgeFactory> detectedFactory)
|
||||
throws IOException {
|
||||
this(listen, cbind, defaultConnect, detectedConnect, defaultFactory, detectedFactory, new DefaultSocketChannelBridgeFactory());
|
||||
}
|
||||
|
||||
public SocketToSocketProxy(MultipurposeSocketAddress listen, MultipurposeSocketAddress cbind,
|
||||
MultipurposeSocketAddress defaultConnect, Map<String, MultipurposeSocketAddress> detectedConnect,SocketBridgeFactory defaultFactory,Map<String, SocketBridgeFactory> detectedFactory,SocketChannelBridgeFactory defaultChannelFactory)
|
||||
throws IOException {
|
||||
this.listen = listen;
|
||||
this.cbind = cbind;
|
||||
this.defaultConnect = defaultConnect;
|
||||
this.detectedConnect = detectedConnect;
|
||||
|
||||
this.defaultFactory=defaultFactory;
|
||||
this.detectedFactory=detectedFactory;
|
||||
|
||||
this.defaultChannelFactory=defaultChannelFactory;
|
||||
|
||||
open();
|
||||
}
|
||||
|
||||
@@ -38,31 +61,67 @@ public class SocketToSocketProxy extends Proxy {
|
||||
|
||||
private void open() throws IOException {
|
||||
if (listen.isStream()) {
|
||||
if (detectedConnect == null || detectedConnect.isEmpty()) {
|
||||
if ((detectedConnect == null || detectedConnect.isEmpty())&&(detectedFactory==null||detectedFactory.isEmpty())) {
|
||||
if(listen.supportNIO()&&defaultConnect.supportNIO()&&defaultChannelFactory!=null) {
|
||||
scl=new SocketChannelListener(listen);
|
||||
}else {
|
||||
sl = new SocketListener(listen);
|
||||
}
|
||||
} else {
|
||||
sl = new SocketListener(new ProtocolDetectorServerSocket(listen.listenServerSocket()));
|
||||
}
|
||||
if(scl!=null) {
|
||||
scl.setCon((socx)->{
|
||||
SocketChannel sck=null;
|
||||
try {
|
||||
sck=defaultConnect.connectSocketChannel(InetAddress.getByName(cbind.getHost()), cbind.getPort());
|
||||
runChannelBridge(defaultChannelFactory, socx, sck);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (sck != null)
|
||||
try {
|
||||
sck.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
socx.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}else {
|
||||
sl.setCon((sox) -> {
|
||||
Socket sk = null;
|
||||
try {
|
||||
if (sox instanceof ProtocolDetectorSocket) {
|
||||
ProtocolStack ps = ((ProtocolDetectorSocket) sox).getProtocolStack();
|
||||
if (!ps.isEmpty()) {
|
||||
MultipurposeSocketAddress pmsa = detectedConnect.get(ps.pop().getName());
|
||||
String pname=ps.pop().getName();
|
||||
MultipurposeSocketAddress pmsa = detectedConnect.get(pname);
|
||||
if(pmsa!=null) {
|
||||
sk = pmsa.connectSocket(InetAddress.getByName(cbind.getHost()), cbind.getPort());
|
||||
}else {
|
||||
sk = defaultConnect.connectSocket(InetAddress.getByName(cbind.getHost()), cbind.getPort());
|
||||
|
||||
}
|
||||
SocketBridgeFactory sbf=detectedFactory.get(pname);
|
||||
if(sbf!=null) {
|
||||
runBridge(sbf,sox,sk);
|
||||
}else {
|
||||
runBridge(defaultFactory,sox,sk);
|
||||
}
|
||||
runBridge(defaultFactory,sox,sk);
|
||||
} else {
|
||||
sk = defaultConnect.connectSocket(InetAddress.getByName(cbind.getHost()), cbind.getPort());
|
||||
runBridge(defaultFactory,sox,sk);
|
||||
}
|
||||
} else {
|
||||
sk = defaultConnect.connectSocket(InetAddress.getByName(cbind.getHost()), cbind.getPort());
|
||||
runBridge(defaultFactory,sox,sk);
|
||||
}
|
||||
runBridge(sox,sk);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
@@ -79,6 +138,7 @@ public class SocketToSocketProxy extends Proxy {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
dsl = new DatagramSocketListener(listen);
|
||||
dsl.setCon((dox) -> {
|
||||
@@ -87,8 +147,13 @@ public class SocketToSocketProxy extends Proxy {
|
||||
}
|
||||
}
|
||||
|
||||
protected void runBridge(Socket sk, Socket sox) throws IOException {
|
||||
SocketBridge sb = new SocketBridge(sk, sox);
|
||||
protected void runBridge(SocketBridgeFactory sbf,Socket sk, Socket sox) throws IOException {
|
||||
SocketBridge sb = sbf.createBridge(sk, sox);
|
||||
sb.run();
|
||||
}
|
||||
|
||||
protected void runChannelBridge(SocketChannelBridgeFactory sbf,SocketChannel sk, SocketChannel sox) throws IOException {
|
||||
SocketChannelBridge sb = sbf.createBridge(sk, sox);
|
||||
sb.run();
|
||||
}
|
||||
|
||||
@@ -103,6 +168,24 @@ public class SocketToSocketProxy extends Proxy {
|
||||
public MultipurposeSocketAddress getDefaultConnect() {
|
||||
return defaultConnect;
|
||||
}
|
||||
|
||||
public Map<String, MultipurposeSocketAddress> getDetectedConnect() {
|
||||
return detectedConnect;
|
||||
}
|
||||
|
||||
public SocketBridgeFactory getDefaultFactory() {
|
||||
return defaultFactory;
|
||||
}
|
||||
|
||||
public Map<String, SocketBridgeFactory> getDetectedFactory() {
|
||||
return detectedFactory;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public SocketChannelBridgeFactory getDefaultChannelFactory() {
|
||||
return defaultChannelFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
@@ -110,12 +193,20 @@ public class SocketToSocketProxy extends Proxy {
|
||||
dsl.close();
|
||||
if (sl != null)
|
||||
sl.close();
|
||||
if(scl!=null)
|
||||
scl.close();
|
||||
}
|
||||
|
||||
public InetAddress getListenAddress() {
|
||||
if (sl != null) {
|
||||
return sl.getServerSocket().getInetAddress();
|
||||
} else {
|
||||
} else if(scl!=null){
|
||||
try {
|
||||
return ((InetSocketAddress)scl.getServerSocketChannel().getLocalAddress()).getAddress();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}else {
|
||||
return dsl.getDatagramServerSocket().getLocalAddress();
|
||||
}
|
||||
}
|
||||
@@ -123,6 +214,12 @@ public class SocketToSocketProxy extends Proxy {
|
||||
public int getListenPort() {
|
||||
if (sl != null) {
|
||||
return sl.getServerSocket().getLocalPort();
|
||||
} else if(scl!=null){
|
||||
try {
|
||||
return ((InetSocketAddress)scl.getServerSocketChannel().getLocalAddress()).getPort();
|
||||
} catch (IOException e) {
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
return dsl.getDatagramServerSocket().getLocalPort();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user