Skip to content

Commit

Permalink
fix: review API interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldin95 committed Jun 15, 2024
1 parent 33bf85b commit 7c27b06
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 36 deletions.
8 changes: 4 additions & 4 deletions pkg/api/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (h ACL) List(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

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

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

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

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down
35 changes: 28 additions & 7 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,39 @@ type Networker interface {
VPNer
}

var workers = make(map[string]Networker)
type IPSecer interface {
AddTunnel(data schema.IPSecTunnel)
DelTunnel(data schema.IPSecTunnel)
ListTunnels(call func(obj schema.IPSecTunnel))
}

type APICall struct {
workers map[string]Networker
secer IPSecer
}

func AddWorker(name string, obj Networker) {
workers[name] = obj
func (i *APICall) AddWorker(name string, obj Networker) {
i.workers[name] = obj
}

func GetWorker(name string) Networker {
return workers[name]
func (i *APICall) GetWorker(name string) Networker {
return i.workers[name]
}

func ListWorker(call func(w Networker)) {
for _, worker := range workers {
func (i *APICall) ListWorker(call func(w Networker)) {
for _, worker := range i.workers {
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),
}
4 changes: 2 additions & 2 deletions pkg/api/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (h Network) Post(w http.ResponseWriter, r *http.Request) {
func (h Network) Delete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
network := vars["id"]
worker := GetWorker(network)
worker := Call.GetWorker(network)
if worker == nil {
http.Error(w, "network not found", http.StatusBadRequest)
return
Expand Down Expand Up @@ -110,7 +110,7 @@ func (h Network) RestartVPN(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (h Output) Post(w http.ResponseWriter, r *http.Request) {
http.Error(w, "network is nil", http.StatusBadRequest)
return
}
worker := GetWorker(name)
worker := Call.GetWorker(name)
if worker == nil {
http.Error(w, "network not found", http.StatusBadRequest)
return
Expand All @@ -73,7 +73,7 @@ func (h Output) Delete(w http.ResponseWriter, r *http.Request) {
http.Error(w, "network is nil", http.StatusBadRequest)
return
}
worker := GetWorker(name)
worker := Call.GetWorker(name)
if worker == nil {
http.Error(w, "network not found", http.StatusBadRequest)
return
Expand All @@ -86,7 +86,7 @@ func (h Output) Save(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusBadRequest)
return
Expand Down
11 changes: 6 additions & 5 deletions pkg/api/qos.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package api

import (
"net/http"

"github.com/gorilla/mux"
"github.com/luscis/openlan/pkg/schema"
"net/http"
)

type QosApi struct {
Expand All @@ -22,7 +23,7 @@ func (h QosApi) List(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand All @@ -47,7 +48,7 @@ func (h QosApi) Add(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down Expand Up @@ -75,7 +76,7 @@ func (h QosApi) Del(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand All @@ -96,7 +97,7 @@ func (h QosApi) Save(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down
9 changes: 5 additions & 4 deletions pkg/api/route.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package api

import (
"net/http"

"github.com/gorilla/mux"
"github.com/luscis/openlan/pkg/cache"
"github.com/luscis/openlan/pkg/models"
"github.com/luscis/openlan/pkg/schema"
"net/http"
)

type Route struct {
Expand Down Expand Up @@ -38,7 +39,7 @@ func (rt Route) Add(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand All @@ -63,7 +64,7 @@ func (rt Route) Del(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand All @@ -88,7 +89,7 @@ func (rt Route) Save(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/ztrust.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (h ZTrust) ListGuest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down Expand Up @@ -77,7 +77,7 @@ func (h ZTrust) AddGuest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down Expand Up @@ -127,7 +127,7 @@ func (h ZTrust) DelGuest(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down Expand Up @@ -165,7 +165,7 @@ func (h ZTrust) ListKnock(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down Expand Up @@ -195,7 +195,7 @@ func (h ZTrust) AddKnock(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]

worker := GetWorker(id)
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/ipsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ func (s *IPSecSpecifies) Correct() {
t.Correct()
}
}

func (s *IPSecSpecifies) AddTunnel(data *IPSecTunnel) {

}

func (s *IPSecSpecifies) DelTunnel(data *IPSecTunnel) {

}
9 changes: 9 additions & 0 deletions pkg/libol/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ type Promise struct {
}

func NewPromise() *Promise {
return &Promise{
First: time.Second * 2,
MaxInt: time.Minute,
MinInt: time.Second * 10,
MaxTry: 10,
}
}

func NewPromiseAlways() *Promise {
return &Promise{
First: time.Second * 2,
MaxInt: time.Minute,
Expand Down
12 changes: 12 additions & 0 deletions pkg/schema/ipsec.go
Original file line number Diff line number Diff line change
@@ -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"`
}
35 changes: 31 additions & 4 deletions pkg/switch/ipsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/luscis/openlan/pkg/api"
co "github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/schema"
)

type IPSecWorker struct {
Expand Down Expand Up @@ -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 := ""
secTmpl := ""

Expand Down Expand Up @@ -152,11 +153,11 @@ func (w *IPSecWorker) Start(v api.Switcher) {
w.uuid = v.UUID()
w.out.Info("IPSecWorker.Start")
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
if tunnel.Transport == "vxlan" {
libol.Exec("ipsec", "auto", "--delete", "--asynchronous", name+"-c1")
Expand Down Expand Up @@ -184,7 +185,7 @@ func (w *IPSecWorker) RemoveTunnel(tunnel *co.IPSecTunnel) error {
func (w *IPSecWorker) Stop() {
w.out.Info("IPSecWorker.Stop")
for _, tunnel := range w.spec.Tunnels {
w.RemoveTunnel(tunnel)
w.removeTunnel(tunnel)
}
}

Expand All @@ -193,3 +194,29 @@ func (w *IPSecWorker) Reload(v api.Switcher) {
w.Initialize()
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)) {

}
6 changes: 4 additions & 2 deletions pkg/switch/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ func NewNetworker(c *co.Network) api.Networker {
var obj api.Networker
switch c.Provider {
case "ipsec":
obj = NewIPSecWorker(c)
secer := NewIPSecWorker(c)
api.Call.SetIPSecer(secer)
obj = secer
case "router":
obj = NewRouterWorker(c)
default:
obj = NewOpenLANWorker(c)
}
api.AddWorker(c.Name, obj)
api.Call.AddWorker(c.Name, obj)
return obj
}

Expand Down

0 comments on commit 7c27b06

Please sign in to comment.