Skip to content

Commit

Permalink
v0.5: fixed nil dereference for empty autocert config, fixed and simp…
Browse files Browse the repository at this point in the history
…lified 'error' module, small readme and docs update
  • Loading branch information
default committed Aug 12, 2024
1 parent 2fc82c3 commit 85fb637
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 232 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build:
CGO_ENABLED=0 GOOS=linux go build -pgo=auto -o bin/go-proxy github.com/yusing/go-proxy

test:
cd src && go test && cd ..
cd src && go test ./... && cd ..

up:
docker compose up -d
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ A [lightweight](docs/benchmark_result.md), easy-to-use, and efficient reverse pr
- [Provider File](#provider-file)
- [Known issues](#known-issues)
- [Build it yourself](#build-it-yourself)
<!-- /TOC -->

## Key Points

Expand Down
23 changes: 10 additions & 13 deletions docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,25 +200,22 @@ services:
volumes:
- nginx:/usr/share/nginx/html
go-proxy:
image: ghcr.io/yusing/go-proxy
image: ghcr.io/yusing/go-proxy:latest
container_name: go-proxy
restart: always
ports:
- 80:80 # http
- 443:443 # optional, https
- 8080:8080 # http panel
- 8443:8443 # optional, https panel
- 53:20000/udp # adguardhome
- 25565:20001/tcp # minecraft
- 8211:20002/udp # palworld
- 27015:20003/udp # palworld
network_mode: host
volumes:
- ./config:/app/config
- /var/run/docker.sock:/var/run/docker.sock:ro
go-proxy-frontend:
image: ghcr.io/yusing/go-proxy-frontend:latest
container_name: go-proxy-frontend
restart: unless-stopped
network_mode: host
labels:
- proxy.aliases=gp
- proxy.gp.port=8080
- proxy.*.aliases=gp
depends_on:
- go-proxy
```

[🔼Back to top](#table-of-content)
Expand Down
1 change: 1 addition & 0 deletions src/autocert/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
)

var providersGenMap = map[string]ProviderGenerator{
"": providerGenerator(NewDummyDefaultConfig, NewDummyDNSProviderConfig),
ProviderLocal: providerGenerator(NewDummyDefaultConfig, NewDummyDNSProviderConfig),
ProviderCloudflare: providerGenerator(cloudflare.NewDefaultConfig, cloudflare.NewDNSProviderConfig),
ProviderClouddns: providerGenerator(clouddns.NewDefaultConfig, clouddns.NewDNSProviderConfig),
Expand Down
8 changes: 4 additions & 4 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ func (cfg *Config) load() E.NestedError {
}
}

errors := E.NewBuilder("errors validating config")
warnings := E.NewBuilder("errors validating config")

cfg.l.Debug("starting autocert")
ap, err := autocert.NewConfig(&model.AutoCert).GetProvider()
if err.IsNotNil() {
errors.Add(E.Failure("autocert provider").With(err))
warnings.Add(E.Failure("autocert provider").With(err))
} else {
cfg.l.Debug("started autocert")
}
Expand All @@ -224,14 +224,14 @@ func (cfg *Config) load() E.NestedError {
p := PR.NewProvider(name, pm)
cfg.proxyProviders.Set(name, p)
if err := p.StartAllRoutes(); err.IsNotNil() {
errors.Add(E.Failure("start routes").Subjectf("provider %s", name).With(err))
warnings.Add(E.Failure("start routes").Subjectf("provider %s", name).With(err))
}
}
cfg.l.Debug("started providers")

cfg.value = model

if err := errors.Build(); err.IsNotNil() {
if err := warnings.Build(); err.IsNotNil() {
cfg.l.Warn(err)
}

Expand Down
3 changes: 0 additions & 3 deletions src/docker/label.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package docker

import (
"errors"
"strings"

E "github.com/yusing/go-proxy/error"
Expand Down Expand Up @@ -77,5 +76,3 @@ func RegisterNamespace(namespace string, pm ValueParserMap) {

// namespace:target.attribute -> func(string) (any, error)
var labelValueParserMap = make(map[string]ValueParserMap)

var ErrInvalidLabel = errors.New("invalid label")
33 changes: 9 additions & 24 deletions src/docker/proxy_label_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package docker

import (
"errors"
"fmt"
"net/http"
"reflect"
Expand All @@ -14,27 +13,13 @@ func makeLabel(namespace string, alias string, field string) string {
return fmt.Sprintf("%s.%s.%s", namespace, alias, field)
}

func TestInvalidLabel(t *testing.T) {
pl, err := ParseLabel("foo.bar", "1234")
if !errors.Is(err, ErrInvalidLabel) {
t.Errorf("expected errInvalidLabel, got %s", err)
}
if pl != nil {
t.Errorf("expected nil, got %v", pl)
}
_, err = ParseLabel("proxy.foo", "bar")
if !errors.Is(err, ErrInvalidLabel) {
t.Errorf("expected errInvalidLabel, got %s", err)
}
}

func TestHomePageLabel(t *testing.T) {
alias := "foo"
field := "ip"
v := "bar"
pl, err := ParseLabel(makeLabel(NSHomePage, alias, field), v)
if err.IsNotNil() {
t.Errorf("expected err=nil, got %s", err)
t.Errorf("expected err=nil, got %s", err.Error())
}
if pl.Target != alias {
t.Errorf("expected alias=%s, got %s", alias, pl.Target)
Expand All @@ -53,7 +38,7 @@ func TestStringProxyLabel(t *testing.T) {
v := "bar"
pl, err := ParseLabel(makeLabel(NSProxy, alias, field), v)
if err.IsNotNil() {
t.Errorf("expected err=nil, got %s", err)
t.Errorf("expected err=nil, got %s", err.Error())
}
if pl.Target != alias {
t.Errorf("expected alias=%s, got %s", alias, pl.Target)
Expand Down Expand Up @@ -83,7 +68,7 @@ func TestBoolProxyLabelValid(t *testing.T) {
for k, v := range tests {
pl, err := ParseLabel(makeLabel(NSProxy, alias, field), k)
if err.IsNotNil() {
t.Errorf("expected err=nil, got %s", err)
t.Errorf("expected err=nil, got %s", err.Error())
}
if pl.Target != alias {
t.Errorf("expected alias=%s, got %s", alias, pl.Target)
Expand All @@ -101,8 +86,8 @@ func TestBoolProxyLabelInvalid(t *testing.T) {
alias := "foo"
field := "no_tls_verify"
_, err := ParseLabel(makeLabel(NSProxy, alias, field), "invalid")
if !errors.Is(err, E.ErrInvalid) {
t.Errorf("expected err InvalidProxyLabel, got %s", err)
if !err.Is(E.ErrInvalid) {
t.Errorf("expected err InvalidProxyLabel, got %v", reflect.TypeOf(err))
}
}

Expand All @@ -121,7 +106,7 @@ func TestHeaderProxyLabelValid(t *testing.T) {

pl, err := ParseLabel(makeLabel(NSProxy, alias, field), v)
if err.IsNotNil() {
t.Errorf("expected err=nil, got %s", err)
t.Errorf("expected err=nil, got %s", err.Error())
}
if pl.Target != alias {
t.Errorf("expected alias=%s, got %s", alias, pl.Target)
Expand Down Expand Up @@ -152,7 +137,7 @@ func TestHeaderProxyLabelInvalid(t *testing.T) {

for _, v := range tests {
_, err := ParseLabel(makeLabel(NSProxy, alias, field), v)
if !errors.Is(err, E.ErrInvalid) {
if !err.Is(E.ErrInvalid) {
t.Errorf("expected err InvalidProxyLabel for %q, got %v", v, err)
}
}
Expand All @@ -164,7 +149,7 @@ func TestCommaSepProxyLabelSingle(t *testing.T) {
v := "X-Custom-Header1"
pl, err := ParseLabel(makeLabel(NSProxy, alias, field), v)
if err.IsNotNil() {
t.Errorf("expected err=nil, got %s", err)
t.Errorf("expected err=nil, got %s", err.Error())
}
if pl.Target != alias {
t.Errorf("expected alias=%s, got %s", alias, pl.Target)
Expand All @@ -188,7 +173,7 @@ func TestCommaSepProxyLabelMulti(t *testing.T) {
v := "X-Custom-Header1, X-Custom-Header2,X-Custom-Header3"
pl, err := ParseLabel(makeLabel(NSProxy, alias, field), v)
if err.IsNotNil() {
t.Errorf("expected err=nil, got %s", err)
t.Errorf("expected err=nil, got %s", err.Error())
}
if pl.Target != alias {
t.Errorf("expected alias=%s, got %s", alias, pl.Target)
Expand Down
28 changes: 28 additions & 0 deletions src/error/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package error

import "testing"

func TestBuilder(t *testing.T) {
eb := NewBuilder("error occurred")
eb.Add(Failure("Action 1").With(Invalid("Inner", "1")).With(Invalid("Inner", "2")))
eb.Add(Failure("Action 2").With(Invalid("Inner", "3")))

got := eb.Build().Error()
expected1 :=
(`error occurred:
- Action 1 failed:
- invalid Inner - 1
- invalid Inner - 2
- Action 2 failed:
- invalid Inner - 3`)
expected2 :=
(`error occurred:
- Action 1 failed:
- invalid Inner - 2
- invalid Inner - 1
- Action 2 failed:
- invalid Inner - 3`)
if got != expected1 && got != expected2 {
t.Errorf("expected \n%s, got \n%s", expected1, got)
}
}
Loading

0 comments on commit 85fb637

Please sign in to comment.