Skip to content

Commit

Permalink
add(Service): Add IpInfo as ApiProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Burial0268 committed Jul 30, 2024
1 parent ffefeb6 commit 4a2b6ec
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 6 deletions.
1 change: 1 addition & 0 deletions js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ out
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
yarn.lock

2 changes: 1 addition & 1 deletion js/dist/admin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/dist/admin.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions js/src/admin/components/ExtensionSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export default class GeoipSettingsPage extends ExtensionPage {
),
})}
</div>
{service === 'ipinfo' ?
this.buildSettingComponent({
type: 'string',
setting: 'gbcl-userip.service.ipinfo.key',
label: app.translator.trans('gbcl-userip.admin.service.ipinfo.key'),
required: true,
})
: []}
{this.submitButton()}
</div>
</div>
Expand Down
7 changes: 6 additions & 1 deletion resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ gbcl-userip:

ipsb:
label: IP.SB
description: Use <b>https://ip.sb</b> to get free IP location information (please note the rate limit)
description: Use <b>https://ip.sb</b> to get free IP location information (please note the rate limit)

ipinfo:
label: IpInfo
description: Use <b>https://ipinfo.io</b> to get more accurate IP information (token required)
key: IpInfo Token(Required)
7 changes: 6 additions & 1 deletion resources/locale/zh-Hans.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ gbcl-userip:

ipsb:
label: IP.SB
description: 使用 <b>https://ip.sb</b> 获取免费的 IP 属地信息 (速率限制注意)
description: 使用 <b>https://ip.sb</b> 获取免费的 IP 属地信息 (速率限制注意)

ipinfo:
label: IpInfo
description: 使用 <b>https://ipinfo.io</b> 获取更为精确的 IP 属地信息 (需要 token)
key: IpInfo Token(必填)
3 changes: 2 additions & 1 deletion src/Api/GeoIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ public function __construct(SettingsRepositoryInterface $settings)
*/
public function get(string $ip)
{
$this->serviceSelected = $this->settings->get('gbcl-userip.service');
$name = $this->settings->get('gbcl-userip.service');
$services = resolve('container')->tagged('gbcl-userip.services');

foreach ($services as $service) {
if ($service->name() === $name) {
$this->serviceSelected = $service;
break;
continue;
}
}

Expand Down
58 changes: 58 additions & 0 deletions src/Api/Service/IpInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/*
* This file is part of GBCLStudio Project.
*
* Copyright (c) 2023 GBCLStudio PHP Project Team.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace GBCLStudio\GeoIp\Api\Service;

use Flarum\Settings\SettingsRepositoryInterface;
use GBCLStudio\GeoIp\Api\GeoIpInterface;
use GBCLStudio\GeoIp\ServiceResponse;
use GuzzleHttp\Client;

class IpInfo implements GeoIpInterface
{

private Client $client;

public function __construct(protected SettingsRepositoryInterface $settings)
{
$this->client = new Client([
'base_uri' => 'https://ipinfo.io/'
]);
}

/**
* @inheritDoc
*/
public function name(): string
{
return 'ipinfo';
}

/**
* @inheritDoc
*/
public function get(string $ip): ServiceResponse
{
$apiKey = $this->settings->get("gbcl-userip.service.ipinfo.key");
if (is_null($apiKey)) throw new \InvalidArgumentException('gbcl-userip.service.ipinfo.key is null, are you set the ipinfo-key label?');

$res = $this->client->get("/$ip/json?token=$apiKey");
$body = json_decode($res->getBody());

// if not, the isp label will be longer than except
preg_match('/^AS(\d+)\s+/', $body->org, $ispASN);

return (new ServiceResponse())
->setCountryCode($body->country)
->setRegion($body->region)
->setIsp($ispASN)
->setAddress($ip);
}
}
2 changes: 2 additions & 0 deletions src/ServiceProvider/GeoIpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Closure;
use Flarum\Foundation\AbstractServiceProvider;
use GBCLStudio\GeoIp\Api\GeoIpInterface;
use GBCLStudio\GeoIp\Api\Service\IpInfo;
use GBCLStudio\GeoIp\Api\Service\IpSb;

class GeoIpServiceProvider extends AbstractServiceProvider
Expand All @@ -21,6 +22,7 @@ public function register()
{
$this->container->tag([
IpSb::class,
IpInfo::class
], 'gbcl-userip.services');

$this->container->singleton('gbcl-userip.services.admin', $this->map(function (GeoIpInterface $service) {
Expand Down

0 comments on commit 4a2b6ec

Please sign in to comment.