mirror of
https://github.com/openziti/zrok.git
synced 2024-11-26 01:54:09 +01:00
back share public web backend (#95)
This commit is contained in:
parent
ac09f2d749
commit
e27257daf6
@ -5,7 +5,7 @@ import (
|
|||||||
ui "github.com/gizak/termui/v3"
|
ui "github.com/gizak/termui/v3"
|
||||||
"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-test-kitchen/zrok/endpoints/backend"
|
"github.com/openziti-test-kitchen/zrok/endpoints/proxy_backend"
|
||||||
"github.com/openziti-test-kitchen/zrok/model"
|
"github.com/openziti-test-kitchen/zrok/model"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok"
|
"github.com/openziti-test-kitchen/zrok/rest_client_zrok"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/service"
|
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/service"
|
||||||
@ -80,7 +80,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
cfg := &backend.Config{
|
cfg := &proxy_backend.Config{
|
||||||
IdentityPath: zif,
|
IdentityPath: zif,
|
||||||
EndpointAddress: target,
|
EndpointAddress: target,
|
||||||
}
|
}
|
||||||
@ -155,8 +155,8 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *sharePrivateCommand) proxyBackendMode(cfg *backend.Config) (backendHandler, error) {
|
func (cmd *sharePrivateCommand) proxyBackendMode(cfg *proxy_backend.Config) (backendHandler, error) {
|
||||||
httpProxy, err := backend.NewHTTP(cfg)
|
httpProxy, err := proxy_backend.NewBackend(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "error creating http proxy backend")
|
return nil, errors.Wrap(err, "error creating http proxy backend")
|
||||||
}
|
}
|
||||||
@ -170,7 +170,7 @@ func (cmd *sharePrivateCommand) proxyBackendMode(cfg *backend.Config) (backendHa
|
|||||||
return httpProxy, nil
|
return httpProxy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *sharePrivateCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
func (cmd *sharePrivateCommand) destroy(id string, cfg *proxy_backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
||||||
logrus.Debugf("shutting down '%v'", cfg.Service)
|
logrus.Debugf("shutting down '%v'", cfg.Service)
|
||||||
req := service.NewUnshareParams()
|
req := service.NewUnshareParams()
|
||||||
req.Body = &rest_model_zrok.UnshareRequest{
|
req.Body = &rest_model_zrok.UnshareRequest{
|
||||||
|
@ -7,7 +7,8 @@ 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"
|
||||||
tb "github.com/nsf/termbox-go"
|
tb "github.com/nsf/termbox-go"
|
||||||
"github.com/openziti-test-kitchen/zrok/endpoints/backend"
|
"github.com/openziti-test-kitchen/zrok/endpoints/proxy_backend"
|
||||||
|
"github.com/openziti-test-kitchen/zrok/endpoints/web_backend"
|
||||||
"github.com/openziti-test-kitchen/zrok/model"
|
"github.com/openziti-test-kitchen/zrok/model"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok"
|
"github.com/openziti-test-kitchen/zrok/rest_client_zrok"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/service"
|
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/service"
|
||||||
@ -68,6 +69,9 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
target = targetEndpoint.String()
|
target = targetEndpoint.String()
|
||||||
|
|
||||||
|
case "web":
|
||||||
|
target = args[0]
|
||||||
|
|
||||||
default:
|
default:
|
||||||
showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil)
|
showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", cmd.backendMode), nil)
|
||||||
}
|
}
|
||||||
@ -99,10 +103,6 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
cfg := &backend.Config{
|
|
||||||
IdentityPath: zif,
|
|
||||||
EndpointAddress: target,
|
|
||||||
}
|
|
||||||
|
|
||||||
zrok, err := zrokdir.ZrokClient(env.ApiEndpoint)
|
zrok, err := zrokdir.ZrokClient(env.ApiEndpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -119,7 +119,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
ShareMode: "public",
|
ShareMode: "public",
|
||||||
FrontendSelection: cmd.frontendSelection,
|
FrontendSelection: cmd.frontendSelection,
|
||||||
BackendMode: "proxy",
|
BackendMode: "proxy",
|
||||||
BackendProxyEndpoint: cfg.EndpointAddress,
|
BackendProxyEndpoint: target,
|
||||||
AuthScheme: string(model.None),
|
AuthScheme: string(model.None),
|
||||||
}
|
}
|
||||||
if len(cmd.basicAuth) > 0 {
|
if len(cmd.basicAuth) > 0 {
|
||||||
@ -142,19 +142,23 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
cfg.Service = resp.Payload.SvcToken
|
|
||||||
|
|
||||||
c := make(chan os.Signal)
|
c := make(chan os.Signal)
|
||||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
go func() {
|
go func() {
|
||||||
<-c
|
<-c
|
||||||
cmd.destroy(env.ZId, cfg, zrok, auth)
|
cmd.destroy(env.ZId, resp.Payload.SvcToken, zrok, auth)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var bh backendHandler
|
var bh backendHandler
|
||||||
switch cmd.backendMode {
|
switch cmd.backendMode {
|
||||||
case "proxy":
|
case "proxy":
|
||||||
|
cfg := &proxy_backend.Config{
|
||||||
|
IdentityPath: zif,
|
||||||
|
EndpointAddress: target,
|
||||||
|
Service: resp.Payload.SvcToken,
|
||||||
|
}
|
||||||
bh, err = cmd.proxyBackendMode(cfg)
|
bh, err = cmd.proxyBackendMode(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Close()
|
ui.Close()
|
||||||
@ -164,6 +168,21 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "web":
|
||||||
|
cfg := &web_backend.Config{
|
||||||
|
IdentityPath: zif,
|
||||||
|
WebRoot: target,
|
||||||
|
Service: resp.Payload.SvcToken,
|
||||||
|
}
|
||||||
|
bh, err = cmd.webBackendMode(cfg)
|
||||||
|
if err != nil {
|
||||||
|
ui.Close()
|
||||||
|
if !panicInstead {
|
||||||
|
showError("unable to create web backend handler", err)
|
||||||
|
}
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
ui.Close()
|
ui.Close()
|
||||||
showError("invalid backend mode", nil)
|
showError("invalid backend mode", nil)
|
||||||
@ -210,7 +229,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
switch e.ID {
|
switch e.ID {
|
||||||
case "q", "<C-c>":
|
case "q", "<C-c>":
|
||||||
ui.Close()
|
ui.Close()
|
||||||
cmd.destroy(env.ZId, cfg, zrok, auth)
|
cmd.destroy(env.ZId, resp.Payload.SvcToken, zrok, auth)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -237,27 +256,42 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *sharePublicCommand) proxyBackendMode(cfg *backend.Config) (backendHandler, error) {
|
func (cmd *sharePublicCommand) proxyBackendMode(cfg *proxy_backend.Config) (backendHandler, error) {
|
||||||
httpProxy, err := backend.NewHTTP(cfg)
|
be, err := proxy_backend.NewBackend(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "error creating http proxy backend")
|
return nil, errors.Wrap(err, "error creating http proxy backend")
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := httpProxy.Run(); err != nil {
|
if err := be.Run(); err != nil {
|
||||||
logrus.Errorf("error running http proxy backend: %v", err)
|
logrus.Errorf("error running http proxy backend: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return httpProxy, nil
|
return be, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *sharePublicCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
func (cmd *sharePublicCommand) webBackendMode(cfg *web_backend.Config) (backendHandler, error) {
|
||||||
logrus.Debugf("shutting down '%v'", cfg.Service)
|
be, err := web_backend.NewBackend(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error creating http web backend")
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := be.Run(); err != nil {
|
||||||
|
logrus.Errorf("error running http web backend: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return be, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cmd *sharePublicCommand) destroy(id string, svcToken string, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
||||||
|
logrus.Debugf("shutting down '%v'", svcToken)
|
||||||
req := service.NewUnshareParams()
|
req := service.NewUnshareParams()
|
||||||
req.Body = &rest_model_zrok.UnshareRequest{
|
req.Body = &rest_model_zrok.UnshareRequest{
|
||||||
EnvZID: id,
|
EnvZID: id,
|
||||||
SvcToken: cfg.Service,
|
SvcToken: svcToken,
|
||||||
}
|
}
|
||||||
if _, err := zrok.Service.Unshare(req, auth); err == nil {
|
if _, err := zrok.Service.Unshare(req, auth); err == nil {
|
||||||
logrus.Debugf("shutdown complete")
|
logrus.Debugf("shutdown complete")
|
||||||
|
@ -3,7 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
ui "github.com/gizak/termui/v3"
|
ui "github.com/gizak/termui/v3"
|
||||||
httptransport "github.com/go-openapi/runtime/client"
|
httptransport "github.com/go-openapi/runtime/client"
|
||||||
"github.com/openziti-test-kitchen/zrok/endpoints/backend"
|
"github.com/openziti-test-kitchen/zrok/endpoints/proxy_backend"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/service"
|
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/service"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
||||||
"github.com/openziti-test-kitchen/zrok/zrokdir"
|
"github.com/openziti-test-kitchen/zrok/zrokdir"
|
||||||
@ -91,7 +91,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
cfg := &backend.Config{
|
cfg := &proxy_backend.Config{
|
||||||
IdentityPath: zif,
|
IdentityPath: zif,
|
||||||
EndpointAddress: targetEndpoint,
|
EndpointAddress: targetEndpoint,
|
||||||
Service: svcToken,
|
Service: svcToken,
|
||||||
@ -115,7 +115,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
|
|||||||
logrus.Infof("using existing backend proxy endpoint: %v", targetEndpoint)
|
logrus.Infof("using existing backend proxy endpoint: %v", targetEndpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
httpProxy, err := backend.NewHTTP(cfg)
|
httpProxy, err := proxy_backend.NewBackend(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Close()
|
ui.Close()
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package backend
|
package proxy_backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -21,14 +21,14 @@ type Config struct {
|
|||||||
Service string
|
Service string
|
||||||
}
|
}
|
||||||
|
|
||||||
type httpBind struct {
|
type backend struct {
|
||||||
cfg *Config
|
cfg *Config
|
||||||
requests func() int32
|
requests func() int32
|
||||||
listener edge.Listener
|
listener edge.Listener
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTP(cfg *Config) (*httpBind, error) {
|
func NewBackend(cfg *Config) (*backend, error) {
|
||||||
options := ziti.ListenOptions{
|
options := ziti.ListenOptions{
|
||||||
ConnectTimeout: 5 * time.Minute,
|
ConnectTimeout: 5 * time.Minute,
|
||||||
MaxConnections: 64,
|
MaxConnections: 64,
|
||||||
@ -48,7 +48,7 @@ func NewHTTP(cfg *Config) (*httpBind, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handler := util.NewProxyHandler(proxy)
|
handler := util.NewProxyHandler(proxy)
|
||||||
return &httpBind{
|
return &backend{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
requests: handler.Requests,
|
requests: handler.Requests,
|
||||||
listener: listener,
|
listener: listener,
|
||||||
@ -56,14 +56,14 @@ func NewHTTP(cfg *Config) (*httpBind, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *httpBind) Run() error {
|
func (self *backend) Run() error {
|
||||||
if err := http.Serve(self.listener, self.handler); err != nil {
|
if err := http.Serve(self.listener, self.handler); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *httpBind) Requests() func() int32 {
|
func (self *backend) Requests() func() int32 {
|
||||||
return self.requests
|
return self.requests
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package backend
|
package proxy_backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
54
endpoints/web_backend/web.go
Normal file
54
endpoints/web_backend/web.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package web_backend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/openziti/sdk-golang/ziti"
|
||||||
|
"github.com/openziti/sdk-golang/ziti/config"
|
||||||
|
"github.com/openziti/sdk-golang/ziti/edge"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
IdentityPath string
|
||||||
|
WebRoot string
|
||||||
|
Service string
|
||||||
|
}
|
||||||
|
|
||||||
|
type backend struct {
|
||||||
|
cfg *Config
|
||||||
|
listener edge.Listener
|
||||||
|
handler http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBackend(cfg *Config) (*backend, error) {
|
||||||
|
options := ziti.ListenOptions{
|
||||||
|
ConnectTimeout: 5 * time.Minute,
|
||||||
|
MaxConnections: 64,
|
||||||
|
}
|
||||||
|
zcfg, err := config.NewFromFile(cfg.IdentityPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error loading config")
|
||||||
|
}
|
||||||
|
listener, err := ziti.NewContextWithConfig(zcfg).ListenWithOptions(cfg.Service, &options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error listening")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &backend{
|
||||||
|
cfg: cfg,
|
||||||
|
listener: listener,
|
||||||
|
handler: http.FileServer(http.Dir(cfg.WebRoot)),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *backend) Run() error {
|
||||||
|
if err := http.Serve(self.listener, self.handler); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *backend) Requests() func() int32 {
|
||||||
|
return func() int32 { return 0 }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user