Skip to content

Commit

Permalink
another provider
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker committed Nov 21, 2024
1 parent 19a7224 commit b9b52a1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
{
"title": "ipinfo.io",
"enum": ["ipinfo"]
},
{
"title": "getmyip.dev",
"enum": ["getmyip"]
}
]
},
Expand Down
8 changes: 3 additions & 5 deletions src/devices/contactsensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'

import type { NoIPPlatform } from '../platform.js'
import type { devicesConfig } from '../settings.js'

import { Buffer } from 'node:buffer'

import { interval, throwError } from 'rxjs'
import { skipWhile, timeout } from 'rxjs/operators'
import { request } from 'undici'

import { type devicesConfig, noip } from '../settings.js'
import { deviceBase } from './device.js'

/**
Expand Down Expand Up @@ -86,8 +86,8 @@ export class ContactSensor extends deviceBase {
try {
const ip = this.device.ipv4or6 === 'ipv6' ? await this.platform.publicIPv6(this.device) : await this.platform.publicIPv4(this.device)
const ipv4or6 = this.device.ipv4or6 === 'ipv6' ? 'IPv6' : 'IPv4'
const ipProvider = this.device.ipProvider === 'ipify' ? 'ipify.org' : 'ipinfo.io'
const { body, statusCode } = await request('https://dynupdate.no-ip.com/nic/update', {
const ipProvider = this.device.ipProvider === 'ipify' ? 'ipify.org' : this.device.ipProvider === 'getmyip' ? 'getmyip.dev' : 'ipinfo.io'
const { body, statusCode } = await request(noip, {
method: 'GET',
query: {
hostname: this.device.hostname,
Expand All @@ -101,8 +101,6 @@ export class ContactSensor extends deviceBase {
const response = await body.text()
await this.debugWarnLog(`statusCode: ${JSON.stringify(statusCode)}`)
await this.debugLog(`${ipProvider} ${ipv4or6} respsonse: ${JSON.stringify(response)}`)

// this.response = await this.platform.axios.get('https://dynupdate.no-ip.com/nic/update', this.options);
const data = response.trim()
const f = data.match(/good|nochg/g)
if (f) {
Expand Down
6 changes: 3 additions & 3 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { request } from 'undici'
import validator from 'validator'

import { ContactSensor } from './devices/contactsensor.js'
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'
import { getmyip_v4, getmyip_v6, ipify_v4, ipify_v6, ipinfo_v4, ipinfo_v6, PLATFORM_NAME, PLUGIN_NAME } from './settings.js'

/**
* HomebridgePlatform
Expand Down Expand Up @@ -223,7 +223,7 @@ export class NoIPPlatform implements DynamicPlatformPlugin {

async publicIPv4(device: devicesConfig) {
try {
const { body, statusCode } = await request(device.ipProvider === 'ipify' ? 'https://api.ipify.org?format=json' : 'https://ipinfo.io/json', {
const { body, statusCode } = await request(device.ipProvider === 'ipify' ? ipify_v4 : device.ipProvider === 'getmyip' ? getmyip_v4 : ipinfo_v4, {
method: 'GET',
})
const pubIp: any = await body.json()
Expand All @@ -238,7 +238,7 @@ export class NoIPPlatform implements DynamicPlatformPlugin {

async publicIPv6(device: devicesConfig) {
try {
const { body, statusCode } = await request(device.ipProvider === 'ipify' ? 'https://api64.ipify.org?format=json' : 'https://v6.ipinfo.io/json', {
const { body, statusCode } = await request(device.ipProvider === 'ipify' ? ipify_v6 : device.ipProvider === 'getmyip' ? getmyip_v6 : ipinfo_v6, {
method: 'GET',
})
const pubIp: any = await body.json()
Expand Down
9 changes: 9 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export const PLATFORM_NAME = 'NoIP'
*/
export const PLUGIN_NAME = 'homebridge-noip'

// API URLs
export const ipinfo_v4 = 'https://ipinfo.io/json'
export const getmyip_v4 = 'https://ipv4.getmyip.dev'
export const ipify_v4 = 'https://api.ipify.org?format=json'
export const ipinfo_v6 = 'https://v6.ipinfo.io/json'
export const getmyip_v6 = 'https://ipv6.getmyip.dev'
export const ipify_v6 = 'https://api64.ipify.org?format=json'
export const noip = 'https://dynupdate.no-ip.com/nic/update'

// Config
export interface NoIPPlatformConfig extends PlatformConfig {
name?: string
Expand Down

0 comments on commit b9b52a1

Please sign in to comment.