mirror of
https://github.com/openziti/zrok.git
synced 2024-11-21 23:53:19 +01:00
--backend-mode caddy rough-in (#391)
This commit is contained in:
parent
ce06804a0f
commit
a96f3e9f96
@ -1,57 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(newCaddyCommand().cmd)
|
||||
}
|
||||
|
||||
type caddyCommand struct {
|
||||
cmd *cobra.Command
|
||||
}
|
||||
|
||||
func newCaddyCommand() *caddyCommand {
|
||||
cmd := &cobra.Command{
|
||||
Use: "caddy <configPath>",
|
||||
Short: "Run an embedded caddy backend",
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
command := &caddyCommand{cmd: cmd}
|
||||
cmd.Run = command.run
|
||||
return command
|
||||
}
|
||||
|
||||
func (cmd *caddyCommand) run(_ *cobra.Command, args []string) {
|
||||
if err := caddy.Run(&caddy.Config{}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(args[0])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var adapter caddyfile.Adapter
|
||||
adapter.ServerType = httpcaddyfile.ServerType{}
|
||||
cfg, warn, err := adapter.Adapt(data, map[string]interface{}{"filename": args[0]})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, w := range warn {
|
||||
fmt.Println(w.Message)
|
||||
}
|
||||
fmt.Printf("cfg: %v\n", string(cfg))
|
||||
if err := caddy.Load(cfg, true); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
time.Sleep(30 * time.Minute)
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/openziti/zrok/endpoints"
|
||||
"github.com/openziti/zrok/endpoints/caddyf"
|
||||
"github.com/openziti/zrok/endpoints/proxy"
|
||||
"github.com/openziti/zrok/environment"
|
||||
"github.com/openziti/zrok/environment/env_core"
|
||||
@ -39,7 +40,7 @@ func newSharePublicCommand() *sharePublicCommand {
|
||||
command := &sharePublicCommand{cmd: cmd}
|
||||
cmd.Flags().StringArrayVar(&command.basicAuth, "basic-auth", []string{}, "Basic authentication users (<username:password>,...)")
|
||||
cmd.Flags().StringArrayVar(&command.frontendSelection, "frontends", []string{"public"}, "Selected frontends to use for the share")
|
||||
cmd.Flags().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web}")
|
||||
cmd.Flags().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web, caddy}")
|
||||
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
|
||||
cmd.Flags().BoolVar(&command.insecure, "insecure", false, "Enable insecure TLS certificate validation for <target>")
|
||||
cmd.Run = command.run
|
||||
@ -63,6 +64,10 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
||||
case "web":
|
||||
target = args[0]
|
||||
|
||||
case "caddy":
|
||||
target = args[0]
|
||||
cmd.headless = true
|
||||
|
||||
default:
|
||||
tui.Error(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil)
|
||||
}
|
||||
@ -95,6 +100,12 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
||||
Target: target,
|
||||
}
|
||||
shr, err := sdk.CreateShare(env, req)
|
||||
if err != nil {
|
||||
if !panicInstead {
|
||||
tui.Error("unable to create share", err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mdl := newShareModel(shr.Token, shr.FrontendEndpoints, sdk.PublicShareMode, sdk.BackendMode(cmd.backendMode))
|
||||
if !cmd.headless {
|
||||
@ -157,6 +168,27 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
||||
}
|
||||
}()
|
||||
|
||||
case "caddy":
|
||||
cfg := &caddyf.BackendConfig{
|
||||
CaddyfilePath: target,
|
||||
Shr: shr,
|
||||
Requests: requests,
|
||||
}
|
||||
|
||||
be, err := caddyf.NewBackend(cfg)
|
||||
if err != nil {
|
||||
if !panicInstead {
|
||||
tui.Error("unable to create caddy backend", err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := be.Run(); err != nil {
|
||||
logrus.Errorf("error running caddy backend: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
default:
|
||||
tui.Error("invalid backend mode", nil)
|
||||
}
|
||||
|
51
endpoints/caddyf/backend.go
Normal file
51
endpoints/caddyf/backend.go
Normal file
@ -0,0 +1,51 @@
|
||||
package caddyf
|
||||
|
||||
import (
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||
"github.com/openziti/zrok/endpoints"
|
||||
"github.com/openziti/zrok/sdk"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type BackendConfig struct {
|
||||
CaddyfilePath string
|
||||
Shr *sdk.Share
|
||||
Requests chan *endpoints.Request
|
||||
}
|
||||
|
||||
type Backend struct {
|
||||
cdycfg []byte
|
||||
}
|
||||
|
||||
func NewBackend(cfg *BackendConfig) (*Backend, error) {
|
||||
cdyf, err := preprocessCaddyfile(cfg.CaddyfilePath, cfg.Shr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var adapter caddyfile.Adapter
|
||||
adapter.ServerType = httpcaddyfile.ServerType{}
|
||||
cdycfg, warnings, err := adapter.Adapt([]byte(cdyf), map[string]interface{}{"filename": cfg.CaddyfilePath})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, warning := range warnings {
|
||||
logrus.Warnf("%v [%d] (%v): %v", cfg.CaddyfilePath, warning.Line, warning.Directive, warning.Message)
|
||||
}
|
||||
return &Backend{cdycfg: cdycfg}, nil
|
||||
}
|
||||
|
||||
func (b *Backend) Run() error {
|
||||
if err := caddy.Run(&caddy.Config{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := caddy.Load(b.cdycfg, true); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Backend) Requests() func() int32 {
|
||||
return nil
|
||||
}
|
24
endpoints/caddyf/preprocessor.go
Normal file
24
endpoints/caddyf/preprocessor.go
Normal file
@ -0,0 +1,24 @@
|
||||
package caddyf
|
||||
|
||||
import (
|
||||
"github.com/openziti/zrok/sdk"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func preprocessCaddyfile(inF string, shr *sdk.Share) (string, error) {
|
||||
input, err := os.ReadFile(inF)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
tmpl, err := template.New(inF).Parse(string(input))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
output := new(strings.Builder)
|
||||
if err := tmpl.Execute(output, shr); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return output.String(), nil
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
http:// {
|
||||
bind zrok/nhtlakfk3pui
|
||||
reverse_proxy 127.0.0.1:3000 {
|
||||
header_up Host http://localhost:3000
|
||||
}
|
||||
bind zrok/{{ .Token }}
|
||||
reverse_proxy 127.0.0.1:3000
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ type ShareRequest struct {
|
||||
AuthUsers []*AuthUser `json:"authUsers"`
|
||||
|
||||
// backend mode
|
||||
// Enum: [proxy web tcpTunnel udpTunnel]
|
||||
// Enum: [proxy web tcpTunnel udpTunnel caddy]
|
||||
BackendMode string `json:"backendMode,omitempty"`
|
||||
|
||||
// backend proxy endpoint
|
||||
@ -100,7 +100,7 @@ var shareRequestTypeBackendModePropEnum []interface{}
|
||||
|
||||
func init() {
|
||||
var res []string
|
||||
if err := json.Unmarshal([]byte(`["proxy","web","tcpTunnel","udpTunnel"]`), &res); err != nil {
|
||||
if err := json.Unmarshal([]byte(`["proxy","web","tcpTunnel","udpTunnel","caddy"]`), &res); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, v := range res {
|
||||
@ -121,6 +121,9 @@ const (
|
||||
|
||||
// ShareRequestBackendModeUDPTunnel captures enum value "udpTunnel"
|
||||
ShareRequestBackendModeUDPTunnel string = "udpTunnel"
|
||||
|
||||
// ShareRequestBackendModeCaddy captures enum value "caddy"
|
||||
ShareRequestBackendModeCaddy string = "caddy"
|
||||
)
|
||||
|
||||
// prop value enum
|
||||
|
@ -1468,7 +1468,8 @@ func init() {
|
||||
"proxy",
|
||||
"web",
|
||||
"tcpTunnel",
|
||||
"udpTunnel"
|
||||
"udpTunnel",
|
||||
"caddy"
|
||||
]
|
||||
},
|
||||
"backendProxyEndpoint": {
|
||||
@ -3064,7 +3065,8 @@ func init() {
|
||||
"proxy",
|
||||
"web",
|
||||
"tcpTunnel",
|
||||
"udpTunnel"
|
||||
"udpTunnel",
|
||||
"caddy"
|
||||
]
|
||||
},
|
||||
"backendProxyEndpoint": {
|
||||
|
@ -7,6 +7,7 @@ const (
|
||||
WebBackendMode BackendMode = "web"
|
||||
TcpTunnelBackendMode BackendMode = "tcpTunnel"
|
||||
UdpTunnelBackendMode BackendMode = "udpTunnel"
|
||||
CaddyBackendMode BackendMode = "caddy"
|
||||
)
|
||||
|
||||
type ShareMode string
|
||||
|
@ -969,7 +969,7 @@ definitions:
|
||||
type: string
|
||||
backendMode:
|
||||
type: string
|
||||
enum: ["proxy", "web", "tcpTunnel", "udpTunnel"]
|
||||
enum: ["proxy", "web", "tcpTunnel", "udpTunnel", "caddy"]
|
||||
backendProxyEndpoint:
|
||||
type: string
|
||||
authScheme:
|
||||
|
Loading…
Reference in New Issue
Block a user