Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sftp-server): do not generate host key until first enabled #7734

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
func Init() {
bootstrap.InitConfig()
bootstrap.Log()
bootstrap.InitHostKey()
bootstrap.InitDB()
data.InitData()
bootstrap.InitIndex()
Expand Down
3 changes: 0 additions & 3 deletions internal/conf/var.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package conf

import (
"golang.org/x/crypto/ssh"
"net/url"
"regexp"
)
Expand Down Expand Up @@ -33,5 +32,3 @@ var (
ManageHtml string
IndexHtml string
)

var SSHSigners []ssh.Signer
6 changes: 4 additions & 2 deletions server/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/ftp"
"github.com/alist-org/alist/v3/server/sftp"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
"net/http"
Expand All @@ -21,6 +22,7 @@ type SftpDriver struct {
}

func NewSftpDriver() (*SftpDriver, error) {
sftp.InitHostKey()
header := &http.Header{}
header.Add("User-Agent", setting.GetStr(conf.FTPProxyUserAgent))
return &SftpDriver{
Expand All @@ -40,7 +42,7 @@ func (d *SftpDriver) GetConfig() *sftpd.Config {
AuthLogCallback: d.AuthLogCallback,
BannerCallback: d.GetBanner,
}
for _, k := range conf.SSHSigners {
for _, k := range sftp.SSHSigners {
serverConfig.AddHostKey(k)
}
d.config = &sftpd.Config{
Expand All @@ -62,7 +64,7 @@ func (d *SftpDriver) GetFileSystem(sc *ssh.ServerConn) (sftpd.FileSystem, error)
ctx = context.WithValue(ctx, "meta_pass", "")
ctx = context.WithValue(ctx, "client_ip", sc.RemoteAddr().String())
ctx = context.WithValue(ctx, "proxy_header", d.proxyHeader)
return &ftp.SftpDriverAdapter{FtpDriver: ftp.NewAferoAdapter(ctx)}, nil
return &sftp.DriverAdapter{FtpDriver: ftp.NewAferoAdapter(ctx)}, nil
}

func (d *SftpDriver) Close() {
Expand Down
2 changes: 1 addition & 1 deletion server/ftp/const.go → server/sftp/const.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ftp
package sftp

// From leffss/sftpd
const (
Expand Down
12 changes: 8 additions & 4 deletions internal/bootstrap/ssh.go → server/sftp/hostkey.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bootstrap
package sftp

import (
"crypto/rand"
Expand All @@ -7,14 +7,18 @@ import (
"encoding/pem"
"fmt"
"github.com/alist-org/alist/v3/cmd/flags"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/pkg/utils"
"golang.org/x/crypto/ssh"
"os"
"path/filepath"
)

var SSHSigners []ssh.Signer

func InitHostKey() {
if SSHSigners != nil {
return
}
sshPath := filepath.Join(flags.DataDir, "ssh")
if !utils.Exists(sshPath) {
err := utils.CreateNestedDirectory(sshPath)
Expand All @@ -23,9 +27,9 @@ func InitHostKey() {
return
}
}
conf.SSHSigners = make([]ssh.Signer, 0, 4)
SSHSigners = make([]ssh.Signer, 0, 4)
if rsaKey, ok := LoadOrGenerateRSAHostKey(sshPath); ok {
conf.SSHSigners = append(conf.SSHSigners, rsaKey)
SSHSigners = append(SSHSigners, rsaKey)
}
// TODO Add keys for other encryption algorithms
}
Expand Down
33 changes: 17 additions & 16 deletions server/ftp/sftp.go → server/sftp/sftp.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,73 @@
package ftp
package sftp

import (
"github.com/KirCute/sftpd-alist"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/ftp"
"os"
)

type SftpDriverAdapter struct {
FtpDriver *AferoAdapter
type DriverAdapter struct {
FtpDriver *ftp.AferoAdapter
}

func (s *SftpDriverAdapter) OpenFile(_ string, _ uint32, _ *sftpd.Attr) (sftpd.File, error) {
func (s *DriverAdapter) OpenFile(_ string, _ uint32, _ *sftpd.Attr) (sftpd.File, error) {
// See also GetHandle
return nil, errs.NotImplement
}

func (s *SftpDriverAdapter) OpenDir(_ string) (sftpd.Dir, error) {
func (s *DriverAdapter) OpenDir(_ string) (sftpd.Dir, error) {
// See also GetHandle
return nil, errs.NotImplement
}

func (s *SftpDriverAdapter) Remove(name string) error {
func (s *DriverAdapter) Remove(name string) error {
return s.FtpDriver.Remove(name)
}

func (s *SftpDriverAdapter) Rename(old, new string, _ uint32) error {
func (s *DriverAdapter) Rename(old, new string, _ uint32) error {
return s.FtpDriver.Rename(old, new)
}

func (s *SftpDriverAdapter) Mkdir(name string, attr *sftpd.Attr) error {
func (s *DriverAdapter) Mkdir(name string, attr *sftpd.Attr) error {
return s.FtpDriver.Mkdir(name, attr.Mode)
}

func (s *SftpDriverAdapter) Rmdir(name string) error {
func (s *DriverAdapter) Rmdir(name string) error {
return s.Remove(name)
}

func (s *SftpDriverAdapter) Stat(name string, _ bool) (*sftpd.Attr, error) {
func (s *DriverAdapter) Stat(name string, _ bool) (*sftpd.Attr, error) {
stat, err := s.FtpDriver.Stat(name)
if err != nil {
return nil, err
}
return fileInfoToSftpAttr(stat), nil
}

func (s *SftpDriverAdapter) SetStat(_ string, _ *sftpd.Attr) error {
func (s *DriverAdapter) SetStat(_ string, _ *sftpd.Attr) error {
return errs.NotSupport
}

func (s *SftpDriverAdapter) ReadLink(_ string) (string, error) {
func (s *DriverAdapter) ReadLink(_ string) (string, error) {
return "", errs.NotSupport
}

func (s *SftpDriverAdapter) CreateLink(_, _ string, _ uint32) error {
func (s *DriverAdapter) CreateLink(_, _ string, _ uint32) error {
return errs.NotSupport
}

func (s *SftpDriverAdapter) RealPath(path string) (string, error) {
func (s *DriverAdapter) RealPath(path string) (string, error) {
return utils.FixAndCleanPath(path), nil
}

func (s *SftpDriverAdapter) GetHandle(name string, flags uint32, _ *sftpd.Attr, offset uint64) (sftpd.FileTransfer, error) {
func (s *DriverAdapter) GetHandle(name string, flags uint32, _ *sftpd.Attr, offset uint64) (sftpd.FileTransfer, error) {
return s.FtpDriver.GetHandle(name, sftpFlagToOpenMode(flags), int64(offset))
}

func (s *SftpDriverAdapter) ReadDir(name string) ([]sftpd.NamedAttr, error) {
func (s *DriverAdapter) ReadDir(name string) ([]sftpd.NamedAttr, error) {
dir, err := s.FtpDriver.ReadDir(name)
if err != nil {
return nil, err
Expand Down
Loading