temporary switch to support tcp/udp toggling (#306)

This commit is contained in:
Michael Quigley 2023-04-21 14:20:40 -04:00
parent 517eeb8412
commit e19186c1a0
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 67 additions and 34 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/go-openapi/runtime" "github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/endpoints/tcpTunnel" "github.com/openziti/zrok/endpoints/tcpTunnel"
"github.com/openziti/zrok/endpoints/udpTunnel"
"github.com/openziti/zrok/rest_client_zrok" "github.com/openziti/zrok/rest_client_zrok"
"github.com/openziti/zrok/rest_client_zrok/share" "github.com/openziti/zrok/rest_client_zrok/share"
"github.com/openziti/zrok/rest_model_zrok" "github.com/openziti/zrok/rest_model_zrok"
@ -23,6 +24,7 @@ func init() {
type accessPrivateTunnelCommand struct { type accessPrivateTunnelCommand struct {
bindAddress string bindAddress string
udp bool
cmd *cobra.Command cmd *cobra.Command
} }
@ -34,6 +36,7 @@ func newAccessPrivateTunnelCommand() *accessPrivateTunnelCommand {
} }
command := &accessPrivateTunnelCommand{cmd: cmd} command := &accessPrivateTunnelCommand{cmd: cmd}
cmd.Flags().StringVarP(&command.bindAddress, "bind", "b", "tcp:127.0.0.1:9191", "The address to bind the private tunnel") cmd.Flags().StringVarP(&command.bindAddress, "bind", "b", "tcp:127.0.0.1:9191", "The address to bind the private tunnel")
cmd.Flags().BoolVar(&command.udp, "udp", false, "Use UDP")
cmd.Run = command.run cmd.Run = command.run
return command return command
} }
@ -76,16 +79,31 @@ func (cmd *accessPrivateTunnelCommand) run(_ *cobra.Command, args []string) {
os.Exit(0) os.Exit(0)
}() }()
fe, err := tcpTunnel.NewFrontend(&tcpTunnel.FrontendConfig{ if cmd.udp {
BindAddress: cmd.bindAddress, fe, err := udpTunnel.NewFrontend(&udpTunnel.FrontendConfig{
IdentityName: "backend", BindAddress: cmd.bindAddress,
ShrToken: args[0], IdentityName: "backend",
}) ShrToken: args[0],
if err != nil { })
panic(err) if err != nil {
} panic(err)
if err := fe.Run(); err != nil { }
panic(err) if err := fe.Run(); err != nil {
panic(err)
}
} else {
fe, err := tcpTunnel.NewFrontend(&tcpTunnel.FrontendConfig{
BindAddress: cmd.bindAddress,
IdentityName: "backend",
ShrToken: args[0],
})
if err != nil {
panic(err)
}
if err := fe.Run(); err != nil {
panic(err)
}
} }
for { for {
time.Sleep(30 * 24 * time.Hour) time.Sleep(30 * 24 * time.Hour)

View File

@ -8,6 +8,7 @@ import (
"github.com/openziti/zrok/endpoints" "github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/endpoints/proxy" "github.com/openziti/zrok/endpoints/proxy"
"github.com/openziti/zrok/endpoints/tcpTunnel" "github.com/openziti/zrok/endpoints/tcpTunnel"
"github.com/openziti/zrok/endpoints/udpTunnel"
"github.com/openziti/zrok/model" "github.com/openziti/zrok/model"
"github.com/openziti/zrok/rest_client_zrok" "github.com/openziti/zrok/rest_client_zrok"
"github.com/openziti/zrok/rest_client_zrok/share" "github.com/openziti/zrok/rest_client_zrok/share"
@ -32,6 +33,7 @@ type sharePrivateCommand struct {
backendMode string backendMode string
headless bool headless bool
insecure bool insecure bool
udp bool
cmd *cobra.Command cmd *cobra.Command
} }
@ -46,6 +48,7 @@ func newSharePrivateCommand() *sharePrivateCommand {
cmd.Flags().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web, tunnel}") cmd.Flags().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web, tunnel}")
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless") 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.Flags().BoolVar(&command.insecure, "insecure", false, "Enable insecure TLS certificate validation for <target>")
cmd.Flags().BoolVar(&command.udp, "udp", false, "Enable UDP for tunnel backend")
cmd.Run = command.run cmd.Run = command.run
return command return command
} }
@ -173,16 +176,43 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
} }
case "tunnel": case "tunnel":
cfg := &tcpTunnel.BackendConfig{ if cmd.udp {
IdentityPath: zif, cfg := &udpTunnel.BackendConfig{
EndpointAddress: target, IdentityPath: zif,
ShrToken: resp.Payload.ShrToken, EndpointAddress: target,
} ShrToken: resp.Payload.ShrToken,
if err := cmd.tunnelBackendMode(cfg); err != nil {
if !panicInstead {
tui.Error("unable to create tunnel backend", err)
} }
panic(err) be, err := udpTunnel.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("unable to create udp tunnel backend", err)
}
panic(err)
}
go func() {
if err := be.Run(); err != nil {
logrus.Errorf("error running udp tunnel backend: %v", err)
}
}()
} else {
cfg := &tcpTunnel.BackendConfig{
IdentityPath: zif,
EndpointAddress: target,
ShrToken: resp.Payload.ShrToken,
}
be, err := tcpTunnel.NewBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("unable to create tunnel backend", err)
}
panic(err)
}
go func() {
if err := be.Run(); err != nil {
logrus.Errorf("error running tunnel backend: %v", err)
}
}()
} }
default: default:
@ -253,21 +283,6 @@ func (cmd *sharePrivateCommand) webBackendMode(cfg *proxy.WebBackendConfig) (end
return be, nil return be, nil
} }
func (cmd *sharePrivateCommand) tunnelBackendMode(cfg *tcpTunnel.BackendConfig) error {
be, err := tcpTunnel.NewBackend(cfg)
if err != nil {
return errors.Wrap(err, "error creating tunnel backend")
}
go func() {
if err := be.Run(); err != nil {
logrus.Errorf("error running tunnel backend: %v", err)
}
}()
return nil
}
func (cmd *sharePrivateCommand) destroy(id string, shrToken string, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) { func (cmd *sharePrivateCommand) destroy(id string, shrToken string, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
logrus.Debugf("shutting down '%v'", shrToken) logrus.Debugf("shutting down '%v'", shrToken)
req := share.NewUnshareParams() req := share.NewUnshareParams()