1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-11 04:25:53 +00:00

Revert commits that set API key via parameter instead of config.

This reverts commits 84afda30a0 and
d81af707ec.
This commit is contained in:
Felix Ableitner 2014-09-28 20:39:18 +03:00
parent 467dad0438
commit bd281abade
8 changed files with 47 additions and 48 deletions

View file

@ -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());
}
}

View file

@ -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();
}

View file

@ -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());
}
}

View file

@ -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());
}
}

View file

@ -169,17 +169,11 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
*/
private HashMap<String, Model> 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;
}
/**

View file

@ -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");

View file

@ -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<String, String>}.
*/
private class StartupTask extends AsyncTask<Void, Void, String> {
private class StartupTask extends AsyncTask<Void, Void, Pair<String, String>> {
@Override
protected String doInBackground(Void... voids) {
protected Pair<String, String> 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<String, String> 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();
}
}

View file

@ -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.
*
* <p/>
* 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).
*
* <p/>
* 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)