diff --git a/cmd/api/v5/link.go b/cmd/api/v5/link.go deleted file mode 100755 index c37c29a..0000000 --- a/cmd/api/v5/link.go +++ /dev/null @@ -1,62 +0,0 @@ -package v5 - -import ( - "github.com/luscis/openlan/pkg/schema" - "github.com/urfave/cli/v2" -) - -type Link struct { - Cmd -} - -func (u Link) Url(prefix, name string) string { - if name == "" { - return prefix + "/api/link" - } else { - return prefix + "/api/link/" + name - } -} - -func (u Link) Tmpl() string { - return `# total {{ len . }} -{{ps -16 "uuid"}} {{ps -8 "alive"}} {{ ps -8 "device" }} {{ps -8 "user"}} {{ps -22 "server"}} {{ps -8 "network"}} {{ ps -6 "state"}} -{{- range . }} -{{ps -16 .UUID}} {{pt .AliveTime | ps -8}} {{ ps -8 .Device}} {{ps -8 .User}} {{ps -22 .Server}} {{ps -8 .Network}} {{ ps -6 .State}} -{{- end }} -` -} - -func (u Link) List(c *cli.Context) error { - url := u.Url(c.String("url"), "") - clt := u.NewHttp(c.String("token")) - var items []schema.Link - if err := clt.GetJSON(url, &items); err != nil { - return err - } - name := c.String("name") - if len(name) > 0 { - tmp := items[:0] - for _, obj := range items { - if obj.Network == name { - tmp = append(tmp, obj) - } - } - items = tmp - } - return u.Out(items, c.String("format"), u.Tmpl()) -} - -func (u Link) Commands() *cli.Command { - return &cli.Command{ - Name: "link", - Usage: "Link connect to others", - Subcommands: []*cli.Command{ - { - Name: "list", - Usage: "Display all links", - Aliases: []string{"ls"}, - Action: u.List, - }, - }, - } -} diff --git a/cmd/api/v5/network.go b/cmd/api/v5/network.go index 487cce2..17343df 100755 --- a/cmd/api/v5/network.go +++ b/cmd/api/v5/network.go @@ -150,7 +150,6 @@ func (u Network) Commands(app *api.App) { OpenVPN{}.Commands(), Output{}.Commands(), PrefixRoute{}.Commands(), - Link{}.Commands(), FindHop{}.Commands(), SNAT{}.Commands(), DNAT{}.Commands(), diff --git a/cmd/api/v5/output.go b/cmd/api/v5/output.go index 9259dda..ccae7f1 100644 --- a/cmd/api/v5/output.go +++ b/cmd/api/v5/output.go @@ -66,15 +66,6 @@ func (o Output) Save(c *cli.Context) error { return nil } -func (o Output) Tmpl() string { - return `# total {{ len . }} -{{ps -24 "network"}} {{ps -15 "protocol"}} {{ps -15 "Remote"}} {{ps -15 "segment"}} {{ps -15 "device"}} -{{- range . }} -{{ps -24 .Network}} {{ps -15 .Protocol}} {{ps -15 .Remote}} {{ if .Segment }}{{pi -15 .Segment }}{{ else }}{{ps -15 .Secret}}{{ end }} {{ps -15 .Device}} {{.Crypt}} -{{- end }} -` -} - func (o Output) List(c *cli.Context) error { url := o.Url(c.String("url"), c.String("name")) clt := o.NewHttp(c.String("token")) @@ -82,7 +73,7 @@ func (o Output) List(c *cli.Context) error { if err := clt.GetJSON(url, &items); err != nil { return err } - return o.Out(items, c.String("format"), o.Tmpl()) + return o.Out(items, c.String("format"), "") } func (o Output) Commands() *cli.Command { diff --git a/pkg/api/link.go b/pkg/api/link.go deleted file mode 100755 index 7d3139a..0000000 --- a/pkg/api/link.go +++ /dev/null @@ -1,43 +0,0 @@ -package api - -import ( - "net/http" - - "github.com/gorilla/mux" - "github.com/luscis/openlan/pkg/cache" - "github.com/luscis/openlan/pkg/libol" - "github.com/luscis/openlan/pkg/models" - "github.com/luscis/openlan/pkg/schema" -) - -type Link struct { - cs SwitchApi -} - -func (h Link) Router(router *mux.Router) { - router.HandleFunc("/api/link", h.List).Methods("GET") - router.HandleFunc("/api/link/{id}", h.Get).Methods("GET") -} - -func (h Link) List(w http.ResponseWriter, r *http.Request) { - links := make([]schema.Link, 0, 1024) - for l := range cache.Link.List() { - if l == nil { - break - } - links = append(links, models.NewLinkSchema(l)) - } - ResponseJson(w, links) -} - -func (h Link) Get(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - - libol.Debug("Link.Get %s", vars["id"]) - link := cache.Link.Get(vars["id"]) - if link != nil { - ResponseJson(w, models.NewLinkSchema(link)) - } else { - http.Error(w, vars["id"], http.StatusNotFound) - } -} diff --git a/pkg/api/url.go b/pkg/api/url.go index 1201d56..2a12c96 100755 --- a/pkg/api/url.go +++ b/pkg/api/url.go @@ -3,7 +3,6 @@ package api import "github.com/gorilla/mux" func Add(router *mux.Router, cs SwitchApi) { - Link{cs: cs}.Router(router) User{}.Router(router) KernelRoute{}.Router(router) KernelNeighbor{}.Router(router) diff --git a/pkg/cache/link.go b/pkg/cache/link.go deleted file mode 100755 index ecce2d4..0000000 --- a/pkg/cache/link.go +++ /dev/null @@ -1,46 +0,0 @@ -package cache - -import ( - "github.com/luscis/openlan/pkg/libol" - "github.com/luscis/openlan/pkg/models" -) - -type link struct { - Links *libol.SafeStrMap -} - -func (p *link) Init(size int) { - p.Links = libol.NewSafeStrMap(size) -} - -func (p *link) Add(uuid string, link *models.Link) { - _ = p.Links.Set(uuid, link) -} - -func (p *link) Get(key string) *models.Link { - ret := p.Links.Get(key) - if ret != nil { - return ret.(*models.Link) - } - return nil -} - -func (p *link) Del(key string) { - p.Links.Del(key) -} - -func (p *link) List() <-chan *models.Link { - c := make(chan *models.Link, 128) - go func() { - p.Links.Iter(func(k string, v interface{}) { - m := v.(*models.Link) - c <- m - }) - c <- nil //Finish channel by nil. - }() - return c -} - -var Link = link{ - Links: libol.NewSafeStrMap(1024), -} diff --git a/pkg/cache/store.go b/pkg/cache/store.go index 35948bf..c062f25 100755 --- a/pkg/cache/store.go +++ b/pkg/cache/store.go @@ -6,7 +6,6 @@ import ( func Init(cfg *config.Perf) { Access.Init(cfg.Access) - Link.Init(cfg.Link) Neighbor.Init(cfg.Neighbor) Online.Init(cfg.OnLine) User.Init(cfg.User) diff --git a/pkg/models/link.go b/pkg/models/link.go deleted file mode 100755 index 2b00ce2..0000000 --- a/pkg/models/link.go +++ /dev/null @@ -1,23 +0,0 @@ -package models - -import ( - "github.com/luscis/openlan/pkg/libol" - "github.com/luscis/openlan/pkg/schema" -) - -type Link struct { - User string - Network string - Protocol string - StatusFile string -} - -func (l *Link) reload() *schema.Access { - status := &schema.Access{} - _ = libol.UnmarshalLoad(status, l.StatusFile) - return status -} - -func (l *Link) Status() *schema.Access { - return l.reload() -} diff --git a/pkg/models/output.go b/pkg/models/output.go index 387352e..07d1c72 100755 --- a/pkg/models/output.go +++ b/pkg/models/output.go @@ -1,21 +1,36 @@ package models -import "time" +import ( + "time" + + "github.com/luscis/openlan/pkg/libol" + "github.com/luscis/openlan/pkg/schema" +) type Output struct { - Network string - Protocol string - Remote string - Segment int - Device string - Secret string - RxBytes uint64 - TxBytes uint64 - ErrPkt uint64 - NewTime int64 - Fallback string + Network string + Protocol string + Remote string + Segment int + Device string + Secret string + RxBytes uint64 + TxBytes uint64 + ErrPkt uint64 + NewTime int64 + Fallback string + StatsFile string } func (o *Output) UpTime() int64 { return time.Now().Unix() - o.NewTime } + +func (o *Output) GetState() string { + if o.StatsFile != "" { + sts := &schema.Access{} + _ = libol.UnmarshalLoad(sts, o.StatsFile) + return sts.State + } + return "" +} diff --git a/pkg/models/schema.go b/pkg/models/schema.go index 2eb076c..52c0d9a 100755 --- a/pkg/models/schema.go +++ b/pkg/models/schema.go @@ -26,24 +26,6 @@ func NewAccessSchema(p *Access) schema.Access { } } -func NewLinkSchema(l *Link) schema.Link { - sts := l.Status() - return schema.Link{ - UUID: sts.UUID, - User: sts.User, - Uptime: sts.Uptime, - Device: sts.Device, - Protocol: sts.Protocol, - Server: sts.Remote, - State: sts.State, - RxBytes: sts.RxBytes, - TxBytes: sts.TxBytes, - ErrPkt: sts.ErrPkt, - Network: sts.Network, - AliveTime: sts.AliveTime, - } -} - func NewNeighborSchema(n *Neighbor) schema.Neighbor { return schema.Neighbor{ Uptime: n.UpTime(), @@ -113,5 +95,6 @@ func NewOutputSchema(o *Output) schema.Output { TxBytes: o.TxBytes, Secret: o.Secret, AliveTime: o.UpTime(), + State: o.GetState(), } } diff --git a/pkg/public/index.html b/pkg/public/index.html index 9037764..d6cf903 100755 --- a/pkg/public/index.html +++ b/pkg/public/index.html @@ -138,7 +138,7 @@