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

@@ -18,5 +18,10 @@ public abstract class PlatformFunction{
public void onGameExit(){} public void onGameExit(){}
public void openDonations(){} public void openDonations(){}
public void requestWritePerms(){} public void requestWritePerms(){}
public String getLocaleName(Locale locale){return locale.toString();} public String getLocaleName(Locale locale){
return locale.toString();
}
public boolean canJoinGame(){
return true;
}
} }

View File

@@ -20,7 +20,7 @@ import io.anuke.ucore.function.Consumer;
import java.io.IOException; import java.io.IOException;
public class Net{ public class Net{
public static final int version = 9; public static final int version = 10;
private static boolean server; private static boolean server;
private static boolean active; private static boolean active;

View File

@@ -49,7 +49,7 @@ public class ChatFragment extends Table implements Fragment{
//TODO put it in input? //TODO put it in input?
update(() -> { update(() -> {
if(!Net.active()){ if(!Net.active() && chatOpen){
hide(); hide();
} }

View File

@@ -28,7 +28,7 @@ public class MenuFragment implements Fragment{
add(new MenuButton("$text.play", group, ui.levels::show)); add(new MenuButton("$text.play", group, ui.levels::show));
row(); row();
if(!Vars.gwt) { if(Mindustry.platforms.canJoinGame()) {
add(new MenuButton("$text.joingame", group, ui.join::show)); add(new MenuButton("$text.joingame", group, ui.join::show));
row(); row();
} }

View File

@@ -25,6 +25,7 @@ public class HtmlLauncher extends GwtApplication {
static final int WIDTH = 800; static final int WIDTH = 800;
static final int HEIGHT = 600; static final int HEIGHT = 600;
static HtmlLauncher instance; static HtmlLauncher instance;
boolean canJoin = true;
@Override @Override
public PreloaderCallback getPreloaderCallback () { public PreloaderCallback getPreloaderCallback () {
@@ -111,6 +112,12 @@ public class HtmlLauncher extends GwtApplication {
public void openLink(String link){ public void openLink(String link){
Window.open(link, "_blank", ""); Window.open(link, "_blank", "");
} }
@Override
public boolean canJoinGame(){
String ref = Document.get().getReferrer();
return !ref.startsWith("https") && !ref.contains("itch.io");
}
}; };
return new Mindustry(); 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.badlogic.gdx.utils.reflect.ReflectionException;
import com.sksamuel.gwt.websockets.Websocket; import com.sksamuel.gwt.websockets.Websocket;
import com.sksamuel.gwt.websockets.WebsocketListener; import com.sksamuel.gwt.websockets.WebsocketListener;
import io.anuke.mindustry.Mindustry;
import io.anuke.mindustry.Vars; import io.anuke.mindustry.Vars;
import io.anuke.mindustry.net.Host; import io.anuke.mindustry.net.Host;
import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.Net;
@@ -104,36 +105,40 @@ public class WebsocketClient implements ClientProvider {
@Override @Override
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<IOException> failed) { public void pingHost(String address, int port, Consumer<Host> valid, Consumer<IOException> failed) {
failed.accept(new IOException()); if(!Mindustry.platforms.canJoinGame()) {
Websocket socket = new Websocket("ws://" + address + ":" + Vars.webPort); failed.accept(new IOException());
final boolean[] accepted = {false}; }else {
socket.addListener(new WebsocketListener() { Websocket socket = new Websocket("ws://" + address + ":" + Vars.webPort);
@Override final boolean[] accepted = {false};
public void onClose() { socket.addListener(new WebsocketListener() {
if(!accepted[0]) failed.accept(new IOException("Failed to connect to host.")); @Override
} public void onClose() {
if (!accepted[0]) failed.accept(new IOException("Failed to connect to host."));
}
@Override @Override
public void onMessage(String msg) { public void onMessage(String msg) {
String[] text = msg.split("\\|"); if(!msg.startsWith("---")) return;
Host host = new Host(text[1], address, Strings.parseInt(text[0])); String[] text = msg.substring(3).split("\\|");
valid.accept(host); Host host = new Host(text[1], address, Strings.parseInt(text[0]));
accepted[0] = true; valid.accept(host);
socket.close(); accepted[0] = true;
} socket.close();
}
@Override @Override
public void onOpen() { public void onOpen() {
socket.send("_ping_"); socket.send("_ping_");
} }
}); });
socket.open(); socket.open();
Timers.runTask(60f*5, () -> { Timers.runTask(60f * 5, () -> {
if(!accepted[0]){ if (!accepted[0]) {
failed.accept(new IOException("Failed to connect to host.")); failed.accept(new IOException("Failed to connect to host."));
socket.close(); socket.close();
} }
}); });
}
} }
@Override @Override

View File

@@ -259,6 +259,7 @@ public class KryoServer implements ServerProvider {
public void dispose(){ public void dispose(){
try { try {
server.dispose(); server.dispose();
UCore.log("Disposing web server...");
if(webServer != null) webServer.stop(1); if(webServer != null) webServer.stop(1);
//kill them all //kill them all
for(Thread thread : Thread.getAllStackTraces().keySet()){ for(Thread thread : Thread.getAllStackTraces().keySet()){
@@ -266,6 +267,7 @@ public class KryoServer implements ServerProvider {
thread.interrupt(); thread.interrupt();
} }
} }
UCore.log("Killed web server.");
}catch (Exception e){ }catch (Exception e){
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@@ -403,7 +405,7 @@ public class KryoServer implements ServerProvider {
if (k == null) return; if (k == null) return;
if(message.equals("_ping_")){ if(message.equals("_ping_")){
conn.send(connections.size() + "|" + Vars.player.name); conn.send("---" + connections.size() + "|" + Vars.player.name);
connections.remove(k); connections.remove(k);
}else { }else {
if (debug) UCore.log("Got message: " + message); if (debug) UCore.log("Got message: " + message);