1
0
Эх сурвалжийг харах

dev: ip geolocation driver (initial development)

KernelDeimos 1 сар өмнө
parent
commit
7945586050

+ 2 - 0
src/backend/exports.js

@@ -43,6 +43,7 @@ const { CaptchaModule } = require("./src/modules/captcha/CaptchaModule.js");
 const { EntityStoreModule } = require("./src/modules/entitystore/EntityStoreModule.js");
 const { AnalyticsModule } = require("./src/modules/analytics/AnalyticsModule.js");
 const { KVStoreModule } = require("./src/modules/kvstore/KVStoreModule.js");
+const { ExternalExtrasModule } = require("./src/modules/external-extras/ExternalExtrasModule.js");
 
 module.exports = {
     helloworld: () => {
@@ -82,6 +83,7 @@ module.exports = {
     PuterExecModule,
     BroadcastModule,
     InternetModule,
+    ExternalExtrasModule,
     MailModule,
     ConvertModule,
     CaptchaModule,

+ 17 - 0
src/backend/src/modules/external-extras/ExternalExtrasModule.js

@@ -0,0 +1,17 @@
+const { AdvancedBase } = require("@heyputer/putility");
+const config = require("../../config");
+
+class ExternalExtrasModule extends AdvancedBase {
+    async install (context) {
+        const services = context.get('services');
+        
+        if ( !! config?.services?.['ip-geo'] ) {
+            const { IPGeoService } = require('./IPGeoService');
+            services.registerService('ip-geo', IPGeoService);
+        }
+    }
+}
+
+module.exports = {
+    ExternalExtrasModule,
+};

+ 56 - 0
src/backend/src/modules/external-extras/IPGeoService.js

@@ -0,0 +1,56 @@
+const BaseService = require("../../services/BaseService");
+
+class IPGeoService extends BaseService {
+    async ['__on_driver.register.interfaces'] () {
+        const svc_registry = this.services.get('registry');
+        const col_interfaces = svc_registry.get('interfaces');
+        
+        col_interfaces.set('ip-geo', {
+            description: 'IP Geolocation',
+            methods: {
+                ipgeo: {
+                    description: 'Report geolocation information',
+                    parameters: {
+                        ip: {
+                            type: 'string',
+                        },
+                    },
+                    result: {
+                        type: 'json'
+                    },
+                },
+            }
+        });
+    }
+    
+    static IMPLEMENTS = {
+        ['ip-geo']: {
+            async ipgeo ({ ip }) {
+                // doing this makes vscode recognize what's being required
+                const require = this.require;
+
+                const axios = require('axios');
+                const querystring = require('querystring');
+                
+                const qstr = querystring.stringify({
+                    // Yep, API key reall does go in the query string.
+                    // This is what the docs say to do.
+                    apiKey: this.config.apiKey,
+                    
+                    ip,
+                });
+                
+                const resp = await axios.request({
+                    method: 'GET',
+                    url:  'https://api.ipgeolocation.io/ipgeo?' + qstr,
+                });
+                
+                return resp.data;
+            }
+        }
+    }
+}
+
+module.exports = {
+    IPGeoService,
+};

+ 2 - 0
tools/run-selfhosted.js

@@ -89,6 +89,7 @@ const main = async () => {
         PuterAIModule,
         PuterExecModule,
         InternetModule,
+        ExternalExtrasModule,
         MailModule,
         ConvertModule,
         DevelopmentModule,
@@ -108,6 +109,7 @@ const main = async () => {
     k.add_module(new PuterAIModule());
     k.add_module(new PuterExecModule());
     k.add_module(new InternetModule());
+    k.add_module(new ExternalExtrasModule());
     k.add_module(new MailModule());
     k.add_module(new ConvertModule());
     if ( process.env.UNSAFE_PUTER_DEV ) {