mirror of
https://github.com/openziti/zrok.git
synced 2024-12-22 23:02:52 +01:00
mvp... minimum viable proxy
This commit is contained in:
parent
38f0076b61
commit
33ff69f85f
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
},
|
||||
|
@ -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
17
util/zdialctx.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user