-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathServerExtensions.java
1286 lines (1121 loc) · 49 KB
/
ServerExtensions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* rscplus
*
* <p>This file is part of rscplus.
*
* <p>rscplus is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* <p>rscplus is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* <p>You should have received a copy of the GNU General Public License along with rscplus. If not,
* see <http://www.gnu.org/licenses/>.
*
* <p>Authors: see <https://github.com/RSCPlus/rscplus>
*/
package Client;
import Client.Extensions.OpenRSCOfficialUtils;
import Client.Extensions.WorldType;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
/**
* Custom server extension configuration management
*
* <p>Follow this process to correctly register a new server extension:
*
* <ol>
* <li>First, decide whether your extension should be exclusive to a specific private server host
* or if it may be freely used by any world.ini file that specifies it. Common extensions are
* not allowed to configure a {@link WorldSubscription} or {@link BinaryInfo}, while exclusive
* extensions MUST register a {@link WorldSubscription}. See below for more information
* regarding these features.
* <li>Above {@link #bootstrap()}, define a public static constant representing the extension
* <li>To register an exclusive extension:
* <ol>
* <li>Create a private static helper method above {@link #setupDefaultExtension}, following
* this naming convention: {@code setupMyExtension()}
* <li>Within your helper method:
* <ol>
* <li>As the first instruction within the helper, invoke {@link
* Config#registerExtension(Extension)}, passing in your new constant
* <li>To enable automatic world downloading from a subscription API and extension
* validation features:
* <ol>
* <li>First, construct a new {@link WorldSubscription} object via {@link
* WorldSubscription#WorldSubscription(String)}.
* <li>Then, for every endpoint you plan on hosting, invoke {@link
* WorldSubscription#addDownloadURL(WorldType, String, boolean)}, providing
* a {@link WorldType} to identify a logical grouping of worlds. Note:
* downloaded world files will be sorted in the same order as the call chain
* here
* <li>Finally, invoke {@link Config#setWorldSubscription(Extension,
* WorldSubscription)} to finalize setup.
* </ol>
* <li><i>(Optional)</i> To enable your extension to work with custom binaries:
* <ol>
* <li>First, construct a new {@link BinaryInfo} object via {@link
* BinaryInfo#BinaryInfo(String, String)}.
* <li>Then, for every distribution type you wish to support, invoke {@link
* BinaryInfo#addBinaryDownload(BINARY_TYPE, String, String)}, providing a
* {@link BINARY_TYPE} to identify each download.
* <li>Finally, invoke {@link Config#setBinaryInfo(Extension, BinaryInfo)} to
* finalize setup.
* </ol>
* <li><i>(Optional)</i> Exclusive extensions that manage their own population
* fetching logic should opt-out of the world file-based population feature by
* invoking {@link #populationFeatureOptOut(Extension)}
* </ol>
* <li>Finally, invoke your helper from within {@link ServerExtensions#bootstrap()}, within
* the block labeled "Exclusive extensions."
* </ol>
* <li>To register a common extension, simply invoke {@link #addCommonExtension(Extension)} within
* {@link #bootstrap()}, above the block labeled "Common extensions."
* </ol>
*
* <p>Validation will be performed on application launch to ensure mistakes weren't made in the
* bootstrapping process - errors will be thrown to assist in correcting any issues.
*
* <p>For more information on Server Extension usage and all related features, see {@code README.md}
* located in the {@link Client.Extensions} package.
*/
public class ServerExtensions {
// Internal usage
public static final Set<Extension> commonExtensions = new HashSet<>();
public static final Extension NONE =
new Extension("NONE", ""); // Used when no extensions are active
private static final Extension RSCPLUS =
new Extension("rscplus", "RSCPlus"); // Built-in extension for the official RSCPlus binary
private static Extension activeServerExtension = NONE; // The currently-active extension
private static final Set<Extension> populationOptOutSet = new HashSet<>();
/* Bootstrap known server data */
// Server extension declarations
public static final Extension OPENRSC_FRAMEWORK = new Extension("openrsc", "OpenRSC Framework");
public static final Extension OPENRSC_OFFICIAL = new Extension("openrsc_official", "OpenRSC");
/** Initialize all {@link ServerExtensions} configurations for use throughout the application */
static void bootstrap() {
// Built-in RSCPlus data
setupDefaultExtension();
/* Common extensions */
addCommonExtension(OPENRSC_FRAMEWORK);
/* Exclusive extensions */
// OpenRSC Extension
setupOpenRSCOfficialExtension();
/* Validate extensions setup */
Config.validateDeveloperExtensionRegistration();
}
/** OpenRSC extension configuration */
private static void setupOpenRSCOfficialExtension() {
Config.registerExtension(OPENRSC_OFFICIAL);
WorldSubscription openRSCOfficialWorldSub = new WorldSubscription("openrsc.com");
openRSCOfficialWorldSub.addDownloadURL(
OpenRSCOfficialUtils.PRESERVATION,
"https://rsc.vet/worlds/preservation.json" + Util.getUTMParams(),
true);
openRSCOfficialWorldSub.addDownloadURL(
OpenRSCOfficialUtils.URANIUM,
"https://rsc.vet/worlds/uranium.json" + Util.getUTMParams(),
false);
// Compile world subscription objects
Config.setWorldSubscription(OPENRSC_OFFICIAL, openRSCOfficialWorldSub);
// OpenRSC binary info
final BinaryInfo openRSCOfficialBinaryInfo =
new BinaryInfo("Open", "https://github.com/RSCPlus/OpenRSCPlus/releases/download/Latest/");
openRSCOfficialBinaryInfo.addBinaryDownload(
BINARY_TYPE.WINDOWS_X64, "OpenRSCPlusSetup.exe", "version_windows_64.txt");
openRSCOfficialBinaryInfo.addBinaryDownload(
BINARY_TYPE.WINDOWS_X32, "OpenRSCPlusSetup32.exe", "version_windows_32.txt");
openRSCOfficialBinaryInfo.addBinaryDownload(
BINARY_TYPE.MACOS_ARM, "OpenRSCPlus-aarch64.dmg", "version_macos.txt");
openRSCOfficialBinaryInfo.addBinaryDownload(
BINARY_TYPE.MACOS_X64, "OpenRSCPlus-x64.dmg", "version_macos.txt");
openRSCOfficialBinaryInfo.addBinaryDownload(
BINARY_TYPE.LINUX_APP_IMAGE, "OpenRSCPlus.AppImage", "version_linux_appimage.txt");
Config.setBinaryInfo(OPENRSC_OFFICIAL, openRSCOfficialBinaryInfo);
// Has a specialized mechanism for fetching populations
populationFeatureOptOut(OPENRSC_OFFICIAL);
}
/**
* Default RSCPlus extension configuration
*
* <p>Used to define download information for the official binary and reserve the "RSCPlus" name
*/
private static void setupDefaultExtension() {
Config.registerExtension(RSCPLUS);
final BinaryInfo RSCPlusBinaryInfo =
new BinaryInfo("", "https://github.com/RSCPlus/rscplus/releases/download/Latest/");
RSCPlusBinaryInfo.addBinaryDownload(
BINARY_TYPE.WINDOWS_X64, "RSCPlusSetup.exe", "version_windows_64.txt");
RSCPlusBinaryInfo.addBinaryDownload(
BINARY_TYPE.WINDOWS_X32, "RSCPlusSetup32.exe", "version_windows_32.txt");
RSCPlusBinaryInfo.addBinaryDownload(
BINARY_TYPE.MACOS_ARM, "RSCPlus-aarch64.dmg", "version_macos.txt");
RSCPlusBinaryInfo.addBinaryDownload(
BINARY_TYPE.MACOS_X64, "RSCPlus-x64.dmg", "version_macos.txt");
RSCPlusBinaryInfo.addBinaryDownload(
BINARY_TYPE.LINUX_APP_IMAGE, "RSCPlus.AppImage", "version_linux_appimage.txt");
Config.setBinaryInfo(RSCPLUS, RSCPlusBinaryInfo);
}
private static void addCommonExtension(Extension extension) {
Config.registerExtension(extension);
commonExtensions.add(extension);
}
/* End of server extension bootstrapping */
/**
* Check whether the provided extension is active, for use when following logic should only apply
* to a single extension
*
* @param extension The {@link Extension} to check
* @return {@code boolean} indicating the result
*/
public static boolean enabled(Extension extension) {
return activeServerExtension.equals(extension);
}
/**
* Check whether any of the provided extensions are active, for use when following logic can apply
* to multiple extensions
*
* @param extensions {@code varargs} list of {@link Extension}s to check
* @return {@code boolean} indicating the result
*/
public static boolean enabled(Extension... extensions) {
return Arrays.asList(extensions).contains(activeServerExtension);
}
/** @return {@code boolean} Indicating whether any extension is currently active */
public static boolean anyEnabled() {
return !activeServerExtension.equals(NONE);
}
/** @return {@code boolean} Indicating whether the provided extension is a common type */
public static boolean isCommonExtension(Extension extension) {
return commonExtensions.contains(extension);
}
/**
* Checks whether the world subscription map has defined data for a given extension
*
* @param extension The {@link Extension} to look for
* @return {@code boolean} indicating found status
*/
public static boolean hasWorldSubscription(Extension extension) {
return Config.worldSubscriptionMap.containsKey(extension);
}
/**
* Checks whether the binary info map has defined data for a given extension
*
* @param extension The {@link Extension} to look for
* @return {@code boolean} indicating found status
*/
public static boolean hasBinaryInfo(Extension extension) {
return Config.binaryInfoMap.containsKey(extension);
}
/**
* Returns the configured {@link WorldSubscription} for a given extension
*
* @param extension The {@link Extension} to retrieve world subscription data for
* @return {@link WorldSubscription} tied to the provided extension
*/
public static WorldSubscription getWorldSubscription(Extension extension) {
return Config.worldSubscriptionMap.get(extension);
}
/**
* Returns the {@link BinaryInfo} for a given extension
*
* @param extension The {@link Extension} to retrieve binary data for
* @return {@link BinaryInfo} tied to the provided extension
*/
public static BinaryInfo getBinaryInfo(Extension extension) {
return Config.binaryInfoMap.get(extension);
}
/**
* Retrieve the currently-active extension
*
* <p>Note: For use solely in cases when following logic is agnostic to the actual extension
*
* @return the active {@link Extension}
* @see #enabled(Extension)
* @see #enabled(Extension...)
*/
public static Extension getActiveExtension() {
return activeServerExtension;
}
/**
* Sets the active extension, ensuring that it is both defined and valid
*
* @param extension The {@link Extension} to be used
*/
protected static void setActiveExtension(Extension extension) {
if (extension.equals(NONE) || extension.equals(RSCPLUS)) {
// Reserved for internal usage
activeServerExtension = NONE;
} else if (Config.knownExtensions.contains(extension)) {
activeServerExtension = extension;
} else {
Logger.Warn("Prevented setting unknown extension: [" + extension + "]");
}
}
/**
* Opt-out of the world.ini-based population fetching feature for a given exclusive extension,
* when it defines its own methodology for fetching populations
*
* @param extension The exclusive {@link Extension} wishing to opt-out
*/
private static void populationFeatureOptOut(Extension extension) {
if (NONE.equals(extension) || RSCPLUS.equals(extension)) {
throw new IllegalArgumentException("Cannot opt-out of the NONE or rscplus extensions");
}
if (isCommonExtension(extension)) {
throw new IllegalArgumentException(
"Common extensions cannot opt out of the world population feature");
}
populationOptOutSet.add(extension);
}
/**
* @return {@code boolean} indicating whether the extension has opted-out of the world.ini-based
* population fetching feature
*/
public static boolean populationFeatureDisabled(Extension extension) {
return populationOptOutSet.contains(extension);
}
/**
* Sanitizes server extension-related {@link Settings} for a specified world by matching the ini
* values to the {@link Config} data declared within {@link ServerExtensions#bootstrap()}.
*
* <p>When a corresponding {@link WorldSubscription} is defined, this will attempt to fetch world
* data to validate extension settings against, unless it was already downloaded on startup. This
* is particularly useful for automatically fetching extension data even when the client is run
* without a provided {@link Launcher#worldSubscriptionId}.
*
* <p>This method is called when parsing world ini files during application startup in {@link
* Settings#initWorlds()} and when settings are saved from the {@link ConfigWindow}.
*
* <p>For more information on Server Extension usage, see {@code README.md} located in the {@link
* Client.Extensions} package.
*
* @param worldIndex {@link Settings} map index for the world being processed
*/
protected static void validateServerExtensionSettings(int worldIndex) {
final World currWorld = World.fromSettings(worldIndex);
// Skip validation for local testing, allows the use of any extension/world ID on a local server
if (Util.isLocalhost(currWorld.getUrl())) {
return;
}
// Check to see if the URL has been retired
if (Config.getWorldSubscriptionMap().entrySet().stream()
.anyMatch((entry) -> entry.getValue().formerDomainMatch(currWorld))) {
// Block connections to the world
Launcher.setWarning(Launcher.LauncherError.WORLD_SUB_MISMATCH);
Launcher.blockedWorlds.add(currWorld);
}
// Check if the world was downloaded this launch and validate the received data
for (Map.Entry<URI, List<World>> downloadedSubWorlds : Launcher.downloadedWorlds.entrySet()) {
List<World> downloadedSubWorldsList = downloadedSubWorlds.getValue();
if (downloadedSubWorldsList != null) {
for (World downloadedWorld : downloadedSubWorldsList) {
// Found the world in the download list
if (downloadedWorld.equals(currWorld)) {
validateDownloadedWorld(currWorld, downloadedWorld, worldIndex);
return; // Skip other validations
}
}
}
}
final String currExtensionVal = currWorld.getServerExtension();
// When no extension exists, attempt to discover it
if (Util.isBlank(currExtensionVal)) {
World sanitizedWorld = currWorld;
// If there was a world ID defined without an extension, remove it
if (!currWorld.getWorldId().isEmpty()) {
Settings.WORLD_ID.put(worldIndex, "");
sanitizedWorld = World.removeExtensionData(currWorld);
}
discoverExtension(worldIndex, sanitizedWorld);
return;
}
final Extension currExtension = Extension.from(currExtensionVal);
// Look for a subscription defined for the extension
WorldSubscription extensionSubscription = Config.getWorldSubscriptionMap().get(currExtension);
// No subscription exists or the extension doesn't exist
if (extensionSubscription == null) {
// No subscription exists for the extension - ensure extension is valid
if (!currExtension.equals(NONE)
&& !currExtension.equals(RSCPLUS)
&& Config.knownExtensions.contains(currExtension)) {
// Check if current world URL matches any host URL for a different subscription
if (Config.getWorldSubscriptionMap().values().stream()
.anyMatch(
sub ->
currWorld.hostMatches(sub.getHostUrl())
|| currWorld.hostMatchesAny(sub.getFormerHostDomains()))) {
invalidateExtensionAttemptDiscovery(worldIndex, currWorld);
return;
}
// A defined World ID without a matching subscription would be invalid,
// as there is no way to validate that it is legitimate
Settings.WORLD_ID.put(worldIndex, "");
return;
}
// Remove the non-existent extension and attempt to resolve the current world by its URL
invalidateExtensionAttemptDiscovery(worldIndex, currWorld);
return;
}
// When sub host and world URL don't match, the world is either misconfigured or someone is
// trying to force the use of an invalid extension for the world
if (!currWorld.hostMatches(extensionSubscription.getHostUrl())) {
// If the file predated a legitimate host domain change and the sub was unreachable after the
// user had already updated their client, the original world file would have been preserved if
// its extension data matched, and it should not have its extension data removed, to prevent
// its deletion in subsequent launches. Users will be unable to connect to it regardless.
if (Launcher.shouldDownloadWorlds()) {
if (!extensionSubscription.formerDomainMatch(currWorld)) {
invalidateExtensionAttemptDiscovery(worldIndex, currWorld);
}
} else {
invalidateExtensionAttemptDiscovery(worldIndex, currWorld);
}
return;
}
// Download subscription data for the world file
downloadAndVerifyWorldData(extensionSubscription, worldIndex, currWorld);
}
/**
* Helper for {@link #validateServerExtensionSettings}
*
* <p>Removes extension data from the world and invokes extension discovery
*
* @param worldIndex Index corresponding to the world in the Settings map
* @param currWorld The {@link World} being processed
*/
private static void invalidateExtensionAttemptDiscovery(int worldIndex, World currWorld) {
Settings.WORLD_SERVER_EXTENSION.put(worldIndex, "");
Settings.WORLD_ID.put(worldIndex, "");
currWorld = World.removeExtensionData(currWorld);
discoverExtension(worldIndex, currWorld);
}
/**
* Helper for {@link #validateServerExtensionSettings}
*
* <p>Performs extension discovery via world URL to host matching
*
* @param worldIndex Index corresponding to the world in the Settings map
* @param currWorld The {@link World} being processed
*/
private static void discoverExtension(int worldIndex, World currWorld) {
// Look for an existing subscription based on the world URL
WorldSubscription matchingSub =
Config.getWorldSubscriptionMap().entrySet().stream()
.filter((entry) -> currWorld.hostMatches(entry.getValue().getHostUrl()))
.findFirst()
.map(Map.Entry::getValue)
.orElse(null);
// Extension or subscription don't exist, or the client is out of date
if (matchingSub == null) {
// A defined world ID would be the result of a misconfiguration
Settings.WORLD_ID.put(worldIndex, "");
return;
}
// Download subscription data for the world file
downloadAndVerifyWorldData(matchingSub, worldIndex, currWorld);
}
/**
* Helper for {@link #validateServerExtensionSettings}
*
* <p>Downloads and verifies world data from a {@link WorldSubscription}
*
* @param worldSub The {@link WorldSubscription} used to download world data
* @param worldIndex Index corresponding to the world in the Settings map
* @param currWorld The {@link World} being processed
*/
private static void downloadAndVerifyWorldData(
WorldSubscription worldSub, int worldIndex, World currWorld) {
URI typeDownloadURL = worldSub.getDownloadURIForWorldID(currWorld.getWorldId());
// An existing world ID was defined in the file, so the exact sub URL is known
if (typeDownloadURL != null) {
List<World> fetchedWorlds = Launcher.fetchServerWorlds(typeDownloadURL);
// Couldn't connect to the sub - keep all data
if (fetchedWorlds == null) {
return;
}
Optional<World> matchedWorldOptional =
fetchedWorlds.stream()
.filter(fetchedWorld -> fetchedWorld.connectionEquals(currWorld))
.findAny();
if (matchedWorldOptional.isPresent()) {
final World downloadedWorld = matchedWorldOptional.get();
// Validate the matched world and update world settings
if (validateDownloadedWorld(currWorld, downloadedWorld, worldIndex)) {
upgradeWorldData(worldIndex, downloadedWorld);
}
} else {
// No matching world was found for the download - block connections
// and tell the user to update
Launcher.setWarning(Launcher.LauncherError.WORLD_SUB_MISMATCH);
Launcher.blockedWorlds.add(currWorld);
}
} else {
// Good world ID was not defined, need to query all sub URLs
boolean allDownloadsSucceeded = true;
final List<World> allWorldsForSub = new ArrayList<>();
for (URI uri : worldSub.getWorldDownloadURIs().values()) {
List<World> fetchedWorlds = Launcher.fetchServerWorlds(uri);
if (fetchedWorlds != null) {
allWorldsForSub.addAll(fetchedWorlds);
} else {
allDownloadsSucceeded = false;
}
}
Optional<World> matchedWorldOptional =
allWorldsForSub.stream()
.filter(fetchedWorld -> fetchedWorld.connectionEquals(currWorld))
.findFirst();
// No matching world was found for the download when all downloads succeeded
if (allDownloadsSucceeded && !matchedWorldOptional.isPresent()) {
// Block connections to the world, telling the user to update
Launcher.setWarning(Launcher.LauncherError.WORLD_SUB_MISMATCH);
Launcher.blockedWorlds.add(currWorld);
return;
}
// Note: Don't block connections if all downloads did not succeed
if (matchedWorldOptional.isPresent()) {
// Upgrade the world file if valid data was presented
World downloadedWorld = matchedWorldOptional.get();
if (validateDownloadedWorld(currWorld, downloadedWorld, worldIndex)) {
upgradeWorldData(worldIndex, downloadedWorld);
}
} else {
// Since we can derive the extension, we can set it when it's not defined
if (currWorld.getServerExtension().isEmpty()) {
Extension currExtension = Config.getExtensionForWorldSub(worldSub);
if (currExtension != null) {
Settings.WORLD_SERVER_EXTENSION.put(worldIndex, currExtension.getId());
}
}
// Remove the world ID when it exists, since it could not have been valid
// in order to reach this block in the first place
Settings.WORLD_ID.put(worldIndex, "");
}
}
}
/**
* Helper for {@link #validateServerExtensionSettings}
*
* <p>Verifies data fields from within the successfully-downloaded {@link World}
*
* @param currWorld The {@link World} being processed
* @param downloadedWorld The downloaded {@link World}
* @param worldIndex Index corresponding to the world in the Settings map
* @return {@code boolean} indicating if the downloaded world was valid
*/
public static boolean validateDownloadedWorld(
World currWorld, World downloadedWorld, int worldIndex) {
// First, ensure the sub defined an extension and a world ID
boolean subDataValid =
!downloadedWorld.getServerExtension().isEmpty() && !downloadedWorld.getWorldId().isEmpty();
// Next, ensure the extension data is valid
if (subDataValid) {
WorldSubscription matchedWorldSub =
getWorldSubscription(Extension.from(downloadedWorld.getServerExtension()));
// Ensure the extension and host match and that the world ID is valid for the type
if (matchedWorldSub == null
|| !downloadedWorld.hostMatches(matchedWorldSub.getHostUrl())
|| matchedWorldSub.getWorldDownloadURIs().keySet().stream()
.noneMatch(downloadedWorld::idMatchesWorldType)) {
subDataValid = false;
}
// Ensure the world ID matches the download location
if (subDataValid) {
URI worldIDDownloadURI =
matchedWorldSub.getDownloadURIForWorldID(downloadedWorld.getWorldId());
Optional<List<World>> worldsForType =
Launcher.downloadedWorlds.entrySet().stream()
.filter(entry -> entry.getKey().equals(worldIDDownloadURI))
.map(Map.Entry::getValue)
.filter(Objects::nonNull)
.findFirst();
if (!worldsForType.isPresent() || !worldsForType.get().contains(downloadedWorld)) {
subDataValid = false;
}
}
}
if (!subDataValid) {
// If the world was blocked during initial download due to a domain mismatch,
// the extension data should not be removed such that subsequent launches don't
// create infinite copies. Users won't be able to connect to the world anyway and
// if the sub gets fixed the data will be replaced in the future.
if (Launcher.blockedWorlds.contains(currWorld)
&& Launcher.subWorldFiles.contains(new File(Settings.WORLD_FILE_PATHS.get(worldIndex)))) {
return false;
}
// Block connections to the world, telling the user to update
Launcher.setWarning(Launcher.LauncherError.WORLD_SUB_MISMATCH);
Launcher.blockedWorlds.add(currWorld);
// Remove server extension and world ID if it doesn't match the subscription
Settings.WORLD_SERVER_EXTENSION.put(worldIndex, "");
Settings.WORLD_ID.put(worldIndex, "");
// Update downloaded world data to prevent incorrect re-processing in the future
final World noExtensionWorld = World.removeExtensionData(downloadedWorld);
Launcher.downloadedWorlds.values().stream()
.filter(worlds -> worlds != null && worlds.contains(downloadedWorld))
.forEach(worlds -> worlds.set(worlds.indexOf(downloadedWorld), noExtensionWorld));
return false;
}
return true;
}
/**
* Helper for {@link #validateServerExtensionSettings}
*
* <p>Upgrades certain fields for an existing {@link World} using values from the subscription API
*
* @param worldIndex Index corresponding to the world in the Settings map
* @param downloadedWorld The downloaded {@link World}
*/
private static void upgradeWorldData(int worldIndex, World downloadedWorld) {
// Update world fields
Settings.WORLD_DOWNLOAD_FLAG.put(worldIndex, String.valueOf(true));
Settings.WORLD_REG_API_URL.put(worldIndex, downloadedWorld.getRegistrationApiUrl());
Settings.WORLD_POPULATION_URL.put(worldIndex, downloadedWorld.getWorldPopulationUrl());
Settings.WORLD_HISCORES_URL.put(worldIndex, downloadedWorld.getHiScoresUrl());
Settings.WORLD_SERVER_EXTENSION.put(worldIndex, downloadedWorld.getServerExtension());
Settings.WORLD_ID.put(worldIndex, downloadedWorld.getWorldId());
}
/**
* Get the eTLD+1 for provided world host URL
*
* <p>e.g. "something.example.com" will return "example.com"
*
* @param url Provided host URL as a {@link String}
* @return {@link String} representation of the eTLD+1 or the original value if none exists
*/
private static String getHostDomain(String url) {
if (Util.isIpV4Address(url)) {
// Will not have an eTLD+1, fall back to current URL
return url;
}
final String eTLD1 = Util.getETLD1(url);
if (eTLD1 == null) {
// No real eTLD1, fall back to current URL
return url;
}
return eTLD1;
}
/* Internal classes */
/** Configuration definition for a Server Extension */
public static final class Extension {
private final String id;
private final String name;
private final int hash;
/**
* Private constructor <b>ONLY</b> for use within {@link ServerExtensions}
*
* <p>NOTE: It is CRITICAL that this never gets called outside of bootstrap()
*
* @param id Extension identifier
* @param name Displayable name
*/
private Extension(String id, String name) {
if (Util.isBlank(id)) {
throw new RuntimeException("Extension id cannot be null or empty");
}
// The special "NONE" extension does not have a displayable name value
if (!id.equals("NONE") && Util.isBlank(name)) {
throw new RuntimeException("Extension name cannot be null or empty");
}
if (id.contains(".")) {
throw new RuntimeException("Extension names cannot contain \".\" characters");
}
this.id = id;
this.name = name;
this.hash = id.toLowerCase().hashCode(); // Normalize
}
/**
* Public constructor which performs data validation, that may be used outside of this class
*
* @param id Displayable id for the extension
* @return The constructed {@link Extension} instance
*/
public static Extension from(String id) {
// Special handling for when no extension was defined
if (Util.isBlank(id)) {
return NONE;
}
// Treat undefined extensions as "NONE"
return Config.knownExtensions.stream()
.filter(extension -> extension.getId().equalsIgnoreCase(id))
.findFirst()
.orElse(NONE);
}
/** @return The extension identifier */
public String getId() {
return id;
}
/** @return The displayable name */
public String getName() {
return name;
}
/** Note: should only be used for display purposes */
@Override
public String toString() {
return getId();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return id.equalsIgnoreCase(((Extension) o).id);
}
@Override
public int hashCode() {
return hash;
}
}
/**
* Configuration data required for serving world files from a subscription API
*
* <p><b>The following is required to utilize this feature:</b>
*
* <ol>
* <li>A host URL corresponding to the hosted server location, used to match world files to
* their subscription APIs. It is recommended to make this your server's eTLD+1 value.
* <li>For downloading and managing world files, a set of:
* <ol>
* <li>{@link WorldType} defining a logical grouping of worlds, matching on the world file
* "world ID" field
* <li>Subscription API URL where worlds JSON data is hosted for a given {@link WorldType}
* <li>A boolean flag indicating whether worlds for the type should be downloaded by
* default
* </ol>
* <i>See {@link #addDownloadURL(WorldType, String, boolean)}</i>
* </ol>
*
* <p>For more information on Server Extension usage, see {@code README.md} located in the {@link
* Client.Extensions} package.
*/
public static class WorldSubscription {
String hostUrl;
Set<String> formerHostDomains = new HashSet<>();
Map<WorldType, URI> worldDownloads = new LinkedHashMap<>();
Map<WorldType, Boolean> worldDownloadDefaults = new LinkedHashMap<>();
/**
* Construct a new {@link WorldSubscription}
*
* @param hostUrl {@link String} value representing a host URL, may contain subdomains
*/
private WorldSubscription(String hostUrl) {
if (Util.isBlank(hostUrl)) {
throw new IllegalArgumentException("Host URL must be defined");
}
this.hostUrl = hostUrl;
}
public Map<WorldType, URI> getWorldDownloadURIs() {
return worldDownloads;
}
/**
* Determine and retrieve the download location for a world ID value
*
* @param worldId {@link String} representing the world ID value from a {@link World}
* @return The matched {@link URI}
*/
public URI getDownloadURIForWorldID(String worldId) {
Optional<Map.Entry<WorldType, URI>> typeURL =
worldDownloads.entrySet().stream()
.filter(entry -> entry.getKey().matchesWorldId(worldId))
.findAny();
return typeURL.map(Map.Entry::getValue).orElse(null);
}
public boolean shouldDownloadWorldsByDefault(WorldType worldType) {
return worldDownloadDefaults.get(worldType);
}
public String getHostUrl() {
return hostUrl;
}
public boolean formerDomainMatch(World world) {
return formerHostDomains.stream().anyMatch(world::hostMatches);
}
public Set<String> getFormerHostDomains() {
return formerHostDomains;
}
/**
* <b>ONLY TO BE USED FOR EMERGENCY SITUATIONS</b>
*
* <p>Adds a previously-used host URL to a world subscription, to be used when a world sub host
* URL needs to change.
*
* <p>If for a <b>very good</b> reason it no longer becomes acceptable to continue using a
* particular host domain for a specific world subscription, it is imperative to perform the
* following steps:
*
* <ol>
* <li>Update the host URL for worlds defined in the actual world subscription endpoint
* <li>Update the host URL value for the {@link WorldSubscription} declaration in the
* extension's setup helper method
* <li>Invoke this method on the object, providing the former domain value as the argument
* <li>Request that a new version of RSCPlus be released, containing the updated host names
* </ol>
*
* Until both the client and the world subscription host agree on a common host domain, users
* will be blocked from accessing the world, unless they do not update the application AND the
* subscription API cannot be reached. As such, this method is to only be used in dire
* situations, such as domain theft, where user's credentials are at risk.
*
* @param formerHostUrl The former host URL
*/
private void addFormerHostUrl(String formerHostUrl) {
if (Util.isBlank(formerHostUrl)) {
throw new IllegalArgumentException("Former host URL must not be null or empty");
}
if (Util.isLocalhost(formerHostUrl)) {
throw new IllegalArgumentException("Former host URL must not be the local host");
}
// First, check for equality in case the values are not valid domains
// (ip address, test value, etc.)
if (formerHostUrl.equals(hostUrl)) {
throw new IllegalArgumentException(
"Cannot add a former host URL that matches the current host URL: ["
+ formerHostUrl
+ "]");
}
final String formerHostDomainValue = getHostDomain(formerHostUrl);
final String currentHostDomainValue = getHostDomain(hostUrl);
if (formerHostDomainValue.equals(currentHostDomainValue)) {
throw new IllegalArgumentException(
"Cannot add a former host URL that matches the current host domain: ["
+ formerHostUrl
+ "]");
}
formerHostDomains.add(formerHostDomainValue);
}
/**
* Defines a world data endpoint for a defined {@link WorldSubscription}
*
* <p>For more information on Server Extension usage, see {@code README.md} located in the
* {@link Client.Extensions} package.
*
* @param worldType The {@link WorldType} defining a logical grouping of worlds, matching on the
* world file "world ID" field
* @param urlString {@link String} value for the actual endpoint URL where worlds JSON data will
* be hosted for the above world type
* @param isDefault {@code boolean} indicating whether worlds belonging to this type should be
* downloaded by default
*/
private void addDownloadURL(WorldType worldType, String urlString, boolean isDefault) {
if (Util.isBlank(urlString)) {
throw new IllegalArgumentException("Subscription URL must not be null or empty");
}
final URI uri = URI.create(urlString);
if (worldDownloads.containsKey(worldType)) {
throw new IllegalArgumentException(
"Attempting to add duplicate World Type: [" + worldType + "]");
}
if (worldDownloads.containsValue(uri)) {
throw new IllegalArgumentException(
"Attempting to add duplicate download URL: [" + urlString + "]");
}
worldDownloads.put(worldType, uri);
worldDownloadDefaults.put(worldType, isDefault);
}
}
/**
* Configuration data required when distributing a custom RSCPlus binary for download
*
* <p><b>The following is required to utilize this feature:</b>
*
* <ol>
* <li>A prefix string, used for namespacing config data on disk, e.g. {@code
* ~/.config/<some>RSCPlus} and updating display labels throughout the client
* <li>For performing application updates, a set of:
* <ol>
* <li>A base URL where binary downloads and versions will be located
* <li>The supported os/architecture type identified by {@link BINARY_TYPE}
* <li>Name of the download file
* <li>Name of the version file
* </ol>
* <i>See {@link #addBinaryDownload(BINARY_TYPE, String, String)}</i>
* </ol>
*
* <p>For more information on Server Extension usage, see {@code README.md} located in the {@link
* Client.Extensions} package.
*/
public static class BinaryInfo {
static final String LINUX_APP_IMAGE_SUFFIX = ".new";
private final String configPrefix;
private final String downloadURI;
private final Map<BINARY_TYPE, String> binaryDownloads = new HashMap<>();
private final Map<BINARY_TYPE, String> binaryVersions = new HashMap<>();
/**
* Constructor for {@link BinaryInfo}
*
* @param configPrefix prefix for the binary, used for config directory naming and for updating
* the displayable application name throughout the client
* @param downloadURI Base URL for all available binary downloads
*/
private BinaryInfo(String configPrefix, String downloadURI) {
if (configPrefix == null) {
throw new IllegalArgumentException("Config prefix must not be null");
}
if (configPrefix.length() > 4) {
throw new IllegalArgumentException("Config prefix must be 4 characters or less");
}
if (Config.getBinaryInfoMap().entrySet().stream()
.anyMatch(entry -> entry.getValue().getConfigPrefix().equals(configPrefix))) {
throw new RuntimeException("Config prefix must be unique");
}
if (downloadURI == null) {
throw new RuntimeException("Download URI must not be null");
}
if (!downloadURI.endsWith("/")) {
downloadURI += "/";