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

@ -36,8 +36,6 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
@ -76,9 +74,6 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
@ -699,43 +694,6 @@ public class Helper {
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) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

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

Loading…
Cancel
Save