111 lines
4.7 KiB
Java
111 lines
4.7 KiB
Java
|
|
package org.openautonomousconnection.webclient.recode;
|
||
|
|
|
||
|
|
import dev.unlegitdqrk.unlegitlibrary.event.EventListener;
|
||
|
|
import dev.unlegitdqrk.unlegitlibrary.event.Listener;
|
||
|
|
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.packets.C_PacketSendEvent;
|
||
|
|
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.state.ClientConnectedEvent;
|
||
|
|
import dev.unlegitdqrk.unlegitlibrary.network.system.client.events.state.ClientDisconnectedEvent;
|
||
|
|
import dev.unlegitdqrk.unlegitlibrary.network.system.utils.TransportProtocol;
|
||
|
|
import org.openautonomousconnection.oacswing.component.OACOptionPane;
|
||
|
|
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
|
||
|
|
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
|
||
|
|
import org.openautonomousconnection.protocol.side.client.events.ConnectedToProtocolINSServerEvent;
|
||
|
|
import org.openautonomousconnection.protocol.side.client.events.ConnectedToProtocolServerEvent;
|
||
|
|
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecord;
|
||
|
|
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSRecordType;
|
||
|
|
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.INSResponseStatus;
|
||
|
|
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.WebRequestMethod;
|
||
|
|
|
||
|
|
import javax.swing.*;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
public class ClientImpl extends ProtocolClient {
|
||
|
|
@Override
|
||
|
|
public boolean trustINS(String caFingerprint) {
|
||
|
|
Object[] options = {"Continue", "Cancel"};
|
||
|
|
int result = OACOptionPane.showOptionDialog(
|
||
|
|
Main.getUi(),
|
||
|
|
"You never connected to this INS before!\n" +
|
||
|
|
"Fingerprint: " + caFingerprint + "\nDo you want to connect?",
|
||
|
|
"INS Connection",
|
||
|
|
OACOptionPane.YES_NO_OPTION,
|
||
|
|
OACOptionPane.INFORMATION_MESSAGE,
|
||
|
|
null,
|
||
|
|
options,
|
||
|
|
options[0] // default button: Continue
|
||
|
|
);
|
||
|
|
|
||
|
|
return result == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public boolean trustNewINSFingerprint(String oldCAFingerprint, String newCAFingerprint) {
|
||
|
|
Object[] options = {"Continue", "Cancel"};
|
||
|
|
|
||
|
|
String table = String.format("""
|
||
|
|
Saved Fingerprint\tNew Fingerprint
|
||
|
|
%s\t%s
|
||
|
|
""", oldCAFingerprint, newCAFingerprint);
|
||
|
|
|
||
|
|
int result = OACOptionPane.showOptionDialog(
|
||
|
|
Main.getUi(),
|
||
|
|
"The fingerprint does not match with the saved fingerprint!\n" +
|
||
|
|
"Saved Fingerprint: " + oldCAFingerprint + "\n" +
|
||
|
|
"New Fingerprint: " + newCAFingerprint + "\n" +
|
||
|
|
"Do you want to connect?",
|
||
|
|
"INS Connection",
|
||
|
|
OACOptionPane.YES_NO_OPTION,
|
||
|
|
OACOptionPane.INFORMATION_MESSAGE,
|
||
|
|
null,
|
||
|
|
options,
|
||
|
|
options[0] // default button: Continue
|
||
|
|
);
|
||
|
|
|
||
|
|
return result == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onResponse(INSResponseStatus status, List<INSRecord> records) {
|
||
|
|
try {
|
||
|
|
String host = records.getFirst().value;
|
||
|
|
if (!host.contains(":")) host = host + ":1028";
|
||
|
|
String[] split = host.split(":");
|
||
|
|
Main.getClient().getClientServerConnection().connect(split[0], Integer.parseInt(split[1]));
|
||
|
|
} catch (Exception e) {
|
||
|
|
Main.getClient().getProtocolBridge().getLogger().exception("Failed to connect to Server", e);
|
||
|
|
OACOptionPane.showMessageDialog(Main.getUi(), "Failed to connect to Server:\n" + e.getMessage(),
|
||
|
|
"Server Connection", OACOptionPane.ERROR_MESSAGE);
|
||
|
|
}
|
||
|
|
|
||
|
|
super.onResponse(status, records);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Listener
|
||
|
|
public void onConnected(ConnectedToProtocolINSServerEvent event) {
|
||
|
|
try {
|
||
|
|
Main.getClient().buildServerConnection(null, true);
|
||
|
|
} catch (Exception e) {
|
||
|
|
Main.getClient().getProtocolBridge().getLogger().exception("Failed to build Server connection", e);
|
||
|
|
OACOptionPane.showMessageDialog(Main.getUi(), "Failed to connect to build Server connection:\n" + e.getMessage(),
|
||
|
|
"Server Connection", OACOptionPane.ERROR_MESSAGE);
|
||
|
|
}
|
||
|
|
|
||
|
|
Main.getUi().openNewTab("register.oac");
|
||
|
|
}
|
||
|
|
|
||
|
|
@Listener
|
||
|
|
public void onConnected(ConnectedToProtocolServerEvent event) {
|
||
|
|
try {
|
||
|
|
this.getClientServerConnection().sendPacket(new WebRequestPacket(
|
||
|
|
"index.html",
|
||
|
|
WebRequestMethod.GET,
|
||
|
|
Map.of(),
|
||
|
|
null
|
||
|
|
), TransportProtocol.TCP);
|
||
|
|
} catch (Exception e) {
|
||
|
|
throw new RuntimeException(e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|