fix: remove cache link.
Some checks failed
Coverage CI / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
Ubuntu CI / build (push) Has been cancelled

This commit is contained in:
Daniel Ding
2025-12-08 19:49:44 +08:00
parent a2ac57b2fc
commit 7a4ef32b25
18 changed files with 47 additions and 333 deletions

View File

@@ -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,
},
},
}
}

View File

@@ -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(),

View File

@@ -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 {

View File

@@ -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)
}
}

View File

@@ -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)

46
pkg/cache/link.go vendored
View File

@@ -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),
}

1
pkg/cache/store.go vendored
View File

@@ -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)

View File

@@ -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()
}

View File

@@ -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 ""
}

View File

@@ -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(),
}
}

View File

@@ -138,7 +138,7 @@
<div class="link">
<div class="panel-header">
<div>
<span>The link which connect to other, and total </span><span>{{ len .Links }}</span>.
<span>The link which connect to other, and total </span><span>{{ len .Outputs }}</span>.
</div>
</div>
<div class="panel-body">
@@ -147,6 +147,7 @@
<tr>
<th>UUID</th>
<th>Alive</th>
<th>Network</th>
<th>Device</th>
<th>Segment</th>
<th>Protocol</th>
@@ -160,6 +161,7 @@
<tr>
<td>output</td>
<td>{{ prettyTime .AliveTime }}</td>
<td>{{ .Network }}</td>
<td>{{ .Device }}</td>
<td>ID:{{ .Segment }}</td>
<td>{{ .Protocol }}</td>
@@ -167,23 +169,11 @@
<td>{{ .RxBytes }} | {{ .TxBytes }}</td>
<td>
{{ if .RxBytes }}<span class="success">success</span>
{{ else }}<span class="unknown">unknown</span>
{{ else }}<span class="unknown">{{ .State }}</span>
{{ end}}
</td>
</tr>
{{ end }}
{{ range .Links }}
<tr>
<td>{{ .UUID }}</td>
<td>{{ prettyTime .AliveTime }}</td>
<td>{{ .Device }}</td>
<td>{{ .User }}@{{ .Network }}</td>
<td>{{ .Protocol }}</td>
<td><a href="https://{{ getIpAddr .Server }}:10000">{{ getIpAddr .Server }}</a></td>
<td>{{ .RxBytes }} | {{ .TxBytes }}</td>
<td><span class="{{ .State }}">{{ .State }}</span></td>
</tr>
{{ end }}
</tbody>
</table>
</div>

View File

@@ -4,7 +4,6 @@ type Index struct {
Version Version `json:"version"`
Worker Worker `json:"worker"`
Access []Access `json:"access"`
Links []Link `json:"links"`
Neighbors []Neighbor `json:"neighbors"`
OnLines []OnLine `json:"online"`
Network []Network `json:"network"`

View File

@@ -1,17 +0,0 @@
package schema
type Link struct {
Uptime int64 `json:"uptime"`
UUID string `json:"uuid"`
Alias string `json:"alias"`
Network string `json:"network"`
User string `json:"user,omitempty"`
Protocol string `json:"protocol"`
Server string `json:"server"`
Device string `json:"device"`
RxBytes uint64 `json:"rxBytes"`
TxBytes uint64 `json:"txBytes"`
ErrPkt uint64 `json:"errors"`
State string `json:"state"`
AliveTime int64 `json:"aliveTime"`
}

View File

@@ -14,4 +14,5 @@ type Output struct {
ErrPkt uint64 `json:"errors,omitempty"`
AliveTime int64 `json:"aliveTime"`
Fallback string `json:"fallback,omitempty"`
State string `json:"state,omitempty"`
}

View File

@@ -272,18 +272,6 @@ func (h *Http) getIndex(body *schema.Index) *schema.Index {
sort.SliceStable(body.Neighbors, func(i, j int) bool {
return body.Neighbors[i].IpAddr > body.Neighbors[j].IpAddr
})
// display links.
for l := range cache.Link.List() {
if l == nil {
break
}
body.Links = append(body.Links, models.NewLinkSchema(l))
}
sort.SliceStable(body.Links, func(i, j int) bool {
ii := body.Links[i]
jj := body.Links[j]
return ii.Network+ii.Server > jj.Network+jj.Server
})
// display online flow.
for l := range cache.Online.List() {
if l == nil {

View File

@@ -9,7 +9,6 @@ import (
co "github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/models"
nl "github.com/vishvananda/netlink"
)
@@ -33,16 +32,6 @@ func NewLink(cfg *co.Access) *Link {
}
}
func (l *Link) Model() *models.Link {
cfg := l.Conf()
return &models.Link{
User: cfg.Username,
Network: cfg.Network,
Protocol: cfg.Protocol,
StatusFile: l.StatusFile(),
}
}
func (l *Link) Initialize() {
file := l.ConfFile()
l.cfg.StatusFile = l.StatusFile()

View File

@@ -151,6 +151,11 @@ func (w *WorkerImpl) AddPhysical(bridge string, output string) {
func (w *WorkerImpl) addOutput(bridge string, port *co.Output) {
mtu := 0
out := &models.Output{
Network: w.cfg.Name,
NewTime: time.Now().Unix(),
}
switch port.Protocol {
case "gre":
mtu = 1450
@@ -209,9 +214,7 @@ func (w *WorkerImpl) addOutput(bridge string, port *co.Output) {
Name: port.Link,
Bridge: bridge,
},
Log: co.Log{
File: "/dev/null",
},
Log: co.Log{File: "/dev/null"},
Connection: port.Remote,
Fallback: port.Fallback,
Protocol: port.Protocol,
@@ -228,6 +231,7 @@ func (w *WorkerImpl) addOutput(bridge string, port *co.Output) {
link.Initialize()
link.Start()
port.Linker = link
out.StatsFile = link.StatusFile()
default:
link, err := nl.LinkByName(port.Remote)
if link == nil {
@@ -262,16 +266,12 @@ func (w *WorkerImpl) addOutput(bridge string, port *co.Output) {
}
}
out := &models.Output{
Network: w.cfg.Name,
NewTime: time.Now().Unix(),
Protocol: port.Protocol,
Remote: port.Remote,
Segment: port.Segment,
Device: port.Link,
Secret: port.Secret,
Fallback: port.Fallback,
}
out.Protocol = port.Protocol
out.Remote = port.Remote
out.Segment = port.Segment
out.Device = port.Link
out.Secret = port.Secret
out.Fallback = port.Fallback
cache.Output.Add(port.Link, out)
w.out.Info("WorkerImpl.addOutput %s %s", port.Link, port.Id())

View File

@@ -48,23 +48,6 @@ func (w *OpenLANWorker) Initialize() {
w.WorkerImpl.Initialize()
}
func (w *OpenLANWorker) LoadLinks() {
if w.cfg.Links != nil {
for _, link := range w.cfg.Links {
link.Correct()
w.AddLink(link)
}
}
}
func (w *OpenLANWorker) UnLoadLinks() {
w.links.lock.RLock()
defer w.links.lock.RUnlock()
for _, l := range w.links.links {
l.Stop()
}
}
func (w *OpenLANWorker) UpBridge(cfg *co.Bridge) {
master := w.br
// new it and configure address
@@ -89,12 +72,8 @@ func (w *OpenLANWorker) UpBridge(cfg *co.Bridge) {
func (w *OpenLANWorker) Start(v api.SwitchApi) {
w.uuid = v.UUID()
w.startTime = time.Now().Unix()
w.out.Info("OpenLANWorker.Start")
w.UpBridge(w.cfg.Bridge)
w.LoadLinks()
w.WorkerImpl.Start(v)
}
@@ -105,7 +84,6 @@ func (w *OpenLANWorker) downBridge() {
func (w *OpenLANWorker) Stop() {
w.out.Info("OpenLANWorker.Close")
w.WorkerImpl.Stop()
w.UnLoadLinks()
w.startTime = 0
w.downBridge()
}
@@ -117,32 +95,6 @@ func (w *OpenLANWorker) UpTime() int64 {
return 0
}
func (w *OpenLANWorker) AddLink(c co.Access) {
br := w.cfg.Bridge
c.Alias = w.alias
c.Network = w.cfg.Name
c.RequestAddr = false
c.Interface.Name = cn.Taps.GenName()
c.Interface.Bridge = br.Name
c.Interface.Address = br.Address
c.Interface.Provider = br.Provider
c.Interface.IPMtu = br.IPMtu
c.Log.File = "/dev/null"
l := NewLink(&c)
l.Initialize()
cache.Link.Add(l.uuid, l.Model())
w.links.Add(l)
l.Start()
}
func (w *OpenLANWorker) DelLink(addr string) {
if l := w.links.Remove(addr); l != nil {
cache.Link.Del(l.uuid)
}
}
func (w *OpenLANWorker) Reload(v api.SwitchApi) {
w.Stop()
w.Initialize()