Start up config handling at app startup

This commit is contained in:
Felix Ableitner 2017-09-04 10:55:11 +09:00
parent f4e1b077d3
commit 679841a6ac
2 changed files with 14 additions and 9 deletions

View File

@ -337,6 +337,7 @@ public class SyncthingService extends Service implements
protected Pair<URL, String> doInBackground(Void... voids) { protected Pair<URL, String> doInBackground(Void... voids) {
try { try {
mConfig = new ConfigXml(SyncthingService.this); mConfig = new ConfigXml(SyncthingService.this);
mConfig.updateIfNeeded();
return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey()); return new Pair<>(mConfig.getWebGuiUrl(), mConfig.getApiKey());
} catch (ConfigXml.OpenConfigException e) { } catch (ConfigXml.OpenConfigException e) {
return null; return null;

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
@ -75,7 +76,6 @@ public class ConfigXml {
changeLocalDeviceName(); changeLocalDeviceName();
changeDefaultFolder(); changeDefaultFolder();
} }
updateIfNeeded();
} }
private void generateKeysConfig(Context context) { private void generateKeysConfig(Context context) {
@ -104,11 +104,12 @@ public class ConfigXml {
/** /**
* Updates the config file. * Updates the config file.
* <p/> *
* Sets ignorePerms flag to true on every folder. * Sets ignorePerms flag to true on every folder, force enables TLS, and sets the
* username/password.
*/ */
@SuppressWarnings("SdCardPath") @SuppressWarnings("SdCardPath")
private void updateIfNeeded() { public void updateIfNeeded() {
Log.i(TAG, "Checking for needed config updates"); Log.i(TAG, "Checking for needed config updates");
boolean changed = false; boolean changed = false;
NodeList folders = mConfig.getDocumentElement().getElementsByTagName("folder"); NodeList folders = mConfig.getDocumentElement().getElementsByTagName("folder");
@ -156,14 +157,17 @@ public class ConfigXml {
} }
String apikey = getApiKey(); String apikey = getApiKey();
boolean passwordOk; boolean passwordOk;
try { // Version 0.9.12 used the default work factor of 10. If this is set, regenerate the
passwordOk = BCrypt.checkpw(apikey, password.getTextContent()); // password with the minimum factor of 4, to speed up loading.
} catch (RuntimeException e) { String pw = password.getTextContent();
Log.w(TAG, e); if (TextUtils.isEmpty(pw) || pw.substring(4, 6).equals("10")) {
passwordOk = false; passwordOk = false;
} else {
passwordOk = BCrypt.checkpw(apikey, pw);
} }
if (!passwordOk) { if (!passwordOk) {
password.setTextContent(BCrypt.hashpw(apikey, BCrypt.gensalt())); Log.i(TAG, "Updating password");
password.setTextContent(BCrypt.hashpw(apikey, BCrypt.gensalt(4)));
changed = true; changed = true;
} }