Skip to content

Commit

Permalink
fix: determine host ip without ip module
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed Aug 3, 2020
1 parent b548cde commit 15d2ace
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
44 changes: 44 additions & 0 deletions api/rtp-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { map, share, takeUntil } from 'rxjs/operators'
import getPort from 'get-port'
import { randomBytes } from 'crypto'
import { logError } from './util'
import os from 'os'
const stun = require('stun'),
ip = require('ip')

Expand Down Expand Up @@ -235,3 +236,46 @@ export function generateSrtpOptions(): SrtpOptions {
srtpSalt: randomBytes(14),
}
}

export function getIpAddresses(family = 'ipv4'): Array<string> {
const interfaces = os.networkInterfaces(),
familyLower = family.toLowerCase()

return Object.entries(interfaces).reduce(
(addresses, [key, interfaceInfos]) => {
// Skip all virtual and bridge interfaces
if (key.startsWith('v') || key.startsWith('br')) {
return addresses
}

const matchingAddresses = (interfaceInfos || []).reduce(
(matches, interfaceInfo) => {
// Remove addresses that have incorrect family or are internal
if (
interfaceInfo.internal ||
interfaceInfo.family.toLowerCase() !== familyLower
) {
return matches
}

return matches.concat([interfaceInfo.address])
},
[] as string[]
)

return addresses.concat(matchingAddresses)
},
[] as string[]
)
}

export function getIpAddress(family?: string) {
const addresses = getIpAddresses(family),
address = addresses[0]

if (!address) {
throw new Error('Unable to detect you IP Address')
}

return address
}
6 changes: 3 additions & 3 deletions api/sip-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { logError, logInfo } from './util'
import {
createCryptoLine,
decodeCryptoValue,
getIpAddress,
RtpOptions,
RtpStreamOptions,
} from './rtp-utils'

const ip = require('ip'),
sip = require('sip'),
const sip = require('sip'),
sdp = require('sdp')

export const expiredDingError = new Error('Ding expired, received 480')
Expand Down Expand Up @@ -114,7 +114,7 @@ export class SipCall {
) {
const { address, audio, video } = rtpOptions,
{ from } = this.sipOptions,
host = ip.address()
host = getIpAddress()

this.sipClient = sip.create(
{
Expand Down
8 changes: 2 additions & 6 deletions homebridge/camera-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RingCamera, SipSession } from '../api'
import { hap } from './hap'
import {
createCryptoLine,
getIpAddress,
getSrtpValue,
getSsrc,
RtpSplitter,
Expand All @@ -28,7 +29,6 @@ import { merge, of, Subject } from 'rxjs'
import { readFile } from 'fs'
import { promisify } from 'util'

const ip = require('ip')
const readFileAsync = promisify(readFile),
cameraOfflinePath = require.resolve('../../media/camera-offline.jpg'),
snapshotsBlockedPath = require.resolve('../../media/snapshots-blocked.jpg')
Expand Down Expand Up @@ -398,12 +398,8 @@ export class CameraSource implements CameraStreamingDelegate {
} (${getDurationSeconds(start)}s)`
)

const currentAddress = ip.address()
callback(undefined, {
address: {
address: currentAddress,
type: ip.isV4Format(currentAddress) ? 'v4' : 'v6',
},
address: getIpAddress(),
audio: {
port: returnAudioPort,
ssrc: audioSsrc,
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"ffmpeg-for-homebridge": "0.0.7",
"get-port": "^5.1.1",
"got": "^11.5.1",
"ip": "1.1.5",
"node-machine-id": "^1.1.12",
"public-ip": "4.0.2",
"rxjs": "^6.6.2",
Expand Down

0 comments on commit 15d2ace

Please sign in to comment.