2018-09-17 10:07:32 +03:00
|
|
|
package ru.gravit.launcher.request;
|
|
|
|
|
|
|
|
import ru.gravit.launcher.Launcher;
|
|
|
|
import ru.gravit.launcher.LauncherAPI;
|
|
|
|
import ru.gravit.launcher.LauncherConfig;
|
|
|
|
import ru.gravit.launcher.serialize.HInput;
|
|
|
|
import ru.gravit.launcher.serialize.HOutput;
|
2018-12-06 05:29:34 +03:00
|
|
|
import ru.gravit.utils.helper.IOHelper;
|
|
|
|
import ru.gravit.utils.helper.SecurityHelper;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-12-20 18:45:01 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public abstract class Request<R> {
|
2019-01-04 18:30:19 +03:00
|
|
|
private static long session = SecurityHelper.secureRandom.nextLong();
|
|
|
|
|
|
|
|
public static void setSession(long session) {
|
|
|
|
Request.session = session;
|
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
|
|
|
public static long getSession() {
|
2019-01-04 18:30:19 +03:00
|
|
|
return Request.session;
|
|
|
|
}
|
2018-09-22 17:33:00 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
@LauncherAPI
|
|
|
|
public static void requestError(String message) throws RequestException {
|
|
|
|
throw new RequestException(message);
|
|
|
|
}
|
2018-09-22 17:33:00 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
@LauncherAPI
|
2019-02-10 12:48:44 +03:00
|
|
|
protected transient final LauncherConfig config;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2019-02-10 12:48:44 +03:00
|
|
|
private transient final AtomicBoolean started = new AtomicBoolean(false);
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
protected Request() {
|
|
|
|
this(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
protected Request(LauncherConfig config) {
|
|
|
|
this.config = config == null ? Launcher.getConfig() : config;
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
2019-02-10 11:55:27 +03:00
|
|
|
public abstract Integer getLegacyType();
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
protected final void readError(HInput input) throws IOException {
|
|
|
|
String error = input.readString(0);
|
|
|
|
if (!error.isEmpty())
|
2018-09-22 17:33:00 +03:00
|
|
|
requestError(error);
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public R request() throws Exception {
|
|
|
|
if (!started.compareAndSet(false, true))
|
2018-09-22 17:33:00 +03:00
|
|
|
throw new IllegalStateException("Request already started");
|
2019-02-10 13:01:19 +03:00
|
|
|
R wsResult = null;
|
|
|
|
if(config.nettyPort != 0)
|
|
|
|
wsResult = requestWebSockets();
|
2019-02-10 11:51:20 +03:00
|
|
|
if(wsResult != null) return wsResult;
|
2018-09-17 10:07:32 +03:00
|
|
|
// Make request to LaunchServer
|
|
|
|
try (Socket socket = IOHelper.newSocket()) {
|
|
|
|
socket.connect(IOHelper.resolve(config.address));
|
|
|
|
try (HInput input = new HInput(socket.getInputStream());
|
|
|
|
HOutput output = new HOutput(socket.getOutputStream())) {
|
|
|
|
writeHandshake(input, output);
|
|
|
|
return requestDo(input, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-10 11:51:20 +03:00
|
|
|
protected R requestWebSockets() throws Exception
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
@LauncherAPI
|
|
|
|
protected abstract R requestDo(HInput input, HOutput output) throws Exception;
|
|
|
|
|
|
|
|
private void writeHandshake(HInput input, HOutput output) throws IOException {
|
|
|
|
// Write handshake
|
|
|
|
output.writeInt(Launcher.PROTOCOL_MAGIC);
|
|
|
|
output.writeBigInteger(config.publicKey.getModulus(), SecurityHelper.RSA_KEY_LENGTH + 1);
|
|
|
|
output.writeLong(session);
|
2019-02-10 11:55:27 +03:00
|
|
|
output.writeVarInt(getLegacyType());
|
2018-09-17 10:07:32 +03:00
|
|
|
output.flush();
|
|
|
|
|
|
|
|
// Verify is accepted
|
|
|
|
if (!input.readBoolean())
|
2018-09-22 17:33:00 +03:00
|
|
|
requestError("Serverside not accepted this connection");
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|