Skip to content

Commit

Permalink
sftp handler alfa (stat/readdir/readfile/writefile)
Browse files Browse the repository at this point in the history
  • Loading branch information
mb committed Oct 28, 2018
1 parent 375b89a commit ba58781
Show file tree
Hide file tree
Showing 9 changed files with 732 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>docker-ssh</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ FROM alpine:3.6
RUN apk update \
&& apk add nodejs nodejs-npm \
&& rm -rf /tmp/* /var/cache/apk/* /root/.npm /root/.node-gyp

RUN apk update && apk upgrade && apk add git bash
# Connect to container with name/id
ENV CONTAINER=

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"forever": "^0.15.1",
"ssh2": "~0.4.11",
"ssh2-connect": "0.0.7",
"ssh2-streams": "^0.0.21",
"tmp": "^0.0.30",
"buffer-equal-constant-time": "1.0.1",
"uuid": "~2.0.1"
}
}
}
4 changes: 3 additions & 1 deletion server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ log = bunyan.createLogger name: 'sshServer'
webserver = require './src/webserver'
handlerFactory = require './src/session-handler-factory'

sshPort = process.env.PORT or 22
sshPort = process.env.PORT or 2222
httpPort = process.env.HTTP_PORT or 80
httpEnabled = process.env.HTTP_ENABLED or true
ip = process.env.IP or '0.0.0.0'
Expand All @@ -32,6 +32,8 @@ exitOnConfigError "Unknown AUTH_MECHANISM: #{authMechanism}" unless authenticat

options =
privateKey: fs.readFileSync keypath
debug: (str) ->
log.info 'ssh2Debug:', str

# support CONTAINER parameter for backwards compatibility
# Apparently the name filter also matches on partial names
Expand Down
1 change: 1 addition & 0 deletions src/auth/cAuth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ password = process.env.AUTH_PASSWORD

module.exports = (ctx) ->
if ctx.method is 'password'
log.info 'checking pass:', password, 'ctxPass', ctx.password
if ctx.password is password
log.info {container: ctx.username}, 'Authentication succeeded'
process.env.CNAME = ctx.username
Expand Down
1 change: 1 addition & 0 deletions src/auth/index.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = (authType) ->
console.log 'authType:', authType
switch authType
when 'noAuth' then require './noAuthHandler'
when 'simpleAuth' then require './simpleAuthHandler'
Expand Down
105 changes: 105 additions & 0 deletions src/fsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"use strict";

var fs = require('fs');

var FSHandler = (function() {

function FSHandler() {
this.sftpStream = null;
this.reqid = null;
this.rootDir ='/i3c/data/sftp';
}

FSHandler.prototype.doStat = function(path, statkind,statresponder){
console.log('FSHandler.doStat');
fs.stat(this.rootDir+path, function (err, stats) {
if (err) {
console.error(err);
return statresponder.nofile();
}
console.log(stats);
console.log("Got file info successfully!");

// Check file type
console.log("isFile ? " + stats.isFile());
console.log("isDirectory ? " + stats.isDirectory());
if (stats.isFile()){//file
statresponder.is_file(); // Tells statresponder that we're describing a file.
statresponder.permissions = 0o644; // Octal permissions, like what you'd send to a chmod command
statresponder.uid = stats.uid; // User ID that owns the file.
statresponder.gid = stats.gid; // Group ID that owns the file.
statresponder.size = stats.size; // File size in bytes.
statresponder.atime = stats.atime/ 1000 | 0; // Created at (unix style timestamp in seconds-from-epoch).
statresponder.mtime = stats.mtime/ 1000 | 0; // Modified at (unix style timestamp in seconds-from-epoch).
statresponder.file(); // Tells the statter to actually send the values above down the wire.
} else if (stats.isDirectory()) {//dir
statresponder.is_directory(); // Tells statresponder that we're describing a directory.
statresponder.permissions = 0o755; // Octal permissions, like what you'd send to a chmod command
statresponder.uid = stats.uid; // User ID that owns the file.
statresponder.gid = stats.gid; // Group ID that owns the file.
statresponder.size = stats.size; // File size in bytes.
statresponder.atime = stats.atime/ 1000 | 0; // Created at (unix style timestamp in seconds-from-epoch).
statresponder.mtime = stats.mtime/ 1000 | 0; // Modified at (unix style timestamp in seconds-from-epoch).
statresponder.file(); // Tells the statter to actually send the values above down the wire.
} else {
console.error("[FSHandler] Unimplemented for fileType:"+stats);
return statresponder.nofile();
}
});
}

FSHandler.prototype.doReadDir = function(path, responder){
console.log('[FSHandler.doReadDir] start ...');
var dirs, i, j, results;

var rootPath = (path.endsWith("/"))?this.rootDir+path:this.rootDir+path+"/";
console.warn("Readdir request for path: " + path);
dirs = [];
fs.readdirSync(rootPath).forEach(file => {
console.log('[FSHandler.doReadDir] file:'+file);
dirs.push(file);
})
i = 0;
responder.on("dir", function() {
if (dirs[i]) {
console.warn("Returning directory "+rootPath+" entry: " + dirs[i]);
var stats = fs.statSync(rootPath+dirs[i]);

var attrs = {
'mode': ((stats.isDirectory())?fs.constants.S_IFDIR:fs.constants.S_IFREG) | 0o644, // Bit mask of file type and permissions
'permissions': 0o644, // Octal permissions, like what you'd send to a chmod command
'uid': stats.uid, // User ID that owns the file.
'gid': stats.gid, // Group ID that owns the file.
'size': stats.size, // File size in bytes.
'atime': stats.atime/ 1000 | 0, // Created at (unix style timestamp in seconds-from-epoch).
'mtime': stats.mtime/ 1000 | 0 // Modified at (unix style timestamp in seconds-from-epoch).
}

responder.file(dirs[i], attrs);
return i++;
} else {
return responder.end();
}
});
}

FSHandler.prototype.doReadFile = function(path, writestream){
console.log('[FSHandler.doReadFile] start ...file:'+path);
var rootPath = this.rootDir+path;
return fs.createReadStream(rootPath).pipe(writestream);
}

FSHandler.prototype.doWriteFile = function(path, readstream){
console.log('[FSHandler.doWriteFile] start ...');
var rootPath = this.rootDir+path;
var something;
something = fs.createWriteStream(rootPath);
readstream.on("end",function() {console.warn("Writefile request has come to an end!!!")});
return readstream.pipe(something);
}

return FSHandler;
})();


module.exports = FSHandler
Loading

0 comments on commit ba58781

Please sign in to comment.