From 46a998c113065982e0bc0f806d7e3b55a14b7bd6 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 9 Sep 2017 13:39:54 -0700 Subject: [PATCH] Convert address fields to strings to better manage DNS names --- .../computers/ComputerDatabaseManager.java | 20 +++------ .../computers/ComputerManagerService.java | 42 +++++++++---------- .../grid/assets/NetworkAssetLoader.java | 3 +- .../preferences/AddComputerManually.java | 18 +++----- .../com/limelight/utils/ServerHelper.java | 7 ++-- moonlight-common | 2 +- 6 files changed, 38 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/limelight/computers/ComputerDatabaseManager.java b/app/src/main/java/com/limelight/computers/ComputerDatabaseManager.java index 142beb0a..54a8d88f 100644 --- a/app/src/main/java/com/limelight/computers/ComputerDatabaseManager.java +++ b/app/src/main/java/com/limelight/computers/ComputerDatabaseManager.java @@ -62,8 +62,8 @@ public class ComputerDatabaseManager { ContentValues values = new ContentValues(); values.put(COMPUTER_NAME_COLUMN_NAME, details.name); values.put(COMPUTER_UUID_COLUMN_NAME, details.uuid.toString()); - values.put(LOCAL_IP_COLUMN_NAME, ADDRESS_PREFIX+details.localAddress.getHostAddress()); - values.put(REMOTE_IP_COLUMN_NAME, ADDRESS_PREFIX+details.remoteAddress.getHostAddress()); + values.put(LOCAL_IP_COLUMN_NAME, ADDRESS_PREFIX+details.localAddress); + values.put(REMOTE_IP_COLUMN_NAME, ADDRESS_PREFIX+details.remoteAddress); values.put(MAC_COLUMN_NAME, details.macAddress); return -1 != computerDb.insertWithOnConflict(COMPUTER_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE); } @@ -86,17 +86,13 @@ public class ComputerDatabaseManager { // too. To disambiguate, we'll need to prefix them with a string // greater than the allowable IP address length. try { - details.localAddress = InetAddress.getByAddress(c.getBlob(2)); + details.localAddress = InetAddress.getByAddress(c.getBlob(2)).getHostAddress(); LimeLog.warning("DB: Legacy local address for "+details.name); } catch (UnknownHostException e) { // This is probably a hostname/address with the prefix string String stringData = c.getString(2); if (stringData.startsWith(ADDRESS_PREFIX)) { - try { - details.localAddress = InetAddress.getByName(c.getString(2).substring(ADDRESS_PREFIX.length())); - } catch (UnknownHostException e1) { - e1.printStackTrace(); - } + details.localAddress = c.getString(2).substring(ADDRESS_PREFIX.length()); } else { LimeLog.severe("DB: Corrupted local address for "+details.name); @@ -104,17 +100,13 @@ public class ComputerDatabaseManager { } try { - details.remoteAddress = InetAddress.getByAddress(c.getBlob(3)); + details.remoteAddress = InetAddress.getByAddress(c.getBlob(3)).getHostAddress(); LimeLog.warning("DB: Legacy remote address for "+details.name); } catch (UnknownHostException e) { // This is probably a hostname/address with the prefix string String stringData = c.getString(3); if (stringData.startsWith(ADDRESS_PREFIX)) { - try { - details.remoteAddress = InetAddress.getByName(c.getString(3).substring(ADDRESS_PREFIX.length())); - } catch (UnknownHostException e1) { - e1.printStackTrace(); - } + details.remoteAddress = c.getString(3).substring(ADDRESS_PREFIX.length()); } else { LimeLog.severe("DB: Corrupted local address for "+details.name); diff --git a/app/src/main/java/com/limelight/computers/ComputerManagerService.java b/app/src/main/java/com/limelight/computers/ComputerManagerService.java index d7f5e8c7..36e04732 100644 --- a/app/src/main/java/com/limelight/computers/ComputerManagerService.java +++ b/app/src/main/java/com/limelight/computers/ComputerManagerService.java @@ -155,7 +155,7 @@ public class ComputerManagerService extends Service { } } }; - t.setName("Polling thread for " + tuple.computer.localAddress.getHostAddress()); + t.setName("Polling thread for " + tuple.computer.localAddress); return t; } @@ -211,7 +211,7 @@ public class ComputerManagerService extends Service { } } - public boolean addComputerBlocking(InetAddress addr) { + public boolean addComputerBlocking(String addr) { return ComputerManagerService.this.addComputerBlocking(addr); } @@ -290,7 +290,7 @@ public class ComputerManagerService extends Service { @Override public void notifyComputerAdded(MdnsComputer computer) { // Kick off a serverinfo poll on this machine - addComputerBlocking(computer.getAddress()); + addComputerBlocking(computer.getAddress().getHostAddress()); } @Override @@ -339,7 +339,7 @@ public class ComputerManagerService extends Service { } } - public boolean addComputerBlocking(InetAddress addr) { + public boolean addComputerBlocking(String addr) { // Setup a placeholder ComputerDetails fakeDetails = new ComputerDetails(); fakeDetails.localAddress = addr; @@ -405,14 +405,14 @@ public class ComputerManagerService extends Service { } } - private ComputerDetails tryPollIp(ComputerDetails details, InetAddress ipAddr) { + private ComputerDetails tryPollIp(ComputerDetails details, String address) { // Fast poll this address first to determine if we can connect at the TCP layer - if (!fastPollIp(ipAddr)) { + if (!fastPollIp(address)) { return null; } try { - NvHTTP http = new NvHTTP(ipAddr, idManager.getUniqueId(), + NvHTTP http = new NvHTTP(address, idManager.getUniqueId(), null, PlatformBinding.getCryptoProvider(ComputerManagerService.this)); ComputerDetails newDetails = http.getComputerDetails(); @@ -433,10 +433,10 @@ public class ComputerManagerService extends Service { // Just try to establish a TCP connection to speculatively detect a running // GFE server - private boolean fastPollIp(InetAddress addr) { + private boolean fastPollIp(String address) { Socket s = new Socket(); try { - s.connect(new InetSocketAddress(addr, NvHTTP.HTTPS_PORT), FAST_POLL_TIMEOUT); + s.connect(new InetSocketAddress(address, NvHTTP.HTTPS_PORT), FAST_POLL_TIMEOUT); s.close(); return true; } catch (IOException e) { @@ -444,11 +444,11 @@ public class ComputerManagerService extends Service { } } - private void startFastPollThread(final InetAddress addr, final boolean[] info) { + private void startFastPollThread(final String address, final boolean[] info) { Thread t = new Thread() { @Override public void run() { - boolean pollRes = fastPollIp(addr); + boolean pollRes = fastPollIp(address); synchronized (info) { info[0] = true; // Done @@ -458,16 +458,16 @@ public class ComputerManagerService extends Service { } } }; - t.setName("Fast Poll - "+addr.getHostAddress()); + t.setName("Fast Poll - "+address); t.start(); } - private ComputerDetails.Reachability fastPollPc(final InetAddress local, final InetAddress remote) throws InterruptedException { + private ComputerDetails.Reachability fastPollPc(final String localAddress, final String remoteAddress) throws InterruptedException { final boolean[] remoteInfo = new boolean[2]; final boolean[] localInfo = new boolean[2]; - startFastPollThread(local, localInfo); - startFastPollThread(remote, remoteInfo); + startFastPollThread(localAddress, localInfo); + startFastPollThread(remoteAddress, remoteInfo); // Check local first synchronized (localInfo) { @@ -524,7 +524,7 @@ public class ComputerManagerService extends Service { polledDetails = tryPollIp(details, details.remoteAddress); } - InetAddress reachableAddr = null; + String reachableAddr = null; if (polledDetails == null && !details.localAddress.equals(details.remoteAddress)) { // Failed, so let's try the fallback if (!localFirst) { @@ -708,12 +708,12 @@ public class ComputerManagerService extends Service { continue; } - NvHTTP http = new NvHTTP(ServerHelper.getCurrentAddressFromComputer(computer), idManager.getUniqueId(), - null, PlatformBinding.getCryptoProvider(ComputerManagerService.this)); - PollingTuple tuple = getPollingTuple(computer); try { + NvHTTP http = new NvHTTP(ServerHelper.getCurrentAddressFromComputer(computer), idManager.getUniqueId(), + null, PlatformBinding.getCryptoProvider(ComputerManagerService.this)); + String appList; if (tuple != null) { // If we're polling this machine too, grab the network lock @@ -808,10 +808,10 @@ class PollingTuple { } class ReachabilityTuple { - public final InetAddress reachableAddress; + public final String reachableAddress; public final ComputerDetails computer; - public ReachabilityTuple(ComputerDetails computer, InetAddress reachableAddress) { + public ReachabilityTuple(ComputerDetails computer, String reachableAddress) { this.computer = computer; this.reachableAddress = reachableAddress; } diff --git a/app/src/main/java/com/limelight/grid/assets/NetworkAssetLoader.java b/app/src/main/java/com/limelight/grid/assets/NetworkAssetLoader.java index 8506f045..7e8adea7 100644 --- a/app/src/main/java/com/limelight/grid/assets/NetworkAssetLoader.java +++ b/app/src/main/java/com/limelight/grid/assets/NetworkAssetLoader.java @@ -22,10 +22,9 @@ public class NetworkAssetLoader { } public InputStream getBitmapStream(CachedAppAssetLoader.LoaderTuple tuple) { - NvHTTP http = new NvHTTP(ServerHelper.getCurrentAddressFromComputer(tuple.computer), uniqueId, null, PlatformBinding.getCryptoProvider(context)); - InputStream in = null; try { + NvHTTP http = new NvHTTP(ServerHelper.getCurrentAddressFromComputer(tuple.computer), uniqueId, null, PlatformBinding.getCryptoProvider(context)); in = http.getBoxArt(tuple.app); } catch (IOException ignored) {} diff --git a/app/src/main/java/com/limelight/preferences/AddComputerManually.java b/app/src/main/java/com/limelight/preferences/AddComputerManually.java index 36158bf4..26e0f81f 100644 --- a/app/src/main/java/com/limelight/preferences/AddComputerManually.java +++ b/app/src/main/java/com/limelight/preferences/AddComputerManually.java @@ -50,18 +50,12 @@ public class AddComputerManually extends Activity { SpinnerDialog dialog = SpinnerDialog.displayDialog(this, getResources().getString(R.string.title_add_pc), getResources().getString(R.string.msg_add_pc), false); - try { - InetAddress addr = InetAddress.getByName(host); - - if (!managerBinder.addComputerBlocking(addr)){ - msg = getResources().getString(R.string.addpc_fail); - } - else { - msg = getResources().getString(R.string.addpc_success); - finish = true; - } - } catch (UnknownHostException e) { - msg = getResources().getString(R.string.addpc_unknown_host); + if (!managerBinder.addComputerBlocking(host)){ + msg = getResources().getString(R.string.addpc_fail); + } + else { + msg = getResources().getString(R.string.addpc_success); + finish = true; } dialog.dismiss(); diff --git a/app/src/main/java/com/limelight/utils/ServerHelper.java b/app/src/main/java/com/limelight/utils/ServerHelper.java index c05736a5..db339843 100644 --- a/app/src/main/java/com/limelight/utils/ServerHelper.java +++ b/app/src/main/java/com/limelight/utils/ServerHelper.java @@ -18,7 +18,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; public class ServerHelper { - public static InetAddress getCurrentAddressFromComputer(ComputerDetails computer) { + public static String getCurrentAddressFromComputer(ComputerDetails computer) { return computer.reachability == ComputerDetails.Reachability.LOCAL ? computer.localAddress : computer.remoteAddress; } @@ -26,8 +26,7 @@ public class ServerHelper { public static Intent createStartIntent(Activity parent, NvApp app, ComputerDetails computer, ComputerManagerService.ComputerManagerBinder managerBinder) { Intent intent = new Intent(parent, Game.class); - intent.putExtra(Game.EXTRA_HOST, - getCurrentAddressFromComputer(computer).getHostAddress()); + intent.putExtra(Game.EXTRA_HOST, getCurrentAddressFromComputer(computer)); intent.putExtra(Game.EXTRA_APP_NAME, app.getAppName()); intent.putExtra(Game.EXTRA_APP_ID, app.getAppId()); intent.putExtra(Game.EXTRA_UNIQUEID, managerBinder.getUniqueId()); @@ -44,7 +43,7 @@ public class ServerHelper { } public static void doQuit(final Activity parent, - final InetAddress address, + final String address, final NvApp app, final ComputerManagerService.ComputerManagerBinder managerBinder, final Runnable onComplete) { diff --git a/moonlight-common b/moonlight-common index bea3830a..2c431919 160000 --- a/moonlight-common +++ b/moonlight-common @@ -1 +1 @@ -Subproject commit bea3830a63b4a8c537dc2f22c8b91e24c0545155 +Subproject commit 2c4319196f62bac1d12d498914b0f4b6f1086e99