Implemented HTTPS checking for web multiplayer, fixed chat jamming all text fields

This commit is contained in:
Anuken
2018-01-21 11:54:54 -05:00
parent 40f7bf51c2
commit 2172daf145
7 changed files with 52 additions and 33 deletions

View File

@@ -25,6 +25,7 @@ public class HtmlLauncher extends GwtApplication {
static final int WIDTH = 800;
static final int HEIGHT = 600;
static HtmlLauncher instance;
boolean canJoin = true;
@Override
public PreloaderCallback getPreloaderCallback () {
@@ -111,6 +112,12 @@ public class HtmlLauncher extends GwtApplication {
public void openLink(String link){
Window.open(link, "_blank", "");
}
@Override
public boolean canJoinGame(){
String ref = Document.get().getReferrer();
return !ref.startsWith("https") && !ref.contains("itch.io");
}
};
return new Mindustry();

View File

@@ -6,6 +6,7 @@ import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import com.sksamuel.gwt.websockets.Websocket;
import com.sksamuel.gwt.websockets.WebsocketListener;
import io.anuke.mindustry.Mindustry;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.net.Host;
import io.anuke.mindustry.net.Net;
@@ -104,36 +105,40 @@ public class WebsocketClient implements ClientProvider {
@Override
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<IOException> failed) {
failed.accept(new IOException());
Websocket socket = new Websocket("ws://" + address + ":" + Vars.webPort);
final boolean[] accepted = {false};
socket.addListener(new WebsocketListener() {
@Override
public void onClose() {
if(!accepted[0]) failed.accept(new IOException("Failed to connect to host."));
}
if(!Mindustry.platforms.canJoinGame()) {
failed.accept(new IOException());
}else {
Websocket socket = new Websocket("ws://" + address + ":" + Vars.webPort);
final boolean[] accepted = {false};
socket.addListener(new WebsocketListener() {
@Override
public void onClose() {
if (!accepted[0]) failed.accept(new IOException("Failed to connect to host."));
}
@Override
public void onMessage(String msg) {
String[] text = msg.split("\\|");
Host host = new Host(text[1], address, Strings.parseInt(text[0]));
valid.accept(host);
accepted[0] = true;
socket.close();
}
@Override
public void onMessage(String msg) {
if(!msg.startsWith("---")) return;
String[] text = msg.substring(3).split("\\|");
Host host = new Host(text[1], address, Strings.parseInt(text[0]));
valid.accept(host);
accepted[0] = true;
socket.close();
}
@Override
public void onOpen() {
socket.send("_ping_");
}
});
socket.open();
Timers.runTask(60f*5, () -> {
if(!accepted[0]){
failed.accept(new IOException("Failed to connect to host."));
socket.close();
}
});
@Override
public void onOpen() {
socket.send("_ping_");
}
});
socket.open();
Timers.runTask(60f * 5, () -> {
if (!accepted[0]) {
failed.accept(new IOException("Failed to connect to host."));
socket.close();
}
});
}
}
@Override