Skip to content

Commit 5374339

Browse files
committed
support WSS connections to ap servers.
1 parent 266e2e9 commit 5374339

File tree

5 files changed

+81
-51
lines changed

5 files changed

+81
-51
lines changed

build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ archivesBaseName = "Archipelago.MultiClient.Java"
99
group = 'gg.archipelago.APClient'
1010
version = '1.5'
1111

12-
java.toolchain.languageVersion = JavaLanguageVersion.of(16)
12+
java.toolchain.languageVersion = JavaLanguageVersion.of(8)
1313

1414
logger.info('Java: {} JVM: {}({}}) Arch: {}',System.getProperty('java.version'),System.getProperty('java.vm.version'),System.getProperty('java.vendor'),System.getProperty('os.arch'))
1515

1616
repositories {
1717
mavenCentral()
1818
}
1919

20+
2021
dependencies {
2122
implementation 'org.java-websocket:Java-WebSocket:1.5.2'
2223
implementation 'com.google.code.gson:gson:2.8.9'
24+
implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
25+
implementation 'org.apache.httpcomponents.core5:httpcore5:5.2'
2326
}
2427

25-
task relocateShadowJar(type: ConfigureShadowRelocation) {
28+
tasks.register('relocateShadowJar', ConfigureShadowRelocation) {
2629
target = tasks.shadowJar
2730
prefix = "archipelagoClient" // Default value is "shadow"
2831
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pluginManagement {
22
plugins {
3-
id 'com.github.johnrengelman.shadow' version '6.0.0'
3+
id 'com.github.johnrengelman.shadow' version '7.1.2'
44
}
55
}

src/main/java/gg/archipelago/APClient/APClient.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import gg.archipelago.APClient.parts.DataPackage;
1010
import gg.archipelago.APClient.parts.NetworkItem;
1111
import gg.archipelago.APClient.parts.Version;
12+
import org.apache.hc.core5.net.URIBuilder;
1213

1314
import java.io.*;
1415
import java.net.URI;
16+
import java.net.URISyntaxException;
1517
import java.util.ArrayList;
1618
import java.util.Arrays;
1719
import java.util.logging.Level;
1820
import java.util.logging.Logger;
19-
import java.util.regex.Pattern;
2021

2122
public abstract class APClient {
2223

@@ -168,24 +169,36 @@ public RoomInfoPacket getRoomInfo() {
168169
return roomInfo;
169170
}
170171

171-
public void connect(String address) {
172-
LOGGER.fine("attempting WebSocket connection to " + address);
172+
public void connect(String address) throws URISyntaxException {
173+
URIBuilder builder = new URIBuilder((!address.contains("//")) ? "//" + address : address);
174+
if (builder.getPort() == -1) { //set default port if not included
175+
builder.setPort(38281);
176+
}
177+
173178
if (apWebSocket != null && apWebSocket.isOpen()) {
174179
LOGGER.fine("previous WebSocket is open, closing.");
175180
apWebSocket.close();
176181
}
177-
//regex match port on end of address
178-
String pattern = ":[0-9]+";
179-
Pattern p = Pattern.compile(pattern);
180-
if(!p.matcher(address).find()) {
181-
LOGGER.fine("no port set assuming default of 38281");
182-
address += ":38281";
182+
183+
if (builder.getScheme() == null) {
184+
builder.setScheme("wss");
185+
connect(builder.build(), true);
186+
return;
183187
}
184-
URI uri = URI.create("ws://" + address);
185-
apWebSocket = new APWebSocket(uri, this);
188+
189+
connect(builder.build());
190+
}
191+
192+
public void connect(URI address) {
193+
connect(address, false);
194+
}
195+
196+
public void connect(URI address, boolean allowDowngrade) {
197+
LOGGER.fine("attempting WebSocket connection to " + address.toString());
198+
apWebSocket = new APWebSocket(address, this);
186199
locationManager.setAPWebSocket(apWebSocket);
187200
itemManager.setAPWebSocket(apWebSocket);
188-
apWebSocket.connect();
201+
apWebSocket.connect(allowDowngrade);
189202
}
190203

191204
public void sendChat(String message) {

src/main/java/gg/archipelago/APClient/APWebSocket.java

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import gg.archipelago.APClient.parts.DataPackage;
1313
import gg.archipelago.APClient.parts.NetworkItem;
1414
import gg.archipelago.APClient.parts.NetworkPlayer;
15+
import org.apache.hc.core5.net.URIBuilder;
1516
import org.java_websocket.client.WebSocketClient;
1617
import org.java_websocket.handshake.ServerHandshake;
1718

1819
import java.net.URI;
20+
import java.net.URISyntaxException;
1921
import java.util.*;
2022
import java.util.logging.Level;
2123

@@ -31,17 +33,17 @@ public class APWebSocket extends WebSocketClient {
3133

3234
private int reconnectAttempt = 0;
3335

34-
private boolean attemptingReconnect;
35-
3636
private String seedName;
3737
private static Timer reconnectTimer;
38+
private boolean downgrade = false;
3839

3940
public APWebSocket(URI serverUri, APClient apClient) {
4041
super(serverUri);
4142
this.apClient = apClient;
42-
if (reconnectTimer == null)
43-
reconnectTimer = new Timer();
44-
reconnectTimer.cancel();
43+
if (reconnectTimer != null) {
44+
reconnectTimer.cancel();
45+
}
46+
reconnectTimer = new Timer();
4547
}
4648

4749
@Override
@@ -228,51 +230,63 @@ private void sendManyPackets(APPacket[] packet) {
228230
@Override
229231
public void onClose(int code, String wsReason, boolean remote) {
230232
LOGGER.fine(String.format("Connection closed by %s Code: %s Reason: %s", (remote ? "remote peer" : "us"), code, wsReason));
231-
authenticated = false;
232-
attemptingReconnect = false;
233-
String reason = wsReason;
234-
if (code == -1)
235-
reason = "Connection refused by the Archipelago server.";
236-
if (code == 1006) {
237-
reason = "Lost connection to the Archipelago server.";
238-
attemptingReconnect = true;
239-
}
240-
241-
if(attemptingReconnect && reconnectAttempt <= 10) {
242-
int reconnectDelay = (int) (5000 * Math.pow(2,reconnectAttempt));
243-
reconnectAttempt++;
244-
apClient.onClose(reason, reconnectDelay/1000);
233+
String reason = (wsReason.isEmpty()) ? "Connection refused by the Archipelago server." : wsReason;
234+
if (code == -1) {
235+
reconnectTimer.cancel();
245236

246-
TimerTask reconnectTask = new TimerTask() {
247-
@Override
248-
public void run() {
249-
apClient.reconnect();
237+
// attempt to reconnect using non-secure web socket if we are failing to connect with a secure socket.
238+
if (uri.getScheme().equalsIgnoreCase("wss") && downgrade) {
239+
try {
240+
apClient.connect(new URIBuilder(uri).setScheme("ws").build());
241+
} catch (URISyntaxException ignored) {
242+
apClient.onClose(reason, 0);
250243
}
251-
};
252-
253-
reconnectTimer.cancel();
254-
reconnectTimer = new Timer();
255-
reconnectTimer.schedule(reconnectTask, reconnectDelay);
244+
return;
245+
}
246+
apClient.onClose(reason, 0);
247+
return;
256248
}
257-
else {
258-
attemptingReconnect = false;
249+
if(code == 1000) {
259250
reconnectTimer.cancel();
260-
apClient.onClose(reason, 0);
251+
apClient.onClose("Disconnected.", 0);
252+
}
253+
254+
if (code == 1006) {
255+
reason = "Lost connection to the Archipelago server.";
256+
if( reconnectAttempt <= 10) {
257+
int reconnectDelay = (int) (5000 * Math.pow(2, reconnectAttempt));
258+
reconnectAttempt++;
259+
TimerTask reconnectTask = new TimerTask() {
260+
@Override
261+
public void run() {
262+
apClient.reconnect();
263+
}
264+
};
265+
266+
reconnectTimer.cancel();
267+
reconnectTimer = new Timer();
268+
reconnectTimer.schedule(reconnectTask, reconnectDelay);
269+
apClient.onClose(reason, reconnectDelay / 1000);
270+
return;
271+
}
261272
}
273+
274+
reconnectTimer.cancel();
275+
apClient.onClose(reason, 0);
262276
}
263277

264278
@Override
265279
public void onError(Exception ex) {
266-
//apClient.onError(ex);
280+
apClient.onError(ex);
267281
LOGGER.log(Level.WARNING, "Error in websocket connection");
282+
ex.printStackTrace();
268283
}
269284

270-
@Override
271-
public void connect(){
285+
public void connect(boolean allowDowngrade){
272286
super.connect();
273287
reconnectTimer.cancel();
274-
attemptingReconnect = false;
275288
reconnectAttempt = 0;
289+
this.downgrade = allowDowngrade;
276290
}
277291

278292
public void sendChat(String message) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package gg.archipelago.APClient;
22

3-
public class SlotData {
3+
public interface SlotData {
44
}

0 commit comments

Comments
 (0)