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 openDonations(){}
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;
public class Net{
public static final int version = 9;
public static final int version = 10;
private static boolean server;
private static boolean active;

View File

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

View File

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

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

View File

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