Skip to content

Commit

Permalink
ipProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker committed Nov 21, 2024
1 parent 668bc0a commit 19a7224
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
17 changes: 17 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@
}
]
},
"ipProvider": {
"title": "IP Provider",
"type": "string",
"required": true,
"default": "default",
"oneOf": [
{
"title": "ipify.org",
"enum": ["ipify"]
},
{
"title": "ipinfo.io",
"enum": ["ipinfo"]
}
]
},
"hostname": {
"title": "Hostname",
"type": "string"
Expand Down Expand Up @@ -155,6 +171,7 @@
"devices[].username",
"devices[].password",
"devices[].ipv4or6",
"devices[].ipProvider",
"devices[].firmware",
"devices[].refreshRate",
"devices[].logging"
Expand Down
5 changes: 3 additions & 2 deletions src/devices/contactsensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ export class ContactSensor extends deviceBase {
*/
async refreshStatus() {
try {
const ip = this.device.ipv4or6 === 'ipv6' ? this.platform.publicIPv6 : this.platform.publicIPv4
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', {
method: 'GET',
query: {
Expand All @@ -99,7 +100,7 @@ export class ContactSensor extends deviceBase {
})
const response = await body.text()
await this.debugWarnLog(`statusCode: ${JSON.stringify(statusCode)}`)
await this.debugLog(`${ipv4or6} respsonse: ${JSON.stringify(response)}`)
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()
Expand Down
32 changes: 12 additions & 20 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ export class NoIPPlatform implements DynamicPlatformPlugin {
await this.verifyConfig()
await this.debugLog('Config OK')
} catch (e: any) {
await this.errorLog(`Verify Config, Error Message: ${e.message}, Submit Bugs Here: https://bit.ly/homebridge-noip-bug-report`)
this.debugErrorLog(`Verify Config, Error: ${e}`)
await this.errorLog(`Verify Config, Error Message: ${e.message ?? e}, Submit Bugs Here: https://bit.ly/homebridge-noip-bug-report`)
}
})()

Expand All @@ -87,8 +86,7 @@ export class NoIPPlatform implements DynamicPlatformPlugin {
try {
await this.discoverDevices()
} catch (e: any) {
await this.errorLog(`Failed to Discover Devices ${JSON.stringify(e.message)}`)
this.debugErrorLog(`Failed to Discover, Error: ${e}`)
await this.errorLog(`Failed to Discover Devices ${JSON.stringify(e.message ?? e)}`)
}
})
}
Expand Down Expand Up @@ -175,7 +173,7 @@ export class NoIPPlatform implements DynamicPlatformPlugin {
? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.userDefinedDeviceName)
: await this.validateAndCleanDisplayName(hostname, 'hostname', hostname)

existingAccessory.context.serialNumber = device.ipv4or6 === 'ipv6' ? this.publicIPv6 : this.publicIPv4
existingAccessory.context.serialNumber = device.ipv4or6 === 'ipv6' ? await this.publicIPv6(device) : await this.publicIPv4(device)
existingAccessory.context.model = 'DUC'
existingAccessory.context.version = await this.getVersion()
this.api.updatePlatformAccessories([existingAccessory])
Expand All @@ -199,7 +197,7 @@ export class NoIPPlatform implements DynamicPlatformPlugin {
accessory.displayName = device.configDeviceName
? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.userDefinedDeviceName)
: await this.validateAndCleanDisplayName(hostname, 'hostname', hostname)
accessory.context.serialNumber = device.ipv4or6 === 'ipv6' ? this.publicIPv6 : this.publicIPv4
accessory.context.serialNumber = device.ipv4or6 === 'ipv6' ? await this.publicIPv6(device) : await this.publicIPv4(device)
accessory.context.model = 'DUC'
accessory.context.version = await this.getVersion()
// the accessory does not yet exist, so we need to create it
Expand All @@ -223,35 +221,29 @@ export class NoIPPlatform implements DynamicPlatformPlugin {
await this.warnLog(`Removing existing accessory from cache: ${existingAccessory.displayName}`)
}

async publicIPv4() {
async publicIPv4(device: devicesConfig) {
try {
const { body, statusCode, headers } = await request('https://ipinfo.io/json', {
const { body, statusCode } = await request(device.ipProvider === 'ipify' ? 'https://api.ipify.org?format=json' : 'https://ipinfo.io/json', {
method: 'GET',
})
const pubIp: any = await body.json()
this.debugWarnLog(`IPv4 Address: ${JSON.stringify(pubIp.ip)}`)
this.debugWarnLog(`Status Code: ${JSON.stringify(statusCode)}`)
this.debugWarnLog(`Headers: ${JSON.stringify(headers)}`)
// const pubIp = (await axios.get('https://ipinfo.io/json')).data;
// await this.debugLog(JSON.stringify(pubIp));
this.debugSuccessLog(`IPv4 Address: ${JSON.stringify(pubIp.ip)}`)
this.debugSuccessLog(`Status Code: ${JSON.stringify(statusCode)}`)
const IPv4 = pubIp.ip
return IPv4
} catch {
await this.errorLog('Not Able To Retreive IPv4 Address')
}
}

async publicIPv6() {
async publicIPv6(device: devicesConfig) {
try {
const { body, statusCode, headers } = await request('https://v6.ipinfo.io/json', {
const { body, statusCode } = await request(device.ipProvider === 'ipify' ? 'https://api64.ipify.org?format=json' : 'https://v6.ipinfo.io/json', {
method: 'GET',
})
const pubIp: any = await body.json()
this.debugWarnLog(`IPv6 Address: ${JSON.stringify(pubIp.ip)}`)
this.debugWarnLog(`Status Code: ${JSON.stringify(statusCode)}`)
this.debugWarnLog(`Headers: ${JSON.stringify(headers)}`)
// const pubIp = (await axios.get('https://ipinfo.io/json')).data;
// await this.debugLog(JSON.stringify(pubIp));
this.debugSuccessLog(`IPv6 Address: ${JSON.stringify(pubIp.ip)}`)
this.debugSuccessLog(`Status Code: ${JSON.stringify(statusCode)}`)
const IPv6 = pubIp.ip
return IPv6
} catch {
Expand Down
1 change: 1 addition & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface devicesConfig {
username?: string
password?: string
ipv4or6?: string
ipProvider?: string
firmware: string
refreshRate?: number
updateRate?: number
Expand Down

0 comments on commit 19a7224

Please sign in to comment.