Skip to content

Commit

Permalink
Update github repo package
Browse files Browse the repository at this point in the history
  • Loading branch information
underpostnet committed Nov 9, 2024
1 parent 0a18a48 commit 7ee13fc
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 20 deletions.
89 changes: 89 additions & 0 deletions bin/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,20 +703,94 @@ ${uniqueArray(logs.all.map((log) => `- ${log.author_name} ([${log.author_email}]

break;
}
case 'ssh-keys': {
// create ssh keys
const sshAccount = process.argv[3]; // [sudo username]@[host/ip]
const destPath = process.argv[4];
// shellExec(`ssh-keygen -t ed25519 -C "${sshAccount}" -f ${destPath}`);
if (fs.existsSync(destPath)) {
fs.removeSync(destPath);
fs.removeSync(destPath + '.pub');
}
shellExec(`ssh-keygen -t rsa -b 4096 -C "${sshAccount}" -f ${destPath}`);
// add host to keyscan
shellExec(`ssh-keyscan -t rsa ${sshAccount.split(`@`)[1]} >> ~/.ssh/known_hosts`);
break;
}

case 'set-ssh-keys': {
const files = ['authorized_keys', 'id_rsa', 'id_rsa.pub', 'known_hosts ', 'known_hosts.old'];

// > write
// >> append

// /root/.ssh/id_rsa
// /root/.ssh/id_rsa.pub
if (process.argv.includes('clean')) {
for (const file of files) {
if (fs.existsSync(`/root/.ssh/${file}`)) {
logger.info('remove', `/root/.ssh/${file}`);
fs.removeSync(`/root/.ssh/${file}`);
}
}
shellExec('eval `ssh-agent -s`' + ` && ssh-add -D`);
}

const destPath = process.argv[3];
const sshAuthKeyTarget = '/root/.ssh/authorized_keys';
if (!fs.existsSync(sshAuthKeyTarget)) shellExec(`touch ${sshAuthKeyTarget}`);
shellExec(`cat ${destPath}.pub >> ${sshAuthKeyTarget}`);

if (!fs.existsSync('/root/.ssh/id_rsa')) shellExec(`touch ${'/root/.ssh/id_rsa'}`);
shellExec(`cat ${destPath} > ${'/root/.ssh/id_rsa'}`);

if (!fs.existsSync('/root/.ssh/id_rsa.pub')) shellExec(`touch ${'/root/.ssh/id_rsa.pub'}`);
shellExec(`cat ${destPath}.pub > ${'/root/.ssh/id_rsa.pub'}`);

shellExec(`chmod 700 /root/.ssh/`);
for (const file of files) {
shellExec(`chmod 600 /root/.ssh/${file}`);
}

// add key
shellExec('eval `ssh-agent -s`' + ' && ssh-add /root/.ssh/id_rsa' + ' && ssh-add -l');
shellExec(`sudo systemctl enable ssh`);
shellExec(`sudo systemctl restart ssh`);
shellExec(`sudo systemctl status ssh`);

break;
}

case 'ssh': {
if (!process.argv.includes('server')) {
shellExec(`sudo apt update`);
shellExec(`sudo apt install openssh-server -y`);
shellExec(`sudo apt install ssh-askpass`);
}
shellExec(`sudo systemctl enable ssh`);
shellExec(`sudo systemctl restart ssh`);
shellExec(`sudo systemctl status ssh`);
// sudo service ssh restart
shellExec(`ip a`);

// adduser newuser
// usermod -aG sudo newuser

// ssh -i '/path/to/keyfile' username@server

// ssh-keygen -t ed25519 -C "your_email@example.com" -f $HOME/.ssh/id_rsa

// legacy: ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f $HOME/.ssh/id_rsa

// vi .ssh/authorized_keys
// chmod 700 .ssh
// chmod 600 authorized_keys

// cat id_rsa.pub > .ssh/authorized_keys

// add public key to authorized keys
// cat .ssh/id_ed25519.pub | ssh [sudo username]@[host/ip] 'cat >> .ssh/authorized_keys'

// 2. Open /etc/ssh/sshd_config file
// nano /etc/ssh/sshd_config

Expand All @@ -727,6 +801,21 @@ ${uniqueArray(logs.all.map((log) => `- ${log.author_name} ([${log.author_email}]
// ssh [sudo username]@[host/ip]
// open port 22

// init ssh agent service
// eval `ssh-agent -s`

// list keys
// ssh-add -l

// add key
// ssh-add /root/.ssh/id_rsa

// remove
// ssh-add -d /path/to/private/key

// remove all
// ssh-add -D

break;
}

Expand Down
1 change: 1 addition & 0 deletions bin/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ try {
for (const deletePath of [
'.github/workflows/coverall.yml',
'.github/workflows/docker-image.yml',
'.github/workflows/deploy.yml',
'bin/web3.js',
'src/ipfs.js',
'src/k8s.js',
Expand Down
12 changes: 12 additions & 0 deletions bin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import merge from 'deepmerge';
import si from 'systeminformation';
import * as dir from 'path';
import { svg } from 'font-awesome-assets';
import axios from 'axios';
import https from 'https';

import { loggerFactory } from '../src/server/logger.js';
import { shellCd, shellExec } from '../src/server/process.js';
Expand All @@ -12,6 +14,11 @@ import { Config } from '../src/server/conf.js';
import { FileFactory } from '../src/api/file/file.service.js';
import { buildTextImg, faBase64Png, getBufferPngText } from '../src/server/client-icons.js';

const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
axios.defaults.httpsAgent = httpsAgent;

const logger = loggerFactory(import.meta);

logger.info('argv', process.argv);
Expand Down Expand Up @@ -174,6 +181,11 @@ try {
fs.writeFileSync('b64-image', `data:image/jpg;base64,${fs.readFileSync(process.argv[3]).toString('base64')}`);
break;

case 'get-ip': {
const response = await axios.get(process.argv[3]);
logger.info(process.argv[3] + ' IP', response.request.socket.remoteAddress);
}

default:
break;
}
Expand Down
8 changes: 4 additions & 4 deletions src/client/components/core/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ const Modal = {
s(`.html-${idModal}`).style.display = 'block';
// s(`.title-modal-${idModal}`).style.display = 'block';
setTimeout(() => {
s(`.main-body-btn-ui-menu-menu`).classList.add('hide');
s(`.main-body-btn-ui-menu-close`).classList.remove('hide');
if (s(`.btn-bar-center-icon-menu`)) {
s(`.btn-bar-center-icon-close`).classList.remove('hide');
s(`.btn-bar-center-icon-menu`).classList.add('hide');
Expand All @@ -213,6 +215,8 @@ const Modal = {
s(`.${idModal}`).style.width = `${this.Data[idModal][options.mode].width}px`;
s(`.html-${idModal}`).style.display = 'none';
setTimeout(() => {
s(`.main-body-btn-ui-menu-close`).classList.add('hide');
s(`.main-body-btn-ui-menu-menu`).classList.remove('hide');
if (s(`.btn-bar-center-icon-menu`)) {
s(`.btn-bar-center-icon-menu`).classList.remove('hide');
s(`.btn-bar-center-icon-close`).classList.add('hide');
Expand Down Expand Up @@ -1739,13 +1743,9 @@ const Modal = {
actionBtnCenter: function () {
// if (!s(`.btn-close-modal-menu`).classList.contains('hide')) return s(`.main-btn-home`).click();
if (!s(`.btn-close-modal-menu`).classList.contains('hide')) {
s(`.main-body-btn-ui-menu-close`).classList.add('hide');
s(`.main-body-btn-ui-menu-menu`).classList.remove('hide');
return s(`.btn-close-modal-menu`).click();
}
if (!s(`.btn-menu-modal-menu`).classList.contains('hide')) {
s(`.main-body-btn-ui-menu-menu`).classList.add('hide');
s(`.main-body-btn-ui-menu-close`).classList.remove('hide');
return s(`.btn-menu-modal-menu`).click();
}
},
Expand Down
6 changes: 4 additions & 2 deletions src/client/components/core/RichText.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ const RichText = {
s(`.${options.parentIdModal}`).style.top = '0px';
s(`.${options.parentIdModal}`).style.height = `${window.innerHeight}px`;
}
Modal.cleanUI();
// Modal.cleanUI();
if (s(`.slide-menu-top-bar`)) s(`.slide-menu-top-bar`).classList.add('hide');
} else {
if (options.parentIdModal) {
s(`.btn-bar-modal-container-${options.parentIdModal}`).classList.remove('hide');
s(`.${options.parentIdModal}`).style.top = top;
s(`.${options.parentIdModal}`).style.height = height;
}
Modal.restoreUI();
if (s(`.slide-menu-top-bar`)) s(`.slide-menu-top-bar`).classList.add('remove');
// Modal.restoreUI();
}
},
});
Expand Down
6 changes: 6 additions & 0 deletions src/server/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ const BackUpManagement = {
` && git pull ${repoUrl}` +
` && git add . && git commit -m "backup ${new Date().toLocaleDateString()}"` +
` && git push ${repoUrl}`,
{
disableLog: true,
},
);
}
}
Expand All @@ -102,6 +105,9 @@ const BackUpManagement = {
` && git pull ${BackUpManagement.repoUrl}` +
` && git add . && git commit -m "backup ${new Date().toLocaleDateString()}"` +
` && git push ${BackUpManagement.repoUrl}`,
{
disableLog: true,
},
);
};
await Callback();
Expand Down
6 changes: 4 additions & 2 deletions src/server/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,10 @@ const Cmd = {
conf: (deployId, env) => `node bin/deploy conf ${deployId} ${env ? env : 'production'}`,
replica: (deployId, host, path) => `node bin/deploy build-single-replica ${deployId} ${host} ${path}`,
syncPorts: (deployGroupId) => `node bin/deploy sync-env-port ${deployGroupId}`,
cron: (deployId, job, expression) =>
`pm2 delete ${deployId}-${job} && env-cmd -f .env.production pm2 start bin/cron.js --no-autorestart --instances 1 --cron "${expression}" --name ${deployId}-${job} -- ${job} ${deployId}`,
cron: (deployId, job, expression) => {
shellExec(Cmd.delete(`${deployId}-${job}`));
return `env-cmd -f .env.production pm2 start bin/cron.js --no-autorestart --instances 1 --cron "${expression}" --name ${deployId}-${job} -- ${job} ${deployId}`;
},
};

const fixDependencies = async () => {
Expand Down
39 changes: 27 additions & 12 deletions src/server/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,29 @@ const Dns = {
}
if (testIp && typeof testIp === 'string' && isIPv4(testIp) && Dns.ip !== testIp) {
logger.info(`New ip`, testIp);
Dns.ip = testIp;
confCronData.ipDaemon.ip = Dns.ip;
fs.writeFileSync(confCronPath, JSON.stringify(confCronData, null, 4), 'utf8');
for (const recordType of Object.keys(confCronData.records)) {
switch (recordType) {
case 'A':
for (const dnsProvider of confCronData.records[recordType]) {
if (typeof Dns.services.updateIp[dnsProvider.dns] === 'function')
await Dns.services.updateIp[dnsProvider.dns](dnsProvider);
await Dns.services.updateIp[dnsProvider.dns]({ ...dnsProvider, ip: testIp });
}
break;

default:
break;
}
}
shellExec(
`cd ./engine-private` +
` && git pull ${Dns.repoUrl}` +
` && git add . && git commit -m "update ip ${new Date().toLocaleDateString()}"` +
` && git push ${Dns.repoUrl}`,
);
try {
const response = await axios.get(process.env.DNS_TEST_URL);
const verifyIp = response.request.socket.remoteAddress;
logger.info(process.env.DNS_TEST_URL + ' IP', verifyIp);
if (verifyIp === testIp) {
await this.saveIp(confCronPath, confCronData, testIp);
} else logger.error('ip not updated');
} catch (error) {
logger.error(error), 'ip not updated';
}
}
};
await callback();
Expand All @@ -78,8 +79,8 @@ const Dns = {
services: {
updateIp: {
dondominio: (options) => {
const { user, api_key, host, dns } = options;
const url = `https://dondns.dondominio.com/json/?user=${user}&password=${api_key}&host=${host}&ip=${Dns.ip}`;
const { user, api_key, host, dns, ip } = options;
const url = `https://dondns.dondominio.com/json/?user=${user}&password=${api_key}&host=${host}&ip=${ip}`;
logger.info(`${dns} update ip url`, url);
if (process.env.NODE_ENV !== 'production') return false;
return new Promise((resolve) => {
Expand All @@ -97,6 +98,20 @@ const Dns = {
},
},
},
saveIp: async (confCronPath, confCronData, ip) => {
Dns.ip = ip;
confCronData.ipDaemon.ip = ip;
fs.writeFileSync(confCronPath, JSON.stringify(confCronData, null, 4), 'utf8');
shellExec(
`cd ./engine-private` +
` && git pull ${Dns.repoUrl}` +
` && git add . && git commit -m "update ip ${new Date().toLocaleDateString()}"` +
` && git push ${Dns.repoUrl}`,
{
disableLog: true,
},
);
},
};

export { Dns };

0 comments on commit 7ee13fc

Please sign in to comment.