{"id":7539868,"url":"https://github.com/nodertc/stun","last_synced_at":"2025-04-05T10:10:42.825Z","repository":{"id":44334486,"uuid":"98049879","full_name":"nodertc/stun","owner":"nodertc","description":"Low-level Session Traversal Utilities for NAT (STUN) client and server","archived":false,"fork":false,"pushed_at":"2023-12-12T04:14:32.000Z","size":290,"stargazers_count":163,"open_issues_count":10,"forks_count":29,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-29T09:11:39.780Z","etag":null,"topics":["js","nodejs","rfc5389","stun","webrtc"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nodertc.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://www.buymeacoffee.com/reklatsmasters"]}},"created_at":"2017-07-22T18:18:39.000Z","updated_at":"2025-03-23T10:39:48.000Z","dependencies_parsed_at":"2024-06-18T14:06:49.467Z","dependency_job_id":null,"html_url":"https://github.com/nodertc/stun","commit_stats":{"total_commits":111,"total_committers":7,"mean_commits":"15.857142857142858","dds":0.5135135135135135,"last_synced_commit":"faa4f26c91090938d4b0c35d5a21a704a5af6cac"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodertc%2Fstun","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodertc%2Fstun/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodertc%2Fstun/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodertc%2Fstun/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nodertc","download_url":"https://codeload.github.com/nodertc/stun/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247318745,"owners_count":20919484,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["js","nodejs","rfc5389","stun","webrtc"],"created_at":"2024-04-08T00:09:13.536Z","updated_at":"2025-04-05T10:10:42.803Z","avatar_url":"https://github.com/nodertc.png","language":"JavaScript","readme":"# stun\n\n[![Build Status](https://travis-ci.org/nodertc/stun.svg?branch=master)](https://travis-ci.org/nodertc/stun)\n[![npm](https://img.shields.io/npm/v/stun.svg)](https://npmjs.org/package/stun)\n[![node](https://img.shields.io/node/v/stun.svg)](https://npmjs.org/package/stun)\n[![license](https://img.shields.io/npm/l/stun.svg)](https://npmjs.org/package/stun)\n[![downloads](https://img.shields.io/npm/dm/stun.svg)](https://npmjs.org/package/stun)\n[![Coverage Status](https://coveralls.io/repos/github/nodertc/stun/badge.svg?branch=master)](https://coveralls.io/github/nodertc/stun?branch=master)\n[![Gitter chat](https://badges.gitter.im/nodertc.png)](https://gitter.im/nodertc/community)\n\nSession Traversal Utilities for NAT (STUN) server. Implements [RFC5389](https://tools.ietf.org/html/rfc5389) with partial support [RFC5766](https://tools.ietf.org/html/rfc5766), [RFC5245](https://tools.ietf.org/html/rfc5245), [RFC5780](https://tools.ietf.org/html/rfc5780).\n\n### Support\n\n[![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png)](https://www.buymeacoffee.com/reklatsmasters)\n\n## Install\n\n```\nnpm i stun\n```\n\n## Usage\n\n```js\nconst stun = require('stun');\n\nstun.request('stun.l.google.com:19302', (err, res) =\u003e {\n  if (err) {\n    console.error(err);\n  } else {\n    const { address } = res.getXorAddress();\n    console.log('your ip', address);\n  }\n});\n\n// or with promise\n\nconst res = await stun.request('stun.l.google.com:19302');\nconsole.log('your ip', res.getXorAddress().address);\n```\n\n## CLI\n\n```bash\n$ npm i -g stun\n$ stun # started on udp/0.0.0.0:3478\n```\n\n## API\n\n* [`createMessage(type: number [, transaction: Buffer]): StunRequest`](#create-message)\n* [`createTransaction(): Buffer`](#create-transaction)\n* [`createServer(options: Object): StunServer`](#create-server)\n* [`validateFingerprint(message: StunMessage): bool`](#validate-fingerprint)\n* [`validateMessageIntegrity(message: StunMessage, key: string): bool`](#validate-message-integrity)\n* [`request(url: string, [options: RequestOptions], callback: function): void`](#request)\n* [`encode(message: StunMessage): Buffer`](#encode)\n* [`decode(message: Buffer): StunResponse`](#decode)\n* [`class StunRequest`](#class-stun-request)\n  * [`setType(type)`](#class-stun-message-set-type)\n  * [`setTransactionId(transaction: Buffer): bool`](#class-stun-message-set-transaction-id)\n  * [`addAttribute(type, address: string, port: number)`](#class-stun-message-add-attribute-address)\n  * [`addAttribute(type, value: String|Buffer[, encoding: string = 'utf8'])`](#class-stun-message-add-attribute-string)\n  * [`addAttribute(type, value: number)`](#class-stun-message-add-attribute-number)\n  * [`addAttribute(type, value: array\u003cnumber\u003e)`](#class-stun-message-add-attribute-array)\n  * [`addAttribute(type, code: number, reason: string)`](#class-stun-message-add-attribute-error)\n  * [`addAddress(ip: string, port: number): StunAddressAttribute`](#class-stun-message-add-address)\n  * [`addAlternateServer(ip: string, port: number): StunAddressAttribute`](#class-stun-message-add-alternate-server)\n  * [`addXorAddress(ip: string, port: number): StunXorAddressAttribute`](#class-stun-message-add-xor-address)\n  * [`addUsername(username: string): StunByteStringAttribute`](#class-stun-message-add-username)\n  * [`addRealm(realm: string): StunByteStringAttribute`](#class-stun-message-add-realm)\n  * [`addNonce(nonce: string): StunByteStringAttribute`](#class-stun-message-add-nonce)\n  * [`addSoftware(software: string): StunByteStringAttribute`](#class-stun-message-add-software)\n  * [`addUnknownAttributes(attributes: number[]): StunUInt16ListAttribute`](#class-stun-message-add-unknown-attributes)\n  * [`addError(code: number, reason: string): StunErrorCodeAttribute`](#class-stun-message-add-error)\n  * [`addPriority(priority: number): StunUInt32Attribute`](#class-stun-message-add-priority)\n  * [`addUseCandidate(): StunByteStringAttribute`](#class-stun-message-add-use-candidate)\n  * [`addIceControlled(tiebreaker: Buffer): StunByteStringAttribute`](#class-stun-message-add-ice-controlled)\n  * [`addIceControlling(tiebreaker: Buffer): StunByteStringAttribute`](#class-stun-message-add-ice-controlling)\n  * [`removeAttribute(type): bool`](#class-stun-message-remove-attribute)\n  * [`addMessageIntegrity(key: string)`](#class-stun-message-add-message-integrity)\n  * [`addFingerprint()`](#class-stun-message-add-fingerprint)\n  * [`toBuffer(): Buffer`](#class-stun-message-to-buffer)\n* [`class StunResponse`](#class-stun-response)\n  * [`getAddress(): Object`](#class-stun-response-get-address)\n  * [`getXorAddress(): Object`](#class-stun-response-get-xor-address)\n  * [`getAlternateServer(): Object`](#class-stun-response-get-alternate-server)\n  * [`getUsername(): string`](#class-stun-response-get-username)\n  * [`getError(): Object`](#class-stun-response-get-error)\n  * [`getRealm(): string`](#class-stun-response-get-realm)\n  * [`getNonce(): string`](#class-stun-response-get-nonce)\n  * [`getSoftware(): string`](#class-stun-response-get-software)\n  * [`getUnknownAttributes(): number[]`](#class-stun-response-get-unknown-attributes)\n  * [`getMessageIntegrity(): Buffer`](#class-stun-response-get-message-integrity)\n  * [`getFingerprint(): number`](#class-stun-response-get-fingerprint)\n  * [`getPriority(): number`](#class-stun-response-get-priority)\n  * [`getIceControlled(): Buffer`](#class-stun-response-get-ice-controlled)\n  * [`getIceControlling(): Buffer`](#class-stun-response-get-ice-controlling)\n* [`class StunMessage`](#class-stun-message)\n  * [`get type`](#class-stun-message-get-type)\n  * [`get transactionId`](#class-stun-message-get-type)\n  * [`isLegacy(): bool`](#class-stun-message-is-legacy)\n  * [`getAttribute(type): StunAttribute`](#class-stun-message-get-attribute)\n  * [`hasAttribute(type): bool`](#class-stun-message-has-attribute)\n  * [`get count: number`](#class-stun-message-get-count)\n* [`class StunServer`](#class-stun-server)\n  * [`new StunServer(socket: dgram.Socket)`](#class-stun-server-new)\n  * [`send(message: StunMessage, port: number, address: string[, cb: function])`](#class-stun-server-send)\n  * [`close()`](#class-stun-server-close)\n  * [`listen(port: number, [address: string], [callback: function()])`](#class-stun-server-listen)\n  * [`Event: bindingRequest`](#class-stun-server-event-binding-request)\n  * [`Event: bindingIndication`](#class-stun-server-event-binding-indication)\n  * [`Event: bindingResponse`](#class-stun-server-event-binding-response)\n  * [`Event: bindingError`](#class-stun-server-event-binding-error)\n  * [`Event: close`](#class-stun-server-event-close)\n  * [`Event: error`](#class-stun-server-event-error)\n  * [`Event: listening`](#class-stun-server-event-listening)\n* [`class StunAttribute`](#class-stun-attribute)\n  * [`get type`](#class-stun-attribute-get-type)\n  * [`get value`](#class-stun-attribute-get-value)\n* [`constants: Object`](#constants)\n* [`class StunError`](#class-stun-error)\n* [`class StunMessageError`](#class-stun-message-error)\n* [`class StunResponseError`](#class-stun-response-error)\n\n\u003ca name=\"create-message\" /\u003e\n\n#### `createMessage(type: number [, transaction: Buffer]): StunRequest`\n\nCreates an `StunRequest` object of the specified `type` with random `transaction` field. The `type` argument is a number that should be a message type. See `constants` below.\n\n\u003ca name=\"create-transaction\" /\u003e\n\n#### `createTransaction(): Buffer`\n\nCreate transaction id for STUN message. Follow [RFC5389](https://tools.ietf.org/html/rfc5389).\n\n\u003ca name=\"create-server\" /\u003e\n\n#### `createServer(options: Object): StunServer`\n\n* `options.type: string`\n\nThe type of socket. Must be 'udp4' or 'udp6'. Required.\n\n* `options.socket: dgram.Socket`\n\nCreates a `StunServer` object of the specified type. The `type` argument should be 'udp' at the moment. An optional `socket` argument should be instance of `dgram.Socket`. If `socket` is not specifed, the `dgram.Socket` will be created with `udp4` type and will bound to the \"all interfaces\" address on a random port.\n\n\u003ca name=\"validate-fingerprint\" /\u003e\n\n#### `validateFingerprint(message: StunMessage): bool`\n\nCheck a `FINGERPRINT` attribute if it is specifed.\n\n\u003ca name=\"validate-message-integrity\" /\u003e\n\n#### `validateMessageIntegrity(message: StunMessage, key: string): bool`\n\nCheck a `MESSAGE_INTEGRITY` attribute if it is specifed.\n\n```js\nstunServer.on('bindingResponse', (msg) =\u003e {\n  if (!stun.validateFingerprint(msg)) {\n    // do stuff..\n  }\n\n  if (!stun.validateMessageIntegrity(msg, icePassword)) {\n    // do stuff...\n  }\n})\n```\n\n\u003ca name=\"request\" /\u003e\n\n#### `request(url: string, [options: RequestOptions], callback: function): void`\n#### `request(url: string, [options: RequestOptions]): Promise`\n\nCreate a request `STUN_BINDING_REQUEST` to stun server, follow [RFC5389](https://tools.ietf.org/html/rfc5389). The first argument may be a host (`stun.example.com`), host with port (`stun.example.com:1234`) or host with port and protocol (`stun://stun.example.com:1234`). By default, port is 3478.\n\nAll options described below are optional.\n\n* `options.server: StunServer` - A stun server to receive responses.\n* `options.socket: dgram.Socket` - A UDP socket over which the message will be send.\n* `options.message: StunMessage` - A `STUN_BINDING_REQUEST` message to send.\n* `options.timeout: number` - Initial retransmission timeout (*RTO*) in ms, default is 500ms.\n* `options.maxTimeout: number`- Maximal RTO, default is infinity.\n* `options.retries: number` - Maximal the number of retries, default is 6\n\nThe last argument is a function with 2 arguments `err` and `res`. It's follow nodejs callback style. The second argument is instance of `StunMessage`.\n\n\u003ca name=\"encode\" /\u003e\n\n#### `encode(message: StunMessage): Buffer`\n\nEncode `StunRequest` or `StunResponse` into the Buffer.\n\n\u003ca name=\"decode\" /\u003e\n\n#### `decode(message: Buffer): StunResponse`\n\nDecode the Buffer into a `StunResponse`.\n\n```js\nconst socket = dgram.createSocket({ type: 'udp4' });\n\nsocket.on('message', (message) =\u003e {\n  const response = stun.decode(message);\n  // do stuff ...\n});\n```\n\n\u003ca name=\"class-stun-message\" /\u003e\n\n#### `class StunMessage`\n\nThe `StunMessage` class is an utility that encapsulates the `STUN` protocol. This is a base class for `StunRequest` and `StunResponse`.\n\n\u003ca name=\"class-stun-message-get-type\" /\u003e\n\n* **get `type`**\n* **get `transactionId`**\n\nReturns the `type` and `transactionId` fields from the current message.\n\n\u003ca name=\"class-stun-message-is-legacy\" /\u003e\n\n* **`isLegacy(): bool`**\n\nReturns true if the message confirms to RFC3489 rather than RFC5389.\n\n\u003ca name=\"class-stun-message-get-attribute\" /\u003e\n\n* **`getAttribute(type): StunAttribute`**\n\nReturns the `StunAttribute` attribute of the specified `type`. The `type` argument is a number that should be an attribute type. See `constants` below. Return `undefined` if attribute is not exist.\n\n**N.B.** This method return only first matched attribute. If you want to get another one, try this:\n\n```js\nconst attributes = Array.from(stunMessage).filter(attribute =\u003e attribute.type === STUN_ATTR_MAPPED_ADDRESS);\n```\n\n\u003ca name=\"class-stun-message-get-count\" /\u003e\n\n* **get `count: number`**\n\nReturns the number of an attributes in the current message.\n\n\u003ca name=\"class-stun-request\" /\u003e\n\n#### `class StunRequest`\n\nThe `StunRequest` encapsulates outgoing messages of the `STUN` protocol. Instances of the `StunRequest` can be created using the `createMessage()`.\n\n\u003ca name=\"class-stun-message-set-type\" /\u003e\n\n* **`setType(type)`**\n\nSet the type of the message. The `type` argument is a number that should be a message type. See `constants` below.\n\n\u003ca name=\"class-stun-message-set-transaction-id\" /\u003e\n\n* **`setTransactionId(transaction: Buffer): bool`**\n\nSet the transaction id of the message. The `transaction` argument should be a `Buffer` and have length 12 bytes.\n\n\u003ca name=\"class-stun-message-add-attribute-address\" /\u003e\n\n* **`addAttribute(type, address: string, port: number)`**\n\nAdds a `type` attribute to the current message. The `type` argument should be one of:\n  * `STUN_ATTR_MAPPED_ADDRESS`\n  * `STUN_ATTR_ALTERNATE_SERVER`\n  * `STUN_ATTR_XOR_MAPPED_ADDRESS`\n  * `STUN_ATTR_RESPONSE_ORIGIN`\n  * `STUN_ATTR_OTHER_ADDRESS`\n  * `STUN_ATTR_XOR_PEER_ADDRESS`\n  * `STUN_ATTR_XOR_RELAYED_ADDRESS`.\n\n```js\nstunMsg.addAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, '8.8.8.8', 19302)\n```\n\n\u003ca name=\"class-stun-message-add-attribute-string\" /\u003e\n\n* **`addAttribute(type, value: String|Buffer[, encoding: string = 'utf8'])`**\n\nAdds a `type` attribute to the current message. The `type` argument should be one of:\n  * `STUN_ATTR_USERNAME`\n  * `STUN_ATTR_REALM`\n  * `STUN_ATTR_NONCE`\n  * `STUN_ATTR_SOFTWARE`\n  * `STUN_ATTR_ORIGIN`\n  * `STUN_ATTR_USE_CANDIDATE`\n  * `STUN_ATTR_ICE_CONTROLLED`\n  * `STUN_ATTR_ICE_CONTROLLING`\n  * `STUN_ATTR_DATA`\n  * `STUN_ATTR_EVEN_PORT`\n  * `STUN_ATTR_RESERVATION_TOKEN`\n  * `STUN_ATTR_DONT_FRAGMENT`\n  * `STUN_ATTR_PADDING`.\n\n```js\nstunMsg.addAttribute(STUN_ATTR_SOFTWARE, 'node/8.2.0 stun/1.0.0')\n```\n\n\u003ca name=\"class-stun-message-add-attribute-number\" /\u003e\n\n* **`addAttribute(type, value: number)`**\n\nAdds a `type` attribute to the current message. The `type` argument should be one of:\n  * `STUN_ATTR_RETRANSMIT_COUNT`\n  * `STUN_ATTR_PRIORITY`\n  * `STUN_ATTR_NETWORK_INFO`\n  * `STUN_ATTR_NOMINATION`\n  * `STUN_ATTR_CHANNEL_NUMBER`\n  * `STUN_ATTR_LIFETIME`\n  * `STUN_ATTR_REQUESTED_TRANSPORT`\n  * `STUN_ATTR_CHANGE_REQUEST`\n  * `STUN_ATTR_RESPONSE_PORT`.\n\n```js\nstunMsg.addAttribute(STUN_ATTR_PRIORITY, 123)\n```\n\n\u003ca name=\"class-stun-message-add-attribute-array\" /\u003e\n\n* **`addAttribute(type, value: array\u003cnumber\u003e)`**\n\nAdds a `type` attribute to the current message. The `type` argument should be `STUN_ATTR_UNKNOWN_ATTRIBUTES`.\n\n```js\nstunMsg.addAttribute(STUN_ATTR_UNKNOWN_ATTRIBUTES, [2, 3, 4])\n```\n\n\u003ca name=\"class-stun-message-add-attribute-error\" /\u003e\n\n* **`addAttribute(type, code: number, reason: string)`**\n\nAdds a `type` attribute to the current message. The `type` argument should be `STUN_ATTR_ERROR_CODE`.\n\n```js\nstunMsg.addAttribute(STUN_ATTR_ERROR_CODE, STUN_CODE_UNAUTHORIZED, STUN_REASON_UNAUTHORIZED)\n```\n\n\u003ca name=\"class-stun-message-add-address\" /\u003e\n\n* **`addAddress(ip: string, port: number): StunAddressAttribute`**\n\nAdds a `MAPPED-ADDRESS` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.1)\n\n\u003ca name=\"class-stun-message-add-alternate-server\" /\u003e\n\n* **`addAlternateServer(ip: string, port: number): StunAddressAttribute`**\n\nAdds a `ALTERNATE-SERVER` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.11)\n\n\u003ca name=\"class-stun-message-add-xor-address\" /\u003e\n\n* **`addXorAddress(ip: string, port: number): StunXorAddressAttribute`**\n\nAdds a `XOR-MAPPED-ADDRESS` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.2)\n\n\u003ca name=\"class-stun-message-add-username\" /\u003e\n\n* **`addUsername(username: string): StunByteStringAttribute`**\n\nAdds a `USERNAME` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.3)\n\n\u003ca name=\"class-stun-message-add-realm\" /\u003e\n\n* **`addRealm(realm: string): StunByteStringAttribute`**\n\nAdds a `REALM` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.7)\n\n\u003ca name=\"class-stun-message-add-nonce\" /\u003e\n\n* **`addNonce(nonce: string): StunByteStringAttribute`**\n\nAdds a `NONCE` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.8)\n\n\u003ca name=\"class-stun-message-add-software\" /\u003e\n\n* **`addSoftware(software: string): StunByteStringAttribute`**\n\nAdds a `SOFTWARE` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.10)\n\n\u003ca name=\"class-stun-message-add-unknown-attributes\" /\u003e\n\n* **`addUnknownAttributes(attributes: number[]): StunUInt16ListAttribute`**\n\nAdds a `UNKNOWN-ATTRIBUTES` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.9)\n\n\u003ca name=\"class-stun-message-add-error\" /\u003e\n\n* **`addError(code: number, reason: string): StunErrorCodeAttribute`**\n\nAdds a `ERROR-CODE` attribute to the message.\n\nSee [RFC5389](https://tools.ietf.org/html/rfc5389#section-15.6)\n\n\u003ca name=\"class-stun-message-add-priority\" /\u003e\n\n* **`addPriority(priority: number): StunUInt32Attribute`**\n\nAdds a `PRIORITY` attribute to the message.\n\nSee [RFC8445](https://tools.ietf.org/html/rfc8445#section-16.1)\n\n\u003ca name=\"class-stun-message-add-use-candidate\" /\u003e\n\n* **`addUseCandidate(): StunByteStringAttribute`**\n\nAdds a `USE-CANDIDATE` attribute to the message.\n\nSee [RFC8445](https://tools.ietf.org/html/rfc8445#section-16.1)\n\n\u003ca name=\"class-stun-message-add-ice-controlled\" /\u003e\n\n* **`addIceControlled(tiebreaker: Buffer): StunByteStringAttribute`**\n\nAdds a `ICE-CONTROLLED` attribute to the message.\n\nSee [RFC8445](https://tools.ietf.org/html/rfc8445#section-16.1)\n\n\u003ca name=\"class-stun-message-add-ice-controlling\" /\u003e\n\n* **`addIceControlling(tiebreaker: Buffer): StunByteStringAttribute`**\n\nAdds a `ICE-CONTROLLING` attribute to the message.\n\nSee [RFC8445](https://tools.ietf.org/html/rfc8445#section-16.1)\n\n\u003ca name=\"class-stun-message-remove-attribute\" /\u003e\n\n* **`removeAttribute(type): bool`**\n\nRemove a `type` attribute from the current message. Returns true if an attribute was removed. The `type` argument is a number that should be an attribute type. See `constants` below.\n\n\u003ca name=\"class-stun-message-add-message-integrity\" /\u003e\n\n* **`addMessageIntegrity(key: string)`**\n\nAdds a `MESSAGE-INTEGRITY` attribute that is valid for the current message. The `key` is the HMAC key used to generate the cryptographic HMAC hash.\n\n\u003ca name=\"class-stun-message-add-fingerprint\" /\u003e\n\n* **`addFingerprint()`**\n\nAdds a `FINGERPRINT` attribute that is valid for the current message.\n\n\u003ca name=\"class-stun-message-to-buffer\" /\u003e\n\n* **`toBuffer(): Buffer`**\n\nConverts a `StunMessage` object to the buffer.\n\n\u003ca name=\"class-stun-server\" /\u003e\n\n#### `class StunServer`\n\nThe `StunServer` class is an EventEmitter that encapsulates a STUN server.\n\n\u003ca name=\"class-stun-server-new\" /\u003e\n\n* **`new StunServer(socket: dgram.Socket)`**\n\nCreates a new `StunServer` object. The `socket` argument should be an instance of `dgram.Socket`. The incoming message is silently ignored when it is not a `stun` one.\n\n\u003ca name=\"class-stun-server-send\" /\u003e\n\n* **`send(message: StunMessage, port: number, address: string[, cb: function])`**\n\nSends the `StunMessage` message on the socket. The destination `port` and `address` must be specified. An optional `callback` function will be called when the message has been sent.\n\n\u003ca name=\"class-stun-server-close\" /\u003e\n\n* **`close()`**\n\nStops the processing of the incoming messages and emits `close` event.\n\n\u003ca name=\"class-stun-server-listen\" /\u003e\n\n* **`listen(port: number, [address: string], [callback: function()])`**\n\nAttempt to listen for messages on a named `port` and optional `address`. For UDP servers calls [`socket.bind`](https://nodejs.org/dist/latest-v10.x/docs/api/dgram.html#dgram_socket_bind_port_address_callback) under the hood.\n\n\u003ca name=\"class-stun-server-event-binding-request\" /\u003e\n\n* **Event: `bindingRequest`**\n\nEmitted when the `STUN_BINDING_REQUEST` message is available on a socket.\n\n\u003ca name=\"class-stun-server-event-binding-indication\" /\u003e\n\n* **Event: `bindingIndication`**\n\nEmitted when the `STUN_BINDING_INDICATION` message is available on a socket.\n\n\u003ca name=\"class-stun-server-event-binding-response\" /\u003e\n\n* **Event: `bindingResponse`**\n\nEmitted when the `STUN_BINDING_RESPONSE` message is available on a socket.\n\n\u003ca name=\"class-stun-server-event-binding-error\" /\u003e\n\n* **Event: `bindingError`**\n\nEmitted when the `STUN_BINDING_ERROR_RESPONSE` message is available on a socket.\n\n\u003ca name=\"class-stun-server-event-close\" /\u003e\n\n* **Event: `close`**\n\nEmitted when the server closes.\n\n\u003ca name=\"class-stun-server-event-error\" /\u003e\n\n* **Event: `error`**\n\nEmitted when the server got an invalid message.\n\n\u003ca name=\"class-stun-server-event-listening\" /\u003e\n\n* **Event: `listening`**\n\nThe `'listening'` event is emitted whenever a socket begins listening for messages.\n\n\u003ca name=\"class-stun-attribute\" /\u003e\n\n#### **`class StunAttribute`**\n\nThe `StunAttribute` class is an utility for adding an attributes to the `StunMessage` message.\n\n\u003ca name=\"class-stun-attribute-get-type\" /\u003e\n\n* **get `type`**\n\nReturns the attribute type. See `constants` below.\n\n\u003ca name=\"class-stun-attribute-get-value\" /\u003e\n\n* **get `value`**\n\nReturns the value of the attribute. It depends on the value type of the attribute.\n\n```js\nstunMsg.getAttribute(STUN_ATTR_USERNAME).value        // string\nstunMsg.getAttribute(STUN_ATTR_PRIORITY).value        // number\nstunMsg.getAttribute(STUN_ATTR_MAPPED_ADDRESS).value  // object\n```\n\n\u003ca name=\"constants\" /\u003e\n\n#### **`constants: object`**\n\nThese are the types of STUN messages defined in [RFC5389](https://tools.ietf.org/html/rfc5389):\n\n* `STUN_BINDING_REQUEST`\n* `STUN_BINDING_INDICATION`\n* `STUN_BINDING_RESPONSE`\n* `STUN_BINDING_ERROR_RESPONSE`\n\nThese are the event names for STUN messages above:\n\n* `STUN_EVENT_BINDING_REQUEST`\n* `STUN_EVENT_BINDING_INDICATION`\n* `STUN_EVENT_BINDING_RESPONSE`\n* `STUN_EVENT_BINDING_ERROR_RESPONSE`\n\nThese are the types of STUN messages defined in [RFC5766](https://tools.ietf.org/html/rfc5766):\n\n* `STUN_ALLOCATE_REQUEST`\n* `STUN_ALLOCATE_RESPONSE`\n* `STUN_ALLOCATE_ERROR_RESPONSE`\n* `STUN_REFRESH_REQUEST`\n* `STUN_REFRESH_RESPONSE`\n* `STUN_REFRESH_ERROR_RESPONSE`\n* `STUN_SEND_INDICATION`\n* `STUN_DATA_INDICATION`\n* `STUN_CREATE_PERMISSION_REQUEST`\n* `STUN_CREATE_PERMISSION_RESPONSE`\n* `STUN_CREATE_PERMISSION_ERROR_RESPONSE`\n* `STUN_CHANNEL_BIND_REQUEST`\n* `STUN_CHANNEL_BIND_RESPONSE`\n* `STUN_CHANNEL_BIND_ERROR_RESPONSE`\n\nThsese are all known STUN attributes, defined in [RFC5389](https://tools.ietf.org/html/rfc5389) and elsewhere:\n\n* `STUN_ATTR_MAPPED_ADDRESS`\n* `STUN_ATTR_USERNAME`\n* `STUN_ATTR_MESSAGE_INTEGRITY`\n* `STUN_ATTR_ERROR_CODE`\n* `STUN_ATTR_UNKNOWN_ATTRIBUTES`\n* `STUN_ATTR_REALM`\n* `STUN_ATTR_NONCE`\n* `STUN_ATTR_XOR_MAPPED_ADDRESS`\n* `STUN_ATTR_SOFTWARE`\n* `STUN_ATTR_ALTERNATE_SERVER`\n* `STUN_ATTR_FINGERPRINT`\n* `STUN_ATTR_ORIGIN`\n* `STUN_ATTR_RETRANSMIT_COUNT`\n* `STUN_ATTR_PRIORITY`\n* `STUN_ATTR_USE_CANDIDATE`\n* `STUN_ATTR_ICE_CONTROLLED`\n* `STUN_ATTR_ICE_CONTROLLING`\n* `STUN_ATTR_NOMINATION`\n* `STUN_ATTR_NETWORK_INFO`\n* `STUN_ATTR_CHANNEL_NUMBER`\n* `STUN_ATTR_LIFETIME`\n* `STUN_ATTR_XOR_PEER_ADDRESS`\n* `STUN_ATTR_DATA`\n* `STUN_ATTR_XOR_RELAYED_ADDRESS`\n* `STUN_ATTR_EVEN_PORT`\n* `STUN_ATTR_REQUESTED_TRANSPORT`\n* `STUN_ATTR_DONT_FRAGMENT`\n* `STUN_ATTR_RESERVATION_TOKEN`\n* `STUN_ATTR_CHANGE_REQUEST`\n* `STUN_ATTR_PADDING`\n* `STUN_ATTR_RESPONSE_PORT`\n* `STUN_ATTR_RESPONSE_ORIGIN`\n* `STUN_ATTR_OTHER_ADDRESS`\n\nThese are the types of STUN error codes defined in [RFC5389](https://tools.ietf.org/html/rfc5389) and elsewhere:\n\n* `STUN_CODE_TRY_ALTERNATE`\n* `STUN_CODE_BAD_REQUEST`\n* `STUN_CODE_UNAUTHORIZED`\n* `STUN_CODE_UNKNOWN_ATTRIBUTE`\n* `STUN_CODE_STALE_CREDENTIALS`\n* `STUN_CODE_STALE_NONCE`\n* `STUN_CODE_SERVER_ERROR`\n* `STUN_CODE_GLOBAL_FAILURE`\n* `STUN_CODE_ROLE_CONFLICT`\n* `STUN_CODE_FORBIDDEN`\n* `STUN_CODE_ALLOCATION_MISMATCH`\n* `STUN_CODE_WRONG_CREDENTIALS`\n* `STUN_CODE_UNSUPPORTED_PROTOCOL`\n* `STUN_CODE_ALLOCATION_QUOTA`\n* `STUN_CODE_INSUFFICIENT_CAPACITY`\n\nThese are the strings for the error codes above:\n\n* `STUN_REASON_TRY_ALTERNATE`\n* `STUN_REASON_BAD_REQUEST`\n* `STUN_REASON_UNAUTHORIZED`\n* `STUN_REASON_UNKNOWN_ATTRIBUTE`\n* `STUN_REASON_STALE_CREDENTIALS`\n* `STUN_REASON_STALE_NONCE`\n* `STUN_REASON_SERVER_ERROR`\n* `STUN_REASON_ROLE_CONFLICT`\n* `STUN_REASON_FORBIDDEN`\n* `STUN_REASON_ALLOCATION_MISMATCH`\n* `STUN_REASON_WRONG_CREDENTIALS`\n* `STUN_REASON_UNSUPPORTED_PROTOCOL`\n* `STUN_REASON_ALLOCATION_QUOTA`\n* `STUN_REASON_INSUFFICIENT_CAPACITY`\n\n\u003ca name=\"class-stun-error\" /\u003e\n\n#### **`class StunError`**\n\nBase class for all generated errors.\n\n* **get `packet: Buffer|StunMessage`**\n\nReceived data.\n\n* **get `sender: object`**\n\nFor UDP, this is an `rinfo` attribute.\n\n\u003ca name=\"class-stun-message-error\" /\u003e\n\n#### **`class StunMessageError`**\n\nThe STUN server may receive invalid messages. This error class represent ones. Inherits from `StunError`.\n\n* **get `packet: Buffer`**\n\nSee above.\n\n\u003ca name=\"class-stun-response-error\" /\u003e\n\n#### **`class StunResponseError`**\n\nThis class represent protocol level errors, for messages with class type `ERROR`. Inherits from `StunError`.\n\n* **get `packet: StunMessage`**\n\nSee above.\n\n## License\n\nMIT, 2017-2019 (c) Dmitriy Tsvettsikh\n","funding_links":["https://www.buymeacoffee.com/reklatsmasters"],"categories":["net protocol (网络库)","Stun \u0026 Turn"],"sub_categories":["C/C++"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodertc%2Fstun","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnodertc%2Fstun","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodertc%2Fstun/lists"}