Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

🎨 Cleaner code for Public IP widget

Alicia Sykesa483adce3564
diff --git a/docs/widgets.md b/docs/widgets.md
index d9ec026e..82b0eec9 100644
--- a/docs/widgets.md
+++ b/docs/widgets.md
@@ -342,7 +342,7 @@ Using `ipgeolocation` with a key:
**Provider** | **Key** | **Proxy** | **Notes**
--- | --- | --- | ---
`freeipapi` _(default)_ | 🟒 Not needed | 🟒 Not needed | Keyless, 60 req/min via [freeipapi.com](https://freeipapi.com/).
-`ipinfo` | 🟒 Not needed / 🟠 Optional | 🟒 Not needed | Industry-standard service from [ipinfo.io](https://ipinfo.io/). Keyless gives ~1k/day; a [free token](https://ipinfo.io/signup) raises this to ~50k/month. Sometimes blocked by adblockers.
+`ipinfo` | 🟒 Not needed / 🟠 Optional | 🟒 Not needed | Service from [ipinfo.io](https://ipinfo.io/). Keyless gives ~1k/day; a [free token](https://ipinfo.io/signup) raises this to ~50k/month. Sometimes blocked by adblockers.
`ipquery` | 🟒 Not needed | 🟒 Not needed | Modern keyless API from [ipquery.io](https://ipquery.io/). Includes VPN/Tor/datacenter risk flags in the raw response.
`ip-api` | 🟒 Not needed | πŸ”΄ **Required** | [ip-api.com](https://ip-api.com/) is HTTP-only on the free tier, so the proxy is needed to avoid mixed-content errors.
`ipgeolocation` | πŸ”΄ **Required** | 🟒 Not needed | Get a free key from [ipgeolocation.io](https://ipgeolocation.io/signup.html).
diff --git a/src/components/Widgets/PublicIp.vue b/src/components/Widgets/PublicIp.vue
index b195ed02..62901568 100644
--- a/src/components/Widgets/PublicIp.vue
+++ b/src/components/Widgets/PublicIp.vue
@@ -2,10 +2,10 @@
<div class="ip-info-wrapper">
<p class="ip-address" :class="{ 'is-long': ipAddr?.length > 16 }">{{ ipAddr }}</p>
<div class="region-wrapper" v-if="!options.hideLocation">
- <img class="flag-image" :src="flagImg" alt="Flag" />
+ <img class="flag-image" v-if="flagImg" :src="flagImg" alt="Flag" />
<div class="info-text">
<p class="isp-name">{{ ispName }}</p>
- <a class="ip-location" :href="mapsUrl" title="πŸ—ΊοΈ Open in Maps">
+ <a class="ip-location" v-if="location" :href="mapsUrl" title="πŸ—ΊοΈ Open in Maps">
{{ location }}
</a>
</div>
@@ -18,9 +18,7 @@ import WidgetMixin from '@/mixins/WidgetMixin';
import { widgetApiEndpoints as urls } from '@/utils/defaults';
import { getCountryFlag, getMapUrl } from '@/utils/MiscHelpers';
-/* Each provider's `parse` returns a normalised shape:
- * { ip, isp, city, region, countryCode, lat, lon }
- * `url` may be a string or a function that takes the user's apiKey. */
+/* List of providers, with their API endpoint and response structure/parser */
const PROVIDERS = {
freeipapi: {
url: urls.publicIp4,
@@ -88,41 +86,18 @@ const PROVIDERS = {
},
};
-/* List of deleted/depricated providers. Used to show warning if user tries to use */
-const REMOVED_PROVIDERS = {
- 'ipapi.co': 'ipapi.co now serves a Cloudflare bot challenge to many clients.',
- 'ifconfig.co': 'ifconfig.co does not send Access-Control-Allow-Origin headers.',
- 'ip2location.io': 'ip2location.io does not send Access-Control-Allow-Origin headers.',
-};
-
+const REMOVED_PROVIDERS = ['ipapi.co', 'ifconfig.co', 'ip2location.io'];
const DEFAULT_PROVIDER = 'freeipapi';
export default {
mixins: [WidgetMixin],
computed: {
provider() {
- const chosen = this.parseAsEnvVar(this.options.provider);
- if (chosen && REMOVED_PROVIDERS[chosen]) {
- this.error(`Provider '${chosen}' is no longer supported: ${REMOVED_PROVIDERS[chosen]} `
- + `Pick one of: ${Object.keys(PROVIDERS).join(', ')}.`);
- return null;
- }
- if (chosen && !PROVIDERS[chosen]) {
- this.error(`Unknown provider '${chosen}'. Pick one of: ${Object.keys(PROVIDERS).join(', ')}.`);
- return null;
- }
- return chosen || DEFAULT_PROVIDER;
- },
- endpoint() {
- if (!this.provider) return null;
- const { url } = PROVIDERS[this.provider];
- return typeof url === 'function' ? url(this.apiKey) : url;
+ const chosen = this.parseAsEnvVar(this.options.provider) || DEFAULT_PROVIDER;
+ return PROVIDERS[chosen] ? chosen : null;
},
apiKey() {
- if (!this.provider) return null;
- const needsKey = PROVIDERS[this.provider].requiresKey;
- if (needsKey && !this.options.apiKey) this.error('Missing API Key');
- return this.options.apiKey;
+ return this.parseAsEnvVar(this.options.apiKey);
},
},
data() {
@@ -135,19 +110,34 @@ export default {
};
},
methods: {
- /* Make GET request to selected provider */
+ /* Validate config, then fetch the user's IP from the selected provider */
fetchData() {
- if (!this.endpoint) return;
- this.makeRequest(this.endpoint).then(this.processData);
+ const raw = this.parseAsEnvVar(this.options.provider);
+ if (raw && !PROVIDERS[raw]) {
+ const note = REMOVED_PROVIDERS.includes(raw) ? 'depricated' : 'unknown';
+ this.error(`Provider '${raw}' is ${note}. Pick one of: ${Object.keys(PROVIDERS).join(', ')}.`);
+ return;
+ }
+ const { url, requiresKey } = PROVIDERS[this.provider];
+ if (requiresKey && !this.apiKey) {
+ this.error('Missing API Key');
+ return;
+ }
+ this.makeRequest(typeof url === 'function' ? url(this.apiKey) : url).then(this.processData);
},
/* Normalise the response and assign to display fields */
processData(ipInfo) {
const parsed = PROVIDERS[this.provider].parse(ipInfo);
+ if (!parsed.ip) {
+ this.error(`Unexpected response from provider '${this.provider}'`);
+ return;
+ }
this.ipAddr = parsed.ip;
this.ispName = parsed.isp;
this.location = [parsed.city, parsed.region].filter(Boolean).join(', ');
this.flagImg = parsed.countryCode ? getCountryFlag(parsed.countryCode) : null;
- this.mapsUrl = getMapUrl({ lat: parsed.lat, lon: parsed.lon });
+ this.mapsUrl = (parsed.lat && parsed.lon)
+ ? getMapUrl({ lat: parsed.lat, lon: parsed.lon }) : null;
},
},
};