0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

node.jsで簡易ftpサーバーを立てる(Qemu用)

Last updated at Posted at 2020-11-22

概要

Qemuではフォルダ共有機能が実質存在しないので、ゲストOSからホストのファイルにアクセスするためのftpサーバーをnode.jsでサクッと構築する。

パッケージのインストール

npm install ftpd

ソース

ftpd.js
var ftpd = require('ftpd');
var fs = require('fs');
var path = require('path');

//コマンドライン引数からポートとルートになるフォルダの設定
var port = process.argv[2] || 10021;
var root = process.argv[3] || process.cwd();

//サーバーの設定
var server = new ftpd.FtpServer('127.0.0.1', {
  //接続後の初期ディレクトリ
  getInitialCwd: function() {
    return '/';
  },
  //ルートとなるフォルダの設定
  getRoot: function() {
    return root;
  },
  pasvPortRangeStart: 1025,
  pasvPortRangeEnd: 1050,
  tlsOptions: null,
  allowUnauthorizedTls: true,
});

//エラー
server.on('error', function(error) {
  console.log('FTP Server error:', error);
});

//クライアントが接続してきたら認証(してるフリ)
server.on('client:connected', function(connection) {
  var username = null;
  connection.on('command:user', function(user, success, failure) {
    if (user) {
      username = user;
      success();
    } else {
      failure();
    }
  });

  connection.on('command:pass', function(pass, success, failure) {
    if (pass) {
      success(username);
    } else {
      failure();
    }
  });
});

//コンソールへの出力を最低限に
server.debugging = 0;

//指定したポートでサーバー起動
server.listen(port);
console.log('Listening on port ' + port);

起動

node ftpd.js 10020 x:\Documents

Qemuからアクセス

(特に設定をしていなければ)IPアドレス10.0.2.2でqemuのゲストからホストのサーバーにアクセスできるようです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?