Skip to content

Commit 4cad701

Browse files
author
pulkit
committed
added random option to return random ipaddress for use on homestead
1 parent fa26ecd commit 4cad701

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/GeoIP.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class GeoIP
1616
*/
1717
protected $driver;
1818

19+
/**
20+
* @var boolean
21+
*/
22+
protected $random;
23+
1924
/**
2025
* @var array
2126
*/
@@ -27,6 +32,7 @@ class GeoIP
2732
public function __construct(array $config = ['driver' => 'ip-api'])
2833
{
2934
$this->driver = with(new GeoIPManager($config))->getDriver();
35+
$this->random = array_get($config, 'random', false);
3036
}
3137

3238
/**
@@ -60,7 +66,15 @@ public function setIp($ip)
6066
*/
6167
public function getIp()
6268
{
63-
return ($this->ip) ?: array_get($_SERVER, 'HTTP_CLIENT_IP', array_get($_SERVER, 'HTTP_X_FORWARDED_FOR', array_get($_SERVER, 'HTTP_X_FORWARDED', array_get($_SERVER, 'HTTP_FORWARDED_FOR', array_get($_SERVER, 'HTTP_FORWARDED', array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1'))))));
69+
if ($this->ip) {
70+
return $this->ip;
71+
}
72+
73+
if ($this->random) {
74+
return long2ip(rand(0, '4294967295'));
75+
}
76+
77+
return array_get($_SERVER, 'HTTP_CLIENT_IP', array_get($_SERVER, 'HTTP_X_FORWARDED_FOR', array_get($_SERVER, 'HTTP_X_FORWARDED', array_get($_SERVER, 'HTTP_FORWARDED_FOR', array_get($_SERVER, 'HTTP_FORWARDED', array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1'))))));
6478
}
6579

6680
/**

src/config/config.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
*/
1212
'driver' => env('GEOIP_DRIVER', 'ip-api'),
1313

14+
/*
15+
|--------------------------------------------------------------------------
16+
| Return random ipaddresses (useful for dev envs)
17+
|--------------------------------------------------------------------------
18+
*/
19+
'random' => env('GEOIP_RANDOM', false),
20+
1421
/*
1522
|--------------------------------------------------------------------------
1623
| IP-API Driver
@@ -22,7 +29,7 @@
2229
| IP-API Pro Service Key
2330
|--------------------------------------------------------------------------
2431
*/
25-
'key' => '',
32+
'key' => env('GEOIP_IPAPI_KEY'),
2633

2734
/*
2835
|--------------------------------------------------------------------------
@@ -33,7 +40,7 @@
3340
| Only applicable with the Pro service
3441
|
3542
*/
36-
'secure' => false,
43+
'secure' => env('GEOIP_IPAPI_SECURE'),
3744
],
3845

3946
/*
@@ -50,7 +57,7 @@
5057
| Use http or https
5158
|
5259
*/
53-
'secure' => false,
60+
'secure' => env('GEOIP_TELIZE_SECURE'),
5461
],
5562

5663
/*
@@ -67,14 +74,14 @@
6774
| Example: app_path().'/database/maxmind/GeoLite2-City.mmdb'
6875
|
6976
*/
70-
'database' => '',
77+
'database' => env('GEOIP_MAXMIND_DATABASE'),
7178

7279
/*
7380
|--------------------------------------------------------------------------
7481
| Maxmind Web Service Info
7582
|--------------------------------------------------------------------------
7683
*/
77-
'user_id' => '',
78-
'license_key' => '',
84+
'user_id' => env('GEOIP_MAXMIND_USER_ID'),
85+
'license_key' => env('GEOIP_MAXMIND_LICENSE_KEY'),
7986
],
8087
];

0 commit comments

Comments
 (0)