diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 1280947..3f45572 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -1,48 +1,48 @@ -name: Build and Release -run-name: ${{ gitea.actor }} build 🚀 - -on: - push: - branches: - - master -jobs: - release: - runs-on: ubuntu-latest - container: - image: azenkain/go-node:latest - - steps: - - uses: actions/checkout@v4 - - - name: Download Go dependencies - run: go mod download - - - name: Build for Windows x64 - run: GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-windows-amd64.exe . - - - name: Build for Windows ARM64 - run: GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-windows-arm64.exe . - - - name: Build for macOS x64 - run: GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-macos-amd64 . - - - name: Build for macOS ARM64 - run: GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-macos-arm64 . - - - name: Build for Linux x64 - run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-linux-amd64 . - - - name: Build for Linux ARM64 - run: GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-linux-arm64 . - - - name: Grant execute permissions - run: | - chmod +x ./script/publish/publish-script - - - name: Publish components to API - run: ./script/publish/publish-script - env: - ENV_ROBOT_TOKEN: ${{ secrets.ENV_ROBOT_TOKEN }} - ENV_GAME_IDS: ${{ secrets.ENV_GAME_IDS }} - ENV_COMPONENT_TYPE: PROXY - ENV_FILES: "firefly-go-proxy-windows-amd64.exe,firefly-go-proxy-windows-arm64.exe,firefly-go-proxy-macos-amd64,firefly-go-proxy-macos-arm64,firefly-go-proxy-linux-amd64,firefly-go-proxy-linux-arm64" +name: Build and Release +run-name: ${{ gitea.actor }} build 🚀 + +on: + push: + branches: + - master +jobs: + release: + runs-on: ubuntu-latest + container: + image: azenkain/go-node:latest + + steps: + - uses: actions/checkout@v4 + + - name: Download Go dependencies + run: go mod download + + - name: Build for Windows x64 + run: GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-windows-amd64.exe . + + - name: Build for Windows ARM64 + run: GOOS=windows GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-windows-arm64.exe . + + - name: Build for macOS x64 + run: GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-macos-amd64 . + + - name: Build for macOS ARM64 + run: GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-macos-arm64 . + + - name: Build for Linux x64 + run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-linux-amd64 . + + - name: Build for Linux ARM64 + run: GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o firefly-go-proxy-linux-arm64 . + + - name: Grant execute permissions + run: | + chmod +x ./script/publish/publish-script + + - name: Publish components to API + run: ./script/publish/publish-script + env: + ENV_ROBOT_TOKEN: ${{ secrets.ENV_ROBOT_TOKEN }} + ENV_GAME_IDS: ${{ secrets.ENV_GAME_IDS }} + ENV_COMPONENT_TYPE: PROXY + ENV_FILES: "firefly-go-proxy-windows-amd64.exe,firefly-go-proxy-windows-arm64.exe,firefly-go-proxy-macos-amd64,firefly-go-proxy-macos-arm64,firefly-go-proxy-linux-amd64,firefly-go-proxy-linux-arm64" diff --git a/LICENSE b/LICENSE index 964aa69..cd58ef1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,18 +1,18 @@ -MIT License - -Copyright (c) 2025 Firefly-Shelter - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and -associated documentation files (the "Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the -following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial -portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT -LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO -EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -USE OR OTHER DEALINGS IN THE SOFTWARE. +MIT License + +Copyright (c) 2025 Firefly-Shelter + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile index 058b779..050f44b 100644 --- a/Makefile +++ b/Makefile @@ -25,9 +25,9 @@ build_mac_arm64: build_ico: @echo Building application icon... magick logo.jpg -define icon:auto-resize=256,128,64,48,32,16 ./logo.ico - @echo Done! - -set_logo: - @echo Embedding application icon... - go-winres simply --icon ./logo.ico + @echo Done! + +set_logo: + @echo Embedding application icon... + go-winres simply --icon ./logo.ico @echo Done! diff --git a/README.md b/README.md index 23a2d27..7813770 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,42 @@ -# Firefly Go Proxy - -A lightweight HTTP/HTTPS proxy server with domain redirection and request blocking capabilities. This tool is designed to help with local development and testing by intercepting and modifying HTTP/HTTPS traffic. - -## Features - -- HTTP/HTTPS proxy with MITM support -- Domain-based request redirection -- URL pattern blocking -- Automatic certificate management -- Cross-platform support (Windows, macOS, Linux) +# Firefly Go Proxy + +A lightweight HTTP/HTTPS proxy server with domain redirection and request blocking capabilities. This tool is designed to help with local development and testing by intercepting and modifying HTTP/HTTPS traffic. + +## Features + +- HTTP/HTTPS proxy with MITM support +- Domain-based request redirection +- URL pattern blocking +- Automatic certificate management +- Cross-platform support (Windows, macOS, Linux) - System proxy configuration - Automatic admin prompt on macOS/Linux for certificate/proxy setup ## Installation - -### Prerequisites - -- Go 1.22 or higher -- Git - -### Building from source - -```bash -cd firefly-go-proxy -go build -``` - -## Usage - -### Basic usage - -```bash -./firefly-proxy [flags] //linux|macos -./firefly-proxy.exe [flags] //windows -``` - -### Available Flags - + +### Prerequisites + +- Go 1.22 or higher +- Git + +### Building from source + +```bash +cd firefly-go-proxy +go build +``` + +## Usage + +### Basic usage + +```bash +./firefly-proxy [flags] //linux|macos +./firefly-proxy.exe [flags] //windows +``` + +### Available Flags + - `-r`: Redirect target host (default: "127.0.0.1:21000") - `-b`: Comma-separated list of blocked ports - `-p`: Proxy listen port (default: auto) @@ -44,25 +44,25 @@ go build - `-no-sys`: Run only the proxy server; skip certificate installation, system proxy setup, and macOS/Linux admin relaunch ### Examples - -1. Start proxy with default settings: - ```bash - ./firefly-proxy //linux|macos - ./firefly-proxy.exe //windows - ``` - -2. Redirect traffic to a different host: - ```bash - ./firefly-proxy -r 192.168.1.100:8080 //linux|macos - ./firefly-proxy.exe -r 192.168.1.100:8080 //windows - ``` - -3. Block specific ports: - ```bash - ./firefly-proxy -b "80,443,8080" //linux|macos - ./firefly-proxy.exe -b "80,443,8080" //windows - ``` - + +1. Start proxy with default settings: + ```bash + ./firefly-proxy //linux|macos + ./firefly-proxy.exe //windows + ``` + +2. Redirect traffic to a different host: + ```bash + ./firefly-proxy -r 192.168.1.100:8080 //linux|macos + ./firefly-proxy.exe -r 192.168.1.100:8080 //windows + ``` + +3. Block specific ports: + ```bash + ./firefly-proxy -b "80,443,8080" //linux|macos + ./firefly-proxy.exe -b "80,443,8080" //windows + ``` + 4. Run an executable with admin privileges: ```bash ./firefly-proxy -e "/path/to/your/executable" //linux|macos @@ -82,17 +82,17 @@ go build ./firefly-proxy -no-sys -p 8888 //linux|macos ./firefly-proxy.exe -no-sys -p 8888 //windows ``` - -## How it works - -The proxy intercepts HTTP/HTTPS traffic and can: -- Redirect requests based on domain names -- Block specific URLs or patterns -- Handle SSL/TLS connections with custom CA certificates -- Automatically configure system proxy settings - -## License - -MIT License - - + +## How it works + +The proxy intercepts HTTP/HTTPS traffic and can: +- Redirect requests based on domain names +- Block specific URLs or patterns +- Handle SSL/TLS connections with custom CA certificates +- Automatically configure system proxy settings + +## License + +MIT License + + diff --git a/cache.go b/cache.go index 4ad8ef9..8cc76d2 100644 --- a/cache.go +++ b/cache.go @@ -1,37 +1,37 @@ -package main - -import ( - "crypto/tls" - "sync" -) - -type CertStorage struct { - certs map[string]*tls.Certificate - mtx sync.RWMutex -} - -func (cs *CertStorage) Fetch(hostname string, gen func() (*tls.Certificate, error)) (*tls.Certificate, error) { - cs.mtx.RLock() - cert, ok := cs.certs[hostname] - cs.mtx.RUnlock() - if ok { - return cert, nil - } - - cert, err := gen() - if err != nil { - return nil, err - } - - cs.mtx.Lock() - cs.certs[hostname] = cert - cs.mtx.Unlock() - - return cert, nil -} - -func NewCertStorage() *CertStorage { - return &CertStorage{ - certs: make(map[string]*tls.Certificate), - } +package main + +import ( + "crypto/tls" + "sync" +) + +type CertStorage struct { + certs map[string]*tls.Certificate + mtx sync.RWMutex +} + +func (cs *CertStorage) Fetch(hostname string, gen func() (*tls.Certificate, error)) (*tls.Certificate, error) { + cs.mtx.RLock() + cert, ok := cs.certs[hostname] + cs.mtx.RUnlock() + if ok { + return cert, nil + } + + cert, err := gen() + if err != nil { + return nil, err + } + + cs.mtx.Lock() + cs.certs[hostname] = cert + cs.mtx.Unlock() + + return cert, nil +} + +func NewCertStorage() *CertStorage { + return &CertStorage{ + certs: make(map[string]*tls.Certificate), + } } \ No newline at end of file diff --git a/cert_linux.go b/cert_linux.go index b83b5ea..1566641 100644 --- a/cert_linux.go +++ b/cert_linux.go @@ -1,62 +1,62 @@ -//go:build linux -// +build linux - -package main - -import ( - "fmt" - "os" - "os/exec" - "path/filepath" - "strings" -) - -func installCA(absPath string) error { - // Detect distro - data, err := os.ReadFile("/etc/os-release") - if err != nil { - return fmt.Errorf("cannot detect distro: %v", err) - } - content := string(data) - - // Debian/Ubuntu/Kali - if strings.Contains(content, "ID=debian") || - strings.Contains(content, "ID=ubuntu") || - strings.Contains(content, "ID=kali") { - - destDir := "/usr/local/share/ca-certificates" - if err := os.MkdirAll(destDir, 0755); err != nil { - return fmt.Errorf("failed to create cert dir: %v", err) - } - - filename := filepath.Base(absPath) - destPath := filepath.Join(destDir, filename) - - inputData, err := os.ReadFile(absPath) - if err != nil { - return fmt.Errorf("failed to read source file: %v", err) - } - if err := os.WriteFile(destPath, inputData, 0644); err != nil { - return fmt.Errorf("failed to write cert file to system: %v", err) - } - - fmt.Printf("Updating certificates for Debian/Ubuntu...\n") - cmd := exec.Command("update-ca-certificates") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - return cmd.Run() - } - - // Arch / Manjaro - if strings.Contains(content, "ID=arch") || - strings.Contains(content, "ID=manjaro") { - - cmd := exec.Command("trust", "anchor", "--store", absPath) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() - } - - return fmt.Errorf("unsupported Linux distribution") -} +//go:build linux +// +build linux + +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" +) + +func installCA(absPath string) error { + // Detect distro + data, err := os.ReadFile("/etc/os-release") + if err != nil { + return fmt.Errorf("cannot detect distro: %v", err) + } + content := string(data) + + // Debian/Ubuntu/Kali + if strings.Contains(content, "ID=debian") || + strings.Contains(content, "ID=ubuntu") || + strings.Contains(content, "ID=kali") { + + destDir := "/usr/local/share/ca-certificates" + if err := os.MkdirAll(destDir, 0755); err != nil { + return fmt.Errorf("failed to create cert dir: %v", err) + } + + filename := filepath.Base(absPath) + destPath := filepath.Join(destDir, filename) + + inputData, err := os.ReadFile(absPath) + if err != nil { + return fmt.Errorf("failed to read source file: %v", err) + } + if err := os.WriteFile(destPath, inputData, 0644); err != nil { + return fmt.Errorf("failed to write cert file to system: %v", err) + } + + fmt.Printf("Updating certificates for Debian/Ubuntu...\n") + cmd := exec.Command("update-ca-certificates") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd.Run() + } + + // Arch / Manjaro + if strings.Contains(content, "ID=arch") || + strings.Contains(content, "ID=manjaro") { + + cmd := exec.Command("trust", "anchor", "--store", absPath) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() + } + + return fmt.Errorf("unsupported Linux distribution") +} diff --git a/cert_other.go b/cert_other.go index b1e8d1c..f69c30b 100644 --- a/cert_other.go +++ b/cert_other.go @@ -1,7 +1,7 @@ -//go:build !windows && !darwin && !linux - -package main - -func installCA(certPath string) error { - return nil -} +//go:build !windows && !darwin && !linux + +package main + +func installCA(certPath string) error { + return nil +} diff --git a/cert_windows.go b/cert_windows.go index 0fe6705..85bab46 100644 --- a/cert_windows.go +++ b/cert_windows.go @@ -1,17 +1,17 @@ -//go:build windows -// +build windows - -package main - -import ( - "os/exec" -) - -func installCA(absPath string) error { - cmd := exec.Command("certutil", "-addstore", "-user", "root", absPath) - if err := cmd.Run(); err != nil { - return err - } - - return nil -} +//go:build windows +// +build windows + +package main + +import ( + "os/exec" +) + +func installCA(absPath string) error { + cmd := exec.Command("certutil", "-addstore", "-user", "root", absPath) + if err := cmd.Run(); err != nil { + return err + } + + return nil +} diff --git a/log.go b/log.go index d6f034e..66e6fb2 100644 --- a/log.go +++ b/log.go @@ -1,17 +1,17 @@ -package main - -import ( - "os" - - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" -) - -func init() { - output := zerolog.ConsoleWriter{ - Out: os.Stdout, - PartsOrder: []string{"level", "message"}, - } - - log.Logger = zerolog.New(output).With().Logger() -} +package main + +import ( + "os" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func init() { + output := zerolog.ConsoleWriter{ + Out: os.Stdout, + PartsOrder: []string{"level", "message"}, + } + + log.Logger = zerolog.New(output).With().Logger() +} diff --git a/main.go b/main.go index 6309735..aae3883 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,13 @@ package main import ( "context" + "crypto/tls" "flag" "fmt" "io" "log" "net/http" + "net/url" "os" "os/signal" "strings" @@ -32,8 +34,17 @@ func rawQueryFromRequestURI(requestURI string) string { return rawQuery } +func parseRedirect(r string) (scheme, host string) { + if strings.Contains(r, "://") { + if u, err := url.Parse(r); err == nil && u.Host != "" { + return u.Scheme, u.Host + } + } + return "http", r +} + func main() { - redirectHost := flag.String("r", "127.0.0.1:21000", "redirect target host") + redirectHost := flag.String("r", "127.0.0.1:21000", "redirect target (host:port or full URL)") blockedStr := flag.String("b", "", "comma separated list of blocked ports") proxyPort := flag.Int("p", 0, "proxy listen port (default: auto)") exePath := flag.String("e", "", "path to the executable") @@ -41,6 +52,8 @@ func main() { noSys := flag.Bool("no-sys", false, "skip certificate installation and system proxy setup") flag.Parse() + redirectScheme, redirectTarget := parseRedirect(*redirectHost) + if !*noSys { relaunched, err := relaunchWithAdminIfNeeded() if err != nil { @@ -119,6 +132,7 @@ func main() { MaxIdleConnsPerHost: 100, IdleConnTimeout: 90 * time.Second, DisableCompression: false, + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } proxy.CertStore = NewCertStorage() proxy.OnRequest().HandleConnect(customAlwaysMitm) @@ -171,8 +185,9 @@ func main() { Str("raw_query", rawQuery). Msg("Force redirect") - req.URL.Scheme = "http" - req.URL.Host = *redirectHost + req.URL.Scheme = redirectScheme + req.URL.Host = redirectTarget + req.Host = redirectTarget req.URL.RawQuery = rawQuery req.RequestURI = "" zlog.Info().Str("to_url", req.URL.String()).Msg("Force redirected") @@ -184,8 +199,9 @@ func main() { Str("from_url", full). Str("raw_query", rawQuery). Msg("Redirect domain") - req.URL.Scheme = "http" - req.URL.Host = *redirectHost + req.URL.Scheme = redirectScheme + req.URL.Host = redirectTarget + req.Host = redirectTarget req.URL.RawQuery = rawQuery req.RequestURI = "" zlog.Info().Str("to_url", req.URL.String()).Msg("Redirected domain") @@ -246,4 +262,4 @@ func main() { if err := srv.Shutdown(ctx); err != nil { zlog.Error().Err(err).Msg("Server shutdown error") } -} +} \ No newline at end of file diff --git a/run_admin.go b/run_admin.go index 3b4f03e..ba4215b 100644 --- a/run_admin.go +++ b/run_admin.go @@ -1,7 +1,7 @@ -//go:build !windows && !darwin && !linux - -package main - -func runWithAdmin(exePath string, env []string) error { - return nil -} +//go:build !windows && !darwin && !linux + +package main + +func runWithAdmin(exePath string, env []string) error { + return nil +} diff --git a/run_admin_linux.go b/run_admin_linux.go index 9965641..a2880b2 100644 --- a/run_admin_linux.go +++ b/run_admin_linux.go @@ -1,15 +1,15 @@ -//go:build linux -// +build linux - -package main - -import ( - "os" - "os/exec" -) - -func runWithAdmin(exePath string, env []string) error { - cmd := exec.Command("pkexec", exePath) - cmd.Env = append(os.Environ(), env...) - return cmd.Start() -} +//go:build linux +// +build linux + +package main + +import ( + "os" + "os/exec" +) + +func runWithAdmin(exePath string, env []string) error { + cmd := exec.Command("pkexec", exePath) + cmd.Env = append(os.Environ(), env...) + return cmd.Start() +} diff --git a/run_admin_windows.go b/run_admin_windows.go index 5cb3000..a84fa6c 100644 --- a/run_admin_windows.go +++ b/run_admin_windows.go @@ -1,15 +1,15 @@ -//go:build windows -// +build windows - -package main - -import ( - "os" - "os/exec" -) - -func runWithAdmin(exePath string, env []string) error { - cmd := exec.Command("powershell", "Start-Process", exePath, "-Verb", "runAs") - cmd.Env = append(os.Environ(), env...) - return cmd.Start() -} +//go:build windows +// +build windows + +package main + +import ( + "os" + "os/exec" +) + +func runWithAdmin(exePath string, env []string) error { + cmd := exec.Command("powershell", "Start-Process", exePath, "-Verb", "runAs") + cmd.Env = append(os.Environ(), env...) + return cmd.Start() +} diff --git a/script/README_Note.md b/script/README_Note.md index 04a14fd..9c3a84d 100644 --- a/script/README_Note.md +++ b/script/README_Note.md @@ -1,4 +1,4 @@ -# Changelog - -### UPDATE +# Changelog + +### UPDATE - Support linux, macos, window \ No newline at end of file diff --git a/script/release.json b/script/release.json index 569c880..01b680d 100644 --- a/script/release.json +++ b/script/release.json @@ -1,5 +1,5 @@ -{ - "tag": "1.3-03", - "title": "PreBuild Version 1.3 - 03" -} +{ + "tag": "1.4-01", + "title": "PreBuild Version 1.4 - 01" +} \ No newline at end of file diff --git a/system_proxy.go b/system_proxy.go index 0e84ed9..dbf0a9d 100644 --- a/system_proxy.go +++ b/system_proxy.go @@ -1,7 +1,7 @@ -//go:build !windows && !darwin && !linux - -package main - -func setProxy(enable bool, host string, port string) error { - return nil +//go:build !windows && !darwin && !linux + +package main + +func setProxy(enable bool, host string, port string) error { + return nil } \ No newline at end of file diff --git a/system_proxy_linux.go b/system_proxy_linux.go index f682a13..e2e5d4e 100644 --- a/system_proxy_linux.go +++ b/system_proxy_linux.go @@ -1,24 +1,24 @@ -//go:build linux -// +build linux - -package main - -import "fmt" - -func setProxy(enable bool, host string, port string) error { - - httpProxy1 := fmt.Sprintf("HTTP_PROXY=http://%s:%s", host, port) - httpProxy2 := fmt.Sprintf("http_proxy=http://%s:%s", host, port) - - ENV_CONFIG = append(ENV_CONFIG, httpProxy1, httpProxy2) - - httpsProxy1 := fmt.Sprintf("HTTPS_PROXY=http://%s:%s", host, port) - httpsProxy2 := fmt.Sprintf("https_proxy=http://%s:%s", host, port) - ENV_CONFIG = append(ENV_CONFIG, httpsProxy1, httpsProxy2) - - if enable { - ENV_CONFIG = make([]string, 0) - } - - return nil -} +//go:build linux +// +build linux + +package main + +import "fmt" + +func setProxy(enable bool, host string, port string) error { + + httpProxy1 := fmt.Sprintf("HTTP_PROXY=http://%s:%s", host, port) + httpProxy2 := fmt.Sprintf("http_proxy=http://%s:%s", host, port) + + ENV_CONFIG = append(ENV_CONFIG, httpProxy1, httpProxy2) + + httpsProxy1 := fmt.Sprintf("HTTPS_PROXY=http://%s:%s", host, port) + httpsProxy2 := fmt.Sprintf("https_proxy=http://%s:%s", host, port) + ENV_CONFIG = append(ENV_CONFIG, httpsProxy1, httpsProxy2) + + if enable { + ENV_CONFIG = make([]string, 0) + } + + return nil +} diff --git a/system_proxy_windows.go b/system_proxy_windows.go index c90032f..c77a094 100644 --- a/system_proxy_windows.go +++ b/system_proxy_windows.go @@ -1,43 +1,43 @@ -//go:build windows -// +build windows - -package main - -import ( - "fmt" - "syscall" - - "golang.org/x/sys/windows/registry" -) - -func setProxy(enable bool, host string, port string) error { - k, _, err := registry.CreateKey( - registry.CURRENT_USER, - `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, - registry.SET_VALUE, - ) - if err != nil { - return err - } - - if enable { - k.SetDWordValue("ProxyEnable", 1) - - addr := fmt.Sprintf("%s:%s", host, port) - val := fmt.Sprintf("http=%s;https=%s", addr, addr) - - k.SetStringValue("ProxyServer", val) - - } else { - k.SetDWordValue("ProxyEnable", 0) - } - - k.Close() - - d := syscall.NewLazyDLL("wininet.dll") - o := d.NewProc("InternetSetOptionW") - o.Call(0, 39, 0, 0) - o.Call(0, 37, 0, 0) - - return nil -} +//go:build windows +// +build windows + +package main + +import ( + "fmt" + "syscall" + + "golang.org/x/sys/windows/registry" +) + +func setProxy(enable bool, host string, port string) error { + k, _, err := registry.CreateKey( + registry.CURRENT_USER, + `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, + registry.SET_VALUE, + ) + if err != nil { + return err + } + + if enable { + k.SetDWordValue("ProxyEnable", 1) + + addr := fmt.Sprintf("%s:%s", host, port) + val := fmt.Sprintf("http=%s;https=%s", addr, addr) + + k.SetStringValue("ProxyServer", val) + + } else { + k.SetDWordValue("ProxyEnable", 0) + } + + k.Close() + + d := syscall.NewLazyDLL("wininet.dll") + o := d.NewProc("InternetSetOptionW") + o.Call(0, 39, 0, 0) + o.Call(0, 37, 0, 0) + + return nil +}