diff --git a/.gitignore b/.gitignore index 4032a9b..192793a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +config + # Logs logs *.log diff --git a/quests/Dockerfile b/quests/Dockerfile index 3ca707b..c3def99 100644 --- a/quests/Dockerfile +++ b/quests/Dockerfile @@ -26,7 +26,7 @@ ENV USERNAME=commander RUN adduser --disabled-password --gecos '' --shell /bin/zsh ${USERNAME} COPY ./quests/sshd_config /etc/ssh/sshd_config -COPY ./ssh_pubkey /home/${USERNAME}/.ssh/authorized_keys +COPY ./config/ssh_key.pub /home/${USERNAME}/.ssh/authorized_keys RUN chmod 600 /home/${USERNAME}/.ssh/authorized_keys \ && chmod 700 /home/${USERNAME}/.ssh \ && chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.ssh diff --git a/server/package.json b/server/package.json index 92e6d08..432f5dd 100644 --- a/server/package.json +++ b/server/package.json @@ -62,6 +62,7 @@ "@types/passport-local": "^1.0.38", "@types/passport-oauth2": "^1.4.15", "@types/source-map-support": "^0", + "@types/ssh2": "^1.15.1", "@types/swagger-ui-express": "^4.1.6", "@types/validator": "^13.11.9", "concurrently": "^8.2.2", diff --git a/server/src/containers/docker.ts b/server/src/containers/docker.ts index 64bda52..09476ce 100644 --- a/server/src/containers/docker.ts +++ b/server/src/containers/docker.ts @@ -1,4 +1,3 @@ -import fs from 'fs' import { Duplex } from 'stream' import { Client } from 'ssh2' import Docker from 'dockerode' @@ -67,6 +66,7 @@ export async function attachContainer( config.docker.network ].IPAddress const conn = new Client() + console.log(config.docker.keypair.privateKey) return new Promise((resolve, reject) => { conn @@ -100,7 +100,7 @@ export async function attachContainer( host: containerIp, port: 22, username: 'commander', - privateKey: fs.readFileSync('/home/wancat/.ssh/id_ed25519'), + privateKey: config.docker.keypair.privateKey, debug: console.log, }) }) diff --git a/server/src/utils/crypto.ts b/server/src/utils/crypto.ts index fbe948c..4f27a03 100644 --- a/server/src/utils/crypto.ts +++ b/server/src/utils/crypto.ts @@ -1,5 +1,9 @@ import fs from 'fs' -import { generateKeyPairSync } from 'crypto' +import ssh2 from 'ssh2' + +const { + utils: { generateKeyPairSync }, +} = ssh2 type KeyPair = { privateKey: string @@ -24,16 +28,7 @@ export function loadOrCreateKeyPair(privateKeyPath: string): KeyPair { } export function generateKeyPair(): KeyPair { - const { privateKey, publicKey } = generateKeyPairSync('rsa', { - modulusLength: 2048, - publicKeyEncoding: { - type: 'spki', - format: 'pem', - }, - privateKeyEncoding: { - type: 'pkcs8', - format: 'pem', - }, - }) + const { private: privateKey, public: publicKey } = + generateKeyPairSync('ed25519') return { privateKey, publicKey } } diff --git a/yarn.lock b/yarn.lock index ff2dab4..78ca72a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2067,6 +2067,13 @@ dependencies: "@types/node" "^18.11.18" +"@types/ssh2@^1.15.1": + version "1.15.1" + resolved "https://registry.yarnpkg.com/@types/ssh2/-/ssh2-1.15.1.tgz#4db4b6864abca09eb299fe5354fa591add412223" + integrity sha512-ZIbEqKAsi5gj35y4P4vkJYly642wIbY6PqoN0xiyQGshKUGXR9WQjF/iF9mXBQ8uBKy3ezfsCkcoHKhd0BzuDA== + dependencies: + "@types/node" "^18.11.18" + "@types/stack-utils@^2.0.0": version "2.0.3" resolved "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz"