[英]Understanding TLS arragement for FTP project: ftp-srv in nodejs
我試圖了解如何基於此 API 為我的 FTP 項目(打字稿,nodejs)准確設置 TLS: ftp-srv
那里的文檔非常基礎。 在項目的相關 github 問題之一中,作者指向他的源代碼/測試示例 - 這里
在該示例中是一個部分:
describe('#EXPLICIT', function () {
before(() => {
return server.close()
.then(() => Promise.all([
readFile(`${process.cwd()}/test/cert/server.key`),
readFile(`${process.cwd()}/test/cert/server.crt`),
readFile(`${process.cwd()}/test/cert/server.csr`)
]))
.then(([key, cert, ca]) => startServer({
url: 'ftp://127.0.0.1:8881',
tls: {key, cert, ca}
}))
.then(() => {
return connectClient({
secure: true,
secureOptions: {
rejectUnauthorized: false,
checkServerIdentity: () => undefined
}
});
});
});
從這個例子中我可以讀到程序員可以創建對 3 個加密文件的引用:
- ../server.key
- ../server.crt
- ../server.csr
比,讀取這些文件並將它們的 output 定向到 tls object 例如:
const tls: SecureContextOptions = {
key: <-- readFile('server.key'),
cert: <-- readFile('server.crt'),
ca: <-- readFile('server.csr'),
}
如果這是對的(如果錯了,請糾正我),我需要從 TLS 的角度了解這 3 個文件所代表的內容。
NodeJS 文檔的 TLS 非常清楚 -這里
根據本文檔,我們創建 5(五)個 SSL 加密文件:
file 1: ryans-key.pem <--- by command: openssl genrsa -out ryans-key.pem 2048
file 2: ryans-csr.pem <--- by command: openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem
file 3: ryans-cert.pem <--- by command: openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem
file 4: ca-cert.pem <--- by command: cat ryans-cert.pem > ca-cert.pem
file 5: ryans.pfx <--- by command: openssl pkcs12 -export -in ryans-cert.pem -inkey ryans-key.pem -certfile ca-cert.pem -out ryans.pfx
現在,這五個加密文件 go 中的哪一個到“tls”object 的哪個屬性? 以下是我的假設,並會要求你們中的一些人進行更正:
const tls: SecureContextOptions = {
key: <-- readFile('server.key'), = <-- ryans-key.pem ?
cert: <-- readFile('server.crt'), = <-- ryans-cert.pem (or ca-cert.pem) ?
ca: <-- readFile('server.csr'), = <-- ryans-csr.pem ?
}
如果需要其他步驟,請說明。
此外,與此安排相關,我將五個 SSL 加密文件中的哪一個提供給 FTP 客戶端,因此服務器和客戶端都正確配置為安全的 ZC728A49363C9A93A54AZ7E7F232B 通信?
我假設上面的這種安排,指向“#EXPLICIT”連接的“tls”object 屬性的加密文件對於“#IMPLICIT”連接實際上也是相同的。 請再次糾正我,如果不是。
此配置的最后一部分是選擇初始 URL header。 ftp-srv 文檔提供:
例如:
"ftp://0.0.0.0:21" or
"ftps://0.0.0.0:21"
它讓我對如何以編程方式組織以下選擇感到困惑:
這是我想出的並且正在工作的。
1、了解少。
根據他們的文檔,標准配置(非 TLS)在客戶端和服務器上都可以正常工作。
TLS:
ftp-srv有兩種模式:
TLS - 顯式模式 - 配置:
生成加密文件:見這里
openssl genrsa -out ryans-key.pem 2048
openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem
openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem
cat ryans-cert.pem > ca-cert.pem
在您的 node.js 應用程序中(示例用於打字稿):
import * as path from 'path';
import { promises as fsAsync, readFileSync } from "fs";
import { FtpServerOptions, FtpSrv } from 'ftp-srv';
configureTls(): Promise<SecureContextOptions> {
const CRYPTO_FILES_DIR = // provide your dir where the crypto files are located
const path_key: string = path.join(CRYPTO_FILES_DIR , 'ryans-key.pem');
const path_cert: string = path.join(CRYPTO_FILES_DIR , 'ryans-cert.pem');
const path_ca: string = path.join(CRYPTO_FILES_DIR , 'ca-cert.pem');
const key: string = await fsAsync.readFile(path_key, 'utf8');
const cert: string = await fsAsync.readFile(path_cert, 'utf8');
const ca: string = await fsAsync.readFile(path_ca, 'utf8');
const tls_ConfigOptions: SecureContextOptions = {
key: key,
cert: cert,
ca: ca,
}
return tls_ConfigOptions;
}
/**
* Start the server and listen for requests / connections
*/
async startFtpServer() {
// make this configurable ...
const url: string = 'ftp://127.0.0.1:21'
const ftpSrvCfg: FtpServerOptions = {
url: url,
pasv_url: pasv_url,
greeting: greeting,
...,
tls = await this.configureTls()
}
...
// create the server instance with all its configs
const ftpServer: FtpSrv = new FtpSrv(ftpSrvCfg);
// FTP server is event driven.
// Here we register the events we need or want to use.
this.onUserLogin(ftpServer);
this.onUserDisconnect(ftpServer);
this.onClientError(ftpServer);
// start listening for requests
ftpServer.listen().then( () => {
// your actions when the server starts ...
...
}
/**
* Occurs on each (user's) login attempt
*/
private onUserLogin(ftpServer: FtpSrv) {
ftpServer.on('login',
async ({ connection, username, password }, resolve, reject) => {
....
}
}
}
在客戶端 - FileZilla:
啟動服務器並嘗試連接。 例如: ftp://127.0.0.1:21
應該管用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.