* Fetch external IP address from ifconfig.me
parent
b29bca2eea
commit
777fe0f767
|
@ -23,12 +23,6 @@ import java.util.Enumeration;
|
|||
import java.util.List;
|
||||
|
||||
import org.apache.http.conn.util.InetAddressUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
@ -51,23 +45,27 @@ public abstract class ServerUtils {
|
|||
// WARNING: this is not threaded
|
||||
public static final String getExternalIpAddress () {
|
||||
if (externalIpAddress == null) {
|
||||
Process suProcess;
|
||||
try {
|
||||
HttpClient httpclient = new DefaultHttpClient();
|
||||
HttpGet httpget = new HttpGet("http://ifconfig.me/ip");
|
||||
HttpResponse response = httpclient.execute(httpget);
|
||||
HttpEntity entity = response.getEntity();
|
||||
suProcess = Runtime.getRuntime().exec("su");
|
||||
|
||||
if (entity != null) {
|
||||
long len = entity.getContentLength();
|
||||
if (len != -1 && len < 1024) {
|
||||
externalIpAddress = EntityUtils.toString(entity);
|
||||
Log.d(TAG, "ServerUtils: getExternalIpAddress(): " + externalIpAddress);
|
||||
return externalIpAddress;
|
||||
}
|
||||
// stdin
|
||||
DataOutputStream stdin = new DataOutputStream(suProcess.getOutputStream());
|
||||
Log.d(TAG, "ServerUtils: getExternalIpAddress(): # busybox wget -qO - http://ifconfig.me/ip");
|
||||
stdin.writeBytes("busybox wget -qO - http://ifconfig.me/ip\n");
|
||||
stdin.flush();
|
||||
stdin.writeBytes("exit\n");
|
||||
stdin.flush();
|
||||
|
||||
// stdout
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(suProcess.getInputStream()));
|
||||
String line = reader.readLine();
|
||||
if (line != null) {
|
||||
externalIpAddress = line;
|
||||
return externalIpAddress;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
Log.e(TAG, "ServerUtils: getExternalIpAddress(): " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "ServerUtils: getLocalIpAddress(): " + e.getMessage());
|
||||
}
|
||||
externalIpAddress = null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue