パッケージの詳細

haraka-net-utils

haraka10.9kMIT1.7.2

haraka network utilities

haraka, mta, network, utility

readme

CI Code Coverage Code Climate

haraka-net-utils

This module provides network utility functions.

Usage

const net_utils = require('haraka-net-utils')`

ip_to_long

// Convert IPv4 to long
const long = net_utils.ip_to_long('11.22.33.44') // 185999660

long_to_ip

// Convert long to IPv4
const ip = net_utils.long_to_ip(185999660) // 11.22.33.44

dec_to_hex

// Convert decimal to hex
const hex = net_utils.dec_to_hex(20111104) // 132df00

hex_to_dec

// Convert hex to decimal
const dec = net_utils.hex_to_dec('132df00') // 20111104

is_local_ipv4

// Is IPv4 address on a local network?
net_utils.is_local_ipv4('127.0.0.200') // true (localhost)
net_utils.is_local_ipv4('169.254.0.0') // true (link local)
net_utils.is_local_ipv4('226.0.0.1') // false

is_private_ipv4

// Is IPv4 address in RFC 1918 reserved private address space?
net_utils.is_private_ipv4('10.0.0.0') // true
net_utils.is_private_ipv4('192.168.0.0') // true
net_utils.is_private_ipv4('172.16.0.0') // true

is_local_ipv6

// Is IPv6 addr on local network?
net_utils.is_local_ipv6('::1') // true (localhost)
net_utils.is_local_ipv6('fe80::') // true (link local)
net_utils.is_local_ipv6('fc00::') // true (unique local)
net_utils.is_local_ipv6('fd00::') // true (unique local)

is_private_ip

Determines if an IPv4 or IPv6 address is on a "private" network. For IPv4, returns true if is_private_ipv4 or is_local_ipv4 are true For IPv6, returns true if is_local_ipv6 is true

is_local_host

Checks to see if a host name matches our server hostname or resolves to any local ip. Local IPs include those bound to a local network interface and public IPs discovered with STUN.

is_local_ip

Checks to see if an IP is bound locally or an IPv4 or IPv6 localhost address.

ip_in_list

// searches for 'ip' as a hash key in the list object or array
// ip can be a host, an IP, or an IPv4 or IPv6 range
net_utils.ip_in_list(object, ip)
net_utils.ip_in_list(array, ip)
net_utils.ip_in_list(tls.no_tls_hosts, '127.0.0.5')

get_ips_by_host

Returns an array of all the IPv4 and IPv6 addresses of the provided hostname.

try {
  const ips = await net_utils.get_ips_by_host(domain)
  for (const ip of ips) {
    // do something with the IPs
  }
} catch (err) {
  // handle any errors
}

get_mx

try {
  const mxList = await net_utils.get_mx(domain)
  for (const mx of mxList) {
    // do something with each mx
  }
} catch (err) {
  // handle any errors
}

HarakaMx

An object class representing a MX. HarakaMx objects may contain the following properties:

{
  exchange: '', // required: a FQDN or IP address
  path: '', // the file path to a socket
  priority: 0, // integer, a MX priority.
  port: 25, // integer: an alternate port
  bind: '', // an outbound IP address to bind to
  bind_helo: '', // an outbound helo hostname
  using_lmtp: false, // boolean, specify LMTP delivery
  auth_user: '', // an AUTH username (required if AUTH is desired)
  auth_pass: '', // an AUTH password (required if AUTH is desired)
  auth_type: '', // an AUTH type that should be used with the MX.
  from_dns: '', // the DNS name from which the MX was queried
}

Create a HarakaMx object in The Usual Way:

const nu = require('haraka-net-utils')
const myMx = new nu.HarakaMx(parameter)

The parameter can be one of:

  • A string in any of the following formats:
    • hostname
    • hostname:port
    • IPv4
    • IPv4:port
    • [IPv6]
    • [IPv6]: port
  • A URL string
    • smtp://mail.example.com:25
    • lmtp://int-mail.example.com:24
    • smtp://user:pass@host.example.com:587
  • An object, containing at least an exchange, and any of the other properties listed at the top of this section.

An optional second parameter is an alias for from_dns.

更新履歴

Changelog

The format is based on Keep a Changelog.

Unreleased

1.7.2 - 2025-01-31

  • deps(all): bump to latest
  • deps(eslint): upgrade to v9
  • style(prettier): moved config into package.json

1.7.1 - 2024-11-13

  • deps: bump several versions to latest
  • test: fix stale IP, new err code

1.7.0 - 2024-04-29

  • feat: added HarakaMx #89
  • feat: add add_line_processor, aka line_socket.setup_line_processor
  • fix(get_public_ip): set timeout in stun request, fixes #84
  • test: added get_implicit_mx tests #89
  • change: get_mx: don't filter implicit MX errors #89

1.6.0 - 2024-04-17

  • feat: normalizeDomain, for punycode/IDN names
  • feat: getmx now _also returns implicit MX records
  • feat: added get_implicit_mx
  • feat: added resolve_mx_hosts
  • doc(Changes): fixed broken tag version links
  • doc(CONTRIBUTORS): added
  • chore: populate [files] in package.json. Delete .npmignore
  • chore(lint): remove duplicate / stale rules from .eslintrc
  • dep(punycode): punycode -> punycode.js (avoid name collission)
  • chore: refactored get_ips_by_host using Promise.allSettled

1.5.4 - 2024-04-02

  • Add timeout to DNS Resolver (#83)

1.5.3 - 2023-12-15

  • dep(punycode): override built-in with trailing /

1.5.2 - 2023-12-11

  • dep(stun): use updated @msimerson/stun

1.5.1 - 2023-12-03

  • feat(is_local_host): also match when
    • the mx dest is a hostname that matches our hostname
    • the mx dest matches our public IP (may not be locally bound)
  • bump dep versions #78
  • test update for node v20 #78
  • ci: enable CI tests on PRs #77
  • test: increase DNS timeouts from 3s to 5s #77

1.5.0 - 2022-12-20

  • feat: add async support for get_public_ip #75
  • dep: replace vs-stun with stun
  • doc: use async/await syntax in examples #74

1.4.1 - 2022-07-22

  • feat(get_mx): use async/await
  • feat(get_mx): call w/o callback for promise API
  • test(get_mx): expand and improve test coverage
  • chore(ci): use more shared haraka/.github workflows

1.3.7 - 2022-06-03

  • ci: fix the dependabot allow syntax

1.3.6 - 2022-06-01

  • chore: replace .release with submodule
  • chore(ci): populate test matrix with Node.js LTS versions
  • chore(ci): limit dependabot updates to production deps

1.3.5 - 2022-05-27

  • chore(ci): use shared GHA workflows
  • style(es6): use dns.promises internally
  • dep(async): replace async dependency with Promise.all
  • doc(README): use code fences around examples (vs indention)

1.3.4 - 2022-01-05

  • promisify get_ips_by_host (backwards compatible)

1.3.3 - 2020-01-05

  • refactored is_local_host function to return a promise instead of using a callback #65

1.3.2 - 2021-12-20

  • add is_local_host function #63

1.3.1 - 2021-10-13

  • get_mx: wrap dns.resolveMx in a try haraka/Haraka#2985
  • add .release scripts
  • add GH workflow, publish release to NPM upon merge to master

1.3.0 - 2021-01-23

  • Support passing an array to ip_in_list #60

1.2.4 - 2021-01-14

  • add "any" IP to is_local_ip
  • add TEST-NET-[1-3] to is_private_ip

1.2.3 - 2020-12-19

  • fix: restore the tests wrapping the resolveMX iterable

1.2.2 - 2020-12-15

  • get_mx: do not include implicit MX

1.2.1 - 2020-11-17

  • bump ipaddr.js to 2.0.0 #56

1.2.0 - 2020-06-23

  • added get_mx
  • remove deprecated load_tls_ini
  • remove deprecated tls_ini_section_with_defaults

1.1.5 - 2020-04-11

  • ipv6_bogus: handle parsing broken ipv6 addresses #49
  • update async to version 3.0.1 #43

1.1.4 - 2019-04-04

  • stop is_private_ip from checking if the IP is bound to a local network interface

1.1.3 - 2019-03-01

  • is_local_ip checks local network interfaces too

1.1.2 - 2018-11-03

  • add is_local_ip

1.1.1 - 2018-07-19

  • ip_in_list doesn't throw on empty list

1.1.0 - 2018-04-11

  • add get_primary_host_name haraka/Haraka#2380

1.0.14 - 2018-01-25

  • restore tls_ini_section_with_defaults function (deprecated since Haraka 2.0.17)

1.0.13 - 2018-01-19

  • get_public_ip: assign timer before calling connect #29
    • avoid race where timeout isn't cleared because stun connect errors immediately
  • remove TLS functions that have been subsumed into Haraka/tls_socket: tls_ini_section_with_defaults, parse_x509_names, parse_x509_expire, parse_x509, load_tls_dir
  • convert concatenated strings to template literals #28
  • eslint updates #25, #27
  • improved x509 parser #22

1.0.10 - 2017-07-27

  • added vs-stun as optional dep (from Haraka) #21

1.0.9 - 2017-06-16

  • lint fixes for compat with eslint 4 #18

1.0.8 - 2017-03-08

  • skip loading expired x509 (TLS) certs
  • make TLS cert dir configurable
  • rename certs -> cert (be consistent with haraka/plugins/tls)
  • store cert/key as buffers (was strings)

1.0.7 - 2017-03-08

  • handle undefined tls.ini section

1.0.6 - 2017-03-04

  • add tls_ini_section_with_defaults()
  • add load_tls_dir()
  • add parse_x509_names()

1.0.5 - 2016-11-20

  • add enableSNI TLS option

1.0.4 - 2016-10-25

  • initialize TLS opts in (section != main) as booleans

1.0.3 - 2016-10-25

  • added tls.ini loading