mirror of
https://github.com/luscis/openlan.git
synced 2025-12-24 11:10:54 +08:00
fix: link convert to output.
This commit is contained in:
2
dist/rootfs/var/openlan/script/switch.sh
vendored
2
dist/rootfs/var/openlan/script/switch.sh
vendored
@@ -14,7 +14,7 @@ sysctl -p /etc/sysctl.d/90-openlan.conf
|
||||
## END
|
||||
|
||||
## START: prepare external dir.
|
||||
for dir in network acl findhop link output route qos dnat; do
|
||||
for dir in network acl findhop output route qos dnat; do
|
||||
[ -e "$cs_dir/$dir" ] || mkdir -p "$cs_dir/$dir"
|
||||
done
|
||||
## END
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -25,7 +24,6 @@ func (h Output) Get(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
name := vars["id"]
|
||||
|
||||
libol.Debug("Output.Get %s")
|
||||
outputs := make([]schema.Output, 0, 1024)
|
||||
for l := range cache.Output.List(name) {
|
||||
if l == nil {
|
||||
|
||||
0
pkg/cache/output.go
vendored
Executable file → Normal file
0
pkg/cache/output.go
vendored
Executable file → Normal file
@@ -24,6 +24,10 @@ func (c *Crypt) Correct() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Crypt) Short() string {
|
||||
return c.Algo + ":" + c.Secret
|
||||
}
|
||||
|
||||
type Cert struct {
|
||||
Dir string `json:"directory" yaml:"directory"`
|
||||
CrtFile string `json:"cert" yaml:"cert"`
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/luscis/openlan/pkg/libol"
|
||||
)
|
||||
@@ -131,7 +132,6 @@ func (n *Network) Correct(sw *Switch) {
|
||||
}
|
||||
|
||||
CorrectRoutes(n.Routes, ipAddr)
|
||||
|
||||
if n.OpenVPN != nil {
|
||||
n.OpenVPN.Correct(sw.AddrPool, n.Name)
|
||||
}
|
||||
@@ -140,6 +140,9 @@ func (n *Network) Correct(sw *Switch) {
|
||||
value.Correct()
|
||||
n.FindHop[key] = value
|
||||
}
|
||||
for _, value := range n.Outputs {
|
||||
value.Correct()
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Network) Dir(module string) string {
|
||||
@@ -159,7 +162,6 @@ func (n *Network) IsYaml() bool {
|
||||
}
|
||||
|
||||
func (n *Network) Load() {
|
||||
n.LoadLink()
|
||||
n.LoadRoute()
|
||||
n.LoadOutput()
|
||||
n.LoadFindHop()
|
||||
@@ -180,11 +182,32 @@ func (n *Network) LoadRoute() {
|
||||
}
|
||||
}
|
||||
|
||||
func UserShort(value string) string {
|
||||
return strings.SplitN(value, "@", 2)[0]
|
||||
}
|
||||
|
||||
func (n *Network) LoadOutput() {
|
||||
file := n.Dir("output")
|
||||
if err := libol.UnmarshalLoad(&n.Outputs, file); err != nil {
|
||||
libol.Error("Network.LoadOutput... %n", err)
|
||||
}
|
||||
|
||||
n.LoadLink()
|
||||
// Clone link to outputs.
|
||||
for _, link := range n.Links {
|
||||
link.Correct()
|
||||
username := UserShort(link.Username)
|
||||
value := &Output{
|
||||
Protocol: link.Protocol,
|
||||
Remote: link.Connection,
|
||||
Secret: username + ":" + link.Password,
|
||||
Crypt: link.Crypt.Short(),
|
||||
}
|
||||
if _, index := n.FindOutput(value); index == -1 {
|
||||
n.Outputs = append(n.Outputs, value)
|
||||
}
|
||||
}
|
||||
n.Links = nil
|
||||
}
|
||||
|
||||
func (n *Network) LoadFindHop() {
|
||||
@@ -203,19 +226,14 @@ func (n *Network) LoadDnat() {
|
||||
|
||||
func (n *Network) Save() {
|
||||
obj := *n
|
||||
|
||||
obj.Routes = nil
|
||||
obj.Links = nil
|
||||
obj.Outputs = nil
|
||||
obj.Dnat = nil
|
||||
obj.FindHop = nil
|
||||
|
||||
obj.FindHop = nil // Clear sub dirs.
|
||||
if err := libol.MarshalSave(&obj, obj.File, true); err != nil {
|
||||
libol.Error("Network.Save %s %s", obj.Name, err)
|
||||
}
|
||||
|
||||
n.SaveRoute()
|
||||
n.SaveLink()
|
||||
n.SaveOutput()
|
||||
n.SaveFindHop()
|
||||
n.SaveDnat()
|
||||
@@ -228,13 +246,6 @@ func (n *Network) SaveRoute() {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Network) SaveLink() {
|
||||
file := n.Dir("link")
|
||||
if err := libol.MarshalSave(n.Links, file, true); err != nil {
|
||||
libol.Error("Network.SaveLink %s %s", n.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Network) SaveOutput() {
|
||||
file := n.Dir("output")
|
||||
if err := libol.MarshalSave(n.Outputs, file, true); err != nil {
|
||||
@@ -318,6 +329,12 @@ func (n *Network) DelOutput(value *Output) (*Output, bool) {
|
||||
return obj, index != -1
|
||||
}
|
||||
|
||||
func (n *Network) ListOutput(call func(value Output)) {
|
||||
for _, obj := range n.Outputs {
|
||||
call(*obj)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Network) FindFindHop(value *FindHop) *FindHop {
|
||||
if n.FindHop == nil {
|
||||
n.FindHop = make(map[string]*FindHop)
|
||||
|
||||
@@ -22,7 +22,7 @@ func (o *Output) Id() string {
|
||||
return fmt.Sprintf("%s-%s-%d", o.Protocol, o.Remote, o.Segment)
|
||||
}
|
||||
|
||||
func (o *Output) GenName() {
|
||||
func (o *Output) Correct() {
|
||||
switch o.Protocol {
|
||||
case "gre":
|
||||
o.Link = fmt.Sprintf("%s%d", "gei", o.Segment)
|
||||
|
||||
@@ -601,7 +601,6 @@ func (w *WorkerImpl) Start(v api.SwitchApi) {
|
||||
if cfg.Bridge != nil {
|
||||
w.toACL(cfg.Bridge.Name)
|
||||
for _, output := range cfg.Outputs {
|
||||
output.GenName()
|
||||
w.addOutput(cfg.Bridge.Name, output)
|
||||
}
|
||||
}
|
||||
@@ -1248,7 +1247,7 @@ func (w *WorkerImpl) AddOutput(data schema.Output) {
|
||||
w.out.Info("WorkerImple.AddOutput %s already existed", output.Id())
|
||||
return
|
||||
}
|
||||
output.GenName()
|
||||
output.Correct()
|
||||
w.addOutput(w.cfg.Bridge.Name, output)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user