日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

js ftpclient linux server,Node.JS用Socket实现FTP Server服务器和Client客户端

發布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js ftpclient linux server,Node.JS用Socket实现FTP Server服务器和Client客户端 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通信過程

FTP協議其實就是主機和服務通過Socket進行固定格式的通信過程,當某客戶端連接到FTP 服務器后,客戶端發送指令: [參數]

服務會按以下格式返回: [參數或說明]

例如以下是FileZilla FTP客戶端與服務器通信的過程:

"命令"為客戶端通過socket發送的消息,“響應”為服務器端的返回響應: 220-FileZilla Server version 0.9.43 beta

響應: 220-written by Tim Kosse (tim.kosse@filezilla-project.org)

響應: 220 Please visit http://sourceforge.net/projects/filezilla/

命令: AUTH TLS

響應: 502 SSL/TLS authentication not allowed

命令: AUTH SSL

響應: 502 SSL/TLS authentication not allowed

命令: USER newghost

響應: 331 Password required for newghost

命令: PASS **************

響應: 230 Logged on

命令: SYST

響應: 215 UNIX emulated by FileZilla

命令: FEAT

響應: 211-Features:

響應: MDTM

響應: REST STREAM

響應: SIZE

響應: MLST type*;size*;modify*;

響應: MLSD

響應: UTF8

響應: CLNT

響應: MFMT

響應: 211 End

命令: PWD

響應: 257 "/" is current directory.

命令: TYPE I

響應: 200 Type set to I

命令: PASV

響應: 227 Entering Passive Mode (121,42,140,131,14,77)

命令: MLSD

響應: 150 Opening data channel for directory listing of "/"

響應: 226 Successfully transferred "/"

以下是FTP中的命令列表{

"ABOR": "Abort an active file transfer.",

"ACCT": "Account information.",

"ADAT": "Authentication/Security Data",

"ALLO": "Allocate sufficient disk space to receive a file.",

"APPE": "Append.",

"AUTH": "Authentication/Security Mechanism",

"CCC": "Clear Command Channel",

"CDUP": "Change to Parent Directory.",

"CONF": "Confidentiality Protection Command",

"CWD": "Change working directory.",

"DELE": "Delete file.",

"ENC": "Privacy Protected Channel",

"EPRT": "Specifies an extended address and port to which the server should connect.",

"EPSV": "Enter extended passive mode.",

"FEAT": "Get the feature list implemented by the server.",

"HELP": "Returns usage documentation on a command if specified, else a general help document is returned.",

"HOST": "Identify desired virtual host on server, by name.",

"LANG": "Language Negotiation",

"LIST": "Returns information of a file or directory if specified, else information of the current working directory is returned.",

"LPRT": "Specifies a long address and port to which the server should connect.",

"LPSV": "Enter long passive mode.",

"MDTM": "Return the last-modified time of a specified file.",

"MFCT": "Modify the creation time of a file.",

"MFF": "Modify fact (the last modification time, creation time, UNIX group/owner/mode of a file).",

"MFMT": "Modify the last modification time of a file.",

"MIC": "Integrity Protected Command",

"MKD": "Make directory.",

"MLSD": "Lists the contents of a directory if a directory is named.",

"MLST": "Provides data about exactly the object named on its command line, and no others.",

"MODE": "Sets the transfer mode (Stream, Block, or Compressed).",

"NLST": "Returns a list of file names in a specified directory.",

"NOOP": "No operation (dummy packet; used mostly on keepalives).",

"OPTS": "Select options for a feature (for example OPTS UTF8 ON).",

"PASS": "Authentication password.",

"PASV": "Enter passive mode.",

"PBSZ": "Protection Buffer Size",

"PORT": "Specifies an address and port to which the server should connect.",

"PROT": "Data Channel Protection Level.",

"PWD": "Print working directory. Returns the current directory of the host.",

"QUIT": "Disconnect.",

"REIN": "Re initializes the connection.",

"REST": "Restart transfer from the specified point.",

"RETR": "Retrieve a copy of the file",

"RMD": "Remove a directory.",

"RNFR": "Rename from.",

"RNTO": "Rename to.",

"SITE": "Sends site specific commands to remote server (like SITE IDLE 60 or SITE UMASK 002). Inspect SITE HELP output for complete list of supported commands.",

"SIZE": "Return the size of a file.",

"SMNT": "Mount file structure.",

"SPSV": "Use single port passive mode (only one TCP port number for both control connections and passive-mode data connections)",

"STAT": "Returns the current status.",

"STOR": "Accept the data and to store the data as a file at the server site",

"STOU": "Store file uniquely.",

"STRU": "Set file transfer structure.",

"SYST": "Return system type.",

"TYPE": "Sets the transfer mode (ASCII/Binary).",

"USER": "Authentication username.",

"XCUP": "Change to the parent of the current working directory",

"XMKD": "Make a directory",

"XPWD": "Print the current working directory",

"XRCP": "",

"XRMD": "Remove the directory",

"XRSQ": "",

"XSEM": "Send, mail if cannot",

"XSEN": "Send to terminal"

}

以下是FTP中的返回狀態碼列表:var REPLY_CODE = {

"110": "Restart marker reply.",

"120": "Service ready in nn minutes.",

"125": "Data Connection already open; transfer starting.",

"150": "File status okay; about to open data connection.",

"200": "Command okay.",

"202": "Command not implemented, superfluous at this site.",

"211": "System status, or system help reply.",

"212": "Directory status.",

"213": "File status.",

"214": "Help message.",

"215": "NAME system type.",

"220": "Service ready for new user.",

"221": "Service closing control connection.",

"225": "Data connection open; no transfer in progress.",

"226": "Closing data connection.",

"227": "Entering Passive Mode.",

"230": "User logged in, proceed. This status code appears after the client sends the correct password. It indicates that the user has successfully logged on.",

"250": "Requested file action okay, completed.",

"257": "'\"'PATHNAME'\"' created.",

"331": "User name okay, need password.",

"332": "Need account for login.",

"350": "Requested file action pending further information.",

"421": "Error 421 Service not available, closing control connection.\n Error 421 User limit reached\n Error 421 You are not authorized to make the connection\n Error 421 Max connections reached\n Error 421 Max connections exceeded",

"425": "Cannot open data connection.",

"426": "Connection closed; transfer aborted.",

"450": "Requested file action not taken.",

"451": "Requested action aborted: local error in processing.",

"452": "Requested action not taken. Insufficient storage space in system.",

"500": "Syntax error, command unrecognized, command line too long.",

"501": "Syntax error in parameters or arguments.",

"502": "Command not implemented.",

"503": "Bad sequence of commands.",

"504": "Command not implemented for that parameter.",

"530": "User not logged in.",

"532": "Need account for storing files.",

"550": "Requested action not taken. File unavailable, not found, not accessible",

"552": "Requested file action aborted. Exceeded storage allocation.",

"553": "Requested action not taken. File name not allowed.",

"10054": "Connection reset by peer. The connection was forcibly closed by the remote host.",

"10060": "Cannot connect to remote server.",

"10061": "Cannot connect to remote server. The connection is actively refused by the server.",

"10066": "Directory not empty.",

"10068": "Too many users, server is full."

}

一個簡易的FTP服務器

通過以上協議,我們可以編寫一個簡易的實現了登陸過程的FTP服務器,var net = require('net')

var COMMANDS = {

AUTH: function() {

this.send('502 SSL/TLS authentication not allowed.')

},

USER: function(username) {

this.session.username = username

this.send('331 User name okay, need password.')

},

PASS: function(password) {

var socket = this

var username = socket.username

if (username == 'newghost' && password == 'dachun') {

socket.send(230, 'Logged on')

} else {

socket.send(450, 'Ensure that you typed the correct user name and password combination.')

}

},

PWD: function(args) {

this.send('257 "/" is current directory')

},

TYPE: function(args) {

this.send('200 Type set to I')

},

EPSV: function(args) {

this.send('229 Entering Extended Passive Mode (|||30324|).')

},

PASV: function(args) {

this.send('227 Entering Passive Mode (112,124,126,185,165,12).')

},

MLSD: function(args) {

this.send('226 Successfully transferred "/"')

},

LIST: function(args) {

this.send('502 Command not implemented.')

this.send('502')

}

}

var sendHandler = function(type, message) {

var socket = this

var command

if (arguments.length < 2) {

if (REPLY_CODE[type]) {

command = REPLY_CODE[type]

} else {

command = type.toString()

}

} else {

command = type + ' ' + message

}

console.log('S:', command)

socket.write(command + '\r\n')

}

var ftpServer = net.createServer(function(socket) {

socket.session = {}

socket.send = sendHandler

socket.send(220, 'Welcome to OnceDoc FTP Server')

var onCommand = function(buffer) {

//receives.push(data)

//var buffer = Buffer.concat(receives).toString()

//receives = []

var buffers = buffer.toString()

var lines = buffers.split('\r\n')

for (var i = 0, l = lines.length; i < l; i++) {

var line = lines[i]

if (line) {

console.log('C:', line)

//lines.push(raw[i])

var cmds = line.split(' ')

var cmd = cmds[0].toUpperCase()

var arg = cmds.slice(1)

var func = COMMANDS[cmd]

func

? func.apply(socket, arg)

: socket.send(502)

}

}

}

socket

.on('data', onCommand)

.on('end', function() {

console.log('end', arguments)

})

.on('close', function () {

console.log('close', arguments)

})

.on('timeout', function () {

console.log('timeout', arguments)

})

.on('error', function (err) {

console.log('error', arguments)

})

}).on('error', function(err) {

// handle errors here

console.error(err)

})

//

ftpServer.listen({ port: 21 }, function() {

console.log('opened server on', ftpServer.address())

})

FTP 服務器端模塊

目前用Node.JS實現的服務端模塊還不多,大多不是很成熟,如

ftp-srvconst FtpSvr = require('ftp-srv')

const ftpServer = new FtpSvr('ftp://127.0.0.1:21')

ftpServer.on('login', function (data, resolve, reject) {

var connection = data.connection

var username = data.username

var password = data.password

if (data.username == 'anonymous') {

resolve({ root: 'D:\\github\\oncedoc\\onceoa' })

} else {

reject()

}

})

ftpServer

.listen()

.then(() => {

console.log('ready')

})

ftpServervar FTPServer = require('ftpserver').FTPServer

var ftpServer = new FTPServer({

host: '127.0.0.1',

port: 21,

pasvStart: null,

pasvEnd: null,

timeout: 30000,

disabledCommands: [],

anonymous: false,

logLevel: 10,

greeting: null,

override: {

fs: null,

authentication: null

}

})

ftpServer.listen().then(() => {

})

nodeftpd

FTP 客戶端模塊

目前基于node.js的FTP客戶端非常成熟,開源項目也比較多

JSFTPvar JSFtp = require("jsftp");

var Ftp = new JSFtp({

host: "myserver.com",

port: 3331, // defaults to 21

user: "user", // defaults to "anonymous"

pass: "1234" // defaults to "@anonymous"

});

Ftp.raw("mkd", "/new_dir", function(err, data) {

if (err) return console.error(err);

console.log(data.text); // Show the FTP response text to the user

console.log(data.code); // Show the FTP response code to the user

});

node-ftpvar c = new Client();

c.on('ready', function() {

c.list(function(err, list) {

if (err) throw err;

console.dir(list);

c.end();

});

});

// connect to localhost:21 as anonymous

c.connect();

node-ftps

這個項目是對lftp的node.js封裝var FTPS = require('ftps');

var ftps = new FTPS({

host: 'domain.com', // required

username: 'Test', // Optional. Use empty username for anonymous access.

password: 'Test', // Required if username is not empty, except when requiresPassword: false

protocol: 'sftp', // Optional, values : 'ftp', 'sftp', 'ftps', ... default: 'ftp'

// protocol is added on beginning of host, ex : sftp://domain.com in this case

port: 22, // Optional

// port is added to the end of the host, ex: sftp://domain.com:22 in this case

escape: true, // optional, used for escaping shell characters (space, $, etc.), default: true

retries: 2, // Optional, defaults to 1 (1 = no retries, 0 = unlimited retries)

timeout: 10, // Optional, Time before failing a connection attempt. Defaults to 10

retryInterval: 5, // Optional, Time in seconds between attempts. Defaults to 5

retryMultiplier: 1, // Optional, Multiplier by which retryInterval is multiplied each time new attempt fails. Defaults to 1

requiresPassword: true, // Optional, defaults to true

autoConfirm: true, // Optional, is used to auto confirm ssl questions on sftp or fish protocols, defaults to false

cwd: '', // Optional, defaults to the directory from where the script is executed

additionalLftpCommands: '', // Additional commands to pass to lftp, splitted by ';'

requireSSHKey: true, // Optional, defaults to false, This option for SFTP Protocol with ssh key authentication

sshKeyPath: '/home1/phrasee/id_dsa' // Required if requireSSHKey: true , defaults to empty string, This option for SFTP Protocol with ssh key authentication

});

// Do some amazing things

ftps.cd('some_directory').addFile(__dirname + '/test.txt').exec(console.log);

總結

以上是生活随笔為你收集整理的js ftpclient linux server,Node.JS用Socket实现FTP Server服务器和Client客户端的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。