Skip to content

Commit

Permalink
fix: improvements in memory consumption (#100)
Browse files Browse the repository at this point in the history
* fix: must close the connection after processing

I think it should help to close #80

* feat: migration to github.com/things-go/go-socks5

- preallocate config slices
- not used interfaces in consumer
- do not allocate new variables in loops

* feat: close connection after full processing

* feat: correct process sigint signal

* feat: improve build system

* fix: http proxy

* feat: update golangci-lint-action to v3.7.0

* feat: correct process routines

* fix: close http conn correctly

* feat: update golangci-lint-action to v4

* fix: goreleaser used clean now
  • Loading branch information
juev authored Feb 11, 2024
1 parent 49f5688 commit a2d7aec
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 140 deletions.
57 changes: 29 additions & 28 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,145 +6,146 @@ on:
pull_request:
branches:
- '**'
workflow_dispatch:

jobs:
windowsAmd64Build:
name: Build Windows amd64 Version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setting up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Building Windows amd64 Version
run: |
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o WireProxy_amd64.exe -v ./cmd/wireproxy
mkdir release_windows_amd64
mv WireProxy_amd64.exe wireproxy.exe
cp wireproxy.exe release_windows_amd64/wireproxy.exe
- name: Upload Windows amd64 Version
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: WireProxy_windows_amd64
path: release_windows_amd64
windowsArm64Build:
name: Build Windows arm64 Version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setting up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Building Windows arm64 Version
run: |
CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -o WireProxy_arm64.exe -v ./cmd/wireproxy
mkdir release_windows_arm64
mv WireProxy_arm64.exe wireproxy.exe
cp wireproxy.exe release_windows_arm64/wireproxy.exe
- name: Upload Windows arm64 Version
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: WireProxy_windows_arm64
path: release_windows_arm64
linuxAmd64Build:
name: Build Linux amd64 Version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setting up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Building Linux amd64 Version
run: |
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o WireProxy_amd64 -v ./cmd/wireproxy
mkdir release_linux_amd64
mv WireProxy_amd64 wireproxy
cp wireproxy release_linux_amd64/wireproxy
- name: Upload Linux amd64 Version
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: WireProxy_linux_amd64
path: release_linux_amd64
linuxArm64Build:
name: Build Linux arm64 Version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setting up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Building Linux arm64 Version
run: |
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o WireProxy_arm64 -v ./cmd/wireproxy
mkdir release_linux_arm64
mv WireProxy_arm64 wireproxy
cp wireproxy release_linux_arm64/wireproxy
- name: Upload Linux arm64 Version
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: WireProxy_linux_arm64
path: release_linux_arm64
linuxS390xBuild:
name: Build Linux s390x Version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setting up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Building Linux s390x Version
run: |
CGO_ENABLED=0 GOOS=linux GOARCH=s390x go build -o WireProxy_s390x -v ./cmd/wireproxy
mkdir release_linux_s390x
mv WireProxy_s390x wireproxy
cp wireproxy release_linux_s390x/wireproxy
- name: Upload Linux s390x Version
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: WireProxy_linux_s390x
path: release_linux_s390x
darwinAmd64Build:
name: Build Darwin amd64 Version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setting up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Building Darwin amd64 Version
run: |
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o WireProxy_amd64 -v ./cmd/wireproxy
mkdir release_darwin_amd64
mv WireProxy_amd64 wireproxy
cp wireproxy release_darwin_amd64/wireproxy
- name: Upload Darwin amd64 Version
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: WireProxy_darwin_amd64
path: release_darwin_amd64
darwinArm64Build:
name: Build Darwin arm64 Version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setting up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Building Darwin arm64 Version
run: |
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o WireProxy_arm64 -v ./cmd/wireproxy
mkdir release_darwin_arm64
mv WireProxy_arm64 wireproxy
cp wireproxy release_darwin_arm64/wireproxy
- name: Upload Darwin arm64 Version
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v4
with:
name: WireProxy_darwin_arm64
path: release_darwin_arm64
10 changes: 5 additions & 5 deletions .github/workflows/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ jobs:

steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2.0.0
uses: docker/setup-buildx-action@v3.0.0

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: recursive

# Needed for buildx gha cache to work
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v2
uses: crazy-max/ghaction-github-runtime@v3

- name: Build container
env:
Expand Down
12 changes: 8 additions & 4 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ on:
pull_request:
branches:
- '**'
workflow_dispatch:

permissions:
contents: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
- uses: actions/setup-go@v5
with:
go-version: '1.20'
- uses: actions/checkout@v3
go-version: '1.21'
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v4
with:
version: latest
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ on:
pull_request:
branches:
- '**'
workflow_dispatch:

jobs:
test:
name: Test wireproxy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setting up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Install dependencies
run: sudo apt install wireguard curl
- name: Building wireproxy
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/wireproxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ jobs:
cp ./.github/wireproxy-releaser.yml ${{ env.workdir }}/.goreleaser.yml
- name: Set up GoReleaser
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
workdir: ${{ env.workdir }}
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Start by building the application.
FROM docker.io/golang:1.20 as build
FROM docker.io/golang:1.21 as build

WORKDIR /usr/src/wireproxy
COPY . .
Expand All @@ -14,6 +14,6 @@ VOLUME [ "/etc/wireproxy"]
ENTRYPOINT [ "/usr/bin/wireproxy" ]
CMD [ "--config", "/etc/wireproxy/config" ]

LABEL org.opencontainers.image.title wireproxy
LABEL org.opencontainers.image.description "Wireguard client that exposes itself as a socks5 proxy"
LABEL org.opencontainers.image.licenses ISC
LABEL org.opencontainers.image.title="wireproxy"
LABEL org.opencontainers.image.description="Wireguard client that exposes itself as a socks5 proxy"
LABEL org.opencontainers.image.licenses="ISC"
15 changes: 13 additions & 2 deletions cmd/wireproxy/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"

"github.com/akamensky/argparse"
"github.com/octeep/wireproxy"
"github.com/pufferffish/wireproxy"
"golang.zx2c4.com/wireguard/device"
"suah.dev/protect"
)
Expand Down Expand Up @@ -46,6 +48,15 @@ func executablePath() string {
}

func main() {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGQUIT)
ctx, cancel := context.WithCancel(context.Background())

go func() {
<-s
cancel()
}()

exePath := executablePath()
unveilOrPanic("/", "r")
unveilOrPanic(exePath, "x")
Expand Down Expand Up @@ -138,5 +149,5 @@ func main() {
go spawner.SpawnRoutine(tnet)
}

select {} // sleep eternally
<-ctx.Done()
}
19 changes: 11 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type TCPClientTunnelConfig struct {
}

type STDIOTunnelConfig struct {
Target string
Target string
}

type TCPServerTunnelConfig struct {
Expand Down Expand Up @@ -125,8 +125,9 @@ func parseNetIP(section *ini.Section, keyName string) ([]netip.Addr, error) {
return []netip.Addr{}, nil
}

var ips []netip.Addr
for _, str := range key.StringsWithShadows(",") {
keys := key.StringsWithShadows(",")
var ips = make([]netip.Addr, 0, len(keys))
for _, str := range keys {
str = strings.TrimSpace(str)
ip, err := netip.ParseAddr(str)
if err != nil {
Expand All @@ -143,8 +144,9 @@ func parseCIDRNetIP(section *ini.Section, keyName string) ([]netip.Addr, error)
return []netip.Addr{}, nil
}

var ips []netip.Addr
for _, str := range key.StringsWithShadows(",") {
keys := key.StringsWithShadows(",")
var ips = make([]netip.Addr, 0, len(keys))
for _, str := range keys {
prefix, err := netip.ParsePrefix(str)
if err != nil {
return nil, err
Expand All @@ -162,8 +164,9 @@ func parseAllowedIPs(section *ini.Section) ([]netip.Prefix, error) {
return []netip.Prefix{}, nil
}

var ips []netip.Prefix
for _, str := range key.StringsWithShadows(",") {
keys := key.StringsWithShadows(",")
var ips = make([]netip.Prefix, 0, len(keys))
for _, str := range keys {
prefix, err := netip.ParsePrefix(str)
if err != nil {
return nil, err
Expand Down Expand Up @@ -237,7 +240,7 @@ func ParseInterface(cfg *ini.File, device *DeviceConfig) error {
return nil
}

// ParsePeer parses the [Peer] section and extract the information into `peers`
// ParsePeers parses the [Peer] section and extract the information into `peers`
func ParsePeers(cfg *ini.File, peers *[]PeerConfig) error {
sections, err := cfg.SectionsByName("Peer")
if len(sections) < 1 || err != nil {
Expand Down
Loading

0 comments on commit a2d7aec

Please sign in to comment.