Skip to content

Commit

Permalink
fix: IP format in peer list is incorrect
Browse files Browse the repository at this point in the history
Issue: #41
  • Loading branch information
kaaass committed Jul 11, 2023
1 parent ec81e97 commit fde9f0d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void onRefresh() {
public void onPeerInfoReplyEvent(PeerInfoReplyEvent event) {
Peer[] peers = event.getPeers();
if (peers == null) {
Snackbar.make(getView(), R.string.fail_retrieve_peer_list, BaseTransientBottomBar.LENGTH_LONG).show();
Snackbar.make(requireView(), R.string.fail_retrieve_peer_list, BaseTransientBottomBar.LENGTH_LONG).show();
return;
}
// 更新数据列表
Expand Down Expand Up @@ -215,7 +215,7 @@ public void onBindViewHolder(final RecyclerViewAdapter.ViewHolder holder, int po
}
String strPreferred = getString(R.string.peer_relay);
if (preferred != null) {
strPreferred = preferred.getAddress().toString();
strPreferred = StringUtils.toString(preferred.getAddress());
}
holder.mPath.setText(strPreferred);
}
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/net/kaaass/zerotierfix/util/StringUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package net.kaaass.zerotierfix.util;

import androidx.annotation.NonNull;

import com.zerotier.sdk.Peer;
import com.zerotier.sdk.Version;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Locale;

/**
Expand Down Expand Up @@ -55,4 +60,20 @@ public static byte[] hexStringToBytes(String hex) {
}
return result;
}

/**
* 将 InetSocketAddress 转为 IP:Port (IPv6 则是 [IP]:Port) 格式的字符串
*/
public static String toString(InetSocketAddress address) {
InetAddress inetAddress = address.getAddress();
int port = address.getPort();

// 将 IP 地址转为字符串
String ipString = inetAddress.getHostAddress();
if (inetAddress instanceof Inet6Address) {
ipString = "[" + ipString + "]";
}

return ipString + ":" + port;
}
}

0 comments on commit fde9f0d

Please sign in to comment.