Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (a *App) BeforeClose(ctx context.Context) bool {
}

func (a *App) DomReady(ctx context.Context) {
systrayMgr, err := systray.NewManager(a.name, func() {
systrayMgr, err := systray.NewManager(a.name, systray.Icon, func() {
a.StartProxy()
}, func() {
a.StopProxy()
Expand Down Expand Up @@ -138,7 +138,7 @@ func (a *App) StartProxy() (err error) {
exceptionRuleMatcher := ruletree.NewRuleTree()

scriptletStore := triestore.NewTrieStore()
scriptletInjector, err := scriptlet.NewInjector(scriptletStore)
scriptletInjector, err := scriptlet.NewInjector(scriptletStore, scriptlet.Bundle)
if err != nil {
return fmt.Errorf("create scriptlets injector: %v", err)
}
Expand Down
20 changes: 11 additions & 9 deletions internal/cfg/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cfg

import (
"embed"
_ "embed"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand All @@ -15,11 +16,11 @@ var (
ConfigDir string
// DataDir is the path to the directory storing the application data.
DataDir string
// InitialConfig is the default configuration for the application.
//go:embed default-config.json
InitialConfig []byte
)

//go:embed default-config.json
var defaultConfig embed.FS

// Config stores and manages the configuration for the application.
// Although all fields are public, this is only for use by the JSON marshaller.
// All access to the Config should be done through the exported methods.
Expand Down Expand Up @@ -88,7 +89,11 @@ func init() {
}
}

func NewConfig() (*Config, error) {
func NewConfig(initialConfig []byte) (*Config, error) {
if initialConfig == nil {
return nil, errors.New("initialConfig is nil")
}

c := &Config{}

configFile := path.Join(ConfigDir, "config.json")
Expand All @@ -99,10 +104,7 @@ func NewConfig() (*Config, error) {
return nil, fmt.Errorf("failed to read config file: %v", err)
}
} else {
configData, err = defaultConfig.ReadFile("default-config.json")
if err != nil {
return nil, fmt.Errorf("failed to read default config file: %v", err)
}
configData = initialConfig
if err := os.WriteFile(configFile, configData, 0644); err != nil {
return nil, fmt.Errorf("failed to write config file: %v", err)
}
Expand Down
19 changes: 9 additions & 10 deletions internal/scriptlet/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"compress/flate"
"compress/gzip"
"embed"
_ "embed"
"errors"
"fmt"
"io"
Expand All @@ -21,8 +21,9 @@ import (
)

var (
// Bundle contains the default embedded scriptlets bundle.
//go:embed bundle.js
scriptletsBundleFS embed.FS
Bundle []byte
// reBody captures contents of the body tag in an HTML document.
reBody = regexp.MustCompile(`(?i)<body[\s\S]*?>([\s\S]*)</body>`)
scriptOpeningTag = []byte("<script>")
Expand All @@ -43,20 +44,18 @@ type Injector struct {
}

// NewInjector creates a new Injector with the embedded scriptlets.
func NewInjector(store Store) (*Injector, error) {
func NewInjector(store Store, scriptletsBundle []byte) (*Injector, error) {
if store == nil {
return nil, errors.New("store is nil")
}

bundleData, err := scriptletsBundleFS.ReadFile("bundle.js")
if err != nil {
return nil, fmt.Errorf("read bundle from embed: %w", err)
if scriptletsBundle == nil {
return nil, errors.New("scriptletsBundle is nil")
}

scriptletsElement := make([]byte, len(scriptOpeningTag)+len(bundleData)+len(scriptClosingTag))
scriptletsElement := make([]byte, len(scriptOpeningTag)+len(scriptletsBundle)+len(scriptClosingTag))
copy(scriptletsElement, scriptOpeningTag)
copy(scriptletsElement[len(scriptOpeningTag):], bundleData)
copy(scriptletsElement[len(scriptOpeningTag)+len(bundleData):], scriptClosingTag)
copy(scriptletsElement[len(scriptOpeningTag):], scriptletsBundle)
copy(scriptletsElement[len(scriptOpeningTag)+len(scriptletsBundle):], scriptClosingTag)

return &Injector{
bundle: scriptletsElement,
Expand Down
4 changes: 2 additions & 2 deletions internal/scriptlet/scriptlet_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestInjectorInternal(t *testing.T) {
t.Parallel()

spyStore := &spyScriptletStore{}
injector, err := NewInjector(spyStore)
injector, err := NewInjector(spyStore, []byte{})
if err != nil {
t.Fatalf("failed to create injector: %v", err)
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestInjectorInternal(t *testing.T) {
t.Parallel()

spyStore := &spyScriptletStore{}
injector, err := NewInjector(spyStore)
injector, err := NewInjector(spyStore, []byte{})
if err != nil {
t.Fatalf("failed to create injector: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/scriptlet/scriptlet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func newBlankHTTPResponse(t *testing.T) *http.Response {
func newInjectorWithTrieStore(t *testing.T) *scriptlet.Injector {
t.Helper()
store := triestore.NewTrieStore()
injector, err := scriptlet.NewInjector(store)
injector, err := scriptlet.NewInjector(store, []byte{})
if err != nil {
t.Fatalf("failed to create injector: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion internal/systray/manager_nonwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"context"
)

var Icon = []byte{}

type Manager struct{}

func NewManager(string, func(), func()) (*Manager, error) {
func NewManager(string, []byte, func(), func()) (*Manager, error) {
return &Manager{}, nil
}

Expand Down
21 changes: 9 additions & 12 deletions internal/systray/manager_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package systray

import (
"context"
"embed"
_ "embed"
"errors"
"fmt"
"log"
"sync"

"github.com/wailsapp/wails/v2/pkg/runtime"
)

//go:embed logo.ico
var logoFS embed.FS
var Icon []byte

type Manager struct {
logoBytes []byte
icon []byte
appName string
proxyStateMu sync.Mutex
proxyActive bool
Expand All @@ -24,24 +23,22 @@ type Manager struct {
startStopMenuItem *menuItem
}

func NewManager(appName string, proxyStart func(), proxyStop func()) (*Manager, error) {
func NewManager(appName string, icon []byte, proxyStart func(), proxyStop func()) (*Manager, error) {
if appName == "" {
return nil, errors.New("appName is empty")
}
if icon == nil {
return nil, errors.New("icon is nil")
}
if proxyStart == nil {
return nil, errors.New("proxyStart is nil")
}
if proxyStop == nil {
return nil, errors.New("proxyStop is nil")
}

logoBytes, err := logoFS.ReadFile("logo.ico")
if err != nil {
return nil, fmt.Errorf("read logo from embed: %w", err)
}

return &Manager{
logoBytes: logoBytes,
icon: icon,
proxyStart: proxyStart,
proxyStop: proxyStop,
appName: appName,
Expand Down Expand Up @@ -95,7 +92,7 @@ func (m *Manager) OnProxyStopped() {

func (m *Manager) onReady(ctx context.Context) func() {
return func() {
setIcon(m.logoBytes)
setIcon(m.icon)
setTooltip(m.appName)

openMenuItem := addMenuItem("Open", "Open the application window")
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
log.Printf("failed to setup logger: %v", err)
}

config, err := cfg.NewConfig()
config, err := cfg.NewConfig(cfg.InitialConfig)
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
Expand Down
Loading