Skip to content

Commit

Permalink
refactoring (#32)
Browse files Browse the repository at this point in the history
* chore: begin refactoring, add timestamps to ip table

* Apply fixes from StyleCI

* additional properties, cli command, remove IPStack

* Apply fixes from StyleCI

* feat: support batch requests when using IPAPI

* Apply fixes from StyleCI

* centralize ip retrieval

* feat: respect rate limits, add ipapi-pro, wip: lookup later

* Apply fixes from StyleCI

* use dep inj rather than resolve

* Apply fixes from StyleCI

* chore: update meta

* chore: php8 self promotion

* chore: php 8.3

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Nov 23, 2023
1 parent ab9765f commit 6e577e7
Show file tree
Hide file tree
Showing 33 changed files with 932 additions and 251 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ jobs:
with:
enable_backend_testing: false
enable_phpstan: true
php_versions: '["8.0", "8.1", "8.2"]'
php_versions: '["8.0", "8.1", "8.2", "8.3"]'

backend_directory: .
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"name": "David Sevilla Martin",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "IanM",
"email": "[email protected]",
"role": "Developer"
}
],
"autoload": {
Expand Down Expand Up @@ -58,7 +63,8 @@
}
},
"require-dev": {
"flarum/phpstan": "*"
"flarum/phpstan": "*",
"fof/drafts": "*"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
Expand Down
13 changes: 6 additions & 7 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Flarum\Post\Post;
use Flarum\Settings\Event\Saving;
use FoF\GeoIP\Api\GeoIP;
use FoF\GeoIP\Repositories\GeoIPRepository;
use FoF\GeoIP\Api\Serializer\IPInfoSerializer;

return [
(new Extend\Frontend('forum'))
Expand All @@ -31,12 +31,8 @@
$document->payload['fof-geoip.services'] = array_keys(GeoIP::$services);
}),

(new Extend\Model(Post::class))->relationship('ip_info', function (Post $model) {
return $model->hasOne(IPInfo::class, 'address', 'ip_address')
->withDefault(function ($instance, $submodel) {
return resolve(GeoIPRepository::class)->retrieveForPost($submodel);
});
}),
(new Extend\Model(Post::class))
->relationship('ip_info', Model\IPInfoRelationship::class),

new Extend\Locales(__DIR__.'/resources/locale'),

Expand Down Expand Up @@ -70,4 +66,7 @@

(new Extend\Routes('api'))
->get('/ip_info/{ip}', 'fof-geoip.api.ip_info', Api\Controller\ShowIpInfoController::class),

(new Extend\Console())
->command(Console\LookupUnknownIPsCommand::class),
];
12 changes: 7 additions & 5 deletions js/src/admin/components/ExtensionSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class GeoipSettingsPage extends ExtensionPage {
)
: ''}

{['ipdata', 'ipstack'].includes(service)
{['ipdata', 'ipapi-pro'].includes(service)
? [
this.buildSettingComponent({
type: 'string',
Expand All @@ -54,11 +54,13 @@ export default class GeoipSettingsPage extends ExtensionPage {
}),
]
: []}
{service === 'ipstack'
{service === 'ipdata'
? this.buildSettingComponent({
type: 'boolean',
setting: 'fof-geoip.services.ipstack.security',
label: app.translator.trans('fof-geoip.admin.settings.security_label'),
type: 'number',
setting: 'fof-geoip.services.ipdata.quota',
label: app.translator.trans('fof-geoip.admin.settings.quota_label'),
min: 1500,
placeholder: 1500,
})
: []}
{this.submitButton()}
Expand Down
4 changes: 3 additions & 1 deletion js/src/forum/components/MapModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export default class MapModal extends Modal<MapModalAttrs> {
{ipInfo.organization() && (
<LabelValue label={app.translator.trans('fof-geoip.forum.map_modal.organization')} value={ipInfo.organization()} />
)}
{ipInfo.as() && <LabelValue label={app.translator.trans('fof-geoip.forum.map_modal.as')} value={ipInfo.as()} />}
{<LabelValue label={app.translator.trans('fof-geoip.forum.map_modal.mobile')} value={ipInfo.mobile() ? 'yes' : 'no'} />}
{ipInfo.threatLevel() && <LabelValue label={app.translator.trans('fof-geoip.forum.map_modal.threat_level')} value={ipInfo.threatLevel()} />}
{ipInfo.threatTypes().length > 0 && (
<LabelValue label={app.translator.trans('fof-geoip.forum.map_modal.threat_types')} value={ipInfo.threatTypes().join(', ')} />
Expand All @@ -79,7 +81,7 @@ export default class MapModal extends Modal<MapModalAttrs> {
</div>
<hr />
<div id="mapContainer">
<ZipCodeMap zip={ipInfo.zipCode()} country={ipInfo.countryCode()} />
<ZipCodeMap ipInfo={ipInfo} />
</div>
</div>
);
Expand Down
40 changes: 36 additions & 4 deletions js/src/forum/components/ZipCodeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ export default class ZipCodeMap extends Component {
oninit(vnode) {
super.oninit(vnode);

this.ipInfo = this.attrs.ipInfo;

this.data = null;

this.search();
// if (this.ipInfo.zipCode()) {
// this.searchZip();
// } else {
// this.searchLatLon();
// }
this.searchLatLon();
}

view() {
Expand All @@ -32,7 +39,7 @@ export default class ZipCodeMap extends Component {
return <div id="geoip-map" oncreate={this.configMap.bind(this)} />;
}

search() {
searchZip() {
if (this.loading) return;

this.loading = true;
Expand All @@ -43,8 +50,8 @@ export default class ZipCodeMap extends Component {
url: `https://nominatim.openstreetmap.org/search`,
method: 'GET',
params: {
q: this.attrs.zip,
countrycodes: this.attrs.country,
q: this.inInfo.zipCode(),
countrycodes: this.inInfo.country(),
limit: 1,
format: 'json',
},
Expand All @@ -58,6 +65,31 @@ export default class ZipCodeMap extends Component {
);
}

searchLatLon() {
if (this.loading) return;

this.loading = true;

return addResources().then(
app
.request({
url: `https://nominatim.openstreetmap.org/reverse`,
method: 'GET',
params: {
lat: this.ipInfo.latitude(),
lon: this.ipInfo.longitude(),
format: 'json',
},
})
.then((data) => {
this.data = data;
this.loading = false;

m.redraw();
})
);
}

configMap(vnode) {
if (!this.data) return;

Expand Down
2 changes: 0 additions & 2 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ export { default as extend } from './extend';
app.initializers.add('fof/geoip', () => {
extendPostMeta();
extendBanIPModal();

// This cannot be enabled until https://github.com/flarum/framework/pull/3888 is merged and released
extendAccessTokensList();
});
28 changes: 28 additions & 0 deletions js/src/forum/models/IPInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export default class IPInfo extends Model {
return Model.attribute<string>('zipCode').call(this);
}

latitude() {
return Model.attribute<number>('latitude').call(this);
}

longitude() {
return Model.attribute<number>('longitude').call(this);
}

isp() {
return Model.attribute<string>('isp').call(this);
}
Expand All @@ -21,6 +29,14 @@ export default class IPInfo extends Model {
return Model.attribute<string>('organization').call(this);
}

as() {
return Model.attribute<string>('as').call(this);
}

mobile() {
return Model.attribute<boolean>('mobile').call(this);
}

threatLevel() {
return Model.attribute<string>('threatLevel').call(this);
}
Expand All @@ -37,4 +53,16 @@ export default class IPInfo extends Model {
error() {
return Model.attribute<string>('error').call(this);
}

dataProvider() {
return Model.attribute<string>('dataProvider').call(this);
}

createdAt() {
return Model.attribute('createdAt', Model.transformDate).call(this);
}

updatedAt() {
return Model.attribute('updatedAt', Model.transformDate).call(this);
}
}
17 changes: 17 additions & 0 deletions migrations/2023_11_09_000000_add_timestamps_to_ip_info_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of fof/geoip.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Flarum\Database\Migration;

return Migration::addColumns('ip_info', [
'created_at' => ['timestamp', 'nullable' => false, 'useCurrent' => true],
'updated_at' => ['datetime', 'nullable' => true],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of fof/geoip.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

return [
'up' => function (Builder $schema) {
$schema->table('ip_info', function (Blueprint $table) {
$table->string('latitude')->after('zip_code')->nullable();
$table->string('longitude')->after('latitude')->nullable();
$table->string('as')->after('organization')->nullable();
$table->boolean('mobile')->after('address')->nullable();
$table->string('data_provider')->after('error')->nullable();
});
},
'down' => function (Builder $schema) {
$schema->table('ip_info', function (Blueprint $table) {
$table->dropColumn('latitude');
$table->dropColumn('longitude');
$table->dropColumn('as');
$table->dropColumn('mobile');
$table->dropColumn('data_provider');
});
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of fof/geoip.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Illuminate\Database\Schema\Builder;

return [
'up' => function (Builder $schema) {
$db = $schema->getConnection();

$db->table('settings')
->where('key', 'fof-geoip.service')
->where('value', 'ipstack')
->delete();
},
'down' => function (Builder $schema) {
//
},
];
12 changes: 7 additions & 5 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ fof-geoip:

service_label: Service

service_ipstack_label: IPStack
service_ipstack_description: Use <b>https://ipstack.com</b> to make up to 10,000 lookups per month for free. Paid plans include connection info (ISP, organization) and threat level.

service_ipdata_label: IPData
service_ipdata_description: Use <b>https://ipdata.co</b> to make up to 1,500 lookups daily for free. Paid plans for higher usage limits are also available.

service_ipapi_label: IP Api
service_ipapi_description: Use <b>http://ip-api.com</b> to make up to 45 lookups per minute for free. If you make more, your IP may get blacklisted, after which you can unban it at <b>http://ip-api.com/docs/unban</b>.
service_ipapi_description: Use <b>http://ip-api.com</b> to make up to 45 lookups per minute for free. If you make more, requests will be queued and looked up once the time window resets.

service_ipapi-pro_label: IP Api Pro
service_ipapi-pro_description: Use <b>https://members.ip-api.com/#pricing</b>. Pro plan has unlimited usage.

service_iplocation_label: IP Location
service_iplocation_description: Use <b>https://www.iplocation.net/</b> to retrieve country and ISP data. Lookup rate limits are unknown.

access_key_label: Access Key
security_label: Security Module (requires professional plus)
quota_label: Lookup quota

forum:
alerts:
Expand All @@ -30,6 +30,8 @@ fof-geoip:
country_code: "Country Code"
zip_code: "Zip Code"
isp: "ISP"
as: "ASN"
mobile: "Cellular network"
organization: "Organization"
threat_level: "Threat Level"
threat_types: "Threat Types"
Expand Down
Loading

0 comments on commit 6e577e7

Please sign in to comment.