1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 19:31:30 +00:00

Replace new announce servers.

This commit is contained in:
Felix Ableitner 2015-10-01 00:25:58 +02:00
parent d9b4c0cbf8
commit af341a80a5

View file

@ -3,10 +3,7 @@ package com.nutomic.syncthingandroid.util;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.SyncthingRunnable; import com.nutomic.syncthingandroid.syncthing.SyncthingRunnable;
@ -19,6 +16,7 @@ import org.xml.sax.SAXException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -42,6 +40,21 @@ public class ConfigXml {
private static final String TAG = "ConfigXml"; private static final String TAG = "ConfigXml";
private static final String[] REPLACE_DISCOVERY_SERVERS = new String[] {
"default",
"udp4://194.126.249.5:22026",
"udp6://[2001:470:28:4d6::5]:22026"
};
private static final String[] DISCOVERY_SERVER_IPS = new String[] {
"https://194.126.249.5/?id=SR7AARM-TCBUZ5O-VFAXY4D-CECGSDE-3Q6IZ4G-XG7AH75-OBIXJQV-QJ6NLQA",
"https://45.55.230.38/?id=AQEHEO2-XOS7QRA-X2COH5K-PO6OPVA-EWOSEGO-KZFMD32-XJ4ZV46-CUUVKAS",
"https://128.199.95.124/?id=7WT2BVR-FX62ZOW-TNVVW25-6AHFJGD-XEXQSBW-VO3MPL2-JBTLL4T-P4572Q4",
"https://[2001:470:28:4d6::5]/?id=SR7AARM-TCBUZ5O-VFAXY4D-CECGSDE-3Q6IZ4G-XG7AH75-OBIXJQV-QJ6NLQA",
"https://[2604:a880:800:10::182:a001]/?id=AQEHEO2-XOS7QRA-X2COH5K-PO6OPVA-EWOSEGO-KZFMD32-XJ4ZV46-CUUVKAS",
"https://[2400:6180:0:d0::d9:d001]/?id=7WT2BVR-FX62ZOW-TNVVW25-6AHFJGD-XEXQSBW-VO3MPL2-JBTLL4T-P4572Q4",
};
/** /**
* File in the config folder that contains configuration. * File in the config folder that contains configuration.
*/ */
@ -122,21 +135,9 @@ public class ConfigXml {
Element options = (Element) mConfig.getDocumentElement() Element options = (Element) mConfig.getDocumentElement()
.getElementsByTagName("options").item(0); .getElementsByTagName("options").item(0);
// Hardcode default globalAnnounceServer ip. if (replaceAnnounceServers(options)) {
NodeList globalAnnounceServer = options.getElementsByTagName("globalAnnounceServer");
for (int i = 0; i < globalAnnounceServer.getLength(); i++) {
Element g = (Element) globalAnnounceServer.item(i);
if (g.getTextContent().equals("udp4://announce.syncthing.net:22026")) {
Log.i(TAG, "Replacing globalAnnounceServer address with ip");
g.setTextContent("udp4://194.126.249.5:22026");
changed = true; changed = true;
} }
if (g.getTextContent().equals("udp6://announce-v6.syncthing.net:22026")) {
Log.i(TAG, "Replacing IPv6 globalAnnounceServer address with ip");
g.setTextContent("udp6://[2001:470:28:4d6::5]:22026");
changed = true;
}
}
NodeList folders = mConfig.getDocumentElement().getElementsByTagName("folder"); NodeList folders = mConfig.getDocumentElement().getElementsByTagName("folder");
for (int i = 0; i < folders.getLength(); i++) { for (int i = 0; i < folders.getLength(); i++) {
@ -180,6 +181,33 @@ public class ConfigXml {
} }
} }
/**
* Replaces the announce servers in {@link #REPLACE_DISCOVERY_SERVERS} with those in
* {@link #DISCOVERY_SERVER_IPS}.
*/
private boolean replaceAnnounceServers(Element options) {
// Hardcode default globalAnnounceServer ip.
NodeList globalAnnounceServers = options.getElementsByTagName("globalAnnounceServer");
boolean needUpdateAnnounceServers = false;
for (int i = 0; i < globalAnnounceServers.getLength(); i++) {
Node announce = globalAnnounceServers.item(i);
if (Arrays.asList(REPLACE_DISCOVERY_SERVERS).contains(announce.getTextContent())) {
options.removeChild(announce);
needUpdateAnnounceServers = true;
}
}
if (needUpdateAnnounceServers) {
Log.i(TAG, "Replacing globalAnnounceServer address with ip");
for (String server : DISCOVERY_SERVER_IPS) {
Element newAnnounce = mConfig.createElement("globalAnnounceServer");
newAnnounce.setTextContent(server);
options.appendChild(newAnnounce);
}
return true;
}
return false;
}
/** /**
* Set 'hashers' (see https://github.com/syncthing/syncthing-android/issues/384) on the * Set 'hashers' (see https://github.com/syncthing/syncthing-android/issues/384) on the
* given folder. * given folder.