1 Star 0 Fork 1

xiagengwen / ohos_ftp-srv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ohos_ftp-srv

简介

ftp-srv是一个用于OpenHarmony的Ftp服务器库。通过ftp-srv,开发者可以轻松地创建和管理Ftp服务器,实现文件的上传、下载、目录查看、目录创建、目录删除等操作。

jpeg

下载安装

ohpm install @ohos/ftp-srv

OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包

需要权限

ohos.permission.INTERNET
ohos.permission.GET_NETWORK_INFO

使用说明

在pages页面中使用

//引入
import { FtpSrv } from '@ohos/ftp-srv';

const ftpSever = new FtpSrv({
    url: "ftp://localhost:8889",
    anonymous: true,
    pasvUrl: "localhost",
    pasvMin: 8881
});
ftpSever.on('login', (user: Option, resolve: Function, reject: Function) => {
    if (user.username === 'demo' && user.password === '123456' || user.username === 'anonymous') {
        resolve({ root: getContext(this).filesDir });
    } else {
        reject('Bad username or password');
    }
});
ftpSever.listen().then(() => {
    console.log('Ftp server is starting...')
});


interface Option {
    username: string,
    password: string
}

接口说明

一、new FtpSrv({options}) option对应的字段参数功能

  1. url URL字符串,指定服务端连接的协议、主机名和端口
  2. pasvUrl 当在被动模式下连接握手过程中收到命令时,向客户端提供IP地址。
  3. pasvMin 被动模式连接的起始端口
  4. pasvMax 被动模式连接的结束端口
  5. tls 用于显式连接的TLS安全上下文对象
  6. anonymous 如果为 true,将允许客户端使用用户名进行身份验证anonymous,而不需要用户提供密码。
  7. blacklist 设置不允许执行的命令数组。
  8. whitelist 设置只允许执行的命令数组。

二、new FtpSrv FtpSrv对象所提供的方法

  1. listen() 启动服务端连接监听,开启ftp服务

  2. quit() 断开服务端连接监听

  3. on(eventName, callback) 提供监听事件,有如下:

    on('login', ({connection, username, password}, resolve, reject) => { ... });

    客户端尝试登录时触发,在这里可以通过用户名和密码判断是否为目标用户。

    on('client-error', ({connection, context, error}) => { ... });

    当客户端连接出现错误时触发。

    on('disconnect', ({connection, id, newConnectionCount}) => { ... });

    当客户端断开连接时触发。

    on('closed', ({}) => { ... });

    当FTP服务器关闭时触发。

    on('server-error', ({error}) => { ... });

    当FTP服务器出现错误时触发。

约束与限制

在下述版本验证通过:

  • DevEco Studio: (5.0.3.122), SDK: API12 (5.0.0.17)

目录结构

|---- ohos_ftp-srv  
|     |---- entry  # 示例代码文件夹
|     |---- library  # ftp-srv库文件夹
|           |---- index.ts  # 对外接口
|           └─src/main/ets/commands
|                          ├─registration # ftp命令实现目录,包含各种命令的处理文件
|                          ├─index.ts # 命令类
|                          └─registry.ts # 注册的命令的处理文件
|           └─src/main/ets/connector
|                          ├─active.ts # 主动模式处理逻辑
|                          ├─base.ts # 模式处理基础类
|                          └─passive.ts # 被动模式处理逻辑
|           └─src/main/ets/helpers
|                          ├─escape-path.ts # 路径处理
|                          ├─event-emitter.ts # 事件监听处理类
|                          ├─file-stat.ts # 服务端交互客户端信息文件条目格式化处理
|                          ├─find-port.ts # 查询空闲端口
|                          ├─logger.ts # 日志打印处理
|                          ├─path-util.ts # 文件路径处理
|                          └─promise-util.ts # promise异步功能工具类
|                     |---- connection.ts  # FTP 连接处理逻辑
|                     |---- errors.ts  # 异常类
|                     |---- fs.ts  # 文件系统接口实现
|                     |---- index.ts  # ftp服务器启动监听等处理逻辑
|                     |---- messages.ts  # ftp状态码信息
|---- README.md  # 安装使用方法                    

贡献代码

使用过程中发现任何问题都可以提 Issue 给我们,当然,我们也非常欢迎你给我们发 PR

开源协议

本项目基于 MIT License ,请自由地享受和参与开源。

MIT License Copyright (c) 2019 Tyler Stewart Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

ftp-srv is a modern and extensible FTP server designed to be simple yet configurable. 展开 收起
TypeScript 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/xiagengwen/ohos_ftp-srv.git
git@gitee.com:xiagengwen/ohos_ftp-srv.git
xiagengwen
ohos_ftp-srv
ohos_ftp-srv
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891