This commit is contained in:
2025-05-15 00:46:57 +02:00
parent 30c71cb449
commit b2b27b2c3d
18 changed files with 622 additions and 234 deletions

View File

@@ -0,0 +1,5 @@
package constants
const Version = "0.0.1"
const ApiVersion = 1

View File

@@ -1,99 +0,0 @@
package remote
import (
"fmt"
"log"
"os"
"path/filepath"
"syscall"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
func ConnectWithKey(host, user string) (*sftp.Client, error) {
authMethods := loadSshKeys()
// Create SSH client configuration
config := &ssh.ClientConfig{
User: user,
Auth: authMethods,
}
return connect(host, config)
}
func ConnectWithPassword(host, user string) (*sftp.Client, error) {
fmt.Printf("%s@%s's password:", user, host)
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
// Create SSH client configuration
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(string(bytePassword)),
},
}
return connect(host, config)
}
func connect(host string, config *ssh.ClientConfig) (*sftp.Client, error) {
// Connect to the SSH server
conn, err := ssh.Dial("tcp", host, config)
if err != nil {
return nil, fmt.Errorf("cannot connect to ssh server: %w", err)
}
defer conn.Close()
// Open SFTP session
sftpClient, err := sftp.NewClient(conn)
if err != nil {
return nil, fmt.Errorf("cannot connect to ssh server: %w", err)
}
return sftpClient, nil
}
func loadSshKeys() []ssh.AuthMethod {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
var auths []ssh.AuthMethod
entries, err := os.ReadDir(filepath.Join(dirname, ".ssh"))
if err != nil {
return auths
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
keyPath := filepath.Join(dirname, name)
keyData, err := os.ReadFile(keyPath)
if err != nil {
continue
}
signer, err := ssh.ParsePrivateKey(keyData)
if err != nil {
fmt.Printf("%s's passphrase:", entry.Name())
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
continue
}
signer, err = ssh.ParsePrivateKeyWithPassphrase(keyData, bytePassword)
if err != nil {
continue
}
}
auths = append(auths, ssh.PublicKeys(signer))
}
return auths
}

View File

@@ -1,32 +0,0 @@
package ssh
import (
"cloudsave/pkg/remote"
"fmt"
"log"
"os/user"
)
type (
SFTPSyncer struct {
}
)
func (SFTPSyncer) Sync(r remote.Remote) error {
currentUser, err := user.Current()
if err != nil {
log.Fatalf("Failed to get current user: %v", err)
}
cli, err := remote.ConnectWithKey(r.URL, currentUser.Username)
if err != nil {
cli, err = remote.ConnectWithPassword(r.URL, currentUser.Username)
if err != nil {
return fmt.Errorf("failed to connect to host: %w", err)
}
}
defer cli.Close()
return nil
}

View File

@@ -0,0 +1,22 @@
package windows
import (
"syscall"
"unsafe"
)
const (
NULL = 0
MB_OK = 0
)
// MessageBox of Win32 API.
func MessageBox(hwnd uintptr, caption, title string, flags uint) int {
ret, _, _ := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call(
uintptr(hwnd),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))),
uintptr(flags))
return int(ret)
}