Repository navigation
Expand file tree
/
Copy pathHttpServer.java
More file actions
76 lines (67 loc) · 3.08 KB
/
Copy pathHttpServer.java
File metadata and controls
76 lines (67 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* This file is part of ViaProxyAuthHook - https://github.com/ViaVersionAddons/ViaProxyAuthHook
* Copyright (C) 2024-2026 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.authhook.httpserver;
import com.google.common.cache.CacheBuilder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import net.raphimc.netminecraft.util.EventLoops;
import net.raphimc.netminecraft.util.TransportType;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import java.net.InetSocketAddress;
import java.net.http.HttpClient;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HttpServer {
private final ChannelFuture channelFuture;
private final Map<String, ProxyConnection> pendingConnections = CacheBuilder.newBuilder().expireAfterWrite(Duration.ofMinutes(1)).<String, ProxyConnection>build().asMap();
private final HttpClient httpClient = HttpClient.newBuilder().executor(Executors.newCachedThreadPool()).build();
public HttpServer(final InetSocketAddress bindAddress) {
this.channelFuture = new ServerBootstrap()
.group(EventLoops.getClientEventLoop(TransportType.NIO))
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new HttpServerChannelInitializer(this.pendingConnections, this.httpClient))
.bind(bindAddress)
.syncUninterruptibly();
}
public void addPendingConnection(final String serverIdHash, final ProxyConnection connection) {
this.pendingConnections.put(serverIdHash + "_" + connection.getGameProfile().name(), connection);
}
public void stop() {
if (this.channelFuture != null) {
this.channelFuture.channel().close();
}
this.httpClient.executor().map(ExecutorService.class::cast).ifPresent(ExecutorService::shutdown);
if (this.httpClient instanceof AutoCloseable closeable) {
try {
closeable.close();
} catch (final Exception ignored) {
}
}
}
public Channel getChannel() {
return this.channelFuture.channel();
}
}