From bd281abade10655d6e4c6dfc0e0257d8f4ead906 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 28 Sep 2014 20:39:18 +0300 Subject: [PATCH] Revert commits that set API key via parameter instead of config. This reverts commits 84afda30a0b3166a8beafd8b1d949fc7bd8bd418 and d81af707ecc4d9bb640e4b6d55fcc4b7bd9b9c3a. --- .../PollWebGuiAvailableTaskTest.java | 4 +-- .../test/syncthing/RestApiTest.java | 5 ++-- .../test/syncthing/SyncthingRunnableTest.java | 6 ----- .../test/util/ConfigXmlTest.java | 1 + .../syncthingandroid/syncthing/RestApi.java | 10 ++----- .../syncthing/SyncthingRunnable.java | 18 +------------ .../syncthing/SyncthingService.java | 25 +++++++++++------- .../syncthingandroid/util/ConfigXml.java | 26 +++++++++++++++++-- 8 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PollWebGuiAvailableTaskTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PollWebGuiAvailableTaskTest.java index 92ab36ed..4fd42255 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PollWebGuiAvailableTaskTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/PollWebGuiAvailableTaskTest.java @@ -46,7 +46,7 @@ public class PollWebGuiAvailableTaskTest extends AndroidTestCase { }.execute(mConfig.getWebGuiUrl()); latch.await(1, TimeUnit.SECONDS); - new PostTask().execute(mConfig.getWebGuiUrl(), PostTask.URI_SHUTDOWN, - mSyncthing.getApiKey()); + new PostTask().execute(mConfig.getWebGuiUrl(), PostTask.URI_SHUTDOWN, mConfig.getApiKey()); + } } diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java index b9d7ce60..4e0f9c97 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/RestApiTest.java @@ -46,14 +46,13 @@ public class RestApiTest extends AndroidTestCase { latch.countDown(); } }.execute(mConfig.getWebGuiUrl()); - mApi = new RestApi(getContext(), mConfig.getWebGuiUrl(), + mApi = new RestApi(getContext(), mConfig.getWebGuiUrl(), mConfig.getApiKey(), new RestApi.OnApiAvailableListener() { @Override public void onApiAvailable() { latch.countDown(); } }); - mApi.setApiKey(mSyncthing.getApiKey()); latch.await(1, TimeUnit.SECONDS); } @@ -68,7 +67,7 @@ public class RestApiTest extends AndroidTestCase { assertTrue(aBoolean); latch.countDown(); } - }.execute(mConfig.getWebGuiUrl(), PostTask.URI_SHUTDOWN, mSyncthing.getApiKey()); + }.execute(mConfig.getWebGuiUrl(), PostTask.URI_SHUTDOWN, mConfig.getApiKey()); latch.await(1, TimeUnit.SECONDS); ConfigXml.getConfigFile(new MockContext(getContext())).delete(); } diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingRunnableTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingRunnableTest.java index c7edc1f6..3eda8cdc 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingRunnableTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/syncthing/SyncthingRunnableTest.java @@ -21,10 +21,4 @@ public class SyncthingRunnableTest extends AndroidTestCase { testFile.delete(); } - @SmallTest - public void testApiKey() { - SyncthingRunnable st = new SyncthingRunnable(new MockContext(getContext()), "ls\n"); - assertEquals(20, st.getApiKey().length()); - } - } diff --git a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java index 84c90fa1..fd0eaf17 100644 --- a/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java +++ b/src/androidTest/java/com/nutomic/syncthingandroid/test/util/ConfigXmlTest.java @@ -59,6 +59,7 @@ public class ConfigXmlTest extends AndroidTestCase { mConfig.updateIfNeeded(); assertNotSame(oldTime, ConfigXml.getConfigFile(mContext).lastModified()); assertNotSame(oldSize, ConfigXml.getConfigFile(mContext).lastModified()); + assertNotNull(mConfig.getApiKey()); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index 5dc9f592..aaa4da04 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -169,17 +169,11 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, */ private HashMap mCachedModelInfo = new HashMap<>(); - public RestApi(Context context, String url, OnApiAvailableListener listener) { + public RestApi(Context context, String url, String apiKey, OnApiAvailableListener listener) { mContext = context; mUrl = url; - mOnApiAvailableListener = listener; - } - - /** - * The API key set in the syncthing instance, required to access the API. - */ - public void setApiKey(String apiKey) { mApiKey = apiKey; + mOnApiAvailableListener = listener; } /** diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java index d455317e..0b9b298f 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java @@ -15,7 +15,6 @@ import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.util.Random; /** * Runs the syncthing binary from command line, and prints its output to logcat. @@ -30,9 +29,7 @@ public class SyncthingRunnable implements Runnable { private final Context mContext; - private final String mCommand; - - private final String mApiKey; + private String mCommand; /** * Constructs instance. @@ -42,18 +39,6 @@ public class SyncthingRunnable implements Runnable { public SyncthingRunnable(Context context, String command) { mContext = context; mCommand = command; - - char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); - StringBuilder sb = new StringBuilder(); - Random random = new Random(); - for (int i = 0; i < 20; i++) { - sb.append(chars[random.nextInt(chars.length)]); - } - mApiKey = sb.toString(); - } - - public String getApiKey() { - return mApiKey; } @Override @@ -67,7 +52,6 @@ public class SyncthingRunnable implements Runnable { dos = new DataOutputStream(process.getOutputStream()); // Set home directory to data folder for syncthing to use. dos.writeBytes("HOME=" + mContext.getFilesDir() + " "); - dos.writeBytes("STGUIAPIKEY=" + mApiKey + " "); dos.writeBytes("STTRACE=" + pm.getString("sttrace", "") + " "); // Call syncthing with -home (as it would otherwise use "~/.config/syncthing/". dos.writeBytes(mCommand + " -home " + mContext.getFilesDir() + "\n"); diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index 8aaf80cc..af46bdb0 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -11,6 +11,7 @@ import android.os.AsyncTask; import android.os.IBinder; import android.preference.PreferenceManager; import android.util.Log; +import android.util.Pair; import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.activities.SettingsActivity; @@ -155,10 +156,8 @@ public class SyncthingService extends Service { mCurrentState = State.STARTING; registerOnWebGuiAvailableListener(mApi); new PollWebGuiAvailableTaskImpl().execute(mConfig.getWebGuiUrl()); - mSyncthingRunnable = - new SyncthingRunnable(this, getApplicationInfo().dataDir + "/" + BINARY_NAME); - mApi.setApiKey(mSyncthingRunnable.getApiKey()); - new Thread(mSyncthingRunnable).start(); + new Thread(new SyncthingRunnable( + this, getApplicationInfo().dataDir + "/" + BINARY_NAME)).start(); } // Stop syncthing. else { @@ -224,11 +223,12 @@ public class SyncthingService extends Service { /** * Sets up the initial configuration, updates the config when coming from an old - * version, and reads syncthing URL.. + * version, and reads syncthing URL and API key (these are passed internally as + * {@code Pair}. */ - private class StartupTask extends AsyncTask { + private class StartupTask extends AsyncTask> { @Override - protected String doInBackground(Void... voids) { + protected Pair doInBackground(Void... voids) { moveConfigFiles(); mConfig = new ConfigXml(SyncthingService.this); mConfig.updateIfNeeded(); @@ -239,12 +239,12 @@ public class SyncthingService extends Service { mConfig.createCameraRepo(); } - return mConfig.getWebGuiUrl(); + return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey()); } @Override - protected void onPostExecute(String webGuiUrl) { - mApi = new RestApi(SyncthingService.this, webGuiUrl, + protected void onPostExecute(Pair urlAndKey) { + mApi = new RestApi(SyncthingService.this, urlAndKey.first, urlAndKey.second, new RestApi.OnApiAvailableListener() { @Override public void onApiAvailable() { @@ -256,6 +256,11 @@ public class SyncthingService extends Service { }); registerOnWebGuiAvailableListener(mApi); Log.i(TAG, "Web GUI will be available at " + mConfig.getWebGuiUrl()); + + // HACK: Make sure there is no syncthing binary left running from an improper + // shutdown (eg Play Store update). + // NOTE: This will log an exception if syncthing is not actually running. + mApi.shutdown(); updateState(); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java index 1f57f825..6a73a25a 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java @@ -15,6 +15,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Random; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -64,12 +65,16 @@ public class ConfigXml { return "http://" + getGuiElement().getElementsByTagName("address").item(0).getTextContent(); } + public String getApiKey() { + return getGuiElement().getElementsByTagName("apikey").item(0).getTextContent(); + } + /** * Updates the config file. - * + *

* Coming from 0.2.0 and earlier, globalAnnounceServer value "announce.syncthing.net:22025" is * replaced with "194.126.249.5:22025" (as domain resolve is broken). - * + *

* Coming from 0.3.0 and earlier, the ignorePerms flag is set to true on every repository. */ @SuppressWarnings("SdCardPath") @@ -78,6 +83,23 @@ public class ConfigXml { boolean changed = false; Element options = (Element) mConfig.getDocumentElement() .getElementsByTagName("options").item(0); + Element gui = (Element) mConfig.getDocumentElement() + .getElementsByTagName("gui").item(0); + + // Create an API key if it does not exist. + if (gui.getElementsByTagName("apikey").getLength() == 0) { + Log.i(TAG, "Initializing API key with random string"); + char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); + StringBuilder sb = new StringBuilder(); + Random random = new Random(); + for (int i = 0; i < 20; i++) { + sb.append(chars[random.nextInt(chars.length)]); + } + Element apiKey = mConfig.createElement("apikey"); + apiKey.setTextContent(sb.toString()); + gui.appendChild(apiKey); + changed = true; + } // Hardcode default globalAnnounceServer ip. Element globalAnnounceServer = (Element)