Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"keywords": ["Laravel", "ipinfolaravel"],
"require": {
"illuminate/support": "^7.0|^8.0|^9.0|^10.0|^11.0|^12.0",
"ipinfo/ipinfo": "^3.3.0"
"ipinfo/ipinfo": "^3.4.0"
},
"require-dev": {
"phpunit/phpunit": "^12.0",
Expand Down
5 changes: 5 additions & 0 deletions config/ipinforesproxylaravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
//
];
144 changes: 144 additions & 0 deletions src/resproxy/ipinforesproxylaravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace ipinfo\ipinfolaravel\resproxy;

use Closure;
use ipinfo\ipinfo\IPinfo as IPinfoClient;
use ipinfo\ipinfolaravel\DefaultCache;
use ipinfo\ipinfolaravel\iphandler\DefaultIPSelector;

class ipinforesproxylaravel
{
/**
* IPinfo API access token.
* @var string
*/
public $access_token = null;

/**
* IPinfo client object settings.
* @var array
*/
public $settings = [];

/**
* Return true to skip IPinfo lookup, otherwise return false.
* @var function
*/
public $filter = null;

/**
* Provides ip.
* @var ipinfo\ipinfolaravel\iphandler\IPHandlerInterface
*/
public $ip_selector = null;

/**
* IPinfo client instance.
* @var IPinfoClient
*/
public $ipinfo = null;

/**
* Whether to swallow exceptions.
* @var bool
*/
public $no_except = false;

const CACHE_MAXSIZE = 4096;
const CACHE_TTL = 60 * 24;

/**
* Handle an incoming request.
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->configure();

if ($this->filter && call_user_func($this->filter, $request)) {
$details = null;
} else {
try {
$details = $this->ipinfo->getResproxy(
$this->ip_selector->getIP($request),
);
} catch (\Exception $e) {
$details = null;

// users can't catch this exception with their own wrapper
// middleware unfortunately, so we catch it for them. but for
// backwards-compatibility, we throw the exception again unless
// they've told us not to.
if ($this->no_except != true) {
throw $e;
}
}
}

$request->attributes->set("ipinfo_resproxy", $details);

return $next($request);
}

/**
* Determine settings based on user-defined configs or use defaults.
*/
public function configure()
{
$this->access_token = config("services.ipinfo.access_token", null);
$this->filter = config("services.ipinfo.filter", [
$this,
"defaultFilter",
]);
$this->no_except = config("services.ipinfo.no_except", false);
$this->ip_selector = config(
"services.ipinfo.ip_selector",
new DefaultIPSelector(),
);

if (
$custom_countries = config("services.ipinfo.countries_file", null)
) {
$this->settings["countries_file"] = $custom_countries;
}

if ($custom_cache = config("services.ipinfo.cache", null)) {
$this->settings["cache"] = $custom_cache;
} else {
$maxsize = config(
"services.ipinfo.cache_maxsize",
self::CACHE_MAXSIZE,
);
$ttl = config("services.ipinfo.cache_ttl", self::CACHE_TTL);
$this->settings["cache"] = new DefaultCache($maxsize, $ttl);
}

$this->ipinfo = new IPinfoClient(
$this->access_token,
$this->settings,
);
}

/**
* Should IP lookup be skipped.
* @param Request $request Request object.
* @return bool Whether or not to filter out.
*/
public function defaultFilter($request)
{
$user_agent = $request->header("user-agent");
if ($user_agent) {
$lower_user_agent = strtolower($user_agent);

$is_spider = strpos($lower_user_agent, "spider") !== false;
$is_bot = strpos($lower_user_agent, "bot") !== false;

return $is_spider || $is_bot;
}

return false;
}
}
64 changes: 64 additions & 0 deletions src/resproxy/ipinforesproxylaravelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace ipinfo\ipinfolaravel\resproxy;

use Illuminate\Support\ServiceProvider;

class ipinforesproxylaravelServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
* @return void
*/
public function boot()
{
// Publishing is only necessary when using the CLI.
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}

/**
* Register any package services.
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__ . "/../../config/ipinforesproxylaravel.php",
"ipinforesproxylaravel",
);

// Register the service the package provides.
$this->app->singleton(
"ipinforesproxylaravel",
fn($app) => new ipinforesproxylaravel(),
);
}

/**
* Get the services provided by the provider.
* @return array
*/
public function provides()
{
return ["ipinforesproxylaravel"];
}

/**
* Console-specific booting.
* @return void
*/
protected function bootForConsole()
{
// Publishing the configuration file.
$this->publishes(
[
__DIR__ . "/../../config/ipinforesproxylaravel.php" => config_path(
"ipinforesproxylaravel.php",
),
],
"ipinforesproxylaravel.config",
);
}
}
Loading