Skip to content

Commit

Permalink
Merge pull request #259 from openziti/websockets
Browse files Browse the repository at this point in the history
Updated ziti sdk package to better handle proxy requests. Corrects websocket support in `proxy` backend.
  • Loading branch information
michaelquigley authored Mar 6, 2023
2 parents 5a2340e + 8ea8ec9 commit 72e3bc9
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 86 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ etc/dev.yml
.env.development.local
.env.test.local
.env.production.local
go.work
go.work.sum

npm-debug.log*
yarn-debug.log*
Expand Down
68 changes: 62 additions & 6 deletions cmd/zrok/testEndpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"net"
"net/http"
"os"
"time"

"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/openziti/zrok/cmd/zrok/endpointUi"
"github.com/openziti/zrok/tui"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/time/rate"
"html/template"
"io"
"net"
"net/http"
"nhooyr.io/websocket"
"time"
)

func init() {
Expand All @@ -26,6 +32,10 @@ type testEndpointCommand struct {
port uint16
t *template.Template
cmd *cobra.Command

enableZiti bool
serviceName string
identityJsonFile string
}

func newTestEndpointCommand() *testEndpointCommand {
Expand All @@ -44,14 +54,60 @@ func newTestEndpointCommand() *testEndpointCommand {
}
cmd.Flags().StringVarP(&command.address, "address", "a", "127.0.0.1", "The address for the HTTP listener")
cmd.Flags().Uint16VarP(&command.port, "port", "P", 9090, "The port for the HTTP listener")

cmd.Flags().BoolVar(&command.enableZiti, "ziti", false, "Enable the usage of a ziti network")
cmd.Flags().StringVar(&command.identityJsonFile, "ziti-identity", "", "Path to Ziti Identity json file")
cmd.Flags().StringVar(&command.serviceName, "ziti-name", "", "Name of the Ziti Service")

cmd.Run = command.run
return command
}

func (cmd *testEndpointCommand) run(_ *cobra.Command, _ []string) {
var listener net.Listener
var err error

if cmd.enableZiti {
identityJsonBytes, err := os.ReadFile(cmd.identityJsonFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to read identity config JSON from file %s: %s\n", cmd.identityJsonFile, err)
os.Exit(1)
}

if len(identityJsonBytes) == 0 {
fmt.Fprintf(os.Stderr, "Error: When running a ziti enabled service must have ziti identity provided\n\n")
flag.Usage()
os.Exit(1)
}
config := config.Config{}
err = json.Unmarshal(identityJsonBytes, &config)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load ziti configuration JSON: %v", err)
os.Exit(1)
}
zitiContext := ziti.NewContextWithConfig(&config)
if err := zitiContext.Authenticate(); err != nil {
fmt.Fprintf(os.Stderr, "Error: Unable to authenticate ziti: %v\n\n", err)
os.Exit(1)
}

listener, err = zitiContext.Listen(cmd.serviceName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Unable to listen on ziti network: %v\n\n", err)
os.Exit(1)
}
} else {
listener, err = net.Listen("tcp", fmt.Sprintf("%v:%d", cmd.address, cmd.port))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Unable to listen on %s: %v\n\n", fmt.Sprintf("%v:%d", cmd.address, cmd.port), err)
os.Exit(1)
}
}
server := &http.Server{}

http.HandleFunc("/", cmd.serveIndex)
http.HandleFunc("/echo", cmd.websocketEcho)
if err := http.ListenAndServe(fmt.Sprintf("%v:%d", cmd.address, cmd.port), nil); err != nil {
if err := server.Serve(listener); err != nil {
if !panicInstead {
tui.Error("unable to start http listener", err)
}
Expand Down
119 changes: 119 additions & 0 deletions cmd/zrok/testWebsocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"

"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)

func init() {
testCmd.AddCommand(newTestWebsocketCommand().cmd)
}

type testWebsocketCommand struct {
cmd *cobra.Command

identityJsonFile string
serviceName string
enableZiti bool
}

func newTestWebsocketCommand() *testWebsocketCommand {
cmd := &cobra.Command{
Use: "websocket",
Args: cobra.RangeArgs(0, 1),
}

command := &testWebsocketCommand{cmd: cmd}

cmd.Flags().BoolVar(&command.enableZiti, "ziti", false, "Enable the usage of a ziti network")
cmd.Flags().StringVar(&command.identityJsonFile, "ziti-identity", "", "Path to Ziti Identity json file")
cmd.Flags().StringVar(&command.serviceName, "ziti-name", "", "Name of the Ziti Service")

cmd.Run = command.run
return command
}

func (cmd *testWebsocketCommand) run(_ *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*6)
defer cancel()
opts := &websocket.DialOptions{}
var addr string
if cmd.enableZiti {
identityJsonBytes, err := os.ReadFile(cmd.identityJsonFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to read identity config JSON from file %s: %s\n", cmd.identityJsonFile, err)
os.Exit(1)
}
if len(identityJsonBytes) == 0 {
fmt.Fprintf(os.Stderr, "Error: When running a ziti enabled service must have ziti identity provided\n\n")
flag.Usage()
os.Exit(1)
}

cfg := &config.Config{}
err = json.Unmarshal(identityJsonBytes, cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load ziti configuration JSON: %v", err)
os.Exit(1)
}
zitiContext := ziti.NewContextWithConfig(cfg)

dial := func(_ context.Context, _, addr string) (net.Conn, error) {
service := strings.Split(addr, ":")[0]
return zitiContext.Dial(service)
}

zitiTransport := http.DefaultTransport.(*http.Transport).Clone()
zitiTransport.DialContext = dial

opts.HTTPClient = &http.Client{Transport: zitiTransport}

addr = cmd.serviceName
} else {
if len(args) == 0 {
logrus.Error("Address required if not using ziti")
flag.Usage()
os.Exit(1)
}
addr = args[0]
}

logrus.Info(fmt.Sprintf("http://%s/echo", addr))
c, _, err := websocket.Dial(ctx, fmt.Sprintf("http://%s/echo", addr), opts)
if err != nil {
logrus.Error(err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")

logrus.Info("Writting to server...")
err = wsjson.Write(ctx, c, "hi")
if err != nil {
logrus.Error(err)
return
}
logrus.Info("Reading response...")
typ, dat, err := c.Read(ctx)
if err != nil {
logrus.Error(err)
return
}
logrus.Info(typ)
logrus.Info(string(dat))

c.Close(websocket.StatusNormalClosure, "")
}
58 changes: 30 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,33 @@ require (
github.com/michaelquigley/cf v0.0.13
github.com/michaelquigley/pfxlog v0.6.9
github.com/muesli/reflow v0.3.0
github.com/opentracing/opentracing-go v1.2.0
github.com/openziti/edge v0.22.39
github.com/openziti/sdk-golang v0.16.125
github.com/openziti/sdk-golang v0.18.61
github.com/pkg/errors v0.9.1
github.com/rubenv/sql-migrate v1.1.2
github.com/shirou/gopsutil/v3 v3.22.8
github.com/shirou/gopsutil/v3 v3.23.2
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.1
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.2
github.com/wneessen/go-mail v0.2.7
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4
golang.org/x/crypto v0.6.0
golang.org/x/net v0.6.0
golang.org/x/time v0.3.0
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
nhooyr.io/websocket v1.8.7
)

require (
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa // indirect
github.com/go-gorp/gorp/v3 v3.0.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand All @@ -58,51 +59,52 @@ require (
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/netfoundry/secretstream v0.1.2 // indirect
github.com/netfoundry/secretstream v0.1.4 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/openziti/channel/v2 v2.0.1 // indirect
github.com/openziti/foundation/v2 v2.0.4 // indirect
github.com/openziti/identity v1.0.13 // indirect
github.com/openziti/metrics v1.1.0 // indirect
github.com/openziti/transport/v2 v2.0.30 // indirect
github.com/orcaman/concurrent-map/v2 v2.0.0 // indirect
github.com/parallaxsecond/parsec-client-go v0.0.0-20220111122524-cb78842db373 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/openziti/channel/v2 v2.0.45 // indirect
github.com/openziti/foundation/v2 v2.0.15 // indirect
github.com/openziti/identity v1.0.37 // indirect
github.com/openziti/metrics v1.2.10 // indirect
github.com/openziti/transport/v2 v2.0.63 // indirect
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
github.com/parallaxsecond/parsec-client-go v0.0.0-20221025095442-f0a77d263cf9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/speps/go-hashids v2.0.0+incompatible // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.mongodb.org/mongo-driver v1.10.0 // indirect
go.mozilla.org/pkcs7 v0.0.0-20200128120323-432b2356ecb1 // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25 // indirect
golang.org/x/term v0.0.0-20220919170432-7a66f970e087 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.7 // indirect
)
Loading

0 comments on commit 72e3bc9

Please sign in to comment.