small refactor and fixes

This commit is contained in:
yusing 2024-12-03 11:45:10 +08:00
parent cebc0c5405
commit 5d5c623f09
12 changed files with 46 additions and 52 deletions

View file

@ -2,12 +2,12 @@
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml # To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1 version: 0.1
cli: cli:
version: 1.22.6 version: 1.22.8
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins) # Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
plugins: plugins:
sources: sources:
- id: trunk - id: trunk
ref: v1.6.3 ref: v1.6.5
uri: https://github.com/trunk-io/plugins uri: https://github.com/trunk-io/plugins
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes) # Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
runtimes: runtimes:
@ -21,18 +21,18 @@ lint:
- markdownlint - markdownlint
- yamllint - yamllint
enabled: enabled:
- hadolint@2.12.0 - hadolint@2.12.1-beta
- actionlint@1.7.3 - actionlint@1.7.4
- checkov@3.2.257 - checkov@3.2.324
- git-diff-check - git-diff-check
- gofmt@1.20.4 - gofmt@1.20.4
- golangci-lint@1.61.0 - golangci-lint@1.62.2
- osv-scanner@1.9.0 - osv-scanner@1.9.1
- oxipng@9.1.2 - oxipng@9.1.3
- prettier@3.3.3 - prettier@3.4.1
- shellcheck@0.10.0 - shellcheck@0.10.0
- shfmt@3.6.0 - shfmt@3.6.0
- trufflehog@3.82.7 - trufflehog@3.84.1
actions: actions:
disabled: disabled:
- trunk-announce - trunk-announce

View file

@ -204,15 +204,14 @@ func (p *Provider) registerACME() error {
p.user.Registration = reg p.user.Registration = reg
logger.Info().Msg("reused acme registration from private key") logger.Info().Msg("reused acme registration from private key")
return nil return nil
} else {
reg, err := p.client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return err
}
p.user.Registration = reg
logger.Info().Interface("reg", reg).Msg("acme registered")
} }
reg, err := p.client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return err
}
p.user.Registration = reg
logger.Info().Interface("reg", reg).Msg("acme registered")
return nil return nil
} }

View file

@ -1,7 +1,6 @@
package common package common
import ( import (
"crypto/rand"
"crypto/sha512" "crypto/sha512"
"encoding/base64" "encoding/base64"
@ -14,14 +13,6 @@ func HashPassword(pwd string) []byte {
return h.Sum(nil) return h.Sum(nil)
} }
func generateJWTKey(size int) string {
bytes := make([]byte, size)
if _, err := rand.Read(bytes); err != nil {
log.Panic().Err(err).Msg("failed to generate jwt key")
}
return base64.StdEncoding.EncodeToString(bytes)
}
func decodeJWTKey(key string) []byte { func decodeJWTKey(key string) []byte {
if key == "" { if key == "" {
return nil return nil

View file

@ -8,12 +8,11 @@ import (
func Inspect(dockerHost string, containerID string) (*Container, error) { func Inspect(dockerHost string, containerID string) (*Container, error) {
client, err := ConnectClient(dockerHost) client, err := ConnectClient(dockerHost)
defer client.Close()
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer client.Close()
return client.Inspect(containerID) return client.Inspect(containerID)
} }

View file

@ -1,6 +1,6 @@
package homepage package homepage
// PredefinedCategories by alias or docker image name // PredefinedCategories by alias or docker image name.
var PredefinedCategories = map[string]string{ var PredefinedCategories = map[string]string{
"sonarr": "Torrenting", "sonarr": "Torrenting",
"radarr": "Torrenting", "radarr": "Torrenting",

View file

@ -10,8 +10,8 @@ type (
Icon string `json:"icon" yaml:"icon"` Icon string `json:"icon" yaml:"icon"`
URL string `json:"url" yaml:"url"` // alias + domain URL string `json:"url" yaml:"url"` // alias + domain
Category string `json:"category" yaml:"category"` Category string `json:"category" yaml:"category"`
Description string `json:"description" yaml:"description"` Description string `json:"description" yaml:"description" aliases:"desc"`
WidgetConfig map[string]any `json:"widget_config" yaml:",flow"` WidgetConfig map[string]any `json:"widget_config" yaml:",flow" aliases:"widget"`
SourceType string `json:"source_type" yaml:"-"` SourceType string `json:"source_type" yaml:"-"`
AltURL string `json:"alt_url" yaml:"-"` // original proxy target AltURL string `json:"alt_url" yaml:"-"` // original proxy target

View file

@ -218,7 +218,9 @@ func (lb *LoadBalancer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if r.Header.Get(common.HeaderCheckRedirect) != "" { if r.Header.Get(common.HeaderCheckRedirect) != "" {
// wake all servers // wake all servers
for _, srv := range srvs { for _, srv := range srvs {
srv.TryWake() if err := srv.TryWake(); err != nil {
lb.l.Warn().Err(err).Str("server", srv.Name).Msg("failed to wake server")
}
} }
} }
lb.impl.ServeHTTP(srvs, rw, r) lb.impl.ServeHTTP(srvs, rw, r)

View file

@ -109,7 +109,7 @@ func fetchUpdateCFIPRange(endpoint string, cfCIDRs []*types.CIDR) error {
return fmt.Errorf("cloudflare responeded an invalid CIDR: %s", line) return fmt.Errorf("cloudflare responeded an invalid CIDR: %s", line)
} }
cfCIDRs = append(cfCIDRs, (*types.CIDR)(cidr)) cfCIDRs = append(cfCIDRs, cidr)
} }
return nil return nil

View file

@ -40,7 +40,10 @@ func jsonIfTemplateNotUsed(fl validator.FieldLevel) bool {
} }
func init() { func init() {
utils.Validator().RegisterValidation("jsonIfTemplateNotUsed", jsonIfTemplateNotUsed) err := utils.Validator().RegisterValidation("jsonIfTemplateNotUsed", jsonIfTemplateNotUsed)
if err != nil {
panic(err)
}
} }
// Name implements Provider. // Name implements Provider.
@ -52,9 +55,8 @@ func (webhook *Webhook) Name() string {
func (webhook *Webhook) Method() string { func (webhook *Webhook) Method() string {
if webhook.Meth != "" { if webhook.Meth != "" {
return webhook.Meth return webhook.Meth
} else {
return http.MethodPost
} }
return http.MethodPost
} }
// URL implements Provider. // URL implements Provider.
@ -71,17 +73,16 @@ func (webhook *Webhook) Token() string {
func (webhook *Webhook) MIMEType() string { func (webhook *Webhook) MIMEType() string {
if webhook.MIMETyp != "" { if webhook.MIMETyp != "" {
return webhook.MIMETyp return webhook.MIMETyp
} else {
return "application/json"
} }
return "application/json"
} }
func (Webhook *Webhook) ColorMode() string { func (webhook *Webhook) ColorMode() string {
switch Webhook.Template { switch webhook.Template {
case "discord": case "discord":
return "dec" return "dec"
default: default:
return Webhook.ColorM return webhook.ColorM
} }
} }
@ -119,7 +120,7 @@ func (webhook *Webhook) MakeBody(logMsg *LogMessage) (io.Reader, error) {
plTempl := strings.NewReplacer( plTempl := strings.NewReplacer(
"$title", string(title), "$title", string(title),
"$message", string(message), "$message", string(message),
"$fields", string(fields), "$fields", fields,
"$color", color, "$color", color,
) )
var pl string var pl string

View file

@ -8,7 +8,6 @@ import (
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/docker"
D "github.com/yusing/go-proxy/internal/docker" D "github.com/yusing/go-proxy/internal/docker"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/route" "github.com/yusing/go-proxy/internal/route"
@ -17,9 +16,7 @@ import (
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
var ( var dummyNames = []string{"/a"}
dummyNames = []string{"/a"}
)
func makeEntries(cont *types.Container, dockerHost ...string) route.RawEntries { func makeEntries(cont *types.Container, dockerHost ...string) route.RawEntries {
var p DockerProvider var p DockerProvider
@ -29,7 +26,7 @@ func makeEntries(cont *types.Container, dockerHost ...string) route.RawEntries {
} else { } else {
host = client.DefaultDockerHost host = client.DefaultDockerHost
} }
entries := E.Must(p.entriesFromContainerLabels(docker.FromDocker(cont, host))) entries := E.Must(p.entriesFromContainerLabels(D.FromDocker(cont, host)))
entries.RangeAll(func(k string, v *route.RawEntry) { entries.RangeAll(func(k string, v *route.RawEntry) {
v.Finalize() v.Finalize()
}) })
@ -269,7 +266,9 @@ func TestPrivateIPLocalhost(t *testing.T) {
"network": { "network": {
IPAddress: "172.17.0.123", IPAddress: "172.17.0.123",
}, },
}}} },
},
}
raw, ok := makeEntries(c).Load("a") raw, ok := makeEntries(c).Load("a")
ExpectTrue(t, ok) ExpectTrue(t, ok)
ExpectEqual(t, raw.Container.PrivateIP, "172.17.0.123") ExpectEqual(t, raw.Container.PrivateIP, "172.17.0.123")
@ -286,7 +285,8 @@ func TestPrivateIPRemote(t *testing.T) {
IPAddress: "172.17.0.123", IPAddress: "172.17.0.123",
}, },
}, },
}} },
}
raw, ok := makeEntries(c, "tcp://1.2.3.4:2375").Load("a") raw, ok := makeEntries(c, "tcp://1.2.3.4:2375").Load("a")
ExpectTrue(t, ok) ExpectTrue(t, ok)
ExpectEqual(t, raw.Container.PrivateIP, "") ExpectEqual(t, raw.Container.PrivateIP, "")

View file

@ -40,8 +40,10 @@ const (
) )
// function alias. // function alias.
var NewRoutes = F.NewMap[Routes] var (
var NewProxyEntries = types.NewProxyEntries NewRoutes = F.NewMap[Routes]
NewProxyEntries = types.NewProxyEntries
)
func (rt *Route) Container() *docker.Container { func (rt *Route) Container() *docker.Container {
if rt.Entry.Container == nil { if rt.Entry.Container == nil {

View file

@ -5,7 +5,7 @@ import (
) )
type HealthCheckConfig struct { type HealthCheckConfig struct {
Disable bool `json:"disable,omitempty" yaml:"disable"` Disable bool `json:"disable,omitempty" yaml:"disable" aliases:"disabled"`
Path string `json:"path,omitempty" yaml:"path"` Path string `json:"path,omitempty" yaml:"path"`
UseGet bool `json:"use_get,omitempty" yaml:"use_get"` UseGet bool `json:"use_get,omitempty" yaml:"use_get"`
Interval time.Duration `json:"interval" yaml:"interval"` Interval time.Duration `json:"interval" yaml:"interval"`