Unverified Commit 9ab4ea69 authored by Eric_Lee's avatar Eric_Lee Committed by GitHub

Dev (#66)

* fix bug; user auth login type

* fix addr bugs

* fix download many files bug
parent d860866b
package auth package auth
import ( import (
"strings" "net"
"github.com/gliderlabs/ssh" "github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh" gossh "golang.org/x/crypto/ssh"
...@@ -30,9 +30,9 @@ func checkAuth(ctx ssh.Context, password, publicKey string) (res ssh.AuthResult) ...@@ -30,9 +30,9 @@ func checkAuth(ctx ssh.Context, password, publicKey string) (res ssh.AuthResult)
if password != "" { if password != "" {
authMethod = "password" authMethod = "password"
} }
remoteAddr := strings.Split(ctx.RemoteAddr().String(), ":")[0] remoteAddr, _, _ := net.SplitHostPort(ctx.RemoteAddr().String())
resp, err := service.Authenticate(username, password, publicKey, remoteAddr, "ST") resp, err := service.Authenticate(username, password, publicKey, remoteAddr, "T")
if err != nil { if err != nil {
action = actionFailed action = actionFailed
logger.Infof("%s %s for %s from %s", action, authMethod, username, remoteAddr) logger.Infof("%s %s for %s from %s", action, authMethod, username, remoteAddr)
...@@ -73,7 +73,7 @@ func CheckUserPublicKey(ctx ssh.Context, key ssh.PublicKey) ssh.AuthResult { ...@@ -73,7 +73,7 @@ func CheckUserPublicKey(ctx ssh.Context, key ssh.PublicKey) ssh.AuthResult {
func CheckMFA(ctx ssh.Context, challenger gossh.KeyboardInteractiveChallenge) (res ssh.AuthResult) { func CheckMFA(ctx ssh.Context, challenger gossh.KeyboardInteractiveChallenge) (res ssh.AuthResult) {
username := ctx.User() username := ctx.User()
remoteAddr := strings.Split(ctx.RemoteAddr().String(), ":")[0] remoteAddr, _, _ := net.SplitHostPort(ctx.RemoteAddr().String())
res = ssh.AuthFailed res = ssh.AuthFailed
defer func() { defer func() {
authMethod := "MFA" authMethod := "MFA"
......
...@@ -370,7 +370,14 @@ func ConstructAssetNodeTree(assetNodes []model.Node) treeprint.Tree { ...@@ -370,7 +370,14 @@ func ConstructAssetNodeTree(assetNodes []model.Node) treeprint.Tree {
tree := treeprint.New() tree := treeprint.New()
for i := 0; i < len(assetNodes); i++ { for i := 0; i < len(assetNodes); i++ {
r := strings.LastIndex(assetNodes[i].Key, ":") r := strings.LastIndex(assetNodes[i].Key, ":")
if _, ok := treeMap[assetNodes[i].Key[:r]]; r < 0 || !ok { if r < 0 {
subtree := tree.AddBranch(fmt.Sprintf("%s.%s(%s)",
strconv.Itoa(i+1), assetNodes[i].Name,
strconv.Itoa(assetNodes[i].AssetsAmount)))
treeMap[assetNodes[i].Key] = subtree
continue
}
if _, ok := treeMap[assetNodes[i].Key[:r]]; !ok {
subtree := tree.AddBranch(fmt.Sprintf("%s.%s(%s)", subtree := tree.AddBranch(fmt.Sprintf("%s.%s(%s)",
strconv.Itoa(i+1), assetNodes[i].Name, strconv.Itoa(i+1), assetNodes[i].Name,
strconv.Itoa(assetNodes[i].AssetsAmount))) strconv.Itoa(assetNodes[i].AssetsAmount)))
......
...@@ -105,7 +105,15 @@ func (fs *sftpHandler) Filewrite(r *sftp.Request) (io.WriterAt, error) { ...@@ -105,7 +105,15 @@ func (fs *sftpHandler) Filewrite(r *sftp.Request) (io.WriterAt, error) {
func (fs *sftpHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) { func (fs *sftpHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) {
logger.Debug("File read: ", r.Filepath) logger.Debug("File read: ", r.Filepath)
f, err := fs.Open(r.Filepath) f, err := fs.Open(r.Filepath)
return NewReaderAt(f), err if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
_ = f.Close()
return nil, err
}
return NewReaderAt(f, fi), err
} }
func (fs *sftpHandler) Close() { func (fs *sftpHandler) Close() {
...@@ -130,33 +138,27 @@ func NewWriterAt(f *sftp.File) io.WriterAt { ...@@ -130,33 +138,27 @@ func NewWriterAt(f *sftp.File) io.WriterAt {
return &clientReadWritAt{f: f, mu: new(sync.RWMutex)} return &clientReadWritAt{f: f, mu: new(sync.RWMutex)}
} }
func NewReaderAt(f *sftp.File) io.ReaderAt { func NewReaderAt(f *sftp.File, fi os.FileInfo) io.ReaderAt {
return &clientReadWritAt{f: f, mu: new(sync.RWMutex)} return &clientReadWritAt{f: f, mu: new(sync.RWMutex), fi: fi}
} }
type clientReadWritAt struct { type clientReadWritAt struct {
f *sftp.File f *sftp.File
mu *sync.RWMutex mu *sync.RWMutex
closed bool fi os.FileInfo
firstErr error firstErr error
} }
func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) { func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if c.closed { if c.firstErr != nil {
return 0, c.firstErr return 0, c.firstErr
} }
if _, err = c.f.Seek(off, 0); err != nil { _, _ = c.f.Seek(off, 0)
c.firstErr = err
c.closed = true
_ = c.f.Close()
return
}
nw, err := c.f.Write(p) nw, err := c.f.Write(p)
if err != nil { if err != nil {
c.firstErr = err c.firstErr = err
c.closed = true
_ = c.f.Close() _ = c.f.Close()
} }
return nw, err return nw, err
...@@ -165,19 +167,16 @@ func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) { ...@@ -165,19 +167,16 @@ func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) {
func (c *clientReadWritAt) ReadAt(p []byte, off int64) (n int, err error) { func (c *clientReadWritAt) ReadAt(p []byte, off int64) (n int, err error) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if c.closed { if c.firstErr != nil {
return 0, c.firstErr return 0, c.firstErr
} }
if _, err = c.f.Seek(off, 0); err != nil { if off >= c.fi.Size() {
c.firstErr = err return 0, io.EOF
c.closed = true
_ = c.f.Close()
return
} }
_, _ = c.f.Seek(off, 0)
nr, err := c.f.Read(p) nr, err := c.f.Read(p)
if err != nil { if err != nil {
c.firstErr = err c.firstErr = err
c.closed = true
_ = c.f.Close() _ = c.f.Close()
} }
return nr, err return nr, err
......
...@@ -2,7 +2,7 @@ package handler ...@@ -2,7 +2,7 @@ package handler
import ( import (
"io" "io"
"strings" "net"
"sync" "sync"
"github.com/gliderlabs/ssh" "github.com/gliderlabs/ssh"
...@@ -85,7 +85,8 @@ func (w *WrapperSession) LoginFrom() string { ...@@ -85,7 +85,8 @@ func (w *WrapperSession) LoginFrom() string {
} }
func (w *WrapperSession) RemoteAddr() string { func (w *WrapperSession) RemoteAddr() string {
return strings.Split(w.Sess.RemoteAddr().String(), ":")[0] host, _, _ := net.SplitHostPort(w.Sess.RemoteAddr().String())
return host
} }
func (w *WrapperSession) Pty() ssh.Pty { func (w *WrapperSession) Pty() ssh.Pty {
......
...@@ -150,7 +150,7 @@ func MakeConfig(asset *model.Asset, systemUser *model.SystemUser, timeout time.D ...@@ -150,7 +150,7 @@ func MakeConfig(asset *model.Asset, systemUser *model.SystemUser, timeout time.D
} }
} }
} }
if systemUser.Password == "" && systemUser.PrivateKey == "" { if systemUser.Password == "" && systemUser.PrivateKey == "" && systemUser.LoginMode != model.LoginModeManual{
info := service.GetSystemUserAssetAuthInfo(systemUser.ID, asset.ID) info := service.GetSystemUserAssetAuthInfo(systemUser.ID, asset.ID)
systemUser.Password = info.Password systemUser.Password = info.Password
systemUser.PrivateKey = info.PrivateKey systemUser.PrivateKey = info.PrivateKey
......
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