package org.yuriey.fasttunnel.eventloop.server;
import org.yuriey.fasttunnel.Bootstrap;
import org.yuriey.fasttunnel.ServerBootstrap;
import org.yuriey.fasttunnel.common.ChannelUtil;
import org.yuriey.fasttunnel.common.Global;
import org.yuriey.fasttunnel.config.Tunnel;
import org.yuriey.fasttunnel.handler.ChannelHandler;
import org.yuriey.fasttunnel.handler.server.FrontendServerChannelHandler;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Map;
public class FrontendServerEventLoop extends ServerEventLoop {
public FrontendServerEventLoop(Bootstrap bootstrap) {
super(bootstrap);
}
@Override
public Acceptor createAcceptor(ServerSocketChannel serverSocketChannel) {
return new FrontendAcceptor(serverSocketChannel);
}
class FrontendAcceptor extends Acceptor {
public FrontendAcceptor(ServerSocketChannel serverSocketChannel) {
super(serverSocketChannel);
}
@Override
protected ChannelHandler createChannelHandler(SocketChannel channel) {
return new FrontendServerChannelHandler(bootstrap, FrontendServerEventLoop.this, channel);
}
@Override
protected void postAccept(ServerSocketChannel serverSocketChannel, SocketChannel channel) {
// 接收连接后,根据时间戳生成id,该id发送给客户端,当客户端和目标主机建立了连接,
// 会同时与服务端建立隧道,并发送该id建立对应关系
Long id = System.currentTimeMillis();
Global.putFrontendChannel(id, channel);
FrontendServerEventLoop.this.addOpenChannel(serverSocketChannel);
FrontendServerEventLoop.this.addOpenChannel(channel);
SelectionKey key = channel.keyFor(selector);
if (key != null && key.isValid() && key.attachment() instanceof ChannelHandler handler) {
((FrontendServerChannelHandler) handler).setId(id);
}
//向Client写入连接请求
try {
int port = ((InetSocketAddress) serverSocketChannel.getLocalAddress()).getPort();
if (!writeConnectRequest2Client(port, id)) {
ChannelUtil.closeChannel(channel, FrontendServerEventLoop.this);
}
} catch (IOException e) {
logger.error("connect request error.");
}
}
private boolean writeConnectRequest2Client(int port, long id) throws IOException {
// 根据监听的端口从配置文件找到对应的目标主机
Tunnel tunnel = Global.getTunnel(port);
if (tunnel != null) {
// port端口对应的目标主机
String destination = tunnel.getDestination();
// clientHost为转发数据的客户端
String clientHost = tunnel.getClientHost();
// 从在线的客户端列表中查找并匹配
for (Map.Entry<String, SocketChannel> e : Global.getClientSessions().entrySet()) {
String host = e.getKey();
SocketChannel client = e.getValue();
if (host.equals(clientHost)) {
String message = String.join(":", String.valueOf(id), destination);
message = message.concat("#");
return ChannelUtil.interestWrite(client, ((ServerBootstrap) bootstrap).getServerEventLoop(), ByteBuffer.wrap(message.getBytes()));
}
}
}
return false;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
内网穿透.zip (41个子文件)
FastTunnel
pom.xml 2KB
src
test
java
main
resources
log4j2.xml 1KB
fastTunnel.yaml 537B
META-INF
MANIFEST.MF 65B
java
org
yuriey
fasttunnel
eventloop
client
ClientEventLoop.java 797B
AbstractEventLoop.java 4KB
server
FrontendServerEventLoop.java 4KB
TunnelServerEventLoop.java 1KB
ServerEventLoop.java 3KB
handler
client
TunnelClientChannelHandler.java 3KB
TransportChannelHandler.java 2KB
CommunicateClientChannelHandler.java 3KB
DestinationChannelHandler.java 2KB
server
FrontendServerChannelHandler.java 2KB
RecognizableChannelHandler.java 1KB
TunnelServerChannelHandler.java 3KB
CommunicateServerChannelHandler.java 1KB
ChannelHandler.java 3KB
common
TunnelConnectRequest.java 1KB
Request.java 176B
TunnelConfirmRequest.java 1KB
Global.java 2KB
ChannelUtil.java 2KB
RequestParser.java 2KB
Main.java 2KB
ClientBootstrap.java 3KB
Bootstrap.java 987B
ServerBootstrap.java 4KB
config
Client.java 227B
Config.java 179B
Tunnel.java 189B
Server.java 256B
.idea
jarRepositories.xml 877B
uiDesigner.xml 9KB
artifacts
FastTunnel_jar.xml 2KB
workspace.xml 4KB
misc.xml 478B
compiler.xml 543B
.gitignore 50B
encodings.xml 267B
.gitignore 490B
共 41 条
- 1
资源评论
Yuriey
- 粉丝: 9
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功