mirror of
https://github.com/status-im/react-native-udp.git
synced 2025-01-12 10:04:21 +00:00
validate hostname in addition to ip address
This commit is contained in:
parent
96728a771d
commit
97e9d137b5
@ -21,6 +21,8 @@ var {
|
|||||||
var Sockets = NativeModules.UdpSockets
|
var Sockets = NativeModules.UdpSockets
|
||||||
var base64 = require('base64-js')
|
var base64 = require('base64-js')
|
||||||
var ipRegex = require('ip-regex')
|
var ipRegex = require('ip-regex')
|
||||||
|
// RFC 952 hostname format
|
||||||
|
var hostnameRegex = /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/;
|
||||||
var noop = function () {}
|
var noop = function () {}
|
||||||
var instances = 0
|
var instances = 0
|
||||||
var STATE = {
|
var STATE = {
|
||||||
@ -162,7 +164,7 @@ UdpSocket.prototype.send = function(buffer, offset, length, port, address, callb
|
|||||||
var self = this
|
var self = this
|
||||||
|
|
||||||
if (typeof port !== 'number') throw new Error('invalid port')
|
if (typeof port !== 'number') throw new Error('invalid port')
|
||||||
if (!isValidIP(address, this._ipRegex)) throw new Error('invalid address')
|
if (!isValidIpOrHostname(address, this._ipRegex)) throw new Error('invalid address')
|
||||||
|
|
||||||
if (offset !== 0) throw new Error('Non-zero offset not supported yet')
|
if (offset !== 0) throw new Error('Non-zero offset not supported yet')
|
||||||
|
|
||||||
@ -269,10 +271,10 @@ UdpSocket.prototype.unref = function() {
|
|||||||
// anything?
|
// anything?
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidIP (address, ipRegex) {
|
function isValidIpOrHostname (address, ipRegex) {
|
||||||
if (typeof address !== 'string') return false
|
if (typeof address !== 'string') return false
|
||||||
|
|
||||||
return ipRegex.test(address)
|
return ipRegex.test(address) || hostnameRegex.test(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeError (err) {
|
function normalizeError (err) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user