From 31989ff18a6d52cfcb5de5634a74a4bba358f641 Mon Sep 17 00:00:00 2001 From: Mai Trung Duc Date: Fri, 18 Sep 2020 10:11:18 +0800 Subject: [PATCH] add auth with privateKey --- .npmignore | 3 ++- README.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- package.json | 2 +- src/index.ts | 5 ++++- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/.npmignore b/.npmignore index 7b5adba..e307af1 100644 --- a/.npmignore +++ b/.npmignore @@ -2,4 +2,5 @@ node_modules src .git* tsconfig.json -tslint.json \ No newline at end of file +tslint.json +test.js \ No newline at end of file diff --git a/README.md b/README.md index 3b19900..64ba45d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,19 @@ And other new features: - Scp a directory from local to remote server and from remote to local - Perform commands on remote server: `mkdir`, `stat`, check if path exists -# Install +# Table of Contents + +* [Installation](#installation) +* [Guide](#guide) + * [Scp file from local to remote server](#scp-file-from-local-to-remote-server) + * [Scp file from remote server to local](#Scp-file-from-remote-server-to-local) + * [Scp a directory from local to remote server](#Scp-a-directory-from-local-to-remote-server) + * [Create a directory on remote server](#Create-a-directory-on-remote-server) + * [Check if a path exists on remote server](#Check-if-a-path-exists-on-remote-server) + * [Get stats of a path on remote server](#Get-stats-of-a-path-on-remote-server) + * [Connection options](#Connection-options) + +# Installation ``` npm install --save node-scp # or @@ -24,6 +36,8 @@ scp({ port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }).then(client => { client.uploadFile('./test.txt', '/workspace/test.txt') .then(response => { @@ -44,6 +58,8 @@ async function test() { port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }) await c.uploadFile('./test.txt', '/workspace/test.txt') c.close() // remember to close connection after you finish @@ -65,6 +81,8 @@ scp({ port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }).then(client => { client.downloadFile('/workspace/test.txt', './test.txt') .then(response => { @@ -85,6 +103,8 @@ async function test () { port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }) await client.downloadFile('/workspace/test.txt', './test.txt') client.close() // remember to close connection after you finish @@ -106,6 +126,8 @@ scp({ port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }).then(client => { client.uploadDir('./local/dir', '/server/path') .then(response => { @@ -126,6 +148,8 @@ async funtion test () { port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }) await client.uploadDir('./local/dir', '/server/path') client.close() // remember to close connection after you finish @@ -147,6 +171,8 @@ scp({ port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }).then(client => { client.mkdir('/server/path') .then(response => { @@ -167,6 +193,8 @@ async function test() { port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }) await client.mkdir('/server/path') client.close() // remember to close connection after you finish @@ -188,6 +216,8 @@ scp({ port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }).then(client => { client.exists('/server/path') .then(response => { @@ -207,6 +237,8 @@ async function test() { port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }) await client.exists('/server/path') client.close() // remember to close connection after you finish @@ -228,6 +260,8 @@ scp({ port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }).then(client => { client.stat('/server/path') .then(response => { @@ -248,6 +282,8 @@ async function test() { port: 22, username: 'username', password: 'password', + // privateKey: fs.readFileSync('./key.pem'), + // passphrase: 'your key passphrase', }) await client.stat('/server/path') client.close() // remember to close connection after you finish @@ -258,6 +294,17 @@ async function test() { test() ``` +## Connection options +Below are available options you can pass when connecting to server: +- **host**: - *string* - Hostname or IP address of the server. **Default**: `localhost` +- **port** - *integer* - Port number of the server. **Default**: `22` +- **forceIPv4** - *boolean* - Only connect via resolved IPv4 address for `host`. **Default**: `false` +- **forceIPv6** - *boolean* - Only connect via resolved IPv6 address for `host`. **Default**: `false` +- **username** - *string* - Username for authentication. **Default**: (none) +- **password** - *string* - Password for password-based user authentication. **Default**: (none) +- **privateKey** - *mixed* - `Buffer` or `string` that contains a private key for either key-based or hostbased user authentication (OpenSSH format). **Default**: (none) +- **passphrase** - *string* - For an encrypted private key, this is the passphrase used to decrypt it. **Default**: (none) +**Default authentication method order**: None -> Password -> Private Key # Support If you like this project, give me 1 ⭐️ \ No newline at end of file diff --git a/package.json b/package.json index 393bc11..2d69062 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "node-scp", "title": "SCP module for NodeJS", "description": "Lightweight, fast and secure SCP function for NodeJS", - "version": "0.0.6", + "version": "0.0.7", "main": "lib/index.js", "types": "lib/index.d.ts", "license": "MIT", diff --git a/src/index.ts b/src/index.ts index 6ac4074..5fa2fa9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,10 @@ export interface IScpOptions { port?: number username?: string password?: string - paths?: string + privateKey?: Buffer | string + passphrase? : string + forceIPv4?: boolean + forceIPv6?: boolean } export class ScpClient extends EventEmitter {