@@ -447,26 +447,60 @@ public static String getFQDN(HttpServletRequest request) {
447447
448448
449449 /**
450- * 判斷IP是否在網段中
451- * 例如:
452- * "192.168.1.127", "192.168.1.64/26"
450+ * [ZH] 判斷IP是否在網段中 <br>
451+ * 例如: <br>
452+ * "192.168.1.127", "192.168.1.64/26" <br>
453+ * [EN] Determine whether the IP is in the network segment <br>
454+ * For example: <br>
455+ * "192.168.1.127", "192.168.1.64/26" <br>
453456 */
454457 public static boolean isIpInNetwork (String ip , String gateway_cidr ) {
455458 try {
456-
457- // 不限網域
459+ // [ZH] 1.特殊情況處理:
460+ // 不限網域(接受任何 IP)
461+ // [EN] 1.Special Situation Handling:
462+ // Unlimited domain (accept any IP)
458463 if ("0.0.0.0/0" .equals (gateway_cidr )) return true ;
459464
465+ // [ZH] 2.IP 地址轉換:
466+ // 將點分十進制的 IP 地址(例如 "192.168.1.1")分割成四個部分
467+ // [EN] 2.IP address conversion:
468+ // Split a dotted decimal IP address (e.g. "192.168.1.1") into four parts
460469 String [] ips = ip .split ("\\ ." );
470+ // [ZH] 將這四個部分轉換為一個 32 位整數,使用位移運算(<<)進行組合
471+ // [EN] CConvert these four parts into a 32-bit integer and combine them using the bit shift operation (<<)
461472 int ipAddr = (Integer .parseInt (ips [0 ]) << 24 ) | (Integer .parseInt (ips [1 ]) << 16 )
462473 | (Integer .parseInt (ips [2 ]) << 8 ) | Integer .parseInt (ips [3 ]);
463- //這段 Regex 已被 Tom Review 過了, 故取消 hotspot 標記
464- int type = Integer .parseInt (gateway_cidr .replaceAll (".*/" , "" ));
474+
475+ // [ZH] 3.CIDR 處理:
476+ // 從 CIDR 表示法(例如 "192.168.1.0/24")中提取掩碼位數(例如 "24")
477+ // [EN] 3.CIDR processing:
478+ // Extract the number of mask bits (for example, "24") from CIDR notation (for example, "192.168.1.0/24")
479+ String [] cidrParts = gateway_cidr .split ("/" );
480+ int type = Integer .parseInt (cidrParts [1 ]);
481+
482+ // [ZH] 計算對應的網路掩碼值(mask)
483+ // [EN] Calculate the corresponding network mask value (mask)
465484 int mask = 0xFFFFFFFF << (32 - type );
466- String cidrIp = gateway_cidr .replaceAll ("/.*" , "" );
485+
486+ // [ZH] 從 CIDR 表示法中提取網路地址部分
487+ // [EN] Extract the network address portion from CIDR notation
488+ String cidrIp = cidrParts [0 ];
489+
490+ // [ZH] 網路地址轉換為整數值
491+ // [EN] Convert network address to integer value
467492 String [] cidrIps = cidrIp .split ("\\ ." );
468493 int cidrIpAddr = (Integer .parseInt (cidrIps [0 ]) << 24 ) | (Integer .parseInt (cidrIps [1 ]) << 16 )
469494 | (Integer .parseInt (cidrIps [2 ]) << 8 ) | Integer .parseInt (cidrIps [3 ]);
495+
496+ // [ZH] 4.網段判斷:
497+ // 使用位運算 &(AND)運算符將 IP 地址和網路掩碼進行運算
498+ // 同樣對網路地址和網路掩碼進行運算
499+ // 比較這兩個結果是否相等,相等則表示 IP 在該網段內
500+ // [EN] 4.Network segment determination:
501+ // Use the bitwise & (AND) operator to perform operations on the IP address and network mask
502+ // Also perform operations on the network address and network mask
503+ // Compare the two results to see if they are equal. If they are equal, it means the IP is in the network segment.
470504 return (ipAddr & mask ) == (cidrIpAddr & mask );
471505
472506 } catch (Exception e ) {
0 commit comments