@@ -22,6 +22,7 @@ import (
22
22
"errors"
23
23
"fmt"
24
24
"net"
25
+ "regexp"
25
26
"strconv"
26
27
"strings"
27
28
"time"
@@ -210,11 +211,24 @@ func (ac *activeClient) fetchIP() (string, error) {
210
211
// example: "inst": "client.4305 172.21.9.34:0/422650892",
211
212
// then returning value will be 172.21.9.34
212
213
clientInfo := ac .Inst
213
- parts := strings .Fields (clientInfo )
214
- if len (parts ) >= 2 {
215
- lastColonIndex := strings .LastIndex (parts [1 ], ":" )
216
- firstPart := parts [1 ][:lastColonIndex ]
217
- ip := net .ParseIP (firstPart )
214
+
215
+ // Attempt to extract the IP address using a regular expression
216
+ // the regular expression aims to match either a complete IPv6
217
+ // address or a complete IPv4 address follows by any prefix (v1 or v2)
218
+ // if exists
219
+ // (?:v[0-9]+:): this allows for an optional prefix starting with "v"
220
+ // followed by one or more digits and a colon.
221
+ // The ? outside the group makes the entire prefix section optional.
222
+ // (?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}: this allows to check for
223
+ // standard IPv6 address.
224
+ // |: Alternation operator to allow matching either the IPv6 pattern
225
+ // with a prefix or the IPv4 pattern.
226
+ // '(?:\d+\.){3}\d+: This part matches a standard IPv4 address.
227
+ re := regexp .MustCompile (`(?:v[0-9]+:)?([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}|(?:\d+\.){3}\d+)` )
228
+ ipMatches := re .FindStringSubmatch (clientInfo )
229
+
230
+ if len (ipMatches ) > 0 {
231
+ ip := net .ParseIP (ipMatches [1 ])
218
232
if ip != nil {
219
233
return ip .String (), nil
220
234
}
0 commit comments