From 208f63f14c1fb22e0ca88be7dd29ee44660de3d2 Mon Sep 17 00:00:00 2001 From: zl9527 Date: Thu, 2 Nov 2023 10:53:06 +0800 Subject: [PATCH] =?UTF-8?q?Added:=20=E5=A2=9E=E5=8A=A0IPv6=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、增加IPv6地址支持功能。 2、增加 server_ipv6.enabled 配置参数,用来说明服务器端是否开启IPv6支持。 3、修改fdht客户端增加IPv6支持。 --- fdfs_client.conf | 9 +++++ .../org/csource/fastdfs/ClientGlobal.java | 39 +++++++++++++++---- .../java/org/csource/fastdfs/ProtoCommon.java | 20 ++++++++-- .../fastdfs-client.properties.sample | 11 +++++- src/main/resources/fdfs_client.conf.sample | 11 +++++- 5 files changed, 78 insertions(+), 12 deletions(-) diff --git a/fdfs_client.conf b/fdfs_client.conf index 918f233..01debe5 100644 --- a/fdfs_client.conf +++ b/fdfs_client.conf @@ -5,6 +5,13 @@ http.tracker_http_port = 8080 http.anti_steal_token = no http.secret_key = FastDFS1234567890 +#tracker_server +# IPv4: +# for example: 192.168.2.100,122.244.141.46:22122 +# +# IPv6: +# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122 +# tracker_server = 10.0.11.247:22122 tracker_server = 10.0.11.248:22122 tracker_server = 10.0.11.249:22122 @@ -13,3 +20,5 @@ connection_pool.enabled = true connection_pool.max_count_per_entry = 500 connection_pool.max_idle_time = 3600 connection_pool.max_wait_time_in_ms = 1000 + +server_ipv6.enabled = false \ No newline at end of file diff --git a/src/main/java/org/csource/fastdfs/ClientGlobal.java b/src/main/java/org/csource/fastdfs/ClientGlobal.java index 98e763d..4041139 100644 --- a/src/main/java/org/csource/fastdfs/ClientGlobal.java +++ b/src/main/java/org/csource/fastdfs/ClientGlobal.java @@ -13,6 +13,7 @@ import org.csource.common.MyException; import java.io.IOException; import java.io.InputStream; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.util.ArrayList; @@ -48,6 +49,8 @@ public class ClientGlobal { public static final String PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME = "fastdfs.connection_pool.max_idle_time"; public static final String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = "fastdfs.connection_pool.max_wait_time_in_ms"; + public static final String PROP_KEY_SERVER_IPV6_ENABLED = "fastdfs.server_ipv6.enabled"; + public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second public static final String DEFAULT_CHARSET = "UTF-8"; @@ -113,12 +116,19 @@ public class ClientGlobal { InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length]; for (int i = 0; i < szTrackerServers.length; i++) { - parts = szTrackerServers[i].split("\\:", 2); + if(szTrackerServers[i].contains("[")){ + parts = new String[2]; + parts[0] = szTrackerServers[i].substring(1, szTrackerServers[i].indexOf("]")); + parts[1] = szTrackerServers[i].substring(szTrackerServers[i].lastIndexOf(":") + 1); + }else { + parts = szTrackerServers[i].split("\\:", 2); + } + if (parts.length != 2) { throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port"); } - tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim())); + tracker_servers[i] = new InetSocketAddress(InetAddress.getByName(parts[0].trim()), Integer.parseInt(parts[1].trim())); } g_tracker_group = new TrackerGroup(tracker_servers); @@ -138,6 +148,9 @@ public class ClientGlobal { if (g_connection_pool_max_wait_time_in_ms < 0) { g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS; } + if(iniReader.getBoolValue("server_ipv6.enabled",false)){ + ProtoCommon.useIPv6(); + } } /** @@ -179,6 +192,7 @@ public class ClientGlobal { String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY); String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME); String poolMaxWaitTimeInMS = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS); + String serverIPv6Enabled = props.getProperty(PROP_KEY_SERVER_IPV6_ENABLED); if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) { g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000; } @@ -209,6 +223,11 @@ public class ClientGlobal { if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) { g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS); } + if (serverIPv6Enabled != null && serverIPv6Enabled.trim().length() != 0) { + if(Boolean.parseBoolean(poolEnabled)){ + ProtoCommon.useIPv6(); + } + } } /** @@ -224,10 +243,16 @@ public class ClientGlobal { String spr2 = ":"; String[] arr1 = trackerServers.trim().split(spr1); for (String addrStr : arr1) { - String[] arr2 = addrStr.trim().split(spr2); - String host = arr2[0].trim(); - int port = Integer.parseInt(arr2[1].trim()); - list.add(new InetSocketAddress(host, port)); + if(addrStr.contains("[")){ + String host = addrStr.substring(1, addrStr.indexOf("]")); + int port = Integer.parseInt(addrStr.substring(addrStr.lastIndexOf(":") + 1)); + list.add(new InetSocketAddress(InetAddress.getByName(host), port)); + }else { + String[] arr2 = addrStr.trim().split(spr2); + String host = arr2[0].trim(); + int port = Integer.parseInt(arr2[1].trim()); + list.add(new InetSocketAddress(InetAddress.getByName(host), port)); + } } InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]); initByTrackers(trackerAddresses); @@ -247,7 +272,7 @@ public class ClientGlobal { public static Socket getSocket(String ip_addr, int port) throws IOException { Socket sock = new Socket(); sock.setSoTimeout(ClientGlobal.g_network_timeout); - sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout); + sock.connect(new InetSocketAddress(InetAddress.getByName(ip_addr), port), ClientGlobal.g_connect_timeout); return sock; } diff --git a/src/main/java/org/csource/fastdfs/ProtoCommon.java b/src/main/java/org/csource/fastdfs/ProtoCommon.java index 6705759..8586a58 100644 --- a/src/main/java/org/csource/fastdfs/ProtoCommon.java +++ b/src/main/java/org/csource/fastdfs/ProtoCommon.java @@ -72,15 +72,17 @@ public class ProtoCommon { public static final int FDFS_PROTO_PKG_LEN_SIZE = 8; public static final int FDFS_PROTO_CMD_SIZE = 1; public static final int FDFS_GROUP_NAME_MAX_LEN = 16; - public static final int FDFS_IPADDR_SIZE = 16; + public static final int FDFS_IPADDR_V4_SIZE = 16; + public static final int FDFS_IPADDR_V6_SIZE = 46; + public static int FDFS_IPADDR_SIZE = FDFS_IPADDR_V4_SIZE; public static final int FDFS_DOMAIN_NAME_MAX_SIZE = 128; public static final int FDFS_VERSION_SIZE = 6; public static final int FDFS_STORAGE_ID_MAX_SIZE = 16; public static final String FDFS_RECORD_SEPERATOR = "\u0001"; public static final String FDFS_FIELD_SEPERATOR = "\u0002"; - public static final int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN + public static int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN + FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE; - public static final int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN + public static int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN + FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE; public static final byte FDFS_FILE_EXT_NAME_MAX_LEN = 6; public static final byte FDFS_FILE_PREFIX_MAX_LEN = 16; @@ -502,4 +504,16 @@ public class ProtoCommon { this.body_len = body_len; } } + + /** + * 设置系统使用IPv6地址进行通信 + */ + public static void useIPv6(){ + FDFS_IPADDR_SIZE = FDFS_IPADDR_V6_SIZE; + + TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN + + FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE; + TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN + + FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE; + } } diff --git a/src/main/resources/fastdfs-client.properties.sample b/src/main/resources/fastdfs-client.properties.sample index dd33b50..38217c9 100644 --- a/src/main/resources/fastdfs-client.properties.sample +++ b/src/main/resources/fastdfs-client.properties.sample @@ -9,6 +9,13 @@ fastdfs.http_anti_steal_token = false fastdfs.http_secret_key = FastDFS1234567890 fastdfs.http_tracker_http_port = 80 +#tracker_server +# IPv4: +# for example: 192.168.2.100,122.244.141.46:22122 +# +# IPv6: +# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122 +# fastdfs.tracker_servers = 185.245.40.70:22122 ## Whether to open the connection pool, if not, create a new connection every time @@ -21,4 +28,6 @@ fastdfs.connection_pool.max_count_per_entry = 500 fastdfs.connection_pool.max_idle_time = 3600 ## Maximum waiting time when the maximum number of connections is reached, unit: millisecond, default value is 1000 -fastdfs.connection_pool.max_wait_time_in_ms = 1000 \ No newline at end of file +fastdfs.connection_pool.max_wait_time_in_ms = 1000 + +server_ipv6.enabled = false \ No newline at end of file diff --git a/src/main/resources/fdfs_client.conf.sample b/src/main/resources/fdfs_client.conf.sample index 741218c..ff99ef7 100644 --- a/src/main/resources/fdfs_client.conf.sample +++ b/src/main/resources/fdfs_client.conf.sample @@ -5,10 +5,19 @@ http.tracker_http_port = 8080 http.anti_steal_token = no http.secret_key = FastDFS1234567890 +#tracker_server +# IPv4: +# for example: 192.168.2.100,122.244.141.46:22122 +# +# IPv6: +# for example: [2409:8a20:42d:2f40:587a:4c47:72c0:ad8e]:22122 +# tracker_server = 10.0.11.243:22122 tracker_server = 10.0.11.244:22122 connection_pool.enabled = true connection_pool.max_count_per_entry = 500 connection_pool.max_idle_time = 3600 -connection_pool.max_wait_time_in_ms = 1000 \ No newline at end of file +connection_pool.max_wait_time_in_ms = 1000 + +server_ipv6.enabled = false \ No newline at end of file