Skip to content

Commit

Permalink
Fixing reported windows bugs (#164)
Browse files Browse the repository at this point in the history
* Use new launcher matching on windows

* Default to debug logging on windows

* Make file regex case-insensitive and Windows-compatible

* Display web app url on windows

* Use appdata for config and data folders

* Partial fix for pn532 on windows
  • Loading branch information
wizzomafizzo authored Jan 3, 2025
1 parent 8cf4263 commit b048be7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 58 deletions.
2 changes: 2 additions & 0 deletions cmd/windows/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func main() {
flags.Pre(pl)

defaults := config.BaseDefaults
defaults.DebugLogging = true
iniPath := filepath.Join(utils.ExeDir(), "tapto.ini")
if migrate.Required(iniPath, filepath.Join(pl.ConfigDir(), config.CfgFile)) {
migrated, err := migrate.IniToToml(iniPath)
Expand Down Expand Up @@ -105,6 +106,7 @@ func main() {
fmt.Println("Device address: Unknown")
} else {
fmt.Println("Device address:", ip.String())
fmt.Printf("Web App: http://%s:%d/app/\n", ip.String(), cfg.ApiPort())
}

fmt.Println("Press any key to exit")
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
)
Expand Down Expand Up @@ -189,6 +190,15 @@ func (c *Instance) Load() error {
// prepare allow files regexes
c.vals.Launchers.allowFileRe = make([]*regexp.Regexp, len(c.vals.Launchers.AllowFile))
for i, allowFile := range c.vals.Launchers.AllowFile {
if runtime.GOOS == "windows" {
// make regex case-insensitive, if not already
if !strings.HasPrefix(allowFile, "(?i)") {
allowFile = "(?i)" + allowFile
}
// replace forward slashes with backslashes
allowFile = strings.ReplaceAll(allowFile, "/", "\\\\")
}

re, err := regexp.Compile(allowFile)
if err != nil {
log.Warn().Msgf("invalid allow file regex: %s", allowFile)
Expand Down
65 changes: 18 additions & 47 deletions pkg/platforms/windows/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ZaparooProject/zaparoo-core/pkg/config"
"github.com/ZaparooProject/zaparoo-core/pkg/service/tokens"
"github.com/ZaparooProject/zaparoo-core/pkg/utils"
"github.com/adrg/xdg"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -42,6 +43,16 @@ func (p *Platform) SupportedReaders(cfg *config.Instance) []readers.Reader {
}

func (p *Platform) StartPre(_ *config.Instance) error {
err := os.MkdirAll(filepath.Join(xdg.ConfigHome, config.AppName), 0755)
if err != nil {
return err
}

err = os.MkdirAll(filepath.Join(xdg.DataHome, config.AppName), 0755)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -70,16 +81,15 @@ func (p *Platform) ZipsAsDirs() bool {
}

func (p *Platform) DataDir() string {
// TODO: this could be AppData instead
return utils.ExeDir()
return filepath.Join(xdg.DataHome, config.AppName)
}

func (p *Platform) LogDir() string {
return utils.ExeDir()
return filepath.Join(xdg.DataHome, config.AppName)
}

func (p *Platform) ConfigDir() string {
return utils.ExeDir()
return filepath.Join(xdg.ConfigHome, config.AppName)
}

func (p *Platform) TempDir() string {
Expand Down Expand Up @@ -132,53 +142,14 @@ func (p *Platform) LaunchSystem(cfg *config.Instance, id string) error {
func (p *Platform) LaunchFile(cfg *config.Instance, path string) error {
log.Info().Msgf("launching file: %s", path)

launchers := make([]platforms.Launcher, 0)
lp := strings.ToLower(path)

// TODO: move to matchsystemfile
for _, l := range p.Launchers() {
match := false

// check for global extensions
for _, ext := range l.Extensions {
if filepath.Ext(lp) == ext && l.Folders == nil {
launchers = append(launchers, l)
match = true
break
}
}
if match {
continue
}

// check for scheme
for _, scheme := range l.Schemes {
if strings.HasPrefix(lp, scheme+"://") {
launchers = append(launchers, l)
break
}
}
}
launchers := utils.PathToLaunchers(cfg, p, path)

if len(launchers) == 0 {
return errors.New("no launcher found for file")
return errors.New("no launcher found")
}

l := launchers[0]

if l.Launch != nil {
if l.AllowListOnly {
if cfg.IsLauncherFileAllowed(path) {
return l.Launch(cfg, path)
} else {
return errors.New("file not in allow list: " + path)
}
}

return l.Launch(cfg, path)
}

return nil
// just pick the first one for now
return launchers[0].Launch(cfg, path)
}

func (p *Platform) KeyboardInput(input string) error {
Expand Down
29 changes: 21 additions & 8 deletions pkg/readers/pn532_uart/ndef.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"github.com/rs/zerolog/log"

"github.com/hsanjuan/go-ndef"
)
Expand All @@ -38,6 +39,15 @@ func ParseRecordText(blocks []byte) (string, error) {
return "", fmt.Errorf("NDEF start not found: %x", blocks)
}

// check if there is another ndef start left, as it can mean we got come
// corrupt data at the beginning
if len(blocks) > startIndex+8 {
nextStart := bytes.Index(blocks[startIndex+4:], NdefStart)
if nextStart != -1 {
startIndex += nextStart
}
}

endIndex := bytes.Index(blocks, NdefEnd)
if endIndex == -1 {
return "", fmt.Errorf("NDEF end not found: %x", blocks)
Expand All @@ -51,17 +61,20 @@ func ParseRecordText(blocks []byte) (string, error) {
return "", fmt.Errorf("end index out of bounds: %d, %x", endIndex, blocks)
}

log.Debug().Msgf("NDEF start: %d, end: %d", startIndex, endIndex)
log.Debug().Msgf("NDEF: %x", blocks[startIndex:endIndex])

tagText := string(blocks[startIndex+4 : endIndex])

// TODO: why does this happen here but not in libnfc?
cleaned := ""
for _, r := range tagText {
if r != '\x00' {
cleaned += string(r)
}
}

return cleaned, nil
//cleaned := ""
//for _, r := range tagText {
// if r != '\x00' {
// cleaned += string(r)
// }
//}

return tagText, nil
}

func BuildMessage(text string) ([]byte, error) {
Expand Down
23 changes: 20 additions & 3 deletions pkg/readers/pn532_uart/pn532_uart.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,27 @@ func (r *Pn532UartReader) Open(device string, iq chan<- readers.Scan) error {
}

i := 3
retryMax := 3
retry := 0
data := make([]byte, 0)
for {
// TODO: this is a random limit i picked, should detect blocks in card
if i >= 256 {
break
}

if retry >= retryMax {
errCount++
break
}

res, err := InDataExchange(r.port, []byte{0x30, byte(i)})
if err != nil {
if errors.Is(err, ErrNoFrameFound) {
// sometimes the response just doesn't work, try again
log.Warn().Msg("no frame found")
retry++
continue
} else if err != nil {
log.Error().Err(err).Msg("failed to run indataexchange")
errCount++
break
Expand All @@ -176,15 +188,20 @@ func (r *Pn532UartReader) Open(device string, iq chan<- readers.Scan) error {
break
} else if res[0] != 0x41 || res[1] != 0x00 {
log.Warn().Msgf("unexpected data format: %x", res)
break
// sometimes we receive the result of the last passive
// target command, so just try request again a few times
retry++
continue
} else if bytes.Equal(res[2:], make([]byte, 16)) {
break
}

data = append(data, res[2:]...)
i += 4

time.Sleep(6 * time.Millisecond) // TODO: needs adjusting to a smaller safe value
retry = 0

time.Sleep(1 * time.Millisecond)
}

log.Debug().Msgf("record bytes: %s", hex.EncodeToString(data))
Expand Down

0 comments on commit b048be7

Please sign in to comment.