Skip to content

Commit

Permalink
feat: add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
skynet2 committed Dec 31, 2024
1 parent 39fb75d commit 172d2a5
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 42 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.PHONY: build-debug
build-debug: build-win
@cp dist/win/BrowserSwitcherProxied.exe /mnt/i/BrowserSwitcherProxied.exe
@cp dist/win/BrowserSwitcher.exe /mnt/i/BrowserSwitcher.exe
@cp dist/win/register.ps1 /mnt/i/register.ps1
@#GOOS=windows go build -o /mnt/i/BrowserSwitcher.exe .
@#cp scripts/register.ps1 /mnt/i/register.ps1
@#cp scripts/reg.reg /mnt/i/reg.reg
Expand All @@ -23,5 +25,9 @@ build-linux:
.PHONY: build-win
build-win:
@mkdir -p dist/win
@GOOS=windows go build -buildvcs=false -o dist/win/BrowserSwitcher.exe .
@cp scripts/reg.reg dist/win/reg.reg
@go install github.com/tc-hib/go-winres@latest
@GOOS=windows go build -buildvcs=false -o dist/win/BrowserSwitcherProxied.exe cmd/switcher/main.go
@GOOS=windows go build -ldflags -H=windowsgui -buildvcs=false -o dist/win/BrowserSwitcher.exe cmd/proxy/main.go cmd/proxy/win.go
@go-winres patch --in winres/winres.json --no-backup dist/win/BrowserSwitcherProxied.exe
@go-winres patch --in winres/winres.json --no-backup dist/win/BrowserSwitcher.exe
@cp scripts/register.ps1 dist/win/register.ps1
16 changes: 16 additions & 0 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"context"

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

func main() {
logger := logger2.GetLogger()
ctx := logger.WithContext(context.Background())

if err := run(ctx); err != nil {
logger.Panic().Err(err).Msg("failed to run")
}
}
57 changes: 57 additions & 0 deletions cmd/proxy/win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"context"
"os"
"os/exec"
"slices"
"syscall"

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

func findAppRegistrationPath(ctx context.Context) (string, error) {
key, err := registry.OpenKey(
registry.CURRENT_USER,
`Software\Clients\StartMenuInternet\Browser Switcher\shell\open\command`,
registry.QUERY_VALUE,
)
if err != nil {
return "", err
}

val, _, err := key.GetStringValue("Proxied")
if err != nil {
return "", err
}

zerolog.Ctx(ctx).Debug().Msgf("found app registration path: %s", val)

return val, err
}

func run(ctx context.Context) error {
appPath, err := findAppRegistrationPath(ctx)
if err != nil {
return err
}

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

cmd := exec.Command(
"cmd.exe",
args...,
)

cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CreationFlags: 0x08000000,
}

return cmd.Start()
}
7 changes: 5 additions & 2 deletions main.go → cmd/switcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"os"
"path/filepath"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand All @@ -15,8 +16,10 @@ import (
)

func main() {
currentDir := filepath.Dir(os.Args[0])

logFile := &lumberjack.Logger{
Filename: "logs/log.log",
Filename: filepath.Join(currentDir, "logs", "log.log"),
MaxSize: 30,
MaxBackups: 3,
MaxAge: 10,
Expand All @@ -25,7 +28,7 @@ func main() {
lg := zerolog.New(zerolog.MultiLevelWriter(os.Stdout, logFile)).With().Timestamp().Logger()
log.Logger = lg

lg.Info().Msg("Starting browser-switcher")
lg.Info().Msgf("Starting browser-switcher. Args: %v", os.Args)
ctx := lg.WithContext(context.Background())

if len(os.Args) < 2 {
Expand Down
26 changes: 26 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package logger

import (
"os"
"path/filepath"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/natefinch/lumberjack.v2"
)

func GetLogger() zerolog.Logger {
currentDir := filepath.Dir(os.Args[0])

logFile := &lumberjack.Logger{
Filename: filepath.Join(currentDir, "logs", "log.log"),
MaxSize: 30,
MaxBackups: 3,
MaxAge: 10,
Compress: false,
}
lg := zerolog.New(zerolog.MultiLevelWriter(os.Stdout, logFile)).With().Timestamp().Logger()
log.Logger = lg

return lg
}
38 changes: 0 additions & 38 deletions scripts/reg.reg

This file was deleted.

102 changes: 102 additions & 0 deletions scripts/register.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
$binaryName = "BrowserSwitcher"
$appPath = "I:\$binaryName.exe"
$proxiedPath = "I:\${binaryName}Proxied.exe"

$AppLongName = "Browser Switcher"
$AppRegDescription = "Browser Switcher"
$CustomProtoName = "x-browser-switcher"

function Get-BrowserRegistrationPath {
return "HKCU:\Software\Clients\StartMenuInternet\$AppLongName"
}

function Register-Browser {
$softPath = "Software\Clients\StartMenuInternet\$AppLongName"
$appRoot = "HKCU:\Software\Clients\StartMenuInternet\$AppLongName"
$capRoot = "$appRoot\Capabilities"

New-Item -Path $appRoot -Force
New-Item -Path $capRoot -Force

# Add basic registration
Set-ItemProperty -Path $appRoot -Name "(Default)" -Value $AppLongName
Set-ItemProperty -Path $capRoot -Name "ApplicationName" -Value $AppLongName
Set-ItemProperty -Path $capRoot -Name "ApplicationDescription" -Value $AppRegDescription
Set-ItemProperty -Path $capRoot -Name "ApplicationIcon" -Value "$appPath,0"

# Supported protocols
$urlAssocPath = "$capRoot\URLAssociations"
New-Item -Path $urlAssocPath -Force
foreach ($protocol in @("https", "http", $CustomProtoName)) {
Set-ItemProperty -Path $urlAssocPath -Name $protocol -Value "BrowserSwitcherHTM"
}

# File associations
$fileAssocPath = "$capRoot\FileAssociations"
if (-not (Test-Path -Path $fileAssocPath)) {
New-Item -Path $fileAssocPath -Force
}

# Define file extensions for HTML and related files
$htmlExtensions = @(".svg", ".htm", ".html", ".shtml", ".webp", ".xht", ".xhtml", ".mht", ".mhtml", ".pdf")
foreach ($ext in $htmlExtensions) {
Set-ItemProperty -Path $fileAssocPath -Name $ext -Value "BrowserSwitcherHTM"
}

# Command registration
New-Item -Path "$appRoot\DefaultIcon" -Force
Set-ItemProperty -Path "$appRoot\DefaultIcon" -Name "(Default)" -Value "$appPath,0"

New-Item -Path "$appRoot\shell\open\command" -Force

Set-ItemProperty -Path "$appRoot\shell\open\command" -Name "(Default)" -Value "`"$appPath`""
Set-ItemProperty -Path "$appRoot\shell\open\command" -Name "Proxied" -Value "$proxiedPath"

# Register capabilities
Set-ItemProperty -Path "HKCU:\Software\RegisteredApplications" -Name $AppLongName -Value "$softPath\Capabilities"
}

function Register-Protocol {
$root = "HKCU:\Software\Classes\$CustomProtoName"

$registrationPath = @("Registry::HKEY_CLASSES_ROOT\BrowserSwitcherHTM", "Registry::HKEY_CLASSES_ROOT\BrowserSwitcherPDF", "Registry::HKEY_CURRENT_USER\Software\Classes\BrowserSwitcherHTM", "Registry::HKEY_CURRENT_USER\Software\Classes\BrowserSwitcherPDF")
foreach ($regPath in $registrationPath) {
New-Item -Path "$regPath" -Force

Set-ItemProperty -Path "$regPath" -Name "(Default)" -Value "Browser Switcher HTML Document"

New-Item -Path "$regPath\Application" -Force
New-Item -Path "$regPath\DefaultIcon" -Force

New-Item -Path "$regPath\shell" -Force
New-Item -Path "$regPath\shell\open" -Force
New-Item -Path "$regPath\shell\open\command" -Force

Set-ItemProperty -Path "$regPath\Application" -Name "ApplicationName" -Value $AppLongName
Set-ItemProperty -Path "$regPath\Application" -Name "ApplicationDescription" -Value $AppRegDescription

Set-ItemProperty -Path "$regPath\DefaultIcon" -Name "(Default)" -Value "$appPath,0"
Set-ItemProperty -Path "$regPath\shell\open\command" -Name "(Default)" -Value "`"$appPath`" `%1"
}

New-Item -Path $root -Force
Set-ItemProperty -Path $root -Name "(Default)" -Value "URL:$CustomProtoName"
Set-ItemProperty -Path $root -Name "URL Protocol" -Value ""

$commandRoot = "$root\shell\open\command"
New-Item -Path $commandRoot -Force
Set-ItemProperty -Path $commandRoot -Name "(Default)" -Value "`"$appPath`" `"%1`""
}

function Unregister-Browser {
$appRoot = Get-BrowserRegistrationPath
Remove-Item -Path $appRoot -Recurse -Force
}

function Unregister-Protocol {
$root = "HKCU:\Software\Classes\$CustomProtoName"
Remove-Item -Path $root -Recurse -Force
}

Register-Browser
Register-Protocol
Binary file added winres/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added winres/icon16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions winres/winres.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"RT_GROUP_ICON": {
"APP": {
"0000": [
"icon.png",
"icon16.png"
]
}
},
"RT_MANIFEST": {
"#1": {
"0409": {
"identity": {
"name": "",
"version": ""
},
"description": "",
"minimum-os": "win7",
"execution-level": "as invoker",
"ui-access": false,
"auto-elevate": false,
"dpi-awareness": "system",
"disable-theming": false,
"disable-window-filtering": false,
"high-resolution-scrolling-aware": false,
"ultra-high-resolution-scrolling-aware": false,
"long-path-aware": false,
"printer-driver-isolation": false,
"gdi-scaling": false,
"segment-heap": false,
"use-common-controls-v6": false
}
}
},
"RT_VERSION": {
"#1": {
"0000": {
"fixed": {
"file_version": "0.0.0.0",
"product_version": "0.0.0.0"
},
"info": {
"0409": {
"Comments": "",
"CompanyName": "",
"FileDescription": "",
"FileVersion": "",
"InternalName": "",
"LegalCopyright": "",
"LegalTrademarks": "",
"OriginalFilename": "",
"PrivateBuild": "",
"ProductName": "",
"ProductVersion": "",
"SpecialBuild": ""
}
}
}
}
}
}

0 comments on commit 172d2a5

Please sign in to comment.