Skip to content

Commit

Permalink
Add networking prefs to qz-tray.properties (qzind#838)
Browse files Browse the repository at this point in the history
Closes qzind#837
  • Loading branch information
tresf authored Jul 19, 2021
1 parent 5ae57f6 commit 5d884a4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/qz/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Constants {
public static final String LOG_FILE = "debug";
public static final String PROPS_FILE = "qz-tray"; // .properties extension is assumed
public static final String PREFS_FILE = "prefs"; // .properties extension is assumed
public static final String[] PERSIST_PROPS = { "file.whitelist", "file.allow" };
public static final String[] PERSIST_PROPS = { "file.whitelist", "file.allow", "networking.hostname", "networking.port" };
public static final String AUTOSTART_FILE = ".autostart";
public static final String DATA_DIR = "qz";
public static final int LOG_SIZE = 524288;
Expand Down
34 changes: 30 additions & 4 deletions src/qz/utils/NetworkUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

/**
* @author Tres
Expand All @@ -31,6 +32,10 @@ public class NetworkUtilities {
private static String systemName = ShellUtilities.getHostName();
private static String userName = System.getProperty("user.name");

// overridable in preferences, see "networking.hostname", "networking.port"
private static String defaultHostname = "google.com";
private static int defaultPort = 443;

private ArrayList<Device> devices;
private Device primaryDevice;

Expand All @@ -49,11 +54,12 @@ public static NetworkUtilities getInstance(String hostname, int port) {
return instance;
}

public static JSONArray getDevicesJSON(String hostname, int port) throws JSONException {
public static JSONArray getDevicesJSON(JSONObject params) throws JSONException {
JSONArray network = new JSONArray();

try {
for(Device device : getInstance(hostname, port).gatherDevices()) {
for(Device device : getInstance(params.optString("hostname", defaultHostname),
params.optInt("port", defaultPort)).gatherDevices()) {
network.put(device.toJSON());
}
}
Expand All @@ -63,8 +69,9 @@ public static JSONArray getDevicesJSON(String hostname, int port) throws JSONExc
return network;
}

public static JSONObject getDeviceJSON(String hostname, int port) throws JSONException {
Device primary = getInstance(hostname, port).primaryDevice;
public static JSONObject getDeviceJSON(JSONObject params) throws JSONException {
Device primary = getInstance(params.optString("hostname", defaultHostname),
params.optInt("port", defaultPort)).primaryDevice;

if (primary != null) {
return primary.toJSON();
Expand Down Expand Up @@ -106,6 +113,25 @@ private static InetAddress getPrimaryInetAddress(String hostname, int port) {
return null;
}

/**
* Sets the <code>defaultHostname</code> and <code>defaultPort</code> by parsing the properties file
*/
public static void setPreferences(Properties props) {
String hostName = props.getProperty("networking.hostname");
if(hostName != null && !hostName.trim().isEmpty()) {
defaultHostname = hostName;
}

String port = props.getProperty("networking.port");
if(port != null && !port.trim().isEmpty()) {
try {
defaultPort = Integer.parseInt(port);
} catch(Exception parseError) {
log.warn("Unable to parse \"networking.port\"", parseError);
}
}
}

private static class Device {

String mac, ip, id, name;
Expand Down
6 changes: 3 additions & 3 deletions src/qz/ws/PrintSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -573,17 +573,17 @@ private void processMessage(Session session, JSONObject json, SocketConnection c
break;
}
case NETWORKING_DEVICE_LEGACY:
JSONObject networkDevice = NetworkUtilities.getDeviceJSON(params.optString("hostname", "google.com"), params.optInt("port", 443));
JSONObject networkDevice = NetworkUtilities.getDeviceJSON(params);
JSONObject legacyDevice = new JSONObject();
legacyDevice.put("ipAddress", networkDevice.optString("ip", null));
legacyDevice.put("macAddress", networkDevice.optString("mac", null));
sendResult(session, UID, legacyDevice);
break;
case NETWORKING_DEVICE:
sendResult(session, UID, NetworkUtilities.getDeviceJSON(params.optString("hostname", "google.com"), params.optInt("port", 443)));
sendResult(session, UID, NetworkUtilities.getDeviceJSON(params));
break;
case NETWORKING_DEVICES:
sendResult(session, UID, NetworkUtilities.getDevicesJSON(params.optString("hostname", "google.com"), params.optInt("port", 443)));
sendResult(session, UID, NetworkUtilities.getDevicesJSON(params));
break;
case GET_VERSION:
sendResult(session, UID, Constants.VERSION);
Expand Down
8 changes: 4 additions & 4 deletions src/qz/ws/PrintSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import qz.installer.certificate.ExpiryTask;
import qz.installer.certificate.KeyPairWrapper;
import qz.installer.certificate.NativeCertificateInstaller;
import qz.utils.ArgParser;
import qz.utils.ArgValue;
import qz.utils.FileUtilities;
import qz.utils.SystemUtilities;
import qz.utils.*;

import javax.swing.*;
import java.io.File;
Expand Down Expand Up @@ -90,6 +87,9 @@ public static void main(String[] args) {
}
Installer.getInstance().addUserSettings();

// Load overridable preferences set in qz-tray.properties file
NetworkUtilities.setPreferences(certificateManager.getProperties());

// Linux needs the cert installed in user-space on every launch for Chrome SSL to work
if (!SystemUtilities.isWindows() && !SystemUtilities.isMac()) {
NativeCertificateInstaller.getInstance().install(certificateManager.getKeyPair(KeyPairWrapper.Type.CA).getCert());
Expand Down

0 comments on commit 5d884a4

Please sign in to comment.