fix: review API interface.

This commit is contained in:
Daniel Ding
2024-06-15 23:42:31 +08:00
parent 33bf85badd
commit 7c27b06b4e
12 changed files with 118 additions and 37 deletions

View File

@@ -22,7 +22,7 @@ func (h ACL) List(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -41,7 +41,7 @@ func (h ACL) Add(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -66,7 +66,7 @@ func (h ACL) Del(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -91,7 +91,7 @@ func (h ACL) Save(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return

View File

@@ -95,18 +95,39 @@ type Networker interface {
VPNer VPNer
} }
var workers = make(map[string]Networker) type IPSecer interface {
AddTunnel(data schema.IPSecTunnel)
func AddWorker(name string, obj Networker) { DelTunnel(data schema.IPSecTunnel)
workers[name] = obj ListTunnels(call func(obj schema.IPSecTunnel))
} }
func GetWorker(name string) Networker { type APICall struct {
return workers[name] workers map[string]Networker
secer IPSecer
} }
func ListWorker(call func(w Networker)) { func (i *APICall) AddWorker(name string, obj Networker) {
for _, worker := range workers { i.workers[name] = obj
}
func (i *APICall) GetWorker(name string) Networker {
return i.workers[name]
}
func (i *APICall) ListWorker(call func(w Networker)) {
for _, worker := range i.workers {
call(worker) call(worker)
} }
} }
func (i *APICall) SetIPSecer(value IPSecer) {
i.secer = value
}
func (i *APICall) GetIPSecer() IPSecer {
return i.secer
}
var Call = &APICall{
workers: make(map[string]Networker),
}

View File

@@ -76,7 +76,7 @@ func (h Network) Post(w http.ResponseWriter, r *http.Request) {
func (h Network) Delete(w http.ResponseWriter, r *http.Request) { func (h Network) Delete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
network := vars["id"] network := vars["id"]
worker := GetWorker(network) worker := Call.GetWorker(network)
if worker == nil { if worker == nil {
http.Error(w, "network not found", http.StatusBadRequest) http.Error(w, "network not found", http.StatusBadRequest)
return return
@@ -110,7 +110,7 @@ func (h Network) RestartVPN(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return

View File

@@ -50,7 +50,7 @@ func (h Output) Post(w http.ResponseWriter, r *http.Request) {
http.Error(w, "network is nil", http.StatusBadRequest) http.Error(w, "network is nil", http.StatusBadRequest)
return return
} }
worker := GetWorker(name) worker := Call.GetWorker(name)
if worker == nil { if worker == nil {
http.Error(w, "network not found", http.StatusBadRequest) http.Error(w, "network not found", http.StatusBadRequest)
return return
@@ -73,7 +73,7 @@ func (h Output) Delete(w http.ResponseWriter, r *http.Request) {
http.Error(w, "network is nil", http.StatusBadRequest) http.Error(w, "network is nil", http.StatusBadRequest)
return return
} }
worker := GetWorker(name) worker := Call.GetWorker(name)
if worker == nil { if worker == nil {
http.Error(w, "network not found", http.StatusBadRequest) http.Error(w, "network not found", http.StatusBadRequest)
return return
@@ -86,7 +86,7 @@ func (h Output) Save(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusBadRequest) http.Error(w, "Network not found", http.StatusBadRequest)
return return

View File

@@ -1,9 +1,10 @@
package api package api
import ( import (
"net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/luscis/openlan/pkg/schema" "github.com/luscis/openlan/pkg/schema"
"net/http"
) )
type QosApi struct { type QosApi struct {
@@ -22,7 +23,7 @@ func (h QosApi) List(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -47,7 +48,7 @@ func (h QosApi) Add(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -75,7 +76,7 @@ func (h QosApi) Del(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -96,7 +97,7 @@ func (h QosApi) Save(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return

View File

@@ -1,11 +1,12 @@
package api package api
import ( import (
"net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/luscis/openlan/pkg/cache" "github.com/luscis/openlan/pkg/cache"
"github.com/luscis/openlan/pkg/models" "github.com/luscis/openlan/pkg/models"
"github.com/luscis/openlan/pkg/schema" "github.com/luscis/openlan/pkg/schema"
"net/http"
) )
type Route struct { type Route struct {
@@ -38,7 +39,7 @@ func (rt Route) Add(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -63,7 +64,7 @@ func (rt Route) Del(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -88,7 +89,7 @@ func (rt Route) Save(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return

View File

@@ -46,7 +46,7 @@ func (h ZTrust) ListGuest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -77,7 +77,7 @@ func (h ZTrust) AddGuest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -127,7 +127,7 @@ func (h ZTrust) DelGuest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -165,7 +165,7 @@ func (h ZTrust) ListKnock(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id := vars["id"] id := vars["id"]
worker := GetWorker(id) worker := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return
@@ -195,7 +195,7 @@ 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 := Call.GetWorker(id)
if worker == nil { if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError) http.Error(w, "Network not found", http.StatusInternalServerError)
return return

View File

@@ -35,3 +35,11 @@ func (s *IPSecSpecifies) Correct() {
t.Correct() t.Correct()
} }
} }
func (s *IPSecSpecifies) AddTunnel(data *IPSecTunnel) {
}
func (s *IPSecSpecifies) DelTunnel(data *IPSecTunnel) {
}

View File

@@ -11,6 +11,15 @@ type Promise struct {
} }
func NewPromise() *Promise { func NewPromise() *Promise {
return &Promise{
First: time.Second * 2,
MaxInt: time.Minute,
MinInt: time.Second * 10,
MaxTry: 10,
}
}
func NewPromiseAlways() *Promise {
return &Promise{ return &Promise{
First: time.Second * 2, First: time.Second * 2,
MaxInt: time.Minute, MaxInt: time.Minute,

12
pkg/schema/ipsec.go Normal file
View File

@@ -0,0 +1,12 @@
package schema
type IPSecTunnel struct {
Left string `json:"local"`
LeftId string `json:"localid"`
LeftPort string `json:"localport"`
Right string `json:"remote"`
RightId string `json:"remoteid"`
RightPort string `json:"remoteport"`
Transport string `json:"transport"`
Secret string `json:"secret"`
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/luscis/openlan/pkg/api" "github.com/luscis/openlan/pkg/api"
co "github.com/luscis/openlan/pkg/config" co "github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol" "github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/schema"
) )
type IPSecWorker struct { type IPSecWorker struct {
@@ -112,7 +113,7 @@ func (w *IPSecWorker) startConn(name string) {
}) })
} }
func (w *IPSecWorker) AddTunnel(tunnel *co.IPSecTunnel) error { func (w *IPSecWorker) addTunnel(tunnel *co.IPSecTunnel) error {
connTmpl := "" connTmpl := ""
secTmpl := "" secTmpl := ""
@@ -152,11 +153,11 @@ func (w *IPSecWorker) Start(v api.Switcher) {
w.uuid = v.UUID() w.uuid = v.UUID()
w.out.Info("IPSecWorker.Start") w.out.Info("IPSecWorker.Start")
for _, tunnel := range w.spec.Tunnels { for _, tunnel := range w.spec.Tunnels {
w.AddTunnel(tunnel) w.addTunnel(tunnel)
} }
} }
func (w *IPSecWorker) RemoveTunnel(tunnel *co.IPSecTunnel) error { func (w *IPSecWorker) removeTunnel(tunnel *co.IPSecTunnel) error {
name := tunnel.Name name := tunnel.Name
if tunnel.Transport == "vxlan" { if tunnel.Transport == "vxlan" {
libol.Exec("ipsec", "auto", "--delete", "--asynchronous", name+"-c1") libol.Exec("ipsec", "auto", "--delete", "--asynchronous", name+"-c1")
@@ -184,7 +185,7 @@ func (w *IPSecWorker) RemoveTunnel(tunnel *co.IPSecTunnel) error {
func (w *IPSecWorker) Stop() { func (w *IPSecWorker) Stop() {
w.out.Info("IPSecWorker.Stop") w.out.Info("IPSecWorker.Stop")
for _, tunnel := range w.spec.Tunnels { for _, tunnel := range w.spec.Tunnels {
w.RemoveTunnel(tunnel) w.removeTunnel(tunnel)
} }
} }
@@ -193,3 +194,29 @@ func (w *IPSecWorker) Reload(v api.Switcher) {
w.Initialize() w.Initialize()
w.Start(v) w.Start(v)
} }
func (w *IPSecWorker) AddTunnel(data schema.IPSecTunnel) {
cfg := &co.IPSecTunnel{
Left: data.Left,
Right: data.Right,
Secret: data.Secret,
Transport: data.Transport,
}
w.spec.AddTunnel(cfg)
w.addTunnel(cfg)
}
func (w *IPSecWorker) DelTunnel(data schema.IPSecTunnel) {
cfg := &co.IPSecTunnel{
Left: data.Left,
Right: data.Right,
Secret: data.Secret,
Transport: data.Transport,
}
w.removeTunnel(cfg)
w.spec.DelTunnel(cfg)
}
func (w *IPSecWorker) ListTunnels(call func(obj schema.IPSecTunnel)) {
}

View File

@@ -20,13 +20,15 @@ func NewNetworker(c *co.Network) api.Networker {
var obj api.Networker var obj api.Networker
switch c.Provider { switch c.Provider {
case "ipsec": case "ipsec":
obj = NewIPSecWorker(c) secer := NewIPSecWorker(c)
api.Call.SetIPSecer(secer)
obj = secer
case "router": case "router":
obj = NewRouterWorker(c) obj = NewRouterWorker(c)
default: default:
obj = NewOpenLANWorker(c) obj = NewOpenLANWorker(c)
} }
api.AddWorker(c.Name, obj) api.Call.AddWorker(c.Name, obj)
return obj return obj
} }