Fixed local IP address class

pull/152/head
M66B 6 years ago
parent d4d843cddf
commit fe5d1b5d0b

@ -52,6 +52,7 @@ import com.android.colorpicker.ColorPickerSwatch;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
import java.net.Inet4Address;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -595,31 +596,29 @@ public class FragmentIdentity extends FragmentBase {
// Check SMTP server // Check SMTP server
if (check) { if (check) {
String protocol = (starttls ? "smtp" : "smtps");
// Get properties // Get properties
Properties props = MessageHelper.getSessionProperties(auth_type, realm, insecure); Properties props = MessageHelper.getSessionProperties(auth_type, realm, insecure);
InetAddress ip = (use_ip ? Helper.getLocalIp(context) : null);
if (ip == null) { String haddr;
Log.i("Check local host=" + host); if (use_ip) {
if (starttls) InetAddress addr = InetAddress.getByName(host);
props.put("mail.smtp.localhost", host); if (addr instanceof Inet4Address)
else haddr = "[" + Inet4Address.getLocalHost().getHostAddress() + "]";
props.put("mail.smtps.localhost", host);
} else {
InetAddress localhost = InetAddress.getLocalHost();
String haddr = "[" + (localhost instanceof Inet6Address ? "IPv6:" : "") + localhost.getHostAddress() + "]";
Log.i("Check local address=" + haddr);
if (starttls)
props.put("mail.smtp.localhost", haddr);
else else
props.put("mail.smtps.localhost", haddr); haddr = "[IPv6:" + Inet6Address.getLocalHost().getHostAddress() + "]";
} } else
haddr = host;
Log.i("Send localhost=" + haddr);
props.put("mail." + protocol + ".localhost", haddr);
// Create session // Create session
Session isession = Session.getInstance(props, null); Session isession = Session.getInstance(props, null);
isession.setDebug(true); isession.setDebug(true);
// Create transport // Create transport
String protocol = (starttls ? "smtp" : "smtps");
try (Transport itransport = isession.getTransport(protocol)) { try (Transport itransport = isession.getTransport(protocol)) {
try { try {
itransport.connect(host, Integer.parseInt(port), user, password); itransport.connect(host, Integer.parseInt(port), user, password);

@ -36,8 +36,6 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.Network; import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkInfo; import android.net.NetworkInfo;
@ -76,9 +74,6 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.text.DateFormat; import java.text.DateFormat;
@ -699,43 +694,6 @@ public class Helper {
return filename.substring(index + 1); return filename.substring(index + 1);
} }
static InetAddress getLocalIp(Context context) throws UnknownHostException {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M)
return InetAddress.getLocalHost();
Network active = cm.getActiveNetwork();
if (active == null)
return null;
NetworkInfo ani = cm.getNetworkInfo(active);
if (ani == null || !ani.isConnected())
return null;
NetworkCapabilities caps = cm.getNetworkCapabilities(active);
if (caps == null)
return null;
if (!caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN))
return null;
LinkProperties props = cm.getLinkProperties(active);
if (props == null)
return null;
List<LinkAddress> addresses = props.getLinkAddresses();
if (addresses == null || addresses.size() == 0)
return null;
// Prefer IPv4
for (LinkAddress address : addresses)
if (address.getAddress() instanceof Inet4Address)
return address.getAddress();
return addresses.get(0).getAddress();
}
static Boolean isMetered(Context context, boolean log) { static Boolean isMetered(Context context, boolean log) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

@ -33,6 +33,7 @@ import android.text.TextUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
@ -193,25 +194,23 @@ public class ServiceSend extends LifecycleService {
} }
EntityIdentity ident = db.identity().getIdentity(message.identity); EntityIdentity ident = db.identity().getIdentity(message.identity);
String protocol = ident.getProtocol();
// Get properties // Get properties
Properties props = MessageHelper.getSessionProperties(ident.auth_type, ident.realm, ident.insecure); Properties props = MessageHelper.getSessionProperties(ident.auth_type, ident.realm, ident.insecure);
InetAddress ip = (ident.use_ip ? Helper.getLocalIp(this) : null);
if (ip == null) { String haddr;
EntityLog.log(this, "Send local host=" + ident.host); if (ident.use_ip) {
if (ident.starttls) InetAddress addr = InetAddress.getByName(ident.host);
props.put("mail.smtp.localhost", ident.host); if (addr instanceof Inet4Address)
else haddr = "[" + Inet4Address.getLocalHost().getHostAddress() + "]";
props.put("mail.smtps.localhost", ident.host);
} else {
InetAddress localhost = InetAddress.getLocalHost();
String haddr = "[" + (localhost instanceof Inet6Address ? "IPv6:" : "") + localhost.getHostAddress() + "]";
EntityLog.log(this, "Send local address=" + haddr);
if (ident.starttls)
props.put("mail.smtp.localhost", haddr);
else else
props.put("mail.smtps.localhost", haddr); haddr = "[IPv6:" + Inet6Address.getLocalHost().getHostAddress() + "]";
} } else
haddr = ident.host;
EntityLog.log(this, "Send localhost=" + haddr);
props.put("mail." + protocol + ".localhost", haddr);
// Create session // Create session
final Session isession = Session.getInstance(props, null); final Session isession = Session.getInstance(props, null);
@ -244,7 +243,7 @@ public class ServiceSend extends LifecycleService {
// Create transport // Create transport
// TODO: cache transport? // TODO: cache transport?
try (Transport itransport = isession.getTransport(ident.getProtocol())) { try (Transport itransport = isession.getTransport(protocol)) {
// Connect transport // Connect transport
db.identity().setIdentityState(ident.id, "connecting"); db.identity().setIdentityState(ident.id, "connecting");
try { try {

Loading…
Cancel
Save