@@ -1134,20 +1134,57 @@ common.getIpAddress = function(req) {
1134
1134
//if ignoreProxies not setup, use outmost left ip address
1135
1135
if ( ! countlyConfig . ignoreProxies || ! countlyConfig . ignoreProxies . length ) {
1136
1136
ipLogger . d ( "From %s found ip %s" , ipAddress , ips [ 0 ] ) ;
1137
- return ips [ 0 ] ;
1137
+ return stripPort ( ips [ 0 ] ) ;
1138
1138
}
1139
1139
//search for the outmost right ip address ignoring provided proxies
1140
1140
var ip = "" ;
1141
1141
for ( var i = ips . length - 1 ; i >= 0 ; i -- ) {
1142
- if ( ips [ i ] . trim ( ) !== "127.0.0.1" && ( ! countlyConfig . ignoreProxies || countlyConfig . ignoreProxies . indexOf ( ips [ i ] . trim ( ) ) === - 1 ) ) {
1143
- ip = ips [ i ] . trim ( ) ;
1142
+ ips [ i ] = stripPort ( ips [ i ] ) ;
1143
+ var masks = false ;
1144
+ if ( countlyConfig . ignoreProxies && countlyConfig . ignoreProxies . length ) {
1145
+ masks = countlyConfig . ignoreProxies . some ( function ( elem ) {
1146
+ return ips [ i ] . startsWith ( elem ) ;
1147
+ } ) ;
1148
+ }
1149
+ if ( ips [ i ] !== "127.0.0.1" && ( ! countlyConfig . ignoreProxies || ! masks ) ) {
1150
+ ip = ips [ i ] ;
1144
1151
break ;
1145
1152
}
1146
1153
}
1147
1154
ipLogger . d ( "From %s found ip %s" , ipAddress , ip ) ;
1148
1155
return ip ;
1149
1156
} ;
1150
1157
1158
+ /**
1159
+ * This function takes ipv4 or ipv6 with possible port, removes port information and returns plain ip address
1160
+ * @param {string } ip - ip address to check for port and return plain ip
1161
+ * @returns returns plain ip address
1162
+ */
1163
+ function stripPort ( ip ) {
1164
+ var parts = ( ip + "" ) . split ( "." ) ;
1165
+ //check if ipv4
1166
+ if ( parts . length === 4 ) {
1167
+ return ip . split ( ":" ) [ 0 ] . trim ( ) ;
1168
+ }
1169
+ else {
1170
+ parts = ( ip + "" ) . split ( ":" ) ;
1171
+ if ( parts . length === 9 ) {
1172
+ parts . pop ( ) ;
1173
+ }
1174
+ if ( parts . length === 8 ) {
1175
+ ip = parts . join ( ":" ) ;
1176
+ //remove enclosing [] for ipv6 if they are there
1177
+ if ( ip [ 0 ] === "[" ) {
1178
+ ip = ip . substring ( 1 ) ;
1179
+ }
1180
+ if ( ip [ ip . length - 1 ] === "]" ) {
1181
+ ip = ip . slice ( 0 , - 1 ) ;
1182
+ }
1183
+ }
1184
+ }
1185
+ return ( ip + "" ) . trim ( ) ;
1186
+ }
1187
+
1151
1188
/**
1152
1189
* Modifies provided object filling properties used in zero documents in the format object["2012.7.20.property"] = increment.
1153
1190
* Usualy used when filling up Countly metric model zero document
0 commit comments