nettunnel: Bug fixes
This commit is contained in:
parent
49e4956894
commit
3408534d6f
nettunnel/main/scala/xyz/warp03/nettunnel
@ -98,7 +98,7 @@ class NTunClient(private val clientMgr: NetClientManager, private val workerCrea
|
||||
}
|
||||
|
||||
def createTunConnection(params: ConnectionParameters): SocketConnection = {
|
||||
var alpName = "";
|
||||
var alpName = "~";
|
||||
var (conn, id) = this.connections.synchronized {
|
||||
var id = this.nextConnId;
|
||||
while(this.connections(id) != null)
|
||||
|
@ -13,7 +13,7 @@ import org.omegazero.net.socket.{AbstractSocketConnection, SocketConnection, TLS
|
||||
class NTunConnection(private val endpoint: NTunEndpoint, private val connectionId: Int, private val localAddress: SocketAddress, private val remoteAddress: SocketAddress)
|
||||
extends AbstractSocketConnection {
|
||||
|
||||
private var connected = true;
|
||||
var connected = true;
|
||||
var lastIOTime = System.currentTimeMillis();
|
||||
|
||||
override def connect(x$0: Int): Unit = ()
|
||||
@ -25,7 +25,7 @@ class NTunConnection(private val endpoint: NTunEndpoint, private val connectionI
|
||||
override def getLastIOTime(): Long = this.lastIOTime;
|
||||
override def getLocalAddress(): SocketAddress = this.localAddress;
|
||||
override def getRemoteAddress(): SocketAddress = this.remoteAddress;
|
||||
override def isConnected(): Boolean = this.connected;
|
||||
override def isConnected(): Boolean = super.hasConnected() && this.connected;
|
||||
override def isWritable(): Boolean = this.endpoint.bconnection.isWritable();
|
||||
|
||||
override def read(): Array[Byte] = null;
|
||||
|
@ -61,11 +61,11 @@ abstract class NTunEndpoint(val bconnection: SocketConnection, maxPacketSize: In
|
||||
|
||||
this.hbCheckInterval = Tasks.I.interval(() => {
|
||||
this.writeFrame(FRAME_TYPE_HEARTBEAT, Array());
|
||||
if(System.nanoTime() - this.lastHeartbeat > 25000000000){
|
||||
if(System.nanoTime() - this.lastHeartbeat > 18000000000L){
|
||||
logger.debug(this.bconnection.getRemoteAddress(), " Heartbeat timeout");
|
||||
this.bconnection.destroy();
|
||||
}
|
||||
}, 10000);
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
protected def onData(data: Array[Byte]): Unit = {
|
||||
@ -154,12 +154,15 @@ abstract class NTunEndpoint(val bconnection: SocketConnection, maxPacketSize: In
|
||||
this.connections(id) = null;
|
||||
if(err.isDefined)
|
||||
conn.handleError(err.get);
|
||||
conn.connected = false;
|
||||
conn.handleClose();
|
||||
if(sendClose && !this.bconnection.hasDisconnected())
|
||||
this.writeFrame(FRAME_TYPE_CLOSE, idToBytes(id));
|
||||
}
|
||||
|
||||
protected def writeFrame(ftype: Byte, data: Array[Byte]): Unit = {
|
||||
if(logger.debug())
|
||||
logger.trace("local -> ", this.bconnection.getRemoteAddress(), " NTun Frame: type=", ftype, " data.length=", data.length);
|
||||
var size = FRAME_HEADER_SIZE + data.length;
|
||||
var hdr = new Array[Byte](FRAME_HEADER_SIZE);
|
||||
hdr(0) = size.toByte;
|
||||
@ -171,7 +174,7 @@ abstract class NTunEndpoint(val bconnection: SocketConnection, maxPacketSize: In
|
||||
|
||||
protected def processFrame(data: Array[Byte]): Unit = {
|
||||
if(logger.debug())
|
||||
logger.trace(this.bconnection.getRemoteAddress(), " Frame: type=", data(2), " data.length=", data.length);
|
||||
logger.trace(this.bconnection.getRemoteAddress(), " -> local NTun Frame: type=", data(2), " data.length=", data.length);
|
||||
data(2) match {
|
||||
case FRAME_TYPE_HANDSHAKE => {
|
||||
if(data.length != FRAME_HEADER_SIZE + 65)
|
||||
|
@ -80,13 +80,13 @@ class NTunServer(private val server: NetServer, private val workerCreator: java.
|
||||
}
|
||||
var remoteAddress = {
|
||||
if(additional(addI) == 0){
|
||||
addI = 1;
|
||||
addI += 1;
|
||||
this.bconnection.getRemoteAddress();
|
||||
}else if(additional(addI) == 1){
|
||||
var addrlen = additional(addI + 3);
|
||||
var addr = InetAddress.getByAddress(additional.slice(addI + 4, addI + 4 + addrlen));
|
||||
var port = additional(addI + 1) & 0xff | (additional(addI + 2) & 0xff) << 8;
|
||||
addI = 4 + addrlen;
|
||||
addI += 4 + addrlen;
|
||||
new InetSocketAddress(addr, port);
|
||||
}else
|
||||
throw new NTunException("Unknown SocketAddress type: " + additional(addI));
|
||||
@ -97,7 +97,7 @@ class NTunServer(private val server: NetServer, private val workerCreator: java.
|
||||
alpName;
|
||||
};
|
||||
var localAddress = new InetSocketAddress(NetTunnel.DUMMY_ADDRESS, targetPort);
|
||||
var conn = if(this.bconnection.isInstanceOf[TLSConnection]) then new NTunTLSConnection(this, id, localAddress, remoteAddress, alpName)
|
||||
var conn = if(this.bconnection.isInstanceOf[TLSConnection] && alpName != "~") then new NTunTLSConnection(this, id, localAddress, remoteAddress, alpName)
|
||||
else new NTunConnection(this, id, localAddress, remoteAddress);
|
||||
conn.setDefaultErrorListener((err: Throwable) => {
|
||||
if(err.isInstanceOf[java.io.IOException])
|
||||
@ -109,8 +109,10 @@ class NTunServer(private val server: NetServer, private val workerCreator: java.
|
||||
conn.setWorker(NTunServer.this.workerCreator.apply(conn));
|
||||
this.connections(id) = conn;
|
||||
logger.debug(this.bconnection.getRemoteAddress(), " Received NEWCONN, created new connection ", id);
|
||||
conn.on("connect", () => {
|
||||
NTunServer.this.connectionCallback.get.accept(conn);
|
||||
});
|
||||
conn.handleConnect();
|
||||
NTunServer.this.connectionCallback.get.accept(conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user