mvp... minimum viable proxy

This commit is contained in:
Michael Quigley 2022-07-20 14:36:14 -04:00
parent 38f0076b61
commit 33ff69f85f
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 46 additions and 16 deletions

View File

@ -1,15 +1,13 @@
package main
import (
"context"
"github.com/openziti-test-kitchen/zrok/util"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/spf13/cobra"
"io"
"net"
"net/http"
"os"
"strings"
)
func init() {
@ -28,7 +26,7 @@ func curl(_ *cobra.Command, args []string) {
panic(err)
}
zCtx := ziti.NewContextWithConfig(zCfg)
zDialContext := zitiDialContext{context: zCtx}
zDialContext := util.ZitiDialContext{Context: zCtx}
zTransport := http.DefaultTransport.(*http.Transport).Clone()
zTransport.DialContext = zDialContext.Dial
client := &http.Client{Transport: zTransport}
@ -41,12 +39,3 @@ func curl(_ *cobra.Command, args []string) {
panic(err)
}
}
type zitiDialContext struct {
context ziti.Context
}
func (dc *zitiDialContext) Dial(_ context.Context, _ string, addr string) (net.Conn, error) {
service := strings.Split(addr, ":")[0] // will always get passed host:port
return dc.context.Dial(service)
}

View File

@ -33,7 +33,7 @@ var proxyCmd = &cobra.Command{
Use: "proxy <configPath>",
Short: "Start a zrok proxy",
Run: func(_ *cobra.Command, args []string) {
if err := proxy.Run(&proxy.Config{Address: "0.0.0.0:10081"}); err != nil {
if err := proxy.Run(&proxy.Config{IdentityPath: args[0], Address: "0.0.0.0:10081"}); err != nil {
panic(err)
}
},

View File

@ -1,11 +1,14 @@
package proxy
import (
"fmt"
"github.com/openziti-test-kitchen/zrok/util"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io"
"net/http"
"time"
)
func Run(cfg *Config) error {
@ -28,8 +31,29 @@ type handler struct {
}
func (self *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(time.Now().String()))
logrus.Infof("handling request from [%v]", r.RemoteAddr)
zDialCtx := util.ZitiDialContext{Context: self.zCtx}
zTransport := http.DefaultTransport.(*http.Transport).Clone()
zTransport.DialContext = zDialCtx.Dial
client := &http.Client{Transport: zTransport}
r.Host = "zrok"
r.URL.Host = "zrok"
r.URL.Scheme = "http"
r.RequestURI = ""
logrus.Warnf("request: %v", r)
pResp, err := client.Do(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprint(w, err)
return
}
n, err := io.Copy(w, pResp.Body)
if err != nil {
panic(err)
}
logrus.Infof("proxied [%d] bytes", n)
}

17
util/zdialctx.go Normal file
View File

@ -0,0 +1,17 @@
package util
import (
"context"
"github.com/openziti/sdk-golang/ziti"
"net"
"strings"
)
type ZitiDialContext struct {
Context ziti.Context
}
func (self *ZitiDialContext) Dial(_ context.Context, _ string, addr string) (net.Conn, error) {
service := strings.Split(addr, ":")[0] // ignore :port (we get passed 'host:port')
return self.Context.Dial(service)
}