MediaWiki master
ProxyLookup.php
Go to the documentation of this file.
1<?php
2
8namespace MediaWiki\Request;
9
10use BagOStuff;
13use Wikimedia\IPSet;
14
19
21 private $proxyServers;
22
24 private $proxyServersComplex;
25
27 private $proxyIPSet;
28
30 private $hookRunner;
31
33 private $cache;
34
41 public function __construct(
42 $proxyServers,
43 $proxyServersComplex,
44 HookContainer $hookContainer,
45 BagOStuff $cache
46 ) {
47 $this->proxyServers = $proxyServers;
48 $this->proxyServersComplex = $proxyServersComplex;
49 $this->hookRunner = new HookRunner( $hookContainer );
50 $this->cache = $cache;
51 }
52
58 private function getIPSet() {
59 if ( $this->proxyIPSet ) {
60 return $this->proxyIPSet;
61 }
62
63 $this->proxyIPSet = $this->cache->getWithSetCallback(
64 $this->cache->makeGlobalKey( 'ProxyLookup', 'ipset', crc32( json_encode( $this->proxyServersComplex ) ) ),
65 BagOStuff::TTL_INDEFINITE,
66 fn () => new IPSet( $this->proxyServersComplex )
67 );
68
69 return $this->proxyIPSet;
70 }
71
78 public function isConfiguredProxy( $ip ) {
79 // Quick check of known singular proxy servers
80 if ( in_array( $ip, $this->proxyServers, true ) ) {
81 return true;
82 }
83
84 // Check against addresses and CIDR nets in the complex list
85 return $this->getIPSet()->match( $ip );
86 }
87
96 public function isTrustedProxy( $ip ) {
97 $trusted = $this->isConfiguredProxy( $ip );
98 $this->hookRunner->onIsTrustedProxy( $ip, $trusted );
99 return $trusted;
100 }
101}
102
104class_alias( ProxyLookup::class, 'ProxyLookup' );
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
isConfiguredProxy( $ip)
Checks if an IP matches a proxy we've configured.
isTrustedProxy( $ip)
Checks if an IP is a trusted proxy provider.
__construct( $proxyServers, $proxyServersComplex, HookContainer $hookContainer, BagOStuff $cache)
Abstract class for any ephemeral data store.
Definition BagOStuff.php:73
getWithSetCallback( $key, $exptime, $callback, $flags=0)
Get an item, regenerating and setting it if not found.