python-ipware 2.0.3__py3-none-any.whl → 2.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- __version__ = "2.0.3"
1
+ __version__ = "2.0.4"
@@ -144,12 +144,9 @@ class IpWareProxy:
144
144
 
145
145
  def __init__(
146
146
  self,
147
- proxy_count: int = 0,
147
+ proxy_count: Optional[int] = None,
148
148
  proxy_list: Optional[List[str]] = None,
149
149
  ) -> None:
150
- if proxy_count is None or proxy_count < 0:
151
- raise ValueError("proxy_count must be a positive integer")
152
-
153
150
  self.proxy_count = proxy_count
154
151
  self.proxy_list = self._is_valid_proxy_trusted_list(proxy_list or [])
155
152
 
@@ -172,15 +169,14 @@ class IpWareProxy:
172
169
  """
173
170
  Checks if the proxy count is valid
174
171
  @param ip_list: list of ip addresses
175
- @param strict: if True, we must have exactly proxy_count proxies
172
+ @param strict: if True, we must have exactly proxy_count proxies, including `0` proxies
176
173
  @return: True if the proxy count is valid, False otherwise
177
174
  """
178
- if self.proxy_count < 1:
175
+ # No proxy count check is required
176
+ if self.proxy_count is None:
179
177
  return True
180
178
 
181
179
  ip_count: int = len(ip_list)
182
- if ip_count < 1:
183
- return False
184
180
 
185
181
  if strict:
186
182
  # our first proxy takes the last ip address and treats it as client ip
@@ -221,10 +217,6 @@ class IpWareProxy:
221
217
  if not str(value).startswith(self.proxy_list[index]):
222
218
  return False
223
219
 
224
- # now all we need is to return the first ip in the list that is not in the trusted proxy list
225
- # best_client_ip_index = proxy_list_count + 1
226
- # best_client_ip = ip_list[-best_client_ip_index]
227
-
228
220
  return True
229
221
 
230
222
 
@@ -237,11 +229,11 @@ class IpWare(IpWareMeta, IpWareProxy, IpWareIpAddress):
237
229
  self,
238
230
  precedence: Optional[Tuple[str, ...]] = None,
239
231
  leftmost: bool = True,
240
- proxy_count: int = 0,
232
+ proxy_count: Optional[int] = None,
241
233
  proxy_list: Optional[List[str]] = None,
242
234
  ) -> None:
243
235
  IpWareMeta.__init__(self, precedence, leftmost)
244
- IpWareProxy.__init__(self, proxy_count or 0, proxy_list or [])
236
+ IpWareProxy.__init__(self, proxy_count, proxy_list)
245
237
 
246
238
  def get_meta_value(self, meta: Dict[str, str], key: str) -> str:
247
239
  """
@@ -257,7 +249,13 @@ class IpWare(IpWareMeta, IpWareProxy, IpWareIpAddress):
257
249
  Given a list of keys, it returns a list of cleaned up values
258
250
  @return: a list of values
259
251
  """
260
- return [self.get_meta_value(meta, key) for key in self.precedence]
252
+ meta_list: List[str] = []
253
+ for key in self.precedence:
254
+ value = self.get_meta_value(meta, key).strip()
255
+ if value:
256
+ meta_list.append(value)
257
+
258
+ return meta_list
261
259
 
262
260
  def get_client_ip(
263
261
  self,
@@ -272,8 +270,6 @@ class IpWare(IpWareMeta, IpWareProxy, IpWareIpAddress):
272
270
  private_list: List[OptionalIpAddressType] = []
273
271
 
274
272
  for ip_str in self.get_meta_values(meta):
275
- if not ip_str:
276
- continue
277
273
 
278
274
  ip_list = self.get_ips_from_string(ip_str)
279
275
  if not ip_list:
@@ -337,7 +333,11 @@ class IpWare(IpWareMeta, IpWareProxy, IpWareIpAddress):
337
333
  return best_client_ip, True
338
334
 
339
335
  # the incoming ips match our proxy count
340
- if self.proxy_count > 0 and proxy_count_validated:
336
+ if (
337
+ self.proxy_count is not None
338
+ and self.proxy_count > 0
339
+ and proxy_count_validated
340
+ ):
341
341
  best_client_ip_index = self.proxy_count + 1
342
342
  best_client_ip = ip_list[-best_client_ip_index]
343
343
  return best_client_ip, True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-ipware
3
- Version: 2.0.3
3
+ Version: 2.0.4
4
4
  Summary: A Python package to retrieve user's IP address
5
5
  Author-email: Val Neekman <info@neekware.com>
6
6
  License: MIT
@@ -216,23 +216,41 @@ If your python server is behind a `known` number of proxies, but you deploy on m
216
216
  You can customize the proxy count by providing your `proxy_count` during initialization when calling `IpWare(proxy_count=2)`.
217
217
 
218
218
  ```python
219
- # In the above scenario, the total number of proxies can be used as a way to filter out unwanted requests.
220
219
  from python_ipware import IpWare
221
220
 
222
- # enforce proxy count
223
- ipw = IpWare(proxy_count=1)
221
+ # Enforce proxy count
222
+ # proxy_count=0 is valid
223
+ # proxy_count=None to disable proxy_count check
224
+ ipw = IpWare(proxy_count=2)
224
225
 
225
- # enforce proxy count and trusted proxies
226
- ipw = IpWare(proxy_count=1, proxy_list=["198.84.193.157"])
226
+ # Example usage in non-strict mode:
227
+ # X-Forwarded-For format: <fake>, <client>, <proxy1>, <proxy2>
228
+ # At least `proxy_count` number of proxies
229
+ ip, trusted_route = ipw.get_client_ip(meta=request.META)
227
230
 
231
+ # Example usage in strict mode:
232
+ # X-Forwarded-For format: <client>, <proxy1>, <proxy2>
233
+ # Exact `proxy_count` number of proxies
234
+ ip, trusted_route = ipw.get_client_ip(meta=request.META, strict=True)
235
+ ```
228
236
 
229
- # usage: non-strict mode (X-Forwarded-For: <fake>, <client>, <proxy1>, <proxy2>)
230
- # total number of ip addresses are greater than the total count
231
- ip, trusted_route = ipw.get_client_ip(meta=request.META)
237
+ ### Proxy Count & Trusted Proxy List Combo
238
+ In this example, we utilize the total number of proxies as a method to filter out unwanted requests while verifying the trust proxies.
232
239
 
240
+ ```python
241
+ from python_ipware import IpWare
233
242
 
234
- # usage: strict mode (X-Forwarded-For: <client>, <proxy1>, <proxy2>)
235
- # total number of ip addresses are exactly equal to client ip + proxy_count
243
+ # Enforce both proxy count and trusted proxies
244
+ ipw = IpWare(proxy_count=1, proxy_list=["198.84.193.157"])
245
+
246
+ # Example usage in non-strict mode:
247
+ # X-Forwarded-For format: <fake>, <client>, <proxy1>, <proxy2>
248
+ # At least `proxy_count` number of proxies
249
+ ip, trusted_route = ipw.get_client_ip(meta=request.META)
250
+
251
+ # Example usage in strict mode:
252
+ # X-Forwarded-For format: <client>, <proxy1>
253
+ # Exact `proxy_count` number of proxies
236
254
  ip, trusted_route = ipw.get_client_ip(meta=request.META, strict=True)
237
255
  ```
238
256
 
@@ -0,0 +1,9 @@
1
+ python_ipware/__init__.py,sha256=TCZx4mt5Gxr2D_zNU6j0ynopdsAN_v4xeiTWz1OXSpY,87
2
+ python_ipware/__version__.py,sha256=ZOg41aCgKEuObK5WhegikXektFWPJaeZ-TMDh0Ke95M,22
3
+ python_ipware/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ python_ipware/python_ipware.py,sha256=sN3NeWWs96beGzI9x3YZ9ucYnMIZh1Q4fWOOIm2_P-s,11987
5
+ python_ipware-2.0.4.dist-info/LICENSE,sha256=MLpNxpqfTc4TLdcDk3x6k7Vz4lJGBNLV-SxQZlFMDU8,1103
6
+ python_ipware-2.0.4.dist-info/METADATA,sha256=-KqYlSwY52kwnnAMA7ym2A31L1yX8BffjMdZ3I32Wl0,15420
7
+ python_ipware-2.0.4.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
8
+ python_ipware-2.0.4.dist-info/top_level.txt,sha256=URE4dvjpReJtRHyQpJqoBow5EWt_RqVXFn85B9Tq5Xw,14
9
+ python_ipware-2.0.4.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- python_ipware/__init__.py,sha256=TCZx4mt5Gxr2D_zNU6j0ynopdsAN_v4xeiTWz1OXSpY,87
2
- python_ipware/__version__.py,sha256=_GEKEa6BYjBV34SZkSlAR87aCM5Y9G0aSI0LXL52iJg,22
3
- python_ipware/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_ipware/python_ipware.py,sha256=kAoCyLbJLxN-zocHB9Ps-AdZYWMUQ8KasHscg6Qcc3Q,12119
5
- python_ipware-2.0.3.dist-info/LICENSE,sha256=MLpNxpqfTc4TLdcDk3x6k7Vz4lJGBNLV-SxQZlFMDU8,1103
6
- python_ipware-2.0.3.dist-info/METADATA,sha256=cwAXk8j8o_cfJUfZT9tQ3sCwjRa6OQRVVX89juDwJgA,14857
7
- python_ipware-2.0.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
8
- python_ipware-2.0.3.dist-info/top_level.txt,sha256=URE4dvjpReJtRHyQpJqoBow5EWt_RqVXFn85B9Tq5Xw,14
9
- python_ipware-2.0.3.dist-info/RECORD,,