Commit bba9f607 authored by Eric's avatar Eric

[update] add sftp volume support

parent 11ef7697
...@@ -329,6 +329,7 @@ ...@@ -329,6 +329,7 @@
"github.com/aws/aws-sdk-go/aws", "github.com/aws/aws-sdk-go/aws",
"github.com/aws/aws-sdk-go/aws/credentials", "github.com/aws/aws-sdk-go/aws/credentials",
"github.com/aws/aws-sdk-go/aws/session", "github.com/aws/aws-sdk-go/aws/session",
"github.com/aws/aws-sdk-go/service/s3",
"github.com/aws/aws-sdk-go/service/s3/s3manager", "github.com/aws/aws-sdk-go/service/s3/s3manager",
"github.com/elastic/go-elasticsearch", "github.com/elastic/go-elasticsearch",
"github.com/gliderlabs/ssh", "github.com/gliderlabs/ssh",
......
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"html/template" "html/template"
"log"
"net/http" "net/http"
"strings" "strings"
...@@ -51,18 +50,15 @@ func AuthDecorator(handler http.HandlerFunc) http.HandlerFunc { ...@@ -51,18 +50,15 @@ func AuthDecorator(handler http.HandlerFunc) http.HandlerFunc {
} }
func OnELFinderConnect(s socketio.Conn) error { func OnELFinderConnect(s socketio.Conn) error {
u := s.URL() data := EmitSidMsg{Sid: s.ID()}
sid := u.Query().Get("sid")
data := EmitSidMsg{Sid: sid}
s.Emit("data", data) s.Emit("data", data)
return nil return nil
} }
func OnELFinderDisconnect(s socketio.Conn, msg string) { func OnELFinderDisconnect(s socketio.Conn, msg string) {
u := s.URL() logger.Debug("disconnect: ", s.ID())
sid := u.Query().Get("sid") logger.Debug("disconnect msg ", msg)
log.Println("disconnect: ", sid) removeUserVolume(s.ID())
log.Println("disconnect msg ", msg)
} }
func sftpHostFinder(wr http.ResponseWriter, req *http.Request) { func sftpHostFinder(wr http.ResponseWriter, req *http.Request) {
...@@ -80,7 +76,27 @@ func sftpFinder(wr http.ResponseWriter, req *http.Request) { ...@@ -80,7 +76,27 @@ func sftpFinder(wr http.ResponseWriter, req *http.Request) {
func sftpHostConnectorView(wr http.ResponseWriter, req *http.Request) { func sftpHostConnectorView(wr http.ResponseWriter, req *http.Request) {
user := req.Context().Value(cctx.ContextKeyUser).(*model.User) user := req.Context().Value(cctx.ContextKeyUser).(*model.User)
remoteIP := req.Context().Value(cctx.ContextKeyRemoteAddr).(string) remoteIP := req.Context().Value(cctx.ContextKeyRemoteAddr).(string)
logger.Debugf("user: %s; remote ip: %s; create connector", user.Name, remoteIP) switch req.Method {
con := elfinder.NewElFinderConnector([]elfinder.Volume{&elfinder.DefaultVolume}) case "GET":
if err := req.ParseForm(); err != nil {
http.Error(wr, err.Error(), http.StatusBadRequest)
return
}
case "POST":
err := req.ParseMultipartForm(32 << 20)
if err != nil {
http.Error(wr, err.Error(), http.StatusBadRequest)
return
}
}
sid := req.Form.Get("sid")
userV, ok := GetUserVolume(sid)
if !ok {
userV = NewUserVolume(user, remoteIP)
addUserVolume(sid, userV)
}
logger.Debugf("sid: %s", sid)
logger.Debug(userVolumes)
con := elfinder.NewElFinderConnector([]elfinder.Volume{userV})
con.ServeHTTP(wr, req) con.ServeHTTP(wr, req)
} }
This diff is collapsed.
package httpd
import (
"sync"
)
var userVolumes = make(map[string]*UserVolume)
var volumeLock = new(sync.RWMutex)
func addUserVolume(sid string, v *UserVolume) {
volumeLock.Lock()
defer volumeLock.Unlock()
userVolumes[sid] = v
}
func removeUserVolume(sid string) {
volumeLock.RLock()
v, ok := userVolumes[sid]
volumeLock.RUnlock()
if !ok {
return
}
v.Close()
volumeLock.Lock()
delete(userVolumes, sid)
volumeLock.Unlock()
}
func GetUserVolume(sid string) (*UserVolume, bool) {
volumeLock.RLock()
defer volumeLock.RUnlock()
v, ok := userVolumes[sid]
return v, ok
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment