Skip to content

Commit

Permalink
feature: modify windows registry via golang
Browse files Browse the repository at this point in the history
  • Loading branch information
yuweizzz committed Jul 28, 2024
1 parent 841ff85 commit 405b669
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ SIDECAR_DIR := $(CURDIR)/cmd/$(SIDECAR)

GOFILE := $(shell find . -name "*.go" | xargs)

default: build_linux

.PHONY: lint
lint:
gofmt -w $(GOFILE)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Sidecar is a https proxy server based on MITM.
- [x] Provide cmd tool
- [x] Support PAC
- [x] Support Custom DNS
- [ ] Support HTTP2
~~ - [ ] Support HTTP2 ~~
- [ ] Refactoring

## Get Started
Expand Down
4 changes: 4 additions & 0 deletions cmd/sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func main() {
runClient(cfg)
}
case "stop":
// windows UnsetRegistry
sidecar.UnsetRegistry()
sidecar.StopDaemonProcess(cfg.Client.WorkDir)
default:
help(filename)
Expand Down Expand Up @@ -121,6 +123,8 @@ func runClient(cfg *sidecar.Config) {
go mitm.Run()
}
sidecar.Info("Now Server is run as a Client.")
// windows SetRegistry
sidecar.SetRegistry(cfg.Client.ProxyPort)
daemon.WatchSignal()
}

Expand Down
6 changes: 6 additions & 0 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,9 @@ func StopDaemonProcess(workDir string) {
Error("Now sidecar.lock is not exist, server is stopped")
}
}

// do nothing
func SetRegistry(port int) {}

// do nothing
func UnsetRegistry() {}
98 changes: 98 additions & 0 deletions daemon_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sidecar

import (
"bytes"
"crypto/rsa"
"crypto/x509"
"fmt"
Expand All @@ -10,6 +11,8 @@ import (
"os/signal"
"strconv"
"syscall"

"golang.org/x/sys/windows/registry"
)

type Daemon struct {
Expand Down Expand Up @@ -163,3 +166,98 @@ func StopDaemonProcess(workDir string) {
Error("Now sidecar.lock is not exist, server is stopped")
}
}

func SetRegistry(port int) {
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections`, registry.ALL_ACCESS)
defer key.Close()
if err != nil {
panic(err)
}

// ref:
// (!$EnableProxy) -and (!$EnablePAC) -and (!$EnableAuto)
// ProxyOptions = "01"
//
// ($EnableProxy) -and (!$EnablePAC) -and (!$EnableAuto)
// ProxyOptions = "03"
//
// (!$EnableProxy) -and ($EnablePAC) -and (!$EnableAuto)
// ProxyOptions = "05"
//
// ($EnableProxy) -and ($EnablePAC) -and (!$EnableAuto)
// ProxyOptions = "07"
//
// (!$EnableProxy) -and (!$EnablePAC) -and ($EnableAuto)
// ProxyOptions = "09"
//
// ($EnableProxy) -and (!$EnablePAC) -and ($EnableAuto)
// ProxyOptions = "11"
//
// (!$EnableProxy) -and ($EnablePAC) -and ($EnableAuto)
// ProxyOptions = "13"
//
// ($EnableProxy) -and ($EnablePAC) -and ($EnableAuto)
// ProxyOptions = "15"
//
// $DefaultConnectionSettings = [byte[]]@(@(70, 0, 0, 0)
// + @($Revision, 0, 0, 0)
// + @($ProxyOptions, 0, 0, 0)
// + @($ProxyBytes.Length, 0, 0, 0) + $ProxyBytes
// + @($BypassBytes.Length, 0, 0, 0) + $BypassBytes
// + @($PacBytes.Length, 0, 0, 0) + $PacBytes
// + @(1..32 | % { 0 }))

raw, _, _ := key.GetBinaryValue("DefaultConnectionSettings")
newBytes := &bytes.Buffer{}
newBytes.Write(raw[0:8])
// ProxyOptions
newBytes.WriteByte(byte(0x03))
// {0x00, 0x00, 0x00}
newBytes.Write(raw[1:4])

serverStr := "127.0.0.1:" + strconv.Itoa(port)
// ProxyBytes.Length
newBytes.WriteByte(byte(len(serverStr)))
newBytes.Write(raw[1:4])
// ProxyBytes
newBytes.WriteString(serverStr)

overrideStr := `localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*;<local>`
// BypassBytes.Length
newBytes.WriteByte(byte(len(overrideStr)))
newBytes.Write(raw[1:4])
// BypassBytes
newBytes.WriteString(overrideStr)

// PacBytes.Length
newBytes.WriteByte(byte(0))
newBytes.Write(raw[1:4])

newBytes.Write([]byte{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
})

err = key.SetBinaryValue("DefaultConnectionSettings", newBytes.Bytes())
if err != nil {
panic(err)
}
}

func UnsetRegistry() {
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections`, registry.ALL_ACCESS)
defer key.Close()
if err != nil {
panic(err)
}

raw, _, _ := key.GetBinaryValue("DefaultConnectionSettings")
raw[8] = 0x01

err = key.SetBinaryValue("DefaultConnectionSettings", raw)
if err != nil {
panic(err)
}
}
10 changes: 5 additions & 5 deletions scripts/windows/start.bat
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@echo off

set PROXYSERVER=127.0.0.1:4396
set PROXYOVERRIDE=localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*
:: set PROXYSERVER=127.0.0.1:4396
:: set PROXYOVERRIDE=localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d %PROXYSERVER% /f > NUL
:: reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d %PROXYSERVER% /f > NUL

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride /t REG_SZ /d %PROXYOVERRIDE% /f > NUL
:: reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride /t REG_SZ /d %PROXYOVERRIDE% /f > NUL

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f > NUL
:: reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f > NUL

cmd /c start %cd%/sidecar.exe client -action start

Expand Down
2 changes: 1 addition & 1 deletion scripts/windows/stop.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f > NUL
:: reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f > NUL

cmd /c %cd%/sidecar.exe client -action stop

Expand Down

0 comments on commit 405b669

Please sign in to comment.