Skip to content

Commit

Permalink
r0b08x [chore] 12/21/2024, 1:55:15 PM
Browse files Browse the repository at this point in the history
  • Loading branch information
p3x-robot committed Dec 21, 2024
1 parent 694543c commit 61d6aea
Show file tree
Hide file tree
Showing 8 changed files with 1,288 additions and 899 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ https://corifeus.com/redis-ui


---
# 🏍️ The p3x-redis-ui-server package motor that is connected to the p3x-redis-ui-material web user interface v2024.10.108
# 🏍️ The p3x-redis-ui-server package motor that is connected to the p3x-redis-ui-material web user interface v2024.10.116



Expand All @@ -26,7 +26,7 @@ https://corifeus.com/redis-ui
### Built on NodeJs version

```txt
v22.4.0
v22.12.0
```


Expand Down Expand Up @@ -122,7 +122,7 @@ All my domains, including [patrikx3.com](https://patrikx3.com) and [corifeus.com
---


[**P3X-REDIS-UI-SERVER**](https://corifeus.com/redis-ui-server) Build v2024.10.108
[**P3X-REDIS-UI-SERVER**](https://corifeus.com/redis-ui-server) Build v2024.10.116

[![NPM](https://img.shields.io/npm/v/p3x-redis-ui-server.svg)](https://www.npmjs.com/package/p3x-redis-ui-server) [![Donate for Corifeus / P3X](https://img.shields.io/badge/Donate-Corifeus-003087.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QZVM4V6HVZJW6) [![Contact Corifeus / P3X](https://img.shields.io/badge/Contact-P3X-ff9900.svg)](https://www.patrikx3.com/en/front/contact) [![Like Corifeus @ Facebook](https://img.shields.io/badge/LIKE-Corifeus-3b5998.svg)](https://www.facebook.com/corifeus.software)

Expand Down
2 changes: 1 addition & 1 deletion artifacts/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ All my domains, including [patrikx3.com](https://patrikx3.com) and [corifeus.com
---


[**P3X-REDIS-UI-SERVER**](https://corifeus.com/redis-ui-server) Build v2024.10.108
[**P3X-REDIS-UI-SERVER**](https://corifeus.com/redis-ui-server) Build v2024.10.116

[![NPM](https://img.shields.io/npm/v/p3x-redis-ui-server.svg)](https://www.npmjs.com/package/p3x-redis-ui-server) [![Donate for Corifeus / P3X](https://img.shields.io/badge/Donate-Corifeus-003087.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QZVM4V6HVZJW6) [![Contact Corifeus / P3X](https://img.shields.io/badge/Contact-P3X-ff9900.svg)](https://www.patrikx3.com/en/front/contact) [![Like Corifeus @ Facebook](https://img.shields.io/badge/LIKE-Corifeus-3b5998.svg)](https://www.facebook.com/corifeus.software)

Expand Down
1,932 changes: 1,163 additions & 769 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "p3x-redis-ui-server",
"version": "2024.10.108",
"version": "2024.10.116",
"description": "🏍️ The p3x-redis-ui-server package motor that is connected to the p3x-redis-ui-material web user interface",
"corifeus": {
"icon": "fas fa-flag-checkered",
"code": "Reverse",
"opencollective": false,
"build": true,
"nodejs": "v22.4.0",
"nodejs": "v22.12.0",
"reponame": "redis-ui-server",
"publish": true,
"prefix": "p3x-",
Expand Down Expand Up @@ -47,8 +47,8 @@
"author": "Patrik Laszlo <[email protected]>",
"license": "MIT",
"devDependencies": {
"corifeus-builder": "^2024.10.105",
"nodemon": "^3.1.4"
"corifeus-builder": "^2024.10.111",
"nodemon": "^3.1.9"
},
"dependencies-saved": {
"koa-body": "^4.0.4",
Expand All @@ -59,13 +59,14 @@
"chalk": "^4.1.2",
"commander": "^12.1.0",
"corifeus-utils": "^2024.10.126",
"ioredis": "^5.4.1",
"get-port": "^7.1.0",
"ioredis": "^5.4.2",
"koa": "^2.15.3",
"koa-send": "^5.0.1",
"koa-static": "^5.0.0",
"lodash": "^4.17.21",
"socket.io": "^4.7.5",
"tunnel-ssh": "^5.1.2"
"node-ssh-no-cpu-features": "^2.0.0",
"socket.io": "^4.8.1"
},
"engines": {
"node": ">=12.13.0"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module.exports = {
boot: require('./boot'),
cli: require('./cli'),
consoleStamp: require('./console-stamp'),
}
}
125 changes: 62 additions & 63 deletions src/service/socket.io/request/connection-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ const sharedIoRedis = require('../shared')
const staticCommands = require('../../../lib/redis-static-commands')

const generateConnectInfo = async (options) => {
const {socket, redis, payload} = options
const { db} = payload
const { socket, redis, payload } = options
const { db } = payload
// console.warn('generateConnectInfo', options.payload)


let databases
//let results
let commands = staticCommands

const probeDatabaseCount = async() => {
const probeDatabaseCount = async () => {
let tryUntilSelectDatabaseIsNotOk = true
let currentDb = 0
let totalDb = 0
let maxDb = 512
await new Promise(resolve => setTimeout(resolve, 1000));

while(tryUntilSelectDatabaseIsNotOk) {
while (tryUntilSelectDatabaseIsNotOk) {
try {
await redis.call('select', currentDb)
//console.info('found correct database index', currentDb)
Expand All @@ -34,7 +34,7 @@ const generateConnectInfo = async (options) => {
tryUntilSelectDatabaseIsNotOk = false
}
currentDb++
} catch(e) {
} catch (e) {
console.error(e);
console.warn('found wrong current db index', currentDb)
tryUntilSelectDatabaseIsNotOk = false
Expand All @@ -44,7 +44,7 @@ const generateConnectInfo = async (options) => {
if (db <= totalDb) {
try {
await redis.call('select', db)
} catch(e) {
} catch (e) {
console.error(e)
}
}
Expand All @@ -59,20 +59,20 @@ const generateConnectInfo = async (options) => {
try {
databases = (await redis.config('get', 'databases'))[1]
console.info(options.payload.connection.name, 'instance successfully works the database listing')
} catch(e) {
} catch (e) {
console.warn(options.payload.connection.name, 'instance get databases listing is disabled', e)
databases = await probeDatabaseCount()
}
}

console.info(options.payload.connection.name, 'databases got', databases)

try {
//commands = await redis.call('command2')
commands = await redis.command()
console.info(options.payload.connection.name, 'instance command listing is available') // , JSON.stringify(commands))
} catch(e) {
console.warn(options,payload.connection.name, 'instance command listing is not available, not all redis instances are not available command listing', e)
} catch (e) {
console.warn(options, payload.connection.name, 'instance command listing is not available, not all redis instances are not available command listing', e)
}

//console.log(JSON.stringify(commands))
Expand All @@ -95,9 +95,9 @@ const generateConnectInfo = async (options) => {
}

module.exports = async (options) => {
const {socket, payload} = options;
const { socket, payload } = options;

const {connection, db} = payload
const { connection, db } = payload


try {
Expand Down Expand Up @@ -172,9 +172,9 @@ module.exports = async (options) => {
}
*/


if (redisConfig.tlsWithoutCert) {
redisConfig.tls = {
redisConfig.tls = {
}
} else if (typeof redisConfig.tlsCa === 'string' && redisConfig.tlsCa.trim() !== '') {
redisConfig.tls = {
Expand All @@ -185,15 +185,15 @@ module.exports = async (options) => {
}
}
if (redisConfig.hasOwnProperty('tls')) {
redisConfig.tls.rejectUnauthorized = redisConfig.tlsRejectUnauthorized === undefined ? false : redisConfig.tlsRejectUnauthorized
redisConfig.tls.rejectUnauthorized = redisConfig.tlsRejectUnauthorized === undefined ? false : redisConfig.tlsRejectUnauthorized
}

if (redisConfig.hasOwnProperty('sentinel') && redisConfig.sentinel === true) {
redisConfig = [redisConfig].concat(actualConnection.nodes)
} else if (redisConfig.cluster === true) {
redisConfig = [redisConfig].concat(actualConnection.nodes)
}

if (Array.isArray(redisConfig) && redisConfig[0].hasOwnProperty('sentinel') && redisConfig[0].sentinel === true) {
redisConfig = {
sentinels: redisConfig,
Expand All @@ -213,62 +213,61 @@ module.exports = async (options) => {
socket.p3xrs.ioredis = undefined
socket.p3xrs.ioredisSubscriber = undefined
}

if (!Array.isArray(redisConfig)) {
if (redisConfig.ssh === true) {

const tunnelOptions = {
autoClose: true
}

const { NodeSSH } = require('node-ssh-no-cpu-features');
const getPort = require('get-port');

const ssh = new NodeSSH();

const sshOptions = {
host: redisConfig.sshHost,
port: redisConfig.sshPort,
port: parseInt(redisConfig.sshPort, 10),
username: redisConfig.sshUsername,
...(redisConfig.sshPrivateKey
? { privateKey: redisConfig.sshPrivateKey }
: { password: redisConfig.sshPassword }),
};
if (redisConfig.sshPrivateKey) {
sshOptions.privateKey = redisConfig.sshPrivateKey
} else {
sshOptions.password = redisConfig.sshPassword
}

const serverOptions = null

const forwardOptions = {
dstAddr: redisConfig.host,
dstPort: redisConfig.port,
}

const { createTunnel } = require('tunnel-ssh')

let [server, client] = await createTunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions);


socket.p3xrs.tunnel = server
socket.p3xrs.tunnelClient = client

redisConfig.port = server.address().port

server.on('error', async(e)=>{
console.error('ssh server error', e);
//socket.p3xrs.tunnelClient.close()
closeRedis()
socket.emit(options.responseEvent, {
status: 'error',
error: e.message
})
});

client.on('error', async(e)=>{
console.error('ssh client error', e);
//socket.p3xrs.tunnelClient.close()
closeRedis()

try {
console.log('Connecting to SSH server...');
await ssh.connect(sshOptions);
console.log('SSH connection established.');

// Dynamically get an available local port
const localPort = await getPort.default();
console.log(`Dynamic local port allocated: ${localPort}`);

// Manually set up port forwarding using the exec command
const forwardCommand = `ssh -L 127.0.0.1:${localPort}:${redisConfig.host}:${redisConfig.port} -N`;
const result = await ssh.execCommand(forwardCommand, { execOptions: { pty: true } });

if (result.stderr) {
throw new Error(`Failed to forward port: ${result.stderr}`);
}

console.log('Port forwarding set up successfully.');

// Update redisConfig with the dynamically assigned port
//redisConfig.port = localPort;

// Assign the SSH connection as tunnel and client
socket.p3xrs.tunnel = ssh;
socket.p3xrs.tunnelClient = ssh;

console.log(`Tunnel created on local port: ${localPort}`);
} catch (error) {
console.error('Failed to set up SSH tunnel:', error);
socket.emit(options.responseEvent, {
status: 'error',
error: e.message
})
});
error: error.message,
});
}

}
}
}
let redis = new Redis(redisConfig)
//console.warn('redis connection', redisConfig)
let redisSubscriber = new Redis(redisConfig)
Expand Down
Loading

0 comments on commit 61d6aea

Please sign in to comment.