forked from totravel/shadowsocks-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.mjs
178 lines (155 loc) · 3.94 KB
/
util.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import 'colors'
import { env } from 'node:process'
import { debug, info, warn, error } from 'node:console'
import { createHash } from 'node:crypto'
import { createConnection } from 'node:net'
import dohjs from 'dohjs'
const { DohResolver } = dohjs
function fromString(value, type) {
if (type === 'string') {
return value
}
let newValue = null
switch (type) {
case 'number':
newValue = Number(value)
break
case 'boolean':
switch (value.toLowerCase()) {
case 'true':
case 'on':
newValue = true
break
case 'false':
case 'off':
newValue = false
break
}
break
}
return newValue
}
export function readEnv(name, defaultValue, possibleValues = []) {
if (name in env) {
const value = fromString(env[name], typeof defaultValue)
if (value !== null) {
if (possibleValues.length === 0 || possibleValues.includes(value)) {
return value
}
}
}
env[name] = defaultValue
return defaultValue
}
export const NODE_ENV = readEnv('NODE_ENV', 'production', ['production', 'development'])
export const APP_DEBUG = NODE_ENV === 'development'
export const debuglog = APP_DEBUG
? (...args) => debug('DEBUG'.gray, ...args)
: () => null
export const infolog = (...args) => info('INFO '.green, ...args)
export const warnlog = (...args) => warn('WARN '.yellow, ...args)
export const errorlog = (...args) => error('ERROR'.red, ...args)
// https://www.openssl.org/docs/man3.0/man3/EVP_BytesToKey.html
export function EVP_BytesToKey(data, keylen, ivlen = 0) {
let d = '', m = [], l = 0
do {
d = createHash('md5').update(d).update(data).digest()
m.push(d)
l += d.length
} while (l < keylen + ivlen)
m = Buffer.concat(m)
const key = m.slice(0, keylen)
const iv = m.slice(keylen, keylen + ivlen)
return { key, iv }
}
export function connect(port, addr) {
return new Promise((resolve, reject) => {
const socket = createConnection(port, addr)
socket.once('connect', () => {
socket.removeAllListeners('error')
resolve(socket)
})
socket.once('error', (err) => {
const sockerr = new Error(`failed to connect to ${addr} port ${port}: ${err.message}`)
reject(sockerr)
})
})
}
export function inet_ntoa(buf) {
const a = []
for (let i = 0; i < 4; i++) {
a.push(buf.readUInt8(i).toString())
}
return a.join('.')
}
export function inet_aton(addr) {
const buf = Buffer.alloc(4)
const a = addr.split('.')
for (let i = 0; i < 4; i++) {
buf.writeUInt8(parseInt(a[i]), i)
}
return buf
}
export function inet_ntop(buf) {
const a = []
for (let i = 0; i < 16; i += 2) {
a.push(buf.readUInt16BE(i).toString(16))
}
return a.join(':')
}
export function inet_pton(addr) {
let count = 8
const parts = addr.split(':')
const head = []
while (count > 0 && parts.length > 0) {
const hex = parts.shift()
if (hex === '') {
break
}
head.push(hex)
count--
}
const tail = []
while (count > 0 && parts.length > 0) {
const hex = parts.shift()
if (hex === '') {
continue
}
tail.push(hex)
count--
}
const buf = Buffer.alloc(16)
if (tail.length > 0) {
const last = tail.pop()
if (last.split('.').length > 1) {
inet_aton(last).copy(buf, 12)
count -= 2
} else {
tail.push(last)
}
}
let i = 0
for (const hex of head) {
buf.writeUInt16BE(parseInt(hex, 16), i)
i += 2
}
i += count * 2
for (const hex of tail) {
buf.writeUInt16BE(parseInt(hex, 16), i)
i += 2
}
return buf
}
export async function lookup(hostname, nameserver) {
const addresses = []
const response = await new DohResolver(nameserver).query(hostname, 'A', 'GET', {}, 5000)
for (const answer of response.answers) {
if (answer.type === 'A') {
addresses.push(answer.data)
}
}
if (addresses.length === 0) {
throw new Error('no address associated with hostname')
}
return addresses
}