From 953f58b565965bf3a690be090a3a35d97f38e80b Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Tue, 17 Feb 2026 23:45:32 +0000 Subject: [PATCH 1/4] Refactor with suggested fixes --- cmd/violet/serve.go | 2 +- cmd/violet/setup.go | 8 ++++---- router/manager.go | 17 +++++------------ servers/https_test.go | 2 +- target/route.go | 13 +++++-------- utils/domain-utils.go | 6 +++--- 6 files changed, 19 insertions(+), 29 deletions(-) diff --git a/cmd/violet/serve.go b/cmd/violet/serve.go index 00706ef..7a3d500 100644 --- a/cmd/violet/serve.go +++ b/cmd/violet/serve.go @@ -49,7 +49,7 @@ func (s *serveCmd) Usage() string { ` } -func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { +func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...any) subcommands.ExitStatus { if s.debugLog { logger.Logger.SetLevel(log.DebugLevel) } diff --git a/cmd/violet/setup.go b/cmd/violet/setup.go index d622cbc..21cd89d 100644 --- a/cmd/violet/setup.go +++ b/cmd/violet/setup.go @@ -38,7 +38,7 @@ func (s *setupCmd) Usage() string { ` } -func (s *setupCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { +func (s *setupCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...any) subcommands.ExitStatus { // get absolute path to specify files wdAbs, err := filepath.Abs(s.wdPath) if err != nil { @@ -96,7 +96,7 @@ func (s *setupCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) { Name: "RateLimit", Prompt: &survey.Input{Message: "Rate limit", Default: "300", Help: "Number of allowed requests per minute per IP"}, - Validate: func(ans interface{}) error { + Validate: func(ans any) error { if ansStr, ok := ans.(string); ok { _, err := strconv.ParseUint(ansStr, 10, 64) return err @@ -160,7 +160,7 @@ func (s *setupCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) // don't bother with this part is the api won't be listening if answers.ApiListen != "" { // ask for url - err = survey.AskOne(&survey.Input{Message: "API URL", Default: "api.example.com/violet", Help: "Enter the URL which should point to the internal Violet API"}, &answers.ApiUrl, survey.WithValidator(func(ans interface{}) error { + err = survey.AskOne(&survey.Input{Message: "API URL", Default: "api.example.com/violet", Help: "Enter the URL which should point to the internal Violet API"}, &answers.ApiUrl, survey.WithValidator(func(ans any) error { if ansStr, ok := ans.(string); ok { _, err := url.Parse(ansStr) return err @@ -202,7 +202,7 @@ func (s *setupCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) return subcommands.ExitSuccess } -func listenAddressValidator(ans interface{}) error { +func listenAddressValidator(ans any) error { if ansStr, ok := ans.(string); ok { // empty string means disable if ansStr == "" { diff --git a/router/manager.go b/router/manager.go index f343525..01fb522 100644 --- a/router/manager.go +++ b/router/manager.go @@ -9,6 +9,7 @@ import ( "github.com/1f349/violet/target" "github.com/mrmelon54/rescheduler" "net/http" + "slices" "strings" "sync" ) @@ -127,12 +128,8 @@ func (m *Manager) GetAllRoutes(hosts []string) ([]target.RouteWithActive, error) Active: row.Active, } - for _, i := range hosts { - // if this is never true then the domain was mistakenly grabbed from the database - if a.OnDomain(i) { - s = append(s, a) - break - } + if slices.ContainsFunc(hosts, a.OnDomain) { + s = append(s, a) } } @@ -177,12 +174,8 @@ func (m *Manager) GetAllRedirects(hosts []string) ([]target.RedirectWithActive, Active: row.Active, } - for _, i := range hosts { - // if this is never true then the domain was mistakenly grabbed from the database - if a.OnDomain(i) { - s = append(s, a) - break - } + if slices.ContainsFunc(hosts, a.OnDomain) { + s = append(s, a) } } diff --git a/servers/https_test.go b/servers/https_test.go index 8394392..3227369 100644 --- a/servers/https_test.go +++ b/servers/https_test.go @@ -44,7 +44,7 @@ func TestNewHttpsServer_RateLimit(t *testing.T) { wg := &sync.WaitGroup{} wg.Add(5) - for i := 0; i < 5; i++ { + for range 5 { go func() { defer wg.Done() rec := httptest.NewRecorder() diff --git a/target/route.go b/target/route.go index 2c340d1..7928c99 100644 --- a/target/route.go +++ b/target/route.go @@ -9,6 +9,7 @@ import ( "github.com/rs/cors" "golang.org/x/net/http/httpguts" "io" + "maps" "net" "net/http" "net/textproto" @@ -70,9 +71,7 @@ func (r Route) HasFlag(flag Flags) bool { // UpdateHeaders takes an existing set of headers and overwrites them with the // extra headers. func (r Route) UpdateHeaders(header http.Header) { - for k, v := range r.Headers { - header[k] = v - } + maps.Copy(header, r.Headers) } // ServeHTTP responds with the data proxied from the internal server to the @@ -146,10 +145,8 @@ func (r Route) internalServeHTTP(rw http.ResponseWriter, req *http.Request) { // if extra route headers are set if r.Headers != nil { // loop over headers - for k, v := range r.Headers { - // copy header into the internal request - req2.Header[k] = v - } + // copy header into the internal request + maps.Copy(req2.Header, r.Headers) } // if forward host is enabled then send the host @@ -316,7 +313,7 @@ var hopHeaders = []string{ func removeHopByHopHeaders(h http.Header) { // RFC 7230, section 6.1: Remove headers listed in the "Connection" header. for _, f := range h["Connection"] { - for _, sf := range strings.Split(f, ",") { + for sf := range strings.SplitSeq(f, ",") { if sf = textproto.TrimString(sf); sf != "" { h.Del(sf) } diff --git a/utils/domain-utils.go b/utils/domain-utils.go index c7387a8..61da22d 100644 --- a/utils/domain-utils.go +++ b/utils/domain-utils.go @@ -55,11 +55,11 @@ func ReplaceSubdomainWithWildcard(domain string) (string, bool) { // www.example.com => example.com func GetParentDomain(domain string) (string, bool) { // if a valid index isn't found then return false - n := strings.IndexByte(domain, '.') - if n == -1 { + _, after, ok := strings.Cut(domain, ".") + if !ok { return "", false } - return domain[n+1:], true + return after, true } // GetTopFqdn returns the top domain stripping off multiple layers of subdomains. From 79408979fca8875c7f24ea372cfd201a72b4f90b Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Tue, 17 Feb 2026 23:46:44 +0000 Subject: [PATCH 2/4] Update go version to 1.26 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e39a3d9..8802ea8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.22.x] + go-version: [1.26.x] runs-on: ubuntu-latest steps: - uses: actions/setup-go@v3 From f758821800954620eddac1c57a6dc4749f5a3231 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Tue, 17 Feb 2026 23:46:54 +0000 Subject: [PATCH 3/4] Update workflow dependencies to their latest majors --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8802ea8..09480b4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,10 +7,10 @@ jobs: go-version: [1.26.x] runs-on: ubuntu-latest steps: - - uses: actions/setup-go@v3 + - uses: actions/setup-go@v6 with: go-version: ${{ matrix.go-version }} - - uses: actions/checkout@v3 + - uses: actions/checkout@v6 - run: sudo add-apt-repository ppa:inkscape.dev/stable - run: sudo apt-get update - run: sudo apt-get install inkscape -y From 60103631a101a564b76a8b062574e9a760942366 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Wed, 18 Feb 2026 22:02:28 +0000 Subject: [PATCH 4/4] Replace rate limit values in https tests with a constant --- servers/https_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/servers/https_test.go b/servers/https_test.go index 3227369..ed4968c 100644 --- a/servers/https_test.go +++ b/servers/https_test.go @@ -28,9 +28,11 @@ func TestNewHttpsServer_RateLimit(t *testing.T) { db, err := violet.InitDB("file:TestNewHttpsServer_RateLimit?mode=memory&cache=shared") assert.NoError(t, err) + const rateLimit = 5 + ft := &fakeTransport{} httpsConf := &conf.Conf{ - RateLimit: 5, + RateLimit: rateLimit, Domains: &fake.Domains{}, Certs: certs.New(nil, nil, true), Signer: fake.SnakeOilProv.KeyStore(), @@ -43,8 +45,8 @@ func TestNewHttpsServer_RateLimit(t *testing.T) { assert.NoError(t, err) wg := &sync.WaitGroup{} - wg.Add(5) - for range 5 { + wg.Add(rateLimit) + for range rateLimit { go func() { defer wg.Done() rec := httptest.NewRecorder()