From 3a295a101912b04dc5d45ec198fea4af0e40240d Mon Sep 17 00:00:00 2001 From: thevindu-w Date: Tue, 14 Jan 2025 23:42:28 +0530 Subject: [PATCH] Implement IPv6 support --- .../java/com/tw/clipshare/ClipShareActivity.java | 7 +++---- .../java/com/tw/clipshare/SettingsActivity.java | 10 ++++++---- app/src/main/java/com/tw/clipshare/Utils.java | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/tw/clipshare/ClipShareActivity.java b/app/src/main/java/com/tw/clipshare/ClipShareActivity.java index 354f5d2..2d39a47 100644 --- a/app/src/main/java/com/tw/clipshare/ClipShareActivity.java +++ b/app/src/main/java/com/tw/clipshare/ClipShareActivity.java @@ -59,7 +59,6 @@ import com.tw.clipshare.protocol.Proto_v3; import com.tw.clipshare.protocol.ProtocolSelector; import java.io.InputStream; -import java.net.Inet4Address; import java.net.InetAddress; import java.net.ProtocolException; import java.util.ArrayList; @@ -502,14 +501,14 @@ ServerConnection getServerConnection(@NonNull String addressStr) { } String[] acceptedServers = settings.getTrustedList().toArray(new String[0]); return new SecureConnection( - Inet4Address.getByName(addressStr), + InetAddress.getByName(addressStr), settings.getPortSecure(), caCertIn, clientCertKeyIn, clientPass, acceptedServers); } else { - return new PlainConnection(Inet4Address.getByName(addressStr), settings.getPort()); + return new PlainConnection(InetAddress.getByName(addressStr), settings.getPort()); } } catch (Exception ignored) { } @@ -625,7 +624,7 @@ private String getServerAddress() { String address; try { address = editAddress.getText().toString(); - if (!Utils.isValidIPv4(address)) { + if (!Utils.isValidIP(address)) { Toast.makeText(ClipShareActivity.this, "Invalid address", Toast.LENGTH_SHORT).show(); return null; } diff --git a/app/src/main/java/com/tw/clipshare/SettingsActivity.java b/app/src/main/java/com/tw/clipshare/SettingsActivity.java index 64722ef..12a7d12 100644 --- a/app/src/main/java/com/tw/clipshare/SettingsActivity.java +++ b/app/src/main/java/com/tw/clipshare/SettingsActivity.java @@ -314,7 +314,7 @@ private void addRowToAutoSendTrustList(boolean addToList, String address) { trustServer.setId(idAutoSend.getAndIncrement()); Settings st = Settings.getInstance(); List servers = st.getAutoSendTrustedList(); - addressTxt.setText(Utils.isValidIPv4(address) ? address : "*"); + addressTxt.setText(Utils.isValidIP(address) ? address : "*"); if (addToList) servers.add(addressTxt.getText().toString()); autoSendTrustList.addView(trustServer, 0); addressTxt.setTextColor(caCnTxt.getTextColors()); @@ -340,7 +340,7 @@ private void addRowToAutoSendTrustList(boolean addToList, String address) { if (!hasFocus) { CharSequence oldText = addressTxt.getText(); String newText = addressEdit.getText().toString(); - boolean isValid = "*".equals(newText) || Utils.isValidIPv4(newText); + boolean isValid = "*".equals(newText) || Utils.isValidIP(newText); if (isValid) addressTxt.setText(newText); else Toast.makeText(SettingsActivity.this, "Invalid IPv4 address", Toast.LENGTH_SHORT) @@ -356,6 +356,8 @@ private void addRowToAutoSendTrustList(boolean addToList, String address) { private void addRowToSavedServersList(boolean addToList, String address) { try { + if (address == null) address = "0.0.0.0"; + else if (!Utils.isValidIP(address)) return; View savedServer = View.inflate(getApplicationContext(), R.layout.list_element, null); ImageButton delBtn = savedServer.findViewById(R.id.delBtn); TextView addressTxt = savedServer.findViewById(R.id.viewTxt); @@ -363,7 +365,7 @@ private void addRowToSavedServersList(boolean addToList, String address) { savedServer.setId(idSavedServer.getAndIncrement()); Settings st = Settings.getInstance(); List servers = st.getSavedServersList(); - addressTxt.setText(Utils.isValidIPv4(address) ? address : "0.0.0.0"); + addressTxt.setText(address); if (addToList) servers.add(addressTxt.getText().toString()); savedServersList.addView(savedServer, 0); addressTxt.setTextColor(caCnTxt.getTextColors()); @@ -389,7 +391,7 @@ private void addRowToSavedServersList(boolean addToList, String address) { if (!hasFocus) { CharSequence oldText = addressTxt.getText(); String newText = addressEdit.getText().toString(); - boolean isValid = Utils.isValidIPv4(newText); + boolean isValid = Utils.isValidIP(newText); if (isValid) addressTxt.setText(newText); else Toast.makeText(SettingsActivity.this, "Invalid IPv4 address", Toast.LENGTH_SHORT) diff --git a/app/src/main/java/com/tw/clipshare/Utils.java b/app/src/main/java/com/tw/clipshare/Utils.java index 8dbd5c2..aaf883a 100644 --- a/app/src/main/java/com/tw/clipshare/Utils.java +++ b/app/src/main/java/com/tw/clipshare/Utils.java @@ -1,13 +1,23 @@ package com.tw.clipshare; +import java.net.Inet6Address; + public class Utils { public static final byte PROTOCOL_SUPPORTED = 1; public static final byte PROTOCOL_OBSOLETE = 2; public static final byte PROTOCOL_UNKNOWN = 3; - public static boolean isValidIPv4(String str) { - if (str == null) return false; - return str.matches("^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}$"); + public static boolean isValidIP(String str) { + try { + if (str == null) return false; + if (str.matches("^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}$")) return true; + if (!str.contains(":")) return false; + //noinspection ResultOfMethodCallIgnored + Inet6Address.getByName(str); + return true; + } catch (Exception ignored) { + } + return false; } private Utils() {}