Re-arrange platform-dependent code.

This commit is contained in:
Greg 2019-09-10 11:31:57 -04:00
parent 09859ca0a9
commit 2c4c708b3a
7 changed files with 75 additions and 67 deletions

View File

@ -11,12 +11,8 @@ import (
"golang.org/x/image/font/sfnt" "golang.org/x/image/font/sfnt"
) )
var (
regular *sfnt.Font
confDir string
)
func init() { func init() {
var err error
regular, err = sfnt.Parse(goregular.TTF) regular, err = sfnt.Parse(goregular.TTF)
if err != nil { if err != nil {
log(Fatal, "Cannot parse default font: ", err) log(Fatal, "Cannot parse default font: ", err)

View File

@ -11,11 +11,6 @@ import (
"golang.org/x/image/font/sfnt" "golang.org/x/image/font/sfnt"
) )
var (
regular *sfnt.Font
confDir string
)
func setFont() error { func setFont() error {
f, err := os.Open("/System/Library/Fonts/AppleSDGothicNeo.ttc") f, err := os.Open("/System/Library/Fonts/AppleSDGothicNeo.ttc")
if err != nil { if err != nil {

View File

@ -17,6 +17,8 @@ import (
"gioui.org/ui/measure" "gioui.org/ui/measure"
"gioui.org/ui/text" "gioui.org/ui/text"
"golang.org/x/image/font/sfnt"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -74,6 +76,8 @@ func main() {
} }
var ( var (
confDir string
regular *sfnt.Font
Config conf Config conf
l []passgo.Pass l []passgo.Pass
mux sync.Mutex mux sync.Mutex

25
impl_android.go Normal file
View File

@ -0,0 +1,25 @@
//+build android
package passgo
import (
"fmt"
)
//Clip copies a string to the clipboard
func Clip(x string) {
fmt.Println("Clipboard not implemented for this platform")
}
func (s *Store) nativeDecrypt(name string) (string, error) {
return "", fmt.Errorf("NOT IMPLEMENTED")
}
func (s *Store) nativeEncrypt(pw string) ([]byte, error) {
return nil, fmt.Errorf("NOT IMPLEMENTED")
}
func nativeIdentities() ([]string, error) {
return nil, fmt.Errorf("NOT IMPLEMENTED")
}

View File

@ -5,6 +5,7 @@ package passgo
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/hex"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
@ -14,10 +15,13 @@ import (
"strconv" "strconv"
"sync" "sync"
"golang.org/x/crypto/openpgp"
"github.com/jcmdev0/gpgagent"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
) )
func (s *Store) gpgDecrypt(name string) (string, error) { func (s *Store) nativeDecrypt(name string) (string, error) {
fmt.Println("calling gpg -d") fmt.Println("calling gpg -d")
file := path.Join(s.Dir, strings.Join([]string{name, ".gpg"}, "")) file := path.Join(s.Dir, strings.Join([]string{name, ".gpg"}, ""))
if _, err := os.Stat(file); os.IsNotExist(err) { if _, err := os.Stat(file); os.IsNotExist(err) {
@ -30,7 +34,7 @@ func (s *Store) gpgDecrypt(name string) (string, error) {
return string(output[:len(output)-1]), nil return string(output[:len(output)-1]), nil
} }
func (s *Store) gpgEncrypt(pw string) ([]byte, error) { func (s *Store) nativeEncrypt(pw string) ([]byte, error) {
if s.Id == "" { if s.Id == "" {
return nil, fmt.Errorf("No ID") return nil, fmt.Errorf("No ID")
} }
@ -51,7 +55,7 @@ var (
cachedIdentities []string cachedIdentities []string
) )
func gpgIdentities() ([]string, error) { func nativeIdentities() ([]string, error) {
idMux.Lock() idMux.Lock()
defer idMux.Unlock() defer idMux.Unlock()
if cachedIdentities != nil { if cachedIdentities != nil {
@ -146,6 +150,34 @@ func setAgentInfo() {
} }
func GPGPrompt(keys []openpgp.Key, symmetric bool) ([]byte, error) {
conn, err := gpgagent.NewGpgAgentConn()
if err != nil {
return nil, err
}
defer conn.Close()
for _, key := range keys {
cacheId := strings.ToUpper(hex.EncodeToString(key.PublicKey.Fingerprint[:]))
request := gpgagent.PassphraseRequest{CacheKey: cacheId}
for i := 0; i < 3; i++ {
var passphrase string
passphrase, err = conn.GetPassphrase(&request)
if err != nil {
continue
}
err = key.PrivateKey.Decrypt([]byte(passphrase))
if err != nil {
conn.RemoveFromCache(cacheId)
continue
}
return []byte(passphrase), nil
}
return nil, err
}
return nil, fmt.Errorf("Unable to find key")
}
func init() { func init() {
setAgentInfo() setAgentInfo()
go func() { go func() {

View File

@ -1,12 +0,0 @@
//+build !darwin
package passgo
import (
"log"
)
//Clip copies a string to the clipboard
func Clip(x string) {
log(Info, "Clipboard not implemented for this platform")
}

52
main.go
View File

@ -2,7 +2,6 @@ package passgo
import ( import (
"bufio" "bufio"
"encoding/hex"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -14,8 +13,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/jcmdev0/gpgagent"
"golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor" "golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet" "golang.org/x/crypto/openpgp/packet"
@ -26,12 +23,12 @@ var (
ask openpgp.PromptFunction ask openpgp.PromptFunction
Keyring openpgp.KeyRing Keyring openpgp.KeyRing
krTime time.Time krTime time.Time
useGPG bool useNative bool
homeDir string homeDir string
) )
func init() { func init() {
useGPG = true // default unless we can get the Go openpgp code to work useNative = true // default unless we can get the Go openpgp code to work
basename = regexp.MustCompile(".gpg$") basename = regexp.MustCompile(".gpg$")
} }
@ -85,7 +82,7 @@ func getKeyring() error {
//return fmt.Errorf("Can't open gnupg keyring.") //return fmt.Errorf("Can't open gnupg keyring.")
return nil return nil
} }
useGPG = false useNative = false
Keyring = kr Keyring = kr
krTime = time.Now() krTime = time.Now()
return nil return nil
@ -219,38 +216,9 @@ func AskPass(prompts ...func() []byte) openpgp.PromptFunction {
return ret return ret
} }
//https://github.com/jcmdev0/gpgagent/blob/master/example/example.go
func GPGPrompt(keys []openpgp.Key, symmetric bool) ([]byte, error) {
conn, err := gpgagent.NewGpgAgentConn()
if err != nil {
return nil, err
}
defer conn.Close()
for _, key := range keys {
cacheId := strings.ToUpper(hex.EncodeToString(key.PublicKey.Fingerprint[:]))
request := gpgagent.PassphraseRequest{CacheKey: cacheId}
for i := 0; i < 3; i++ {
var passphrase string
passphrase, err = conn.GetPassphrase(&request)
if err != nil {
continue
}
err = key.PrivateKey.Decrypt([]byte(passphrase))
if err != nil {
conn.RemoveFromCache(cacheId)
continue
}
return []byte(passphrase), nil
}
return nil, err
}
return nil, fmt.Errorf("Unable to find key")
}
func (s *Store) Decrypt(name string, prompts ...func() []byte) (string, error) { func (s *Store) Decrypt(name string, prompts ...func() []byte) (string, error) {
if useGPG { if useNative {
return s.gpgDecrypt(name) return s.nativeDecrypt(name)
} }
if ask == nil { if ask == nil {
ask = AskPass(prompts...) ask = AskPass(prompts...)
@ -295,8 +263,8 @@ func (s *Store) Decrypt(name string, prompts ...func() []byte) (string, error) {
func Identities() ([]string, error) { func Identities() ([]string, error) {
getKeyring() getKeyring()
if useGPG { if useNative {
return gpgIdentities() return nativeIdentities()
} }
ret := make([]string, 0) ret := make([]string, 0)
for _, k := range Keyring.DecryptionKeys() { for _, k := range Keyring.DecryptionKeys() {
@ -310,9 +278,9 @@ func Identities() ([]string, error) {
func (s *Store) Insert(name, value string) error { func (s *Store) Insert(name, value string) error {
var enc []byte var enc []byte
var err error var err error
//if !useGo { // golang openpgp code not implemented yet //if useNative { // golang openpgp code not implemented yet
fmt.Println("Calling gpgEncrypt") fmt.Println("Calling nativeEncrypt")
enc, err = s.gpgEncrypt(value) enc, err = s.nativeEncrypt(value)
if err != nil { if err != nil {
return err return err
} }