Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Caddy to the latest #27

Merged
merged 14 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
41 changes: 21 additions & 20 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,45 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
os: [macos-13, ubuntu-latest]
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ^1.17
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
go-version-file: go.mod
check-latest: true
- name: Compile
run: make install
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
os: [macos-13, ubuntu-latest]
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ^1.17
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
go-version-file: go.mod
check-latest: true
- name: Install certutil on macos
if: ${{ matrix.os == 'macos-13' }}
run: |
brew install nss
- name: Test
run: make test
vet:
name: Vet
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
os: [macos-13, ubuntu-latest]
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
check-latest: true
- uses: golangci/golangci-lint-action@v3
with:
version: v1.35
args: --timeout 180s
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ build:

.PHONY: vet
vet:
docker run --rm -v $$(pwd):/app -w /app golangci/golangci-lint:latest golangci-lint run -v
docker run --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:latest golangci-lint run -v

.PHONY: test
test:
go test ./... -timeout=180s -coverprofile=c.out -covermode=atomic -count=1 -race -v
go test ./... -timeout=5m -coverprofile=c.out -covermode=atomic -count=1 -race -v
6 changes: 3 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package candy

import (
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -29,7 +29,7 @@ type AppService struct {
}

func (f *AppService) FindApps() ([]App, error) {
files, err := ioutil.ReadDir(f.cfg.HostRoot)
files, err := os.ReadDir(f.cfg.HostRoot)
if err != nil {
return nil, err
}
Expand All @@ -41,7 +41,7 @@ func (f *AppService) FindApps() ([]App, error) {
continue
}

b, err := ioutil.ReadFile(filepath.Join(f.cfg.HostRoot, file.Name()))
b, err := os.ReadFile(filepath.Join(f.cfg.HostRoot, file.Name()))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package candy

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -104,7 +104,7 @@ func Test_AppService_FindApps(t *testing.T) {
dir := t.TempDir()

for k, v := range cc.Hosts {
if err := ioutil.WriteFile(filepath.Join(dir, k), []byte(v), 0o0644); err != nil {
if err := os.WriteFile(filepath.Join(dir, k), []byte(v), 0o0644); err != nil {
t.Fatalf("error writing test hosts: %s", err)
}
}
Expand Down
36 changes: 30 additions & 6 deletions caddy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -54,6 +53,29 @@ type caddyServer struct {
caddyCfgMutex sync.Mutex
}

func (c *caddyServer) waitForServer(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

t := time.NewTicker(1 * time.Second)
defer t.Stop()

for {
select {
case <-ctx.Done():
return ctx.Err()
case <-t.C:
c.cfg.Logger.Info("waiting for Caddy server", zap.Any("cfg", c.cfg))
err := c.apiRequest(ctx, http.MethodGet, "/config/", nil)
if err == nil {
return nil
} else {
c.cfg.Logger.Debug("error waiting for Caddy server", zap.Error(err))
}
}
}
}

func (c *caddyServer) Run(ctx context.Context) error {
c.cfg.Logger.Info("starting Caddy server", zap.Any("cfg", c.cfg))
defer c.cfg.Logger.Info("shutting down Caddy server")
Expand All @@ -64,6 +86,10 @@ func (c *caddyServer) Run(ctx context.Context) error {
return err
}

if err := c.waitForServer(ctx); err != nil {
return err
}

<-ctx.Done()

if err := c.stopServer(); err != nil {
Expand Down Expand Up @@ -139,7 +165,6 @@ func (c *caddyServer) buildConfig(apps []candy.App) *caddy.Config {
),
Listen: []string{c.cfg.HTTPAddr},
AutoHTTPS: &caddyhttp.AutoHTTPSConfig{Disabled: true},
AllowH2C: true,
}

httpsServer := &caddyhttp.Server{
Expand All @@ -155,8 +180,7 @@ func (c *caddyServer) buildConfig(apps []candy.App) *caddy.Config {
},
apps,
),
Listen: []string{c.cfg.HTTPSAddr},
AllowH2C: true,
Listen: []string{c.cfg.HTTPSAddr},
}

// Best efforts of parsing corresponding port from addr
Expand All @@ -180,7 +204,7 @@ func (c *caddyServer) buildConfig(apps []candy.App) *caddy.Config {
Automation: &caddytls.AutomationConfig{
Policies: []*caddytls.AutomationPolicy{
{
Subjects: appHosts(apps),
SubjectsRaw: appHosts(apps),
IssuersRaw: []json.RawMessage{
caddyconfig.JSONModuleObject(caddytls.InternalIssuer{}, "module", "internal", nil),
},
Expand Down Expand Up @@ -275,7 +299,7 @@ func (c *caddyServer) apiRequest(ctx context.Context, method, uri string, v inte

// if it didn't work, let the user know
if resp.StatusCode >= 400 {
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024*10))
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1024*10))
if err != nil {
return fmt.Errorf("HTTP %d: reading error message: %v", resp.StatusCode, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/candy/cmd/launch_darwin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin
//go:build darwin

package cmd

Expand Down
7 changes: 3 additions & 4 deletions cmd/candy/cmd/setup_darwin.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// +build darwin
//go:build darwin

package cmd

import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -75,7 +74,7 @@ func runSetupRunE(c *cobra.Command, args []string) error {
file := filepath.Join(resolverDir, "candy-"+domain)
content := fmt.Sprintf(resolverTmpl, domain, host, port)

b, err := ioutil.ReadFile(file)
b, err := os.ReadFile(file)
if err == nil {
if string(b) == content {
logger.Info("resolver configuration file unchanged", zap.String("file", file))
Expand All @@ -84,7 +83,7 @@ func runSetupRunE(c *cobra.Command, args []string) error {
}

logger.Info("writing resolver configuration file", zap.String("file", file))
if err := ioutil.WriteFile(file, []byte(content), 0o644); err != nil {
if err := os.WriteFile(file, []byte(content), 0o644); err != nil {
return err
}
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/candy/cmd/setup_linux.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// +build linux
//go:build linux

package cmd

import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -69,7 +68,7 @@ func runSetupRunE(c *cobra.Command, args []string) error {
logger = candy.Log()
)

b, err := ioutil.ReadFile(file)
b, err := os.ReadFile(file)
if err == nil {
if string(b) == content {
logger.Info("network name resolution file unchanged", zap.String("file", file))
Expand All @@ -78,7 +77,7 @@ func runSetupRunE(c *cobra.Command, args []string) error {
}

logger.Info("writing network name resolution file", zap.String("file", file))
if err := ioutil.WriteFile(file, []byte(content), 0o644); err != nil {
if err := os.WriteFile(file, []byte(content), 0o644); err != nil {
return err
}

Expand Down
11 changes: 0 additions & 11 deletions dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net"
"sync"
"time"

"github.com/miekg/dns"
Expand Down Expand Up @@ -41,36 +40,26 @@ func (d *dnsServer) Run(ctx context.Context) error {

var g run.Group
{
var wg sync.WaitGroup
wg.Add(1)
udp := &dns.Server{
Handler: mux,
Addr: d.cfg.Addr,
Net: "udp",
}
g.Add(func() error {
wg.Done()
return udp.ListenAndServe()
}, func(err error) {
// Wait for udp server before shutting it down
wg.Wait()
_ = udp.ShutdownContext(ctx)
})
}
{
var wg sync.WaitGroup
wg.Add(1)
tcp := &dns.Server{
Handler: mux,
Addr: d.cfg.Addr,
Net: "tcp",
}
g.Add(func() error {
wg.Done()
return tcp.ListenAndServe()
}, func(err error) {
// Wait for tcp server before shutting it down
wg.Wait()
_ = tcp.ShutdownContext(ctx)
})
}
Expand Down
Loading
Loading