Skip to content

Commit b3fd647

Browse files
authored
Merge pull request #150 from CodeDead/feature/http-proxy
feat: add proxy configuration support with encryption and update test…
2 parents 9cb8f9d + cdb7780 commit b3fd647

25 files changed

Lines changed: 1148 additions & 235 deletions

‎src/main/java/com/codedead/opal/OpalApplication.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codedead.opal;
22

3+
import com.codedead.opal.controller.LanguageController;
34
import com.codedead.opal.controller.ThemeController;
45
import com.codedead.opal.controller.UpdateController;
56
import com.codedead.opal.utils.FxUtils;
@@ -35,6 +36,11 @@ public class OpalApplication extends Application {
3536
* @param args The application arguments
3637
*/
3738
public static void main(final String[] args) {
39+
// The JDK disables Basic authentication for HTTPS proxy tunneling by default
40+
if (System.getProperty("jdk.http.auth.tunneling.disabledSchemes") == null) {
41+
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
42+
}
43+
3844
System.setProperty("logBasePath", SharedVariables.PROPERTIES_BASE_PATH);
3945
logger = LogManager.getLogger(OpalApplication.class);
4046

@@ -85,7 +91,7 @@ public void start(final Stage primaryStage) {
8591

8692
ThemeController.setTheme(properties.getProperty("theme", "light"));
8793

88-
final Locale locale = Locale.forLanguageTag(languageTag);
94+
final Locale locale = LanguageController.getLocaleFromLanguageTag(languageTag);
8995
final ResourceBundle translationBundle = ResourceBundle.getBundle("translations.OpalApplication", locale);
9096

9197
final FXMLLoader loader = new FXMLLoader(getClass().getResource("/windows/MainWindow.fxml"), translationBundle);

‎src/main/java/com/codedead/opal/controller/LanguageController.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.codedead.opal.controller;
22

3+
import java.util.Locale;
4+
35
import static com.codedead.opal.utils.SharedVariables.DEFAULT_LOCALE;
46

57
public final class LanguageController {
@@ -23,7 +25,7 @@ public static int getLanguageIndexFromLocale(final String locale) {
2325
case "es-es" -> 2;
2426
case "fr-fr" -> 3;
2527
case "hi-in" -> 4;
26-
case "jp-jp" -> 5;
28+
case "ja-jp", "jp-jp" -> 5;
2729
case "nl-nl" -> 6;
2830
case "ru-ru" -> 7;
2931
case "tr-tr" -> 8;
@@ -33,6 +35,17 @@ public static int getLanguageIndexFromLocale(final String locale) {
3335
};
3436
}
3537

38+
/**
39+
* Get the locale from the language tag, migrating the legacy Japanese tag
40+
*
41+
* @param languageTag The language tag
42+
* @return The locale
43+
*/
44+
public static Locale getLocaleFromLanguageTag(final String languageTag) {
45+
final String normalizedLanguageTag = "jp-JP".equalsIgnoreCase(languageTag) ? "ja-JP" : languageTag;
46+
return Locale.forLanguageTag(normalizedLanguageTag);
47+
}
48+
3649
/**
3750
* Get the locale from the language index
3851
*
@@ -45,7 +58,7 @@ public static String getLocaleFromLanguageIndex(final int index) {
4558
case 2 -> "es-ES";
4659
case 3 -> "fr-FR";
4760
case 4 -> "hi-in";
48-
case 5 -> "jp-JP";
61+
case 5 -> "ja-JP";
4962
case 6 -> "nl-NL";
5063
case 7 -> "ru-RU";
5164
case 8 -> "tr-TR";

‎src/main/java/com/codedead/opal/controller/MainWindowController.java‎

Lines changed: 89 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.codedead.opal.interfaces.TrayIconListener;
77
import com.codedead.opal.utils.FxUtils;
88
import com.codedead.opal.utils.HelpUtils;
9+
import com.codedead.opal.utils.PasswordCipher;
910
import com.codedead.opal.utils.RunnableFileOpener;
1011
import com.codedead.opal.utils.SharedVariables;
1112
import javafx.application.Platform;
@@ -34,9 +35,9 @@
3435
import java.io.File;
3536
import java.io.IOException;
3637
import java.io.InputStreamReader;
37-
import java.net.URISyntaxException;
3838
import java.nio.file.Files;
3939
import java.nio.file.Path;
40+
import java.security.GeneralSecurityException;
4041
import java.util.*;
4142

4243
import static com.codedead.opal.utils.SharedVariables.DEFAULT_LOCALE;
@@ -197,6 +198,7 @@ public final class MainWindowController implements IAudioTimer, TrayIconListener
197198
private TimerTask countDownTask;
198199
private final String platformName;
199200
private final HelpUtils helpUtils;
201+
private final PasswordCipher passwordCipher;
200202
private final JsonMapper jsonMapper;
201203
private final Timer timer;
202204
private final Timer countDownTimer;
@@ -215,6 +217,7 @@ public MainWindowController() {
215217

216218
platformName = OsCheck.getOperatingSystemType().name();
217219
helpUtils = new HelpUtils();
220+
passwordCipher = new PasswordCipher();
218221

219222
this.timer = new Timer();
220223
this.countDownTimer = new Timer();
@@ -255,7 +258,7 @@ public void setControllers(final SettingsController settingsController, final Up
255258

256259
logger.info("Attempting to load the ResourceBundle for locale {}", languageTag);
257260

258-
final Locale locale = Locale.forLanguageTag(languageTag);
261+
final Locale locale = LanguageController.getLocaleFromLanguageTag(languageTag);
259262
resourceFactory.setResources(ResourceBundle.getBundle("translations.OpalApplication", locale));
260263

261264
final boolean mediaButtons = Boolean.parseBoolean(properties.getProperty("mediaButtons", "true"));
@@ -364,6 +367,37 @@ public void loadMediaButtonVisibility(final boolean visible) {
364367
getSoundPanes(grpCustom).forEach(s -> s.setMediaButton(visible));
365368
}
366369

370+
/**
371+
* Configure the proxy that should be used when checking for updates
372+
*/
373+
private void configureUpdateProxy() {
374+
final Properties properties = settingsController.getProperties();
375+
final boolean proxyEnabled = Boolean.parseBoolean(properties.getProperty("updateProxyEnabled", "false"));
376+
final boolean authenticationEnabled = proxyEnabled && Boolean.parseBoolean(properties.getProperty("updateProxyAuthEnabled", "false"));
377+
final String proxyPort = properties.getProperty("updateProxyPort", "8080").trim();
378+
final int port = proxyPort.isBlank() ? 8080 : Integer.parseInt(proxyPort);
379+
380+
updateController.setProxy(proxyEnabled, properties.getProperty("updateProxyHost", ""), port);
381+
updateController.setProxyAuthentication(
382+
authenticationEnabled,
383+
properties.getProperty("updateProxyUsername", ""),
384+
authenticationEnabled ? decryptProxyPassword(properties.getProperty("updateProxyPassword", "")) : "");
385+
}
386+
387+
/**
388+
* Decrypt the stored proxy password
389+
*
390+
* @param storedPassword The stored password
391+
* @return The decrypted password
392+
*/
393+
private String decryptProxyPassword(final String storedPassword) {
394+
try {
395+
return passwordCipher.decrypt(storedPassword);
396+
} catch (final IOException | GeneralSecurityException ex) {
397+
throw new IllegalArgumentException("Unable to decrypt the proxy password", ex);
398+
}
399+
}
400+
367401
/**
368402
* Check for application updates
369403
*
@@ -374,6 +408,8 @@ private void checkForUpdates(final boolean showNoUpdates, final boolean showErro
374408
logger.info("Attempting to check for updates");
375409

376410
try {
411+
configureUpdateProxy();
412+
377413
final Optional<PlatformUpdate> platformUpdate = updateController.checkForUpdates(platformName, SharedVariables.PORTABLE);
378414
if (platformUpdate.isPresent()) {
379415
final PlatformUpdate update = platformUpdate.get();
@@ -413,7 +449,7 @@ private void checkForUpdates(final boolean showNoUpdates, final boolean showErro
413449
FxUtils.showInformationAlert(resourceFactory.getResources().getString("NoUpdateAvailable"), getClass().getResourceAsStream(SharedVariables.ICON_URL));
414450
}
415451
}
416-
} catch (final InterruptedException | IOException | InvalidHttpResponseCodeException | URISyntaxException ex) {
452+
} catch (final InterruptedException | IOException | InvalidHttpResponseCodeException | IllegalArgumentException ex) {
417453
if (ex instanceof InterruptedException)
418454
Thread.currentThread().interrupt();
419455

@@ -461,6 +497,33 @@ private List<SoundPane> getSoundPanes(final GridPane parent) {
461497
return elements;
462498
}
463499

500+
/**
501+
* Filter the {@link SoundPane} objects in a {@link GridPane} based on a search query
502+
*
503+
* @param gridPane The {@link GridPane} containing the {@link SoundPane} objects
504+
* @param query The trimmed, lower-case query to filter on
505+
*/
506+
private void filterSoundPanes(final GridPane gridPane, final String query) {
507+
getSoundPanes(gridPane).forEach(e -> {
508+
e.setVisible(e.getName().toLowerCase().contains(query));
509+
e.setManaged(e.isVisible());
510+
});
511+
}
512+
513+
/**
514+
* Update the visibility of a {@link TitledPane} based on whether any of its {@link SoundPane} children are visible
515+
*
516+
* @param titledPane The {@link TitledPane} to update
517+
* @param gridPane The {@link GridPane} containing the {@link SoundPane} objects
518+
*/
519+
private void updateTitledPaneVisibility(final TitledPane titledPane, final GridPane gridPane) {
520+
final boolean hasVisibleSoundPane = getSoundPanes(gridPane).stream().anyMatch(Node::isVisible);
521+
522+
titledPane.setExpanded(hasVisibleSoundPane);
523+
titledPane.setVisible(hasVisibleSoundPane);
524+
titledPane.setManaged(hasVisibleSoundPane);
525+
}
526+
464527
/**
465528
* Open a file on the local filesystem
466529
*
@@ -559,86 +622,25 @@ private void initialize() {
559622
}
560623

561624
Platform.runLater(() -> {
562-
getAllSoundPanes()
563-
.forEach(e -> {
564-
e.setVisible(e.getName().toLowerCase().contains(newValue.trim().toLowerCase()));
565-
e.setManaged(e.isVisible());
566-
});
625+
final String query = newValue.trim().toLowerCase();
567626

568-
getSoundPanes(grpCustom)
569-
.forEach(e -> {
570-
e.setVisible(e.getName().toLowerCase().contains(newValue.trim().toLowerCase()));
571-
e.setManaged(e.isVisible());
572-
});
627+
filterSoundPanes(grpNature, query);
628+
filterSoundPanes(grpOffice, query);
629+
filterSoundPanes(grpAudiences, query);
630+
filterSoundPanes(grpRadioFrequencyStatic, query);
631+
filterSoundPanes(grpOther, query);
632+
filterSoundPanes(grpCustom, query);
573633

574-
// Check if there are still active sound panes on pneCustom
634+
// Hide the TitledPanes that no longer contain any visible SoundPane
575635
final List<SoundPane> customSoundPanes = getSoundPanes(grpCustom);
576636
if (!customSoundPanes.isEmpty()) {
577-
customSoundPanes.stream().filter(Node::isVisible).findFirst().ifPresentOrElse(_ -> {
578-
pneCustom.setExpanded(true);
579-
pneCustom.setVisible(true);
580-
pneCustom.setManaged(true);
581-
}, () -> {
582-
pneCustom.setExpanded(false);
583-
pneCustom.setVisible(false);
584-
pneCustom.setManaged(false);
585-
});
637+
updateTitledPaneVisibility(pneCustom, grpCustom);
586638
}
587-
588-
// Check if there are still active sound panes on pneNature
589-
getSoundPanes(grpNature).stream().filter(Node::isVisible).findFirst().ifPresentOrElse(_ -> {
590-
pneNature.setExpanded(true);
591-
pneNature.setVisible(true);
592-
pneNature.setManaged(true);
593-
}, () -> {
594-
pneNature.setExpanded(false);
595-
pneNature.setVisible(false);
596-
pneNature.setManaged(false);
597-
});
598-
599-
// Check if there are still active sound panes on pneOffice
600-
getSoundPanes(grpOffice).stream().filter(Node::isVisible).findFirst().ifPresentOrElse(_ -> {
601-
pneOffice.setExpanded(true);
602-
pneOffice.setVisible(true);
603-
pneOffice.setManaged(true);
604-
}, () -> {
605-
pneOffice.setExpanded(false);
606-
pneOffice.setVisible(false);
607-
pneOffice.setManaged(false);
608-
});
609-
610-
// Check if there are still active sound panes on pneAudiences
611-
getSoundPanes(grpAudiences).stream().filter(Node::isVisible).findFirst().ifPresentOrElse(_ -> {
612-
pneAudiences.setExpanded(true);
613-
pneAudiences.setVisible(true);
614-
pneAudiences.setManaged(true);
615-
}, () -> {
616-
pneAudiences.setExpanded(false);
617-
pneAudiences.setVisible(false);
618-
pneAudiences.setManaged(false);
619-
});
620-
621-
// Check if there are still active sound panes on pneRadioFrequencyStatic
622-
getSoundPanes(grpRadioFrequencyStatic).stream().filter(Node::isVisible).findFirst().ifPresentOrElse(_ -> {
623-
pneRadioFrequencyStatic.setExpanded(true);
624-
pneRadioFrequencyStatic.setVisible(true);
625-
pneRadioFrequencyStatic.setManaged(true);
626-
}, () -> {
627-
pneRadioFrequencyStatic.setExpanded(false);
628-
pneRadioFrequencyStatic.setVisible(false);
629-
pneRadioFrequencyStatic.setManaged(false);
630-
});
631-
632-
// Check if there are still active sound panes on pneOther
633-
getSoundPanes(grpOther).stream().filter(Node::isVisible).findFirst().ifPresentOrElse(_ -> {
634-
pneOther.setExpanded(true);
635-
pneOther.setVisible(true);
636-
pneOther.setManaged(true);
637-
}, () -> {
638-
pneOther.setExpanded(false);
639-
pneOther.setVisible(false);
640-
pneOther.setManaged(false);
641-
});
639+
updateTitledPaneVisibility(pneNature, grpNature);
640+
updateTitledPaneVisibility(pneOffice, grpOffice);
641+
updateTitledPaneVisibility(pneAudiences, grpAudiences);
642+
updateTitledPaneVisibility(pneRadioFrequencyStatic, grpRadioFrequencyStatic);
643+
updateTitledPaneVisibility(pneOther, grpOther);
642644

643645
btnClearSearch.setVisible(true);
644646
btnClearSearch.setManaged(true);
@@ -1019,16 +1021,17 @@ public void fired() {
10191021
try {
10201022
final ProcessBuilder p = new ProcessBuilder(command.split(" "));
10211023

1022-
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(p.start().getInputStream()))) {
1023-
final StringBuilder builder = new StringBuilder();
1024-
String line;
1025-
while ((line = reader.readLine()) != null) {
1026-
builder.append(line);
1027-
builder.append(System.lineSeparator());
1024+
try (final Process process = p.start()) {
1025+
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
1026+
final StringBuilder output = new StringBuilder();
1027+
String line;
1028+
while ((line = reader.readLine()) != null) {
1029+
output.append(line).append(System.lineSeparator());
1030+
}
1031+
logger.info("Shutdown command result: {}", output.toString());
10281032
}
1029-
final String result = builder.toString();
1030-
logger.info("Shutdown command result: {}", result);
10311033
}
1034+
10321035
exitAction();
10331036
} catch (final IOException ex) {
10341037
logger.error("Unable to execute shutdown command", ex);

0 commit comments

Comments
 (0)