Update server/status.js
Add Fulcrum address data queries
This commit is contained in:
+556
-155
@@ -1,18 +1,19 @@
|
|||||||
const http = require('http');
|
const http = require('http')
|
||||||
const net = require('net');
|
const net = require('net')
|
||||||
|
const crypto = require('crypto')
|
||||||
const {
|
const {
|
||||||
checkPassword,
|
checkPassword,
|
||||||
createSession,
|
createSession,
|
||||||
isValidSession,
|
isValidSession,
|
||||||
} = require('./auth');
|
} = require('./auth')
|
||||||
const {
|
const {
|
||||||
listAddresses,
|
listAddresses,
|
||||||
addAddress,
|
addAddress,
|
||||||
removeAddress,
|
removeAddress,
|
||||||
} = require('./addresses');
|
} = require('./addresses')
|
||||||
|
|
||||||
const port = 8080;
|
const port = 8080
|
||||||
const fulcrumTimeoutMs = 3000;
|
const fulcrumTimeoutMs = 3000
|
||||||
|
|
||||||
function queryFulcrum(fulcrumUrl, method, params = []) {
|
function queryFulcrum(fulcrumUrl, method, params = []) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
@@ -21,21 +22,21 @@ function queryFulcrum(fulcrumUrl, method, params = []) {
|
|||||||
ok: false,
|
ok: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: 'Fulcrum URL is not configured',
|
error: 'Fulcrum URL is not configured',
|
||||||
});
|
})
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsed;
|
let parsed
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parsed = new URL(fulcrumUrl);
|
parsed = new URL(fulcrumUrl)
|
||||||
} catch {
|
} catch {
|
||||||
resolve({
|
resolve({
|
||||||
ok: false,
|
ok: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: 'Invalid Fulcrum URL',
|
error: 'Invalid Fulcrum URL',
|
||||||
});
|
})
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parsed.protocol !== 'tcp:') {
|
if (parsed.protocol !== 'tcp:') {
|
||||||
@@ -43,149 +44,473 @@ function queryFulcrum(fulcrumUrl, method, params = []) {
|
|||||||
ok: false,
|
ok: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: 'Fulcrum URL must use tcp://',
|
error: 'Fulcrum URL must use tcp://',
|
||||||
});
|
})
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const host = parsed.hostname;
|
const host = parsed.hostname
|
||||||
const fulcrumPort = Number(parsed.port);
|
const fulcrumPort = Number(parsed.port)
|
||||||
|
|
||||||
if (!host || !fulcrumPort) {
|
if (!host || !fulcrumPort) {
|
||||||
resolve({
|
resolve({
|
||||||
ok: false,
|
ok: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: 'Fulcrum URL is missing host or port',
|
error: 'Fulcrum URL is missing host or port',
|
||||||
});
|
})
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const socket = net.createConnection({
|
const socket = net.createConnection({
|
||||||
host,
|
host,
|
||||||
port: fulcrumPort,
|
port: fulcrumPort,
|
||||||
});
|
})
|
||||||
|
|
||||||
let response = '';
|
let response = ''
|
||||||
let settled = false;
|
let settled = false
|
||||||
|
|
||||||
const finish = (value) => {
|
const finish = (value) => {
|
||||||
if (settled) return;
|
if (settled) return
|
||||||
settled = true;
|
|
||||||
|
|
||||||
socket.destroy();
|
settled = true
|
||||||
resolve(value);
|
socket.destroy()
|
||||||
};
|
resolve(value)
|
||||||
|
}
|
||||||
|
|
||||||
socket.setTimeout(fulcrumTimeoutMs);
|
socket.setTimeout(fulcrumTimeoutMs)
|
||||||
|
|
||||||
socket.on('connect', () => {
|
socket.on('connect', () => {
|
||||||
console.log(
|
console.log(
|
||||||
`Fulcrum connected for ${method} (${host}:${fulcrumPort})`,
|
`Fulcrum connected for ${method} (${host}:${fulcrumPort})`,
|
||||||
);
|
)
|
||||||
|
|
||||||
const request = JSON.stringify({
|
const request =
|
||||||
|
JSON.stringify({
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
id: 1,
|
id: 1,
|
||||||
method,
|
method,
|
||||||
params,
|
params,
|
||||||
}) + '\n';
|
}) + '\n'
|
||||||
|
|
||||||
console.log(`Sending Fulcrum request: ${request.trim()}`);
|
console.log(`Sending Fulcrum request: ${request.trim()}`)
|
||||||
|
|
||||||
socket.write(request);
|
socket.write(request)
|
||||||
});
|
})
|
||||||
|
|
||||||
socket.on('data', (data) => {
|
socket.on('data', (data) => {
|
||||||
const chunk = data.toString();
|
const chunk = data.toString()
|
||||||
|
|
||||||
console.log(`Fulcrum response data: ${chunk.trim()}`);
|
console.log(`Fulcrum response data: ${chunk.trim()}`)
|
||||||
|
|
||||||
response += chunk;
|
response += chunk
|
||||||
|
|
||||||
const lines = response.split('\n');
|
const lines = response.split('\n')
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (!line.trim()) continue;
|
if (!line.trim()) continue
|
||||||
|
|
||||||
let message;
|
let message
|
||||||
|
|
||||||
try {
|
try {
|
||||||
message = JSON.parse(line);
|
message = JSON.parse(line)
|
||||||
} catch {
|
} catch {
|
||||||
continue;
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.error) {
|
if (message.error) {
|
||||||
console.error(
|
console.error(
|
||||||
`Fulcrum returned an error for ${method}:`,
|
`Fulcrum returned an error for ${method}:`,
|
||||||
JSON.stringify(message.error),
|
JSON.stringify(message.error),
|
||||||
);
|
)
|
||||||
|
|
||||||
finish({
|
finish({
|
||||||
ok: false,
|
ok: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: message.error,
|
error: message.error,
|
||||||
});
|
})
|
||||||
|
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Object.prototype.hasOwnProperty.call(message, 'result')) {
|
if (
|
||||||
|
Object.prototype.hasOwnProperty.call(
|
||||||
|
message,
|
||||||
|
'result',
|
||||||
|
)
|
||||||
|
) {
|
||||||
finish({
|
finish({
|
||||||
ok: true,
|
ok: true,
|
||||||
result: message.result,
|
result: message.result,
|
||||||
error: null,
|
error: null,
|
||||||
});
|
})
|
||||||
|
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
socket.on('timeout', () => {
|
socket.on('timeout', () => {
|
||||||
console.error(
|
console.error(
|
||||||
`Fulcrum request timed out after ${fulcrumTimeoutMs}ms: ${method}`,
|
`Fulcrum request timed out after ${fulcrumTimeoutMs}ms: ${method}`,
|
||||||
);
|
)
|
||||||
|
|
||||||
finish({
|
finish({
|
||||||
ok: false,
|
ok: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: 'Connection timed out',
|
error: 'Connection timed out',
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
socket.on('error', (error) => {
|
socket.on('error', (error) => {
|
||||||
console.error(
|
console.error(
|
||||||
`Fulcrum socket error for ${method}:`,
|
`Fulcrum socket error for ${method}:`,
|
||||||
error.message,
|
error.message,
|
||||||
);
|
)
|
||||||
|
|
||||||
finish({
|
finish({
|
||||||
ok: false,
|
ok: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: error.message,
|
error: error.message,
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
socket.on('close', (hadError) => {
|
socket.on('close', (hadError) => {
|
||||||
console.log(
|
console.log(
|
||||||
`Fulcrum socket closed for ${method}; hadError=${hadError}`,
|
`Fulcrum socket closed for ${method}; hadError=${hadError}`,
|
||||||
);
|
)
|
||||||
|
|
||||||
|
if (!settled) {
|
||||||
finish({
|
finish({
|
||||||
ok: false,
|
ok: false,
|
||||||
result: null,
|
result: null,
|
||||||
error: 'Connection closed before a response was received',
|
error: 'Connection closed before a response was received',
|
||||||
});
|
})
|
||||||
});
|
}
|
||||||
});
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getFulcrumStatus(fulcrumUrl) {
|
const BECH32_CHARSET =
|
||||||
|
'qpzry9x8gf2tvdw0s3jn54khce6mua7l'
|
||||||
|
|
||||||
|
function bech32Polymod(values) {
|
||||||
|
const generators = [
|
||||||
|
0x3b6a57b2,
|
||||||
|
0x26508e6d,
|
||||||
|
0x1ea119fa,
|
||||||
|
0x3d4233dd,
|
||||||
|
0x2a1462b3,
|
||||||
|
]
|
||||||
|
|
||||||
|
let chk = 1
|
||||||
|
|
||||||
|
for (const value of values) {
|
||||||
|
const top = chk >>> 25
|
||||||
|
chk = ((chk & 0x1ffffff) << 5) ^ value
|
||||||
|
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
if ((top >>> i) & 1) {
|
||||||
|
chk ^= generators[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return chk >>> 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function bech32HrpExpand(hrp) {
|
||||||
|
const values = []
|
||||||
|
|
||||||
|
for (const char of hrp) {
|
||||||
|
values.push(char.charCodeAt(0) >> 5)
|
||||||
|
}
|
||||||
|
|
||||||
|
values.push(0)
|
||||||
|
|
||||||
|
for (const char of hrp) {
|
||||||
|
values.push(char.charCodeAt(0) & 31)
|
||||||
|
}
|
||||||
|
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
|
function bech32VerifyChecksum(hrp, data) {
|
||||||
|
const polymod = bech32Polymod([
|
||||||
|
...bech32HrpExpand(hrp),
|
||||||
|
...data,
|
||||||
|
])
|
||||||
|
|
||||||
|
return polymod === 1 || polymod === 0x2bc830a3
|
||||||
|
}
|
||||||
|
|
||||||
|
function bech32Decode(address) {
|
||||||
|
if (typeof address !== 'string') {
|
||||||
|
throw new Error('Bitcoin address must be a string')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (address.length < 8 || address.length > 90) {
|
||||||
|
throw new Error('Invalid Bitcoin address length')
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasLower = address !== address.toUpperCase()
|
||||||
|
const hasUpper = address !== address.toLowerCase()
|
||||||
|
|
||||||
|
if (hasLower && hasUpper) {
|
||||||
|
throw new Error('Bitcoin address must not mix uppercase and lowercase')
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = address.toLowerCase()
|
||||||
|
const separator = normalized.lastIndexOf('1')
|
||||||
|
|
||||||
|
if (separator < 1 || separator + 7 > normalized.length) {
|
||||||
|
throw new Error('Invalid Bech32 separator')
|
||||||
|
}
|
||||||
|
|
||||||
|
const hrp = normalized.slice(0, separator)
|
||||||
|
const dataPart = normalized.slice(separator + 1)
|
||||||
|
|
||||||
|
const data = []
|
||||||
|
|
||||||
|
for (const char of dataPart) {
|
||||||
|
const value = BECH32_CHARSET.indexOf(char)
|
||||||
|
|
||||||
|
if (value === -1) {
|
||||||
|
throw new Error('Invalid Bech32 character')
|
||||||
|
}
|
||||||
|
|
||||||
|
data.push(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bech32VerifyChecksum(hrp, data)) {
|
||||||
|
throw new Error('Invalid Bitcoin address checksum')
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
hrp,
|
||||||
|
data: data.slice(0, -6),
|
||||||
|
spec:
|
||||||
|
bech32Polymod([
|
||||||
|
...bech32HrpExpand(hrp),
|
||||||
|
...data,
|
||||||
|
]) === 1
|
||||||
|
? 'bech32'
|
||||||
|
: 'bech32m',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertBits(data, fromBits, toBits, pad) {
|
||||||
|
let accumulator = 0
|
||||||
|
let bits = 0
|
||||||
|
const result = []
|
||||||
|
const maxValue = (1 << toBits) - 1
|
||||||
|
|
||||||
|
for (const value of data) {
|
||||||
|
if (value < 0 || value >> fromBits !== 0) {
|
||||||
|
throw new Error('Invalid bit conversion input')
|
||||||
|
}
|
||||||
|
|
||||||
|
accumulator =
|
||||||
|
(accumulator << fromBits) | value
|
||||||
|
bits += fromBits
|
||||||
|
|
||||||
|
while (bits >= toBits) {
|
||||||
|
bits -= toBits
|
||||||
|
result.push(
|
||||||
|
(accumulator >> bits) & maxValue,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pad) {
|
||||||
|
if (bits > 0) {
|
||||||
|
result.push(
|
||||||
|
(accumulator << (toBits - bits)) & maxValue,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (bits >= fromBits) {
|
||||||
|
throw new Error('Invalid padding')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
((accumulator << (toBits - bits)) &
|
||||||
|
maxValue) !==
|
||||||
|
0
|
||||||
|
) {
|
||||||
|
throw new Error('Non-zero padding')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
function addressToScriptPubKey(address) {
|
||||||
|
const decoded = bech32Decode(address)
|
||||||
|
|
||||||
|
if (decoded.hrp !== 'bc') {
|
||||||
|
throw new Error(
|
||||||
|
'Only mainnet Bitcoin addresses are supported',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (decoded.data.length < 1) {
|
||||||
|
throw new Error('Invalid SegWit address')
|
||||||
|
}
|
||||||
|
|
||||||
|
const witnessVersion = decoded.data[0]
|
||||||
|
|
||||||
|
if (witnessVersion > 16) {
|
||||||
|
throw new Error('Invalid SegWit witness version')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
witnessVersion === 0 &&
|
||||||
|
decoded.spec !== 'bech32'
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
'Witness version 0 must use Bech32',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
witnessVersion !== 0 &&
|
||||||
|
decoded.spec !== 'bech32m'
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
'Witness version 1+ must use Bech32m',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const program = convertBits(
|
||||||
|
decoded.data.slice(1),
|
||||||
|
5,
|
||||||
|
8,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (program.length < 2 || program.length > 40) {
|
||||||
|
throw new Error('Invalid witness program length')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
witnessVersion === 0 &&
|
||||||
|
program.length !== 20 &&
|
||||||
|
program.length !== 32
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
'Invalid witness version 0 program length',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const versionOpcode =
|
||||||
|
witnessVersion === 0
|
||||||
|
? 0x00
|
||||||
|
: 0x50 + witnessVersion
|
||||||
|
|
||||||
|
const script = Buffer.from([
|
||||||
|
versionOpcode,
|
||||||
|
program.length,
|
||||||
|
...program,
|
||||||
|
])
|
||||||
|
|
||||||
|
return script
|
||||||
|
}
|
||||||
|
|
||||||
|
function scriptPubKeyToScripthash(scriptPubKey) {
|
||||||
|
const hash = crypto
|
||||||
|
.createHash('sha256')
|
||||||
|
.update(scriptPubKey)
|
||||||
|
.digest()
|
||||||
|
|
||||||
|
return Buffer.from(hash)
|
||||||
|
.reverse()
|
||||||
|
.toString('hex')
|
||||||
|
}
|
||||||
|
|
||||||
|
function addressToScripthash(address) {
|
||||||
|
const scriptPubKey =
|
||||||
|
addressToScriptPubKey(address)
|
||||||
|
|
||||||
|
return scriptPubKeyToScripthash(scriptPubKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAddressData(
|
||||||
|
fulcrumUrl,
|
||||||
|
address,
|
||||||
|
) {
|
||||||
|
let scripthash
|
||||||
|
|
||||||
|
try {
|
||||||
|
scripthash = addressToScripthash(address)
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
address,
|
||||||
|
balance: null,
|
||||||
|
utxos: [],
|
||||||
|
history: [],
|
||||||
|
error:
|
||||||
|
error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: 'Invalid Bitcoin address',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const balance = await queryFulcrum(
|
||||||
|
fulcrumUrl,
|
||||||
|
'blockchain.scripthash.get_balance',
|
||||||
|
[scripthash],
|
||||||
|
)
|
||||||
|
|
||||||
|
const utxos = await queryFulcrum(
|
||||||
|
fulcrumUrl,
|
||||||
|
'blockchain.scripthash.listunspent',
|
||||||
|
[scripthash],
|
||||||
|
)
|
||||||
|
|
||||||
|
const history = await queryFulcrum(
|
||||||
|
fulcrumUrl,
|
||||||
|
'blockchain.scripthash.get_history',
|
||||||
|
[scripthash],
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
address,
|
||||||
|
scripthash,
|
||||||
|
balance: balance.ok
|
||||||
|
? balance.result
|
||||||
|
: null,
|
||||||
|
utxos: utxos.ok
|
||||||
|
? utxos.result
|
||||||
|
: [],
|
||||||
|
history: history.ok
|
||||||
|
? history.result
|
||||||
|
: [],
|
||||||
|
error:
|
||||||
|
balance.ok &&
|
||||||
|
utxos.ok &&
|
||||||
|
history.ok
|
||||||
|
? null
|
||||||
|
: {
|
||||||
|
balance: balance.ok
|
||||||
|
? null
|
||||||
|
: balance.error,
|
||||||
|
utxos: utxos.ok
|
||||||
|
? null
|
||||||
|
: utxos.error,
|
||||||
|
history: history.ok
|
||||||
|
? null
|
||||||
|
: history.error,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getFulcrumStatus(
|
||||||
|
fulcrumUrl,
|
||||||
|
) {
|
||||||
const version = await queryFulcrum(
|
const version = await queryFulcrum(
|
||||||
fulcrumUrl,
|
fulcrumUrl,
|
||||||
'server.version',
|
'server.version',
|
||||||
['Munin Bitcoin', '1.4'],
|
['Munin Bitcoin', '1.4'],
|
||||||
);
|
)
|
||||||
|
|
||||||
if (!version.ok) {
|
if (!version.ok) {
|
||||||
return {
|
return {
|
||||||
@@ -195,14 +520,14 @@ async function getFulcrumStatus(fulcrumUrl) {
|
|||||||
serverVersion: null,
|
serverVersion: null,
|
||||||
blockchainHeight: null,
|
blockchainHeight: null,
|
||||||
error: version.error,
|
error: version.error,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const headers = await queryFulcrum(
|
const headers = await queryFulcrum(
|
||||||
fulcrumUrl,
|
fulcrumUrl,
|
||||||
'blockchain.headers.subscribe',
|
'blockchain.headers.subscribe',
|
||||||
[],
|
[],
|
||||||
);
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
configured: Boolean(fulcrumUrl),
|
configured: Boolean(fulcrumUrl),
|
||||||
@@ -214,12 +539,15 @@ async function getFulcrumStatus(fulcrumUrl) {
|
|||||||
? headers.result.height ?? null
|
? headers.result.height ?? null
|
||||||
: null,
|
: null,
|
||||||
error: headers.ok ? null : headers.error,
|
error: headers.ok ? null : headers.error,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getStatus() {
|
async function getStatus() {
|
||||||
const fulcrumUrl = process.env.MUNIN_FULCRUM_URL || null;
|
const fulcrumUrl =
|
||||||
const fulcrum = await getFulcrumStatus(fulcrumUrl);
|
process.env.MUNIN_FULCRUM_URL || null
|
||||||
|
|
||||||
|
const fulcrum =
|
||||||
|
await getFulcrumStatus(fulcrumUrl)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
service: 'running',
|
service: 'running',
|
||||||
@@ -228,11 +556,23 @@ async function getStatus() {
|
|||||||
labels: 0,
|
labels: 0,
|
||||||
transactions: 0,
|
transactions: 0,
|
||||||
lastScan: 'never',
|
lastScan: 'never',
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const server = http.createServer(async (req, res) => {
|
function sendJson(
|
||||||
if (req.method === 'POST' && req.url === '/api/login') {
|
res,
|
||||||
|
statusCode,
|
||||||
|
body,
|
||||||
|
) {
|
||||||
|
res.writeHead(statusCode, {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
})
|
||||||
|
|
||||||
|
res.end(JSON.stringify(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
function readRequestBody(req) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
let body = ''
|
let body = ''
|
||||||
|
|
||||||
req.on('data', (chunk) => {
|
req.on('data', (chunk) => {
|
||||||
@@ -240,14 +580,29 @@ const server = http.createServer(async (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
req.on('end', () => {
|
req.on('end', () => {
|
||||||
|
resolve(body)
|
||||||
|
})
|
||||||
|
|
||||||
|
req.on('error', reject)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const server = http.createServer(
|
||||||
|
async (req, res) => {
|
||||||
|
if (
|
||||||
|
req.method === 'POST' &&
|
||||||
|
req.url === '/api/login'
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
|
const body =
|
||||||
|
await readRequestBody(req)
|
||||||
|
|
||||||
const input = JSON.parse(body)
|
const input = JSON.parse(body)
|
||||||
|
|
||||||
if (!checkPassword(input.password)) {
|
if (!checkPassword(input.password)) {
|
||||||
res.writeHead(401, {
|
sendJson(res, 401, {
|
||||||
'Content-Type': 'application/json',
|
error: 'Invalid password',
|
||||||
})
|
})
|
||||||
res.end(JSON.stringify({ error: 'Invalid password' }))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,70 +610,130 @@ const server = http.createServer(async (req, res) => {
|
|||||||
|
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Set-Cookie': 'munin_session=' + encodeURIComponent(token) +
|
'Set-Cookie':
|
||||||
|
'munin_session=' +
|
||||||
|
encodeURIComponent(token) +
|
||||||
'; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=86400',
|
'; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=86400',
|
||||||
})
|
})
|
||||||
res.end(JSON.stringify({ ok: true }))
|
|
||||||
|
res.end(
|
||||||
|
JSON.stringify({
|
||||||
|
ok: true,
|
||||||
|
}),
|
||||||
|
)
|
||||||
} catch {
|
} catch {
|
||||||
res.writeHead(400, {
|
sendJson(res, 400, {
|
||||||
'Content-Type': 'application/json',
|
error: 'Invalid request',
|
||||||
})
|
})
|
||||||
res.end(JSON.stringify({ error: 'Invalid request' }))
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'GET' && req.url === '/api/addresses') {
|
if (
|
||||||
|
req.method === 'GET' &&
|
||||||
|
req.url === '/api/addresses'
|
||||||
|
) {
|
||||||
if (!isValidSession(req)) {
|
if (!isValidSession(req)) {
|
||||||
res.writeHead(401, {
|
sendJson(res, 401, {
|
||||||
'Content-Type': 'application/json',
|
error: 'Authentication required',
|
||||||
})
|
})
|
||||||
res.end(JSON.stringify({ error: 'Authentication required' }))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const addresses = await listAddresses()
|
const addresses =
|
||||||
|
await listAddresses()
|
||||||
|
|
||||||
res.writeHead(200, {
|
sendJson(res, 200, {
|
||||||
'Content-Type': 'application/json',
|
addresses,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.end(JSON.stringify({ addresses }))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to list addresses:', error)
|
console.error(
|
||||||
|
'Failed to list addresses:',
|
||||||
|
error,
|
||||||
|
)
|
||||||
|
|
||||||
res.writeHead(500, {
|
sendJson(res, 500, {
|
||||||
'Content-Type': 'application/json',
|
error:
|
||||||
|
'Failed to read address watchlist',
|
||||||
})
|
})
|
||||||
|
|
||||||
res.end(JSON.stringify({
|
|
||||||
error: 'Failed to read address watchlist',
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'POST' && req.url === '/api/addresses') {
|
if (
|
||||||
|
req.method === 'GET' &&
|
||||||
|
req.url === '/api/addresses/data'
|
||||||
|
) {
|
||||||
if (!isValidSession(req)) {
|
if (!isValidSession(req)) {
|
||||||
res.writeHead(401, {
|
sendJson(res, 401, {
|
||||||
'Content-Type': 'application/json',
|
error: 'Authentication required',
|
||||||
})
|
})
|
||||||
res.end(JSON.stringify({ error: 'Authentication required' }))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = ''
|
|
||||||
|
|
||||||
req.on('data', (chunk) => {
|
|
||||||
body += chunk
|
|
||||||
})
|
|
||||||
|
|
||||||
req.on('end', async () => {
|
|
||||||
try {
|
try {
|
||||||
|
const fulcrumUrl =
|
||||||
|
process.env.MUNIN_FULCRUM_URL ||
|
||||||
|
null
|
||||||
|
|
||||||
|
if (!fulcrumUrl) {
|
||||||
|
sendJson(res, 503, {
|
||||||
|
error:
|
||||||
|
'Fulcrum URL is not configured',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const addresses =
|
||||||
|
await listAddresses()
|
||||||
|
|
||||||
|
const results = []
|
||||||
|
|
||||||
|
for (const entry of addresses) {
|
||||||
|
results.push(
|
||||||
|
await getAddressData(
|
||||||
|
fulcrumUrl,
|
||||||
|
entry.address,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
sendJson(res, 200, {
|
||||||
|
addresses: results,
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
'Failed to get address data:',
|
||||||
|
error,
|
||||||
|
)
|
||||||
|
|
||||||
|
sendJson(res, 500, {
|
||||||
|
error:
|
||||||
|
'Failed to retrieve address data',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
req.method === 'POST' &&
|
||||||
|
req.url === '/api/addresses'
|
||||||
|
) {
|
||||||
|
if (!isValidSession(req)) {
|
||||||
|
sendJson(res, 401, {
|
||||||
|
error: 'Authentication required',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const body =
|
||||||
|
await readRequestBody(req)
|
||||||
|
|
||||||
const input = JSON.parse(body)
|
const input = JSON.parse(body)
|
||||||
|
|
||||||
const entry = await addAddress(
|
const entry = await addAddress(
|
||||||
@@ -326,97 +741,80 @@ const server = http.createServer(async (req, res) => {
|
|||||||
input.label,
|
input.label,
|
||||||
)
|
)
|
||||||
|
|
||||||
res.writeHead(201, {
|
sendJson(res, 201, entry)
|
||||||
'Content-Type': 'application/json',
|
|
||||||
})
|
|
||||||
|
|
||||||
res.end(JSON.stringify(entry))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error
|
const message =
|
||||||
|
error instanceof Error
|
||||||
? error.message
|
? error.message
|
||||||
: 'Failed to add address'
|
: 'Failed to add address'
|
||||||
|
|
||||||
res.writeHead(400, {
|
sendJson(res, 400, {
|
||||||
'Content-Type': 'application/json',
|
error: message,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.end(JSON.stringify({ error: message }))
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'DELETE' && req.url === '/api/addresses') {
|
if (
|
||||||
|
req.method === 'DELETE' &&
|
||||||
|
req.url === '/api/addresses'
|
||||||
|
) {
|
||||||
if (!isValidSession(req)) {
|
if (!isValidSession(req)) {
|
||||||
res.writeHead(401, {
|
sendJson(res, 401, {
|
||||||
'Content-Type': 'application/json',
|
error: 'Authentication required',
|
||||||
})
|
})
|
||||||
res.end(JSON.stringify({ error: 'Authentication required' }))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = ''
|
|
||||||
|
|
||||||
req.on('data', (chunk) => {
|
|
||||||
body += chunk
|
|
||||||
})
|
|
||||||
|
|
||||||
req.on('end', async () => {
|
|
||||||
try {
|
try {
|
||||||
|
const body =
|
||||||
|
await readRequestBody(req)
|
||||||
|
|
||||||
const input = JSON.parse(body)
|
const input = JSON.parse(body)
|
||||||
|
|
||||||
await removeAddress(input.address)
|
await removeAddress(input.address)
|
||||||
|
|
||||||
res.writeHead(200, {
|
sendJson(res, 200, {
|
||||||
'Content-Type': 'application/json',
|
ok: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.end(JSON.stringify({ ok: true }))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error
|
const message =
|
||||||
|
error instanceof Error
|
||||||
? error.message
|
? error.message
|
||||||
: 'Failed to remove address'
|
: 'Failed to remove address'
|
||||||
|
|
||||||
res.writeHead(400, {
|
sendJson(res, 400, {
|
||||||
'Content-Type': 'application/json',
|
error: message,
|
||||||
})
|
})
|
||||||
|
|
||||||
res.end(JSON.stringify({ error: message }))
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.url === '/api/status') {
|
if (req.url === '/api/status') {
|
||||||
if (!isValidSession(req)) {
|
if (!isValidSession(req)) {
|
||||||
res.writeHead(401, {
|
sendJson(res, 401, {
|
||||||
'Content-Type': 'application/json',
|
error: 'Authentication required',
|
||||||
})
|
})
|
||||||
res.end(JSON.stringify({ error: 'Authentication required' }))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const status = await getStatus()
|
const status = await getStatus()
|
||||||
|
|
||||||
res.writeHead(200, {
|
sendJson(res, 200, status)
|
||||||
'Content-Type': 'application/json',
|
|
||||||
})
|
|
||||||
|
|
||||||
res.end(JSON.stringify(status))
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to get service status:', error)
|
console.error(
|
||||||
|
'Failed to get service status:',
|
||||||
|
error,
|
||||||
|
)
|
||||||
|
|
||||||
res.writeHead(500, {
|
sendJson(res, 500, {
|
||||||
'Content-Type': 'application/json',
|
|
||||||
})
|
|
||||||
|
|
||||||
res.end(JSON.stringify({
|
|
||||||
service: 'running',
|
service: 'running',
|
||||||
error: 'Failed to determine service status',
|
error:
|
||||||
}))
|
'Failed to determine service status',
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -424,8 +822,11 @@ const server = http.createServer(async (req, res) => {
|
|||||||
|
|
||||||
res.writeHead(404)
|
res.writeHead(404)
|
||||||
res.end('Not found')
|
res.end('Not found')
|
||||||
})
|
},
|
||||||
|
)
|
||||||
|
|
||||||
server.listen(port, () => {
|
server.listen(port, () => {
|
||||||
console.log(`Status API listening on port ${port}`);
|
console.log(
|
||||||
});
|
`Status API listening on port ${port}`,
|
||||||
|
)
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user