Skip to content

Commit

Permalink
Implement IPv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
thevindu-w committed Jan 14, 2025
1 parent abbb177 commit 3a295a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/com/tw/clipshare/ClipShareActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/tw/clipshare/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private void addRowToAutoSendTrustList(boolean addToList, String address) {
trustServer.setId(idAutoSend.getAndIncrement());
Settings st = Settings.getInstance();
List<String> 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());
Expand All @@ -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)
Expand All @@ -356,14 +356,16 @@ 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);
EditText addressEdit = savedServer.findViewById(R.id.editTxt);
savedServer.setId(idSavedServer.getAndIncrement());
Settings st = Settings.getInstance();
List<String> 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());
Expand All @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/com/tw/clipshare/Utils.java
Original file line number Diff line number Diff line change
@@ -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() {}
Expand Down

0 comments on commit 3a295a1

Please sign in to comment.