wocao
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package gitctl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type localKey struct {
|
||||
Filename string
|
||||
KeyType string
|
||||
Fingerprint string
|
||||
Comment string
|
||||
Line string
|
||||
Registered bool
|
||||
}
|
||||
|
||||
func scanLocalSSHKeys(registered map[string]bool) ([]localKey, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("find home dir: %w", err)
|
||||
}
|
||||
pubs, err := filepath.Glob(filepath.Join(home, ".ssh", "*.pub"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan ~/.ssh: %w", err)
|
||||
}
|
||||
out := make([]localKey, 0, len(pubs))
|
||||
for _, path := range pubs {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
key, comment, _, _, err := ssh.ParseAuthorizedKey(raw)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
line := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key)))
|
||||
if comment != "" {
|
||||
line += " " + comment
|
||||
}
|
||||
fp := ssh.FingerprintSHA256(key)
|
||||
out = append(out, localKey{
|
||||
Filename: filepath.Base(path),
|
||||
KeyType: key.Type(),
|
||||
Fingerprint: fp,
|
||||
Comment: comment,
|
||||
Line: line,
|
||||
Registered: registered[fp],
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func reconstructAuthorizedLine(marshaled string) (string, error) {
|
||||
key, err := ssh.ParsePublicKey([]byte(marshaled))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("reconstruct ssh key: %w", err)
|
||||
}
|
||||
return strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))), nil
|
||||
}
|
||||
Reference in New Issue
Block a user