Skip to content

Commit

Permalink
feat: escape support (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
skynet2 authored Jan 2, 2025
1 parent b70e4c6 commit ea8ca7c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"context"
"os"

"github.com/rs/zerolog"

logger2 "github.com/ft-t/browser-switcher/pkg/logger"
)
Expand All @@ -10,6 +13,8 @@ func main() {
logger := logger2.GetLogger()
ctx := logger.WithContext(context.Background())

zerolog.Ctx(ctx).Debug().Msgf("starting proxy with arguments: %v", os.Args)

if err := run(ctx); err != nil {
logger.Panic().Err(err).Msg("failed to run")
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/proxy/win.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/rs/zerolog"
"golang.org/x/sys/windows/registry"

"github.com/ft-t/browser-switcher/pkg/escaper"
)

func findAppRegistrationPath(ctx context.Context) (string, error) {
Expand Down Expand Up @@ -39,11 +41,18 @@ func run(ctx context.Context) error {
return err
}

var escapedArgs []string
for _, arg := range os.Args[1:] {
escapedArgs = append(escapedArgs, escaper.Escape(arg))
}

args := slices.Concat([]string{
"/C",
"start",
appPath,
}, os.Args[1:])
}, escapedArgs)

zerolog.Ctx(ctx).Debug().Msgf("running command: cmd.exe %v", escapedArgs)

cmd := exec.Command(
"cmd.exe",
Expand Down
6 changes: 5 additions & 1 deletion cmd/switcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gopkg.in/natefinch/lumberjack.v2"

config2 "github.com/ft-t/browser-switcher/pkg/config"
"github.com/ft-t/browser-switcher/pkg/escaper"
"github.com/ft-t/browser-switcher/pkg/launcher"
"github.com/ft-t/browser-switcher/pkg/selector"
"github.com/ft-t/browser-switcher/pkg/ui"
Expand Down Expand Up @@ -37,8 +38,11 @@ func main() {
}

targetURL := os.Args[1]
targetURL = escaper.Unescape(targetURL)

ctx = lg.With().Str("targetURL", targetURL).Logger().WithContext(ctx)
ctx = lg.With().Str("rawTargetURL", os.Args[1]).
Str("targetURL", targetURL).
Logger().WithContext(ctx)

browserConfig, err := config2.ReadConfig(ctx)

Expand Down
20 changes: 20 additions & 0 deletions pkg/escaper/escape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package escaper

import (
"fmt"
"strings"
)

var charsToEscape = []string{
"&",
}

func Escape(
input string,
) string {
for _, char := range charsToEscape {
input = strings.ReplaceAll(input, char, fmt.Sprintf(`"%s"`, char))
}

return input
}
16 changes: 16 additions & 0 deletions pkg/escaper/unescape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package escaper

import (
"fmt"
"strings"
)

func Unescape(
input string,
) string {
for _, char := range charsToEscape {
input = strings.ReplaceAll(input, fmt.Sprintf(`"%s"`, char), char)
}

return input
}

0 comments on commit ea8ca7c

Please sign in to comment.