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
import (
"strings"
"net"
"github.com/gliderlabs/ssh"
gossh "golang.org/x/crypto/ssh"
......@@ -30,9 +30,9 @@ func checkAuth(ctx ssh.Context, password, publicKey string) (res ssh.AuthResult)
if 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 {
action = actionFailed
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 {
func CheckMFA(ctx ssh.Context, challenger gossh.KeyboardInteractiveChallenge) (res ssh.AuthResult) {
username := ctx.User()
remoteAddr := strings.Split(ctx.RemoteAddr().String(), ":")[0]
remoteAddr, _, _ := net.SplitHostPort(ctx.RemoteAddr().String())
res = ssh.AuthFailed
defer func() {
authMethod := "MFA"
......
......@@ -370,7 +370,14 @@ func ConstructAssetNodeTree(assetNodes []model.Node) treeprint.Tree {
tree := treeprint.New()
for i := 0; i < len(assetNodes); i++ {
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)",
strconv.Itoa(i+1), assetNodes[i].Name,
strconv.Itoa(assetNodes[i].AssetsAmount)))
......
......@@ -105,7 +105,15 @@ func (fs *sftpHandler) Filewrite(r *sftp.Request) (io.WriterAt, error) {
func (fs *sftpHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) {
logger.Debug("File read: ", 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() {
......@@ -130,33 +138,27 @@ func NewWriterAt(f *sftp.File) io.WriterAt {
return &clientReadWritAt{f: f, mu: new(sync.RWMutex)}
}
func NewReaderAt(f *sftp.File) io.ReaderAt {
return &clientReadWritAt{f: f, mu: new(sync.RWMutex)}
func NewReaderAt(f *sftp.File, fi os.FileInfo) io.ReaderAt {
return &clientReadWritAt{f: f, mu: new(sync.RWMutex), fi: fi}
}
type clientReadWritAt struct {
f *sftp.File
mu *sync.RWMutex
closed bool
fi os.FileInfo
firstErr error
}
func (c *clientReadWritAt) WriteAt(p []byte, off int64) (n int, err error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
if c.firstErr != nil {
return 0, c.firstErr
}
if _, err = c.f.Seek(off, 0); err != nil {
c.firstErr = err
c.closed = true
_ = c.f.Close()
return
}
_, _ = c.f.Seek(off, 0)
nw, err := c.f.Write(p)
if err != nil {
c.firstErr = err
c.closed = true
_ = c.f.Close()
}
return nw, err
......@@ -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) {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
if c.firstErr != nil {
return 0, c.firstErr
}
if _, err = c.f.Seek(off, 0); err != nil {
c.firstErr = err
c.closed = true
_ = c.f.Close()
return
if off >= c.fi.Size() {
return 0, io.EOF
}
_, _ = c.f.Seek(off, 0)
nr, err := c.f.Read(p)
if err != nil {
c.firstErr = err
c.closed = true
_ = c.f.Close()
}
return nr, err
......
......@@ -2,7 +2,7 @@ package handler
import (
"io"
"strings"
"net"
"sync"
"github.com/gliderlabs/ssh"
......@@ -85,7 +85,8 @@ func (w *WrapperSession) LoginFrom() 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 {
......
......@@ -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)
systemUser.Password = info.Password
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