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"
)
var (
regular *sfnt.Font
confDir string
)
func init() {
var err error
regular, err = sfnt.Parse(goregular.TTF)
if err != nil {
log(Fatal, "Cannot parse default font: ", err)

View File

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

View File

@ -17,6 +17,8 @@ import (
"gioui.org/ui/measure"
"gioui.org/ui/text"
"golang.org/x/image/font/sfnt"
"github.com/fsnotify/fsnotify"
"gopkg.in/yaml.v2"
@ -74,6 +76,8 @@ func main() {
}
var (
confDir string
regular *sfnt.Font
Config conf
l []passgo.Pass
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 (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"os"
"os/exec"
@ -14,10 +15,13 @@ import (
"strconv"
"sync"
"golang.org/x/crypto/openpgp"
"github.com/jcmdev0/gpgagent"
"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")
file := path.Join(s.Dir, strings.Join([]string{name, ".gpg"}, ""))
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
}
func (s *Store) gpgEncrypt(pw string) ([]byte, error) {
func (s *Store) nativeEncrypt(pw string) ([]byte, error) {
if s.Id == "" {
return nil, fmt.Errorf("No ID")
}
@ -51,7 +55,7 @@ var (
cachedIdentities []string
)
func gpgIdentities() ([]string, error) {
func nativeIdentities() ([]string, error) {
idMux.Lock()
defer idMux.Unlock()
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() {
setAgentInfo()
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 (
"bufio"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
@ -14,8 +13,6 @@ import (
"strings"
"time"
"github.com/jcmdev0/gpgagent"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
@ -26,12 +23,12 @@ var (
ask openpgp.PromptFunction
Keyring openpgp.KeyRing
krTime time.Time
useGPG bool
useNative bool
homeDir string
)
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$")
}
@ -85,7 +82,7 @@ func getKeyring() error {
//return fmt.Errorf("Can't open gnupg keyring.")
return nil
}
useGPG = false
useNative = false
Keyring = kr
krTime = time.Now()
return nil
@ -219,38 +216,9 @@ func AskPass(prompts ...func() []byte) openpgp.PromptFunction {
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) {
if useGPG {
return s.gpgDecrypt(name)
if useNative {
return s.nativeDecrypt(name)
}
if ask == nil {
ask = AskPass(prompts...)
@ -295,8 +263,8 @@ func (s *Store) Decrypt(name string, prompts ...func() []byte) (string, error) {
func Identities() ([]string, error) {
getKeyring()
if useGPG {
return gpgIdentities()
if useNative {
return nativeIdentities()
}
ret := make([]string, 0)
for _, k := range Keyring.DecryptionKeys() {
@ -310,9 +278,9 @@ func Identities() ([]string, error) {
func (s *Store) Insert(name, value string) error {
var enc []byte
var err error
//if !useGo { // golang openpgp code not implemented yet
fmt.Println("Calling gpgEncrypt")
enc, err = s.gpgEncrypt(value)
//if useNative { // golang openpgp code not implemented yet
fmt.Println("Calling nativeEncrypt")
enc, err = s.nativeEncrypt(value)
if err != nil {
return err
}