Updated to new Protocol Version
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package org.openautonomousconnection.webserver;
|
||||
|
||||
import jdk.dynalink.linker.LinkerServices;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public final class ContentTypeResolver {
|
||||
@@ -14,6 +16,7 @@ public final class ContentTypeResolver {
|
||||
Map.entry("txt", "text/plain"),
|
||||
Map.entry("png", "image/png"),
|
||||
Map.entry("jpg", "image/jpeg"),
|
||||
Map.entry("ico", "image/x-icon"),
|
||||
Map.entry("jpeg", "image/jpeg"),
|
||||
Map.entry("gif", "image/gif"),
|
||||
Map.entry("svg", "image/svg+xml"),
|
||||
@@ -30,6 +33,22 @@ public final class ContentTypeResolver {
|
||||
Map.entry("py", "text/plain")
|
||||
);
|
||||
|
||||
public static boolean isVideoFile(String name) {
|
||||
return isFile(name, "video");
|
||||
}
|
||||
|
||||
public static boolean isImage(String name) {
|
||||
return isFile(name, "image");
|
||||
}
|
||||
|
||||
public static boolean isAudio(String name) {
|
||||
return isFile(name, "audio");
|
||||
}
|
||||
|
||||
public static boolean isFile(String name, String type) {
|
||||
return resolve(name).split("/")[0].equalsIgnoreCase(type);
|
||||
}
|
||||
|
||||
public static String resolve(String name) {
|
||||
int i = name.lastIndexOf('.');
|
||||
if (i == -1) return "application/octet-stream";
|
||||
|
||||
@@ -4,14 +4,12 @@ import dev.unlegitdqrk.unlegitlibrary.command.CommandExecutor;
|
||||
import dev.unlegitdqrk.unlegitlibrary.command.CommandManager;
|
||||
import dev.unlegitdqrk.unlegitlibrary.command.CommandPermission;
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.EventManager;
|
||||
import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.ProtocolSettings;
|
||||
import org.openautonomousconnection.protocol.side.ins.ProtocolINSServer;
|
||||
import org.openautonomousconnection.protocol.side.web.WebServerConfig;
|
||||
import org.openautonomousconnection.protocol.ProtocolValues;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
import org.openautonomousconnection.protocol.versions.v1_0_0.classic.handlers.builtin.WebClassic;
|
||||
|
||||
import javax.annotation.processing.Generated;
|
||||
import java.io.File;
|
||||
@@ -25,28 +23,56 @@ public class Main {
|
||||
@Getter
|
||||
private static ProtocolBridge protocolBridge;
|
||||
|
||||
@Getter
|
||||
private static ProtocolValues values;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ProtocolSettings settings = new ProtocolSettings();
|
||||
settings.packetHandler = new PacketHandler();
|
||||
settings.eventManager = new EventManager();
|
||||
settings.port = 9824;
|
||||
values = new ProtocolValues();
|
||||
values.packetHandler = new PacketHandler();
|
||||
values.eventManager = new EventManager();
|
||||
|
||||
// TODO: Refactoring with port in Protocol-Project
|
||||
WebServerConfig serverConfig = new WebServerConfig(new File("config.properties"));
|
||||
settings.port = serverConfig.getPort();
|
||||
ConfigurationManager config = new ConfigurationManager(new File("config.properties"));
|
||||
|
||||
protocolBridge = new ProtocolBridge(new WebServer(serverConfig,
|
||||
new File("auth.ini"), new File("rules.ini")),
|
||||
settings, ProtocolVersion.PV_1_0_0_BETA, new File("logs"));
|
||||
protocolBridge.setClassicHandlerWebServer(new WebClassic(protocolBridge));
|
||||
protocolBridge.getProtocolServer().getPipelineServer().start();
|
||||
if (!config.isSet("port.udp")) {
|
||||
config.set("port.udp", 1027);
|
||||
config.saveProperties();
|
||||
}
|
||||
|
||||
commandManager = new CommandManager(protocolBridge.getProtocolSettings().eventManager);
|
||||
if (!config.isSet("port.tcp")) {
|
||||
config.set("port.tcp", 1028);
|
||||
config.saveProperties();
|
||||
}
|
||||
|
||||
if (!config.isSet("sessionexpiremin")) {
|
||||
config.set("sessionexpiremin", 1000);
|
||||
config.saveProperties();
|
||||
}
|
||||
|
||||
if (!config.isSet("maxuploadmb")) {
|
||||
config.set("maxuploadmb", 1024);
|
||||
config.saveProperties();
|
||||
}
|
||||
|
||||
int tcpPort = config.getInt("port.tcp");
|
||||
int udpPort = config.getInt("port.udp");
|
||||
|
||||
int sessionExpire = config.getInt("sessionexpiremin");
|
||||
int maxUpload = config.getInt("maxuploadmb");
|
||||
|
||||
protocolBridge = new ProtocolBridge(new WebServer(
|
||||
new File("auth.ini"), new File("rules.ini"),
|
||||
tcpPort, udpPort,
|
||||
sessionExpire, maxUpload),
|
||||
values, ProtocolVersion.PV_1_0_0_BETA, new File("logs"));
|
||||
|
||||
protocolBridge.getProtocolServer().getNetwork().start();
|
||||
|
||||
commandManager = new CommandManager(values.eventManager);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
System.out.println(commandExecutor.getName() + "> ");
|
||||
String line = scanner.nextLine();
|
||||
System.out.println(commandExecutor.getName() + "> ");
|
||||
commandManager.execute(commandExecutor, line);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.openautonomousconnection.webserver;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.Transport;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.*;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
|
||||
@@ -9,7 +10,6 @@ import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream.WebS
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.stream.WebStreamStartPacket;
|
||||
import org.openautonomousconnection.protocol.side.web.ConnectedWebClient;
|
||||
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
|
||||
import org.openautonomousconnection.protocol.side.web.WebServerConfig;
|
||||
import org.openautonomousconnection.protocol.side.web.managers.RuleManager;
|
||||
import org.openautonomousconnection.protocol.side.web.managers.SessionManager;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
@@ -26,8 +26,9 @@ public final class WebServer extends ProtocolWebServer {
|
||||
private static final int STREAM_CHUNK_SIZE = 64 * 1024;
|
||||
private static final long STREAM_THRESHOLD = 2L * 1024 * 1024;
|
||||
|
||||
public WebServer(WebServerConfig config, File authFile, File rulesFile) throws Exception {
|
||||
super(config, authFile, rulesFile);
|
||||
public WebServer(File authFile, File rulesFile, int tcpPort, int udpPort,
|
||||
int sessionExpire, int maxUpload) throws Exception {
|
||||
super(authFile, rulesFile, tcpPort, udpPort, sessionExpire, maxUpload);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,7 +78,7 @@ public final class WebServer extends ProtocolWebServer {
|
||||
private void streamFile(ConnectedWebClient client, File file, String contentType) throws IOException, ClassNotFoundException {
|
||||
long total = file.length();
|
||||
|
||||
client.getPipelineConnection().sendPacket(new WebStreamStartPacket(200, contentType, Map.of("name", file.getName()), total));
|
||||
client.getConnection().sendPacket(new WebStreamStartPacket(200, contentType, Map.of("name", file.getName()), total), Transport.TCP);
|
||||
|
||||
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
|
||||
byte[] buf = new byte[STREAM_CHUNK_SIZE];
|
||||
@@ -85,10 +86,11 @@ public final class WebServer extends ProtocolWebServer {
|
||||
int r;
|
||||
while ((r = in.read(buf)) != -1) {
|
||||
byte[] chunk = (r == buf.length) ? buf : Arrays.copyOf(buf, r);
|
||||
client.getPipelineConnection().sendPacket(new WebStreamChunkPacket(seq++, chunk));
|
||||
client.getConnection().sendPacket(new WebStreamChunkPacket(seq++, chunk),
|
||||
ContentTypeResolver.isVideoFile(file.getName()) ? Transport.UDP : Transport.TCP);
|
||||
}
|
||||
}
|
||||
|
||||
client.getPipelineConnection().sendPacket(new WebStreamEndPacket(true));
|
||||
client.getConnection().sendPacket(new WebStreamEndPacket(true), Transport.TCP);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ public final class SessionContext {
|
||||
String sessionId = headers.get("session");
|
||||
if (sessionId == null) return new SessionContext(null, null, false);
|
||||
|
||||
String ip = (client.getPipelineConnection().getSocket() != null && client.getPipelineConnection().getSocket().getInetAddress() != null)
|
||||
? client.getPipelineConnection().getSocket().getInetAddress().getHostAddress() : "";
|
||||
String ip = (client.getConnection().getSocket() != null && client.getConnection().getSocket().getInetAddress() != null)
|
||||
? client.getConnection().getSocket().getInetAddress().getHostAddress() : "";
|
||||
|
||||
String userAgent = headers.getOrDefault("user-agent", "");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user