Skip to content

Commit

Permalink
Add MUD-Visualizer to page
Browse files Browse the repository at this point in the history
  • Loading branch information
hslatman committed Jul 30, 2021
1 parent f30e49e commit 7121eaa
Show file tree
Hide file tree
Showing 166 changed files with 28,507 additions and 83 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The access control policies described in a MUD File allow network controllers to
* Improve README.md
* Add 'Use' to commands
* Add tests
* Customize / improve the MUD Visualizer?
* Add logging (with levels)
* Replace calls to fmt with proper logging / output
* Allow the tool to be chained (i.e. use STDIN/STDOUT, pipes, etc.)
* A command for generating MUD files (from pcap or some different way)
Expand Down
98 changes: 93 additions & 5 deletions cmd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ limitations under the License.
package cmd

import (
"context"
"log"
"net/http"
"strconv"
"time"

"github.com/hslatman/mud-cli/internal"
"github.com/hslatman/mud-cli/web"
"github.com/pkg/browser"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/antage/eventsource"
)

// viewCmd represents the view command
Expand All @@ -44,31 +49,55 @@ var viewCmd = &cobra.Command{
return errors.Wrap(err, "getting JSON representation of MUD file failed")
}

// TODO: open browser; show MUD Visualizer with the chosen MUD file
// TODO: provide option to show it in terminal?
// TODO: provide option to show it in terminal with some ASCII art?
// TODO: provide option to show it in a Wails UI (instead of default browser)?

closeChan := make(chan struct{}, 1)
stopChan := make(chan error, 1)

mudHandler := newMUDHandler(json)
streamHandler := newStreamHandler(closeChan)

// Strip / and prepend build, so that a file `a/b.js` would be
// found in web/build/a/b.js, but served from localhost:8080/a/b.js.
webHandler := web.AssetHandler("/", "build")

mux := http.NewServeMux()
mux.Handle("/mud", mudHandler)
mux.Handle("/heartbeat", streamHandler)
mux.Handle("/", webHandler)
mux.Handle("/*filepath", webHandler)

s := &http.Server{
Addr: ":8080",
Addr: "localhost:8080",
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}

log.Fatal(s.ListenAndServe()) // TODO: add some logging?
s.RegisterOnShutdown(streamHandler.Close)

go func() {
<-closeChan
log.Println("closing server ...")
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
stopChan <- s.Shutdown(ctx)
}()

// TODO: add some more logging?

log.Println("serving at ...")
go s.ListenAndServe()

log.Println("go to ...")
go browser.OpenURL("http://localhost:8080/")

return nil
err = <-stopChan

return err
},
}

Expand All @@ -86,6 +115,65 @@ func newMUDHandler(json string) http.Handler {
}
}

type streamHandler struct {
es eventsource.EventSource
c chan struct{}
}

func newStreamHandler(c chan struct{}) *streamHandler {
es := eventsource.New(
&eventsource.Settings{
Timeout: 2 * time.Second,
CloseOnTimeout: true,
IdleTimeout: 2 * time.Second,
Gzip: true,
},
func(req *http.Request) [][]byte {
return [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Access-Control-Allow-Origin: *"),
}
},
)
return &streamHandler{
es: es,
c: c,
}
}

func (s *streamHandler) Close() {
s.es.Close()
}

func (s *streamHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {

numberOfConsumers := s.es.ConsumersCount()

// serve sse to the (new) connection
s.es.ServeHTTP(resp, req)

// the routine for pings should only be started once
if numberOfConsumers > 0 {
return
}

// send a ping to all consumers every second
go func() {
var id int
for {
id++
time.Sleep(1 * time.Second)
s.es.SendEventMessage("ping", "message", strconv.Itoa(id))
// break out of the loop when 0 consumers are reached again
if s.es.ConsumersCount() == 0 {
break
}
}
s.c <- struct{}{}
}()

}

func init() {
rootCmd.AddCommand(viewCmd)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module github.com/hslatman/mud-cli
go 1.16

require (
github.com/antage/eventsource v0.0.0-20190412115600-84b661236871
github.com/github/ietf-cms v0.1.0
github.com/hslatman/mud.yang.go v0.0.0-20201016091044-991f3fbb23d0
github.com/openconfig/ygot v0.11.2
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.2.1
go.step.sm/crypto v0.9.0
Expand Down
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/Masterminds/sprig/v3 v3.1.0/go.mod h1:ONGMf7UfYGAbMXCZmQLy8x3lCDIPrEZ
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/antage/eventsource v0.0.0-20190412115600-84b661236871 h1:CU/EFaNIixooGaewG9cX4BShXURU/ejdC/r0Ly0jESo=
github.com/antage/eventsource v0.0.0-20190412115600-84b661236871/go.mod h1:WOB/cuaphqWnO64ntuJ3CbpM+kDsKaQp1aeIB4AuXGI=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand Down Expand Up @@ -262,6 +264,8 @@ github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3/go.mod h1:85jBQOZwp
github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 h1:acNfDZXmm28D2Yg/c3ALnZStzNaZMSagpbr96vY6Zjc=
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -502,8 +506,9 @@ golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71 h1:X/2sJAybVknnUnV7AD2HdT6rm2p5BP6eH2j+igduWgk=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
25 changes: 25 additions & 0 deletions vendor/github.com/antage/eventsource/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/antage/eventsource/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/antage/eventsource/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions vendor/github.com/antage/eventsource/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7121eaa

Please sign in to comment.