fix: support list guest and knock

This commit is contained in:
Daniel Ding
2024-01-02 11:14:54 +08:00
parent 9a039a6d3c
commit 1af91f2f65
9 changed files with 131 additions and 34 deletions

View File

@@ -2,11 +2,12 @@ package api
import ( import (
"fmt" "fmt"
"github.com/ghodss/yaml"
"github.com/luscis/openlan/pkg/libol"
"os" "os"
"strconv" "strconv"
"text/template" "text/template"
"github.com/ghodss/yaml"
"github.com/luscis/openlan/pkg/libol"
) )
func OutJson(data interface{}) error { func OutJson(data interface{}) error {
@@ -61,6 +62,9 @@ func OutTable(data interface{}, tmpl string) error {
} }
return fmt.Sprintf(format, value) return fmt.Sprintf(format, value)
}, },
"ut": func(value int64) string {
return libol.UnixTime(value)
},
} }
if tmpl, err := template.New("main").Funcs(funcMap).Parse(tmpl); err != nil { if tmpl, err := template.New("main").Funcs(funcMap).Parse(tmpl); err != nil {
return err return err

View File

@@ -15,7 +15,7 @@ type Knock struct {
func (u Knock) Url(prefix, name string) string { func (u Knock) Url(prefix, name string) string {
name, network := api.SplitName(name) name, network := api.SplitName(name)
return prefix + "/api/ztrust/" + network + "/guest/" + name + "/knock" return prefix + "/api/network/" + network + "/guest/" + name + "/knock"
} }
func (u Knock) Add(c *cli.Context) error { func (u Knock) Add(c *cli.Context) error {
@@ -25,7 +25,7 @@ func (u Knock) Add(c *cli.Context) error {
} }
socket := c.String("socket") socket := c.String("socket")
knock := &schema.KnockRule{ knock := &schema.KnockRule{
Protocl: c.String("protocol"), Protocol: c.String("protocol"),
} }
knock.Name, knock.Network = api.SplitName(username) knock.Name, knock.Network = api.SplitName(username)
knock.Dest, knock.Port = api.SplitSocket(socket) knock.Dest, knock.Port = api.SplitSocket(socket)
@@ -45,7 +45,7 @@ func (u Knock) Remove(c *cli.Context) error {
} }
socket := c.String("socket") socket := c.String("socket")
knock := &schema.KnockRule{ knock := &schema.KnockRule{
Protocl: c.String("protocol"), Protocol: c.String("protocol"),
} }
knock.Name, knock.Network = api.SplitName(username) knock.Name, knock.Network = api.SplitName(username)
knock.Dest, knock.Port = api.SplitSocket(socket) knock.Dest, knock.Port = api.SplitSocket(socket)
@@ -60,15 +60,25 @@ func (u Knock) Remove(c *cli.Context) error {
func (u Knock) Tmpl() string { func (u Knock) Tmpl() string {
return `# total {{ len . }} return `# total {{ len . }}
{{ps -24 "username"}} {{ps -24 "address"}} {{ps -24 "username"}} {{ps -8 "protocol"}} {{ps -24 "socket"}} {{ps -24 "createAt"}}
{{- range . }} {{- range . }}
{{p2 -24 "%s@%s" .Name .Network}} {{ps -24 .Address}} {{p2 -24 "%s@%s" .Name .Network}} {{ps -8 .Protocol}} {{p2 -24 "%s:%s" .Dest .Port}} {{ut .CreateAt}}
{{- end }} {{- end }}
` `
} }
func (u Knock) List(c *cli.Context) error { func (u Knock) List(c *cli.Context) error {
return nil name := c.String("name")
url := u.Url(c.String("url"), name)
clt := u.NewHttp(c.String("token"))
var items []schema.KnockRule
if err := clt.GetJSON(url, &items); err != nil {
return err
}
return u.Out(items, c.String("format"), u.Tmpl())
} }
func (u Knock) Commands(app *api.App) { func (u Knock) Commands(app *api.App) {
@@ -103,7 +113,7 @@ func (u Knock) Commands(app *api.App) {
Usage: "Display all knock", Usage: "Display all knock",
Aliases: []string{"ls"}, Aliases: []string{"ls"},
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "network"}, &cli.StringFlag{Name: "name"},
}, },
Action: u.List, Action: u.List,
}, },

View File

@@ -16,10 +16,9 @@ type ZGuest struct {
func (u ZGuest) Url(prefix, name string) string { func (u ZGuest) Url(prefix, name string) string {
name, network := api.SplitName(name) name, network := api.SplitName(name)
if name == "" { if name == "" {
return prefix + "/api/ztrust/" + network + "/guest" return prefix + "/api/network/" + network + "/guest"
} else {
return prefix + "/api/ztrust/" + network + "/guest/" + name
} }
return prefix + "/api/network/" + network + "/guest/" + name
} }
func (u ZGuest) Add(c *cli.Context) error { func (u ZGuest) Add(c *cli.Context) error {
@@ -68,7 +67,17 @@ func (u ZGuest) Tmpl() string {
} }
func (u ZGuest) List(c *cli.Context) error { func (u ZGuest) List(c *cli.Context) error {
return nil network := c.String("network")
url := u.Url(c.String("url"), "@"+network)
clt := u.NewHttp(c.String("token"))
var items []schema.ZGuest
if err := clt.GetJSON(url, &items); err != nil {
return err
}
return u.Out(items, c.String("format"), u.Tmpl())
} }
func (u ZGuest) Commands(app *api.App) { func (u ZGuest) Commands(app *api.App) {

View File

@@ -34,6 +34,8 @@ type ZTruster interface {
AddGuest(name, source string) error AddGuest(name, source string) error
DelGuest(name, source string) error DelGuest(name, source string) error
Knock(name string, protocol, dest, port string, age int) error Knock(name string, protocol, dest, port string, age int) error
ListGuest(call func(obj schema.ZGuest))
ListKnock(name string, call func(obj schema.KnockRule))
} }
type Networker interface { type Networker interface {

View File

@@ -1,8 +1,9 @@
package api package api
import ( import (
"github.com/gorilla/mux"
"net/http" "net/http"
"github.com/gorilla/mux"
) )
type VxLAN struct { type VxLAN struct {

View File

@@ -13,13 +13,12 @@ type ZTrust struct {
} }
func (h ZTrust) Router(router *mux.Router) { func (h ZTrust) Router(router *mux.Router) {
router.HandleFunc("/api/ztrust", h.List).Methods("GET") router.HandleFunc("/api/network/{id}/ztrust", h.List).Methods("GET")
router.HandleFunc("/api/ztrust/{id}", h.Get).Methods("GET") router.HandleFunc("/api/network/{id}/guest", h.ListGuest).Methods("GET")
router.HandleFunc("/api/ztrust/{id}/guest/{user}", h.GetGuest).Methods("GET") router.HandleFunc("/api/network/{id}/guest/{user}", h.AddGuest).Methods("POST")
router.HandleFunc("/api/ztrust/{id}/guest/{user}", h.AddGuest).Methods("POST") router.HandleFunc("/api/network/{id}/guest/{user}", h.DelGuest).Methods("DELETE")
router.HandleFunc("/api/ztrust/{id}/guest/{user}", h.DelGuest).Methods("DELETE") router.HandleFunc("/api/network/{id}/guest/{user}/knock", h.ListKnock).Methods("GET")
router.HandleFunc("/api/ztrust/{id}/guest/{user}/knock", h.ListKnock).Methods("GET") router.HandleFunc("/api/network/{id}/guest/{user}/knock", h.AddKnock).Methods("POST")
router.HandleFunc("/api/ztrust/{id}/guest/{user}/knock", h.AddKnock).Methods("POST")
} }
func (h ZTrust) List(w http.ResponseWriter, r *http.Request) { func (h ZTrust) List(w http.ResponseWriter, r *http.Request) {
@@ -32,10 +31,27 @@ func (h ZTrust) Get(w http.ResponseWriter, r *http.Request) {
ResponseJson(w, "TODO") ResponseJson(w, "TODO")
} }
func (h ZTrust) GetGuest(w http.ResponseWriter, r *http.Request) { func (h ZTrust) ListGuest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
libol.Info("ZTrust.AddGuest %s", vars["id"]) id := vars["id"]
ResponseJson(w, "TODO")
worker := GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
}
ztrust := worker.ZTruster()
if ztrust == nil {
http.Error(w, "ZTrust disabled", http.StatusInternalServerError)
return
}
guests := make([]schema.ZGuest, 0, 1024)
ztrust.ListGuest(func(obj schema.ZGuest) {
guests = append(guests, obj)
})
ResponseJson(w, guests)
} }
func (h ZTrust) AddGuest(w http.ResponseWriter, r *http.Request) { func (h ZTrust) AddGuest(w http.ResponseWriter, r *http.Request) {
@@ -103,13 +119,30 @@ func (h ZTrust) DelGuest(w http.ResponseWriter, r *http.Request) {
func (h ZTrust) ListKnock(w http.ResponseWriter, r *http.Request) { func (h ZTrust) ListKnock(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
libol.Info("ZTrust.ListKnock %s", vars["id"]) id := vars["id"]
ResponseJson(w, "TODO")
worker := GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
}
ztrust := worker.ZTruster()
if ztrust == nil {
http.Error(w, "ZTrust disabled", http.StatusInternalServerError)
return
}
name := vars["user"]
rules := make([]schema.KnockRule, 0, 1024)
ztrust.ListKnock(name, func(obj schema.KnockRule) {
rules = append(rules, obj)
})
ResponseJson(w, rules)
} }
func (h ZTrust) AddKnock(w http.ResponseWriter, r *http.Request) { func (h ZTrust) AddKnock(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := GetWorker(id)
@@ -131,7 +164,7 @@ func (h ZTrust) AddKnock(w http.ResponseWriter, r *http.Request) {
name := vars["user"] name := vars["user"]
libol.Info("ZTrust.AddKnock %s@%s", rule.Name, id) libol.Info("ZTrust.AddKnock %s@%s", rule.Name, id)
if err := ztrust.Knock(name, rule.Protocl, rule.Dest, rule.Port, 0); err == nil { if err := ztrust.Knock(name, rule.Protocol, rule.Dest, rule.Port, 0); err == nil {
ResponseJson(w, "success") ResponseJson(w, "success")
} else { } else {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@@ -192,6 +192,10 @@ func IPNetwork(ipAddr string) (string, error) {
} }
} }
func UnixTime(value int64) string {
return time.Unix(value, 0).UTC().String()
}
func PrettyTime(t int64) string { func PrettyTime(t int64) string {
s := "" s := ""
if t < 0 { if t < 0 {

View File

@@ -8,10 +8,11 @@ type ZGuest struct {
} }
type KnockRule struct { type KnockRule struct {
Network string `json:"network"` Network string `json:"network"`
Name string `json:"name"` Name string `json:"name"`
Dest string `json:"destination"` Dest string `json:"destination"`
Protocl string `json:"protocol"` Protocol string `json:"protocol"`
Port string `json:"port"` Port string `json:"port"`
Age int `json:"age"` Age int `json:"age"`
CreateAt int64 `json:"createAt"`
} }

View File

@@ -7,6 +7,7 @@ import (
"github.com/luscis/openlan/pkg/libol" "github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/network" "github.com/luscis/openlan/pkg/network"
cn "github.com/luscis/openlan/pkg/network" cn "github.com/luscis/openlan/pkg/network"
"github.com/luscis/openlan/pkg/schema"
) )
type KnockRule struct { type KnockRule struct {
@@ -215,3 +216,35 @@ func (z *ZTrust) Stop() {
guest.Stop() guest.Stop()
} }
} }
func (z *ZTrust) ListGuest(call func(obj schema.ZGuest)) {
for _, guest := range z.guests {
for _, source := range guest.sources {
obj := schema.ZGuest{
Name: guest.username,
Network: guest.network,
Address: source,
}
call(obj)
}
}
}
func (z *ZTrust) ListKnock(name string, call func(obj schema.KnockRule)) {
guest, ok := z.guests[name]
if !ok {
return
}
for _, rule := range guest.rules {
obj := schema.KnockRule{
Name: name,
Network: z.network,
Protocol: rule.protocol,
Dest: rule.destination,
Port: rule.port,
CreateAt: rule.createAt,
}
call(obj)
}
}