|
5 | 5 |
|
6 | 6 | import { check } from 'express-validator' |
7 | 7 |
|
8 | | -const ipv4 = |
9 | | - '^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$' |
10 | | -const ipv6 = |
11 | | - '^((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*::((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4}))*|((?:[0-9A-Fa-f]{1,4}))((?::[0-9A-Fa-f]{1,4})){7}$' |
12 | | -const fqdn = '^(?=.{1,254}$)((?=[a-z0-9-]{1,63}\\.)(xn--+)?[a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,63}$' |
13 | | - |
14 | 8 | export const proxyValidator = (): any => [ |
15 | | - check('accessInfo') |
16 | | - .not() |
17 | | - .isEmpty() |
18 | | - .withMessage('Server address is required') |
19 | | - .custom((value, { req }) => { |
20 | | - if (accessInfoFormatValidator(value, req)) { |
21 | | - return true |
22 | | - } |
23 | | - return false |
24 | | - }), |
| 9 | + // Validate and normalize infoFormat first so conditional checks can rely on a number |
25 | 10 | check('infoFormat') |
26 | 11 | .not() |
27 | 12 | .isEmpty() |
28 | 13 | .withMessage('Server address format is required') |
| 14 | + .isInt() |
| 15 | + .toInt() |
29 | 16 | .isIn([ |
30 | 17 | 3, |
31 | 18 | 4, |
32 | 19 | 201 |
33 | 20 | ]) |
34 | 21 | .withMessage('Server address format should be either 3(IPV4), 4(IPV6) or 201(FQDN)'), |
| 22 | + |
| 23 | + // accessInfo presence |
| 24 | + check('accessInfo').not().isEmpty().withMessage('Server address is required'), |
| 25 | + |
| 26 | + // accessInfo format based on infoFormat |
| 27 | + check('accessInfo') |
| 28 | + .if((_, { req }) => req.body.infoFormat === 3) |
| 29 | + .isIP(4) |
| 30 | + .withMessage('infoFormat 3 requires IPV4 server address'), |
| 31 | + check('accessInfo') |
| 32 | + .if((_, { req }) => req.body.infoFormat === 4) |
| 33 | + .isIP(6) |
| 34 | + .withMessage('infoFormat 4 requires IPV6 server address'), |
| 35 | + check('accessInfo') |
| 36 | + .if((_, { req }) => req.body.infoFormat === 201) |
| 37 | + .isFQDN({ require_tld: true, allow_underscores: false, allow_numeric_tld: false }) |
| 38 | + .withMessage('infoFormat 201 requires FQDN server address'), |
| 39 | + |
35 | 40 | check('port') |
36 | | - .not() |
37 | | - .isEmpty() |
38 | | - .withMessage('Port is required') |
39 | | - .isInt({ min: 0, max: 65535 }) |
40 | | - .withMessage('Port value should range between 0 and 65535'), |
| 41 | + .exists() |
| 42 | + .isPort() |
| 43 | + .withMessage('Port value should range between 1 and 65535'), |
41 | 44 | check('networkDnsSuffix') |
42 | 45 | .not() |
43 | 46 | .isEmpty() |
44 | 47 | .withMessage('Domain name of the network is required') |
45 | | - .matches('^(?=.{1,192}$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z]{2,})+$') |
| 48 | + .isFQDN({ require_tld: true, allow_underscores: false }) |
46 | 49 | .withMessage('Domain name of the network should contain alphanumeric and hyphens in the middle') |
47 | 50 | .isLength({ max: 192 }) |
48 | 51 | .withMessage('Domain name of the network maximum length is 192') |
49 | 52 | ] |
50 | | - |
51 | | -function accessInfoFormatValidator(value, req): boolean { |
52 | | - if (req.body.infoFormat == null) { |
53 | | - throw new Error('accessInfo is required') |
54 | | - } |
55 | | - if (value != null) { |
56 | | - if (req.body.infoFormat === 3) { |
57 | | - if (!value.match(ipv4)) { |
58 | | - throw new Error('infoFormat 3 requires IPV4 server address') |
59 | | - } |
60 | | - } else if (req.body.infoFormat === 4) { |
61 | | - if (!value.match(ipv6)) { |
62 | | - throw new Error('infoFormat 4 requires IPV6 server address') |
63 | | - } |
64 | | - } else if (req.body.infoFormat === 201) { |
65 | | - if (!value.match(fqdn)) { |
66 | | - throw new Error('infoFormat 201 requires FQDN server address') |
67 | | - } |
68 | | - } |
69 | | - } |
70 | | - return true |
71 | | -} |
0 commit comments