root / HServer / 00.Server / 00.Program / node_modules / ftp-srv / README.md
이력 | 보기 | 이력해설 | 다운로드 (8.03 KB)
1 | 39 | HKM | <p align="center"> |
---|---|---|---|
2 | <a href="https://github.com/trs/ftp-srv"> |
||
3 | <img alt="ftp-srv" src="logo.png" width="600px" /> |
||
4 | </a> |
||
5 | </p> |
||
6 | |||
7 | |||
8 | <p align="center"> |
||
9 | Modern, extensible FTP Server |
||
10 | </p> |
||
11 | |||
12 | <p align="center"> |
||
13 | <a href="https://www.npmjs.com/package/ftp-srv"> |
||
14 | <img alt="npm" src="https://img.shields.io/npm/dm/ftp-srv.svg?style=for-the-badge" /> |
||
15 | </a> |
||
16 | |||
17 | <a href="https://circleci.com/gh/trs/ftp-srv"> |
||
18 | <img alt="npm" src="https://img.shields.io/circleci/project/github/trs/ftp-srv.svg?style=for-the-badge" /> |
||
19 | </a> |
||
20 | |||
21 | <a href="https://coveralls.io/github/trs/ftp-srv?branch=master"> |
||
22 | <img alt="npm" src="https://img.shields.io/coveralls/github/trs/ftp-srv.svg?style=for-the-badge" /> |
||
23 | </a> |
||
24 | </p> |
||
25 | |||
26 | --- |
||
27 | |||
28 | - [Overview](#overview) |
||
29 | - [Features](#features) |
||
30 | - [Install](#install) |
||
31 | - [Usage](#usage) |
||
32 | - [API](#api) |
||
33 | - [Events](#events) |
||
34 | - [Supported Commands](#supported-commands) |
||
35 | - [File System](#file-system) |
||
36 | - [Contributing](#contributing) |
||
37 | - [License](#license) |
||
38 | |||
39 | ## Overview |
||
40 | `ftp-srv` is a modern and extensible FTP server designed to be simple yet configurable. |
||
41 | |||
42 | ## Features |
||
43 | - Extensible [file systems](#file-system) per connection |
||
44 | - Passive and active transfers |
||
45 | - [Explicit](https://en.wikipedia.org/wiki/FTPS#Explicit) & [Implicit](https://en.wikipedia.org/wiki/FTPS#Implicit) TLS connections |
||
46 | - Promise based API |
||
47 | |||
48 | ## Install |
||
49 | `npm install ftp-srv --save` |
||
50 | |||
51 | ## Usage |
||
52 | |||
53 | ```js |
||
54 | // Quick start |
||
55 | |||
56 | const FtpSrv = require('ftp-srv'); |
||
57 | const ftpServer = new FtpSrv('ftp://0.0.0.0:9876', { options ... }); |
||
58 | |||
59 | ftpServer.on('login', (data, resolve, reject) => { ... }); |
||
60 | ... |
||
61 | |||
62 | ftpServer.listen() |
||
63 | .then(() => { ... }); |
||
64 | ``` |
||
65 | |||
66 | ## API |
||
67 | |||
68 | ### `new FtpSrv(url, [{options}])` |
||
69 | #### url |
||
70 | [URL string](https://nodejs.org/api/url.html#url_url_strings_and_url_objects) indicating the protocol, hostname, and port to listen on for connections. |
||
71 | Supported protocols: |
||
72 | - `ftp` Plain FTP |
||
73 | - `ftps` Implicit FTP over TLS |
||
74 | |||
75 | _Note:_ The hostname must be the external IP address to accept external connections. Setting the hostname to `0.0.0.0` will automatically set the external IP. |
||
76 | __Default:__ `"ftp://127.0.0.1:21"` |
||
77 | |||
78 | #### options |
||
79 | |||
80 | ##### `pasv_range` |
||
81 | A starting port (eg `8000`) or a range (eg `"8000-9000"`) to accept passive connections. |
||
82 | This range is then queried for an available port to use when required. |
||
83 | __Default:__ `22` |
||
84 | |||
85 | ##### `greeting` |
||
86 | A human readable array of lines or string to send when a client connects. |
||
87 | __Default:__ `null` |
||
88 | |||
89 | ##### `tls` |
||
90 | Node [TLS secure context object](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options) used for implicit (`ftps` protocol) or explicit (`AUTH TLS`) connections. |
||
91 | __Default:__ `false` |
||
92 | |||
93 | ##### `anonymous` |
||
94 | If true, will allow clients to authenticate using the username `anonymous`, not requiring a password from the user. |
||
95 | Can also set as a string which allows users to authenticate using the username provided. |
||
96 | The `login` event is then sent with the provided username and `@anonymous` as the password. |
||
97 | __Default:__ `false` |
||
98 | |||
99 | ##### `blacklist` |
||
100 | Array of commands that are not allowed. |
||
101 | Response code `502` is sent to clients sending one of these commands. |
||
102 | __Example:__ `['RMD', 'RNFR', 'RNTO']` will not allow users to delete directories or rename any files. |
||
103 | __Default:__ `[]` |
||
104 | |||
105 | ##### `whitelist` |
||
106 | Array of commands that are only allowed. |
||
107 | Response code `502` is sent to clients sending any other command. |
||
108 | __Default:__ `[]` |
||
109 | |||
110 | ##### `file_format` |
||
111 | Sets the format to use for file stat queries such as `LIST`. |
||
112 | __Default:__ `"ls"` |
||
113 | __Allowable values:__ |
||
114 | - `ls` [bin/ls format](https://cr.yp.to/ftp/list/binls.html) |
||
115 | - `ep` [Easily Parsed LIST format](https://cr.yp.to/ftp/list/eplf.html) |
||
116 | - `function () {}` A custom function returning a format or promise for one. |
||
117 | - Only one argument is passed in: a node [file stat](https://nodejs.org/api/fs.html#fs_class_fs_stats) object with additional file `name` parameter |
||
118 | |||
119 | ##### `log` |
||
120 | A [bunyan logger](https://github.com/trentm/node-bunyan) instance. Created by default. |
||
121 | |||
122 | ## Events |
||
123 | |||
124 | The `FtpSrv` class extends the [node net.Server](https://nodejs.org/api/net.html#net_class_net_server). Some custom events can be resolved or rejected, such as `login`. |
||
125 | |||
126 | ### `login` |
||
127 | ```js |
||
128 | on('login', ({connection, username, password}, resolve, reject) => { ... }); |
||
129 | ``` |
||
130 | |||
131 | Occurs when a client is attempting to login. Here you can resolve the login request by username and password. |
||
132 | |||
133 | `connection` [client class object](src/connection.js) |
||
134 | `username` string of username from `USER` command |
||
135 | `password` string of password from `PASS` command |
||
136 | `resolve` takes an object of arguments: |
||
137 | - `fs` |
||
138 | - Set a custom file system class for this connection to use. |
||
139 | - See [File System](#file-system) for implementation details. |
||
140 | - `root` |
||
141 | - If `fs` is not provided, this will set the root directory for the connection. |
||
142 | - The user cannot traverse lower than this directory. |
||
143 | - `cwd` |
||
144 | - If `fs` is not provided, will set the starting directory for the connection |
||
145 | - This is relative to the `root` directory. |
||
146 | - `blacklist` |
||
147 | - Commands that are forbidden for only this connection |
||
148 | - `whitelist` |
||
149 | - If set, this connection will only be able to use the provided commands |
||
150 | |||
151 | `reject` takes an error object |
||
152 | |||
153 | ### `client-error` |
||
154 | ```js |
||
155 | on('client-error', ({connection, context, error}) => { ... }); |
||
156 | ``` |
||
157 | |||
158 | Occurs when an error arises in the client connection. |
||
159 | |||
160 | `connection` [client class object](src/connection.js) |
||
161 | `context` string of where the error occured |
||
162 | `error` error object |
||
163 | |||
164 | ### `RETR` |
||
165 | ```js |
||
166 | on('RETR', (error, filePath) => { ... }); |
||
167 | ``` |
||
168 | |||
169 | Occurs when a file is downloaded. |
||
170 | |||
171 | `error` if successful, will be `null` |
||
172 | `filePath` location to which file was downloaded |
||
173 | |||
174 | ### `STOR` |
||
175 | ```js |
||
176 | on('STOR', (error, fileName) => { ... }); |
||
177 | ``` |
||
178 | |||
179 | Occurs when a file is uploaded. |
||
180 | |||
181 | `error` if successful, will be `null` |
||
182 | `fileName` name of the file that was downloaded |
||
183 | |||
184 | ## Supported Commands |
||
185 | |||
186 | See the [command registry](src/commands/registration) for a list of all implemented FTP commands. |
||
187 | |||
188 | ## File System |
||
189 | The default [file system](src/fs.js) can be overwritten to use your own implementation. |
||
190 | This can allow for virtual file systems, and more. |
||
191 | Each connection can set it's own file system based on the user. |
||
192 | |||
193 | The default file system is exported and can be extended as needed: |
||
194 | ```js |
||
195 | const {FtpSrv, FileSystem} = require('ftp-srv'); |
||
196 | |||
197 | class MyFileSystem extends FileSystem { |
||
198 | constructor() { |
||
199 | super(...arguments); |
||
200 | } |
||
201 | |||
202 | get(fileName) { |
||
203 | ... |
||
204 | } |
||
205 | } |
||
206 | ``` |
||
207 | |||
208 | Custom file systems can implement the following variables depending on the developers needs: |
||
209 | |||
210 | ### Methods |
||
211 | #### [`currentDirectory()`](src/fs.js#L29) |
||
212 | Returns a string of the current working directory |
||
213 | __Used in:__ `PWD` |
||
214 | |||
215 | #### [`get(fileName)`](src/fs.js#L33) |
||
216 | Returns a file stat object of file or directory |
||
217 | __Used in:__ `LIST`, `NLST`, `STAT`, `SIZE`, `RNFR`, `MDTM` |
||
218 | |||
219 | #### [`list(path)`](src/fs.js#L39) |
||
220 | Returns array of file and directory stat objects |
||
221 | __Used in:__ `LIST`, `NLST`, `STAT` |
||
222 | |||
223 | #### [`chdir(path)`](src/fs.js#L56) |
||
224 | Returns new directory relative to current directory |
||
225 | __Used in:__ `CWD`, `CDUP` |
||
226 | |||
227 | #### [`mkdir(path)`](src/fs.js#L96) |
||
228 | Returns a path to a newly created directory |
||
229 | __Used in:__ `MKD` |
||
230 | |||
231 | #### [`write(fileName, {append, start})`](src/fs.js#L68) |
||
232 | Returns a writable stream |
||
233 | Options: |
||
234 | `append` if true, append to existing file |
||
235 | `start` if set, specifies the byte offset to write to |
||
236 | __Used in:__ `STOR`, `APPE` |
||
237 | |||
238 | #### [`read(fileName, {start})`](src/fs.js#L75) |
||
239 | Returns a readable stream |
||
240 | Options: |
||
241 | `start` if set, specifies the byte offset to read from |
||
242 | __Used in:__ `RETR` |
||
243 | |||
244 | #### [`delete(path)`](src/fs.js#L87) |
||
245 | Delete a file or directory |
||
246 | __Used in:__ `DELE` |
||
247 | |||
248 | #### [`rename(from, to)`](src/fs.js#L102) |
||
249 | Renames a file or directory |
||
250 | __Used in:__ `RNFR`, `RNTO` |
||
251 | |||
252 | #### [`chmod(path)`](src/fs.js#L108) |
||
253 | Modifies a file or directory's permissions |
||
254 | __Used in:__ `SITE CHMOD` |
||
255 | |||
256 | #### [`getUniqueName()`](src/fs.js#L113) |
||
257 | Returns a unique file name to write to |
||
258 | __Used in:__ `STOU` |
||
259 | |||
260 | <!--[RM_CONTRIBUTING]--> |
||
261 | ## Contributing |
||
262 | |||
263 | See [CONTRIBUTING.md](CONTRIBUTING.md). |
||
264 | |||
265 | |||
266 | <!--[]--> |
||
267 | |||
268 | <!--[RM_LICENSE]--> |
||
269 | ## License |
||
270 | |||
271 | This software is licensed under the MIT Licence. See [LICENSE](LICENSE). |
||
272 | |||
273 | <!--[]--> |
||
274 | |||
275 | ## References |
||
276 | |||
277 | - [https://cr.yp.to/ftp.html](https://cr.yp.to/ftp.html) |