From b02517805350a4bb8ab909d2a4b85d5d6b1e6057 Mon Sep 17 00:00:00 2001 From: Daniel Ding Date: Thu, 24 Apr 2025 17:03:41 +0800 Subject: [PATCH] docs: update install.md --- cmd/api/v5/config.go | 135 +---------------------- dist/rootfs/var/openlan/script/switch.sh | 11 +- docs/central.md | 8 +- docs/install.md | 10 +- docs/multiarea.md | 8 +- docs/proxy.md | 2 +- pkg/api/network.go | 2 +- pkg/config/switch.go | 56 ++++++---- pkg/socks5/socks5.go | 2 +- pkg/switch/switch_linux.go | 4 +- 10 files changed, 57 insertions(+), 181 deletions(-) diff --git a/cmd/api/v5/config.go b/cmd/api/v5/config.go index 8f6630e..f810d1c 100755 --- a/cmd/api/v5/config.go +++ b/cmd/api/v5/config.go @@ -2,11 +2,9 @@ package v5 import ( "fmt" - "path/filepath" "github.com/luscis/openlan/cmd/api" "github.com/luscis/openlan/pkg/config" - "github.com/luscis/openlan/pkg/libol" "github.com/luscis/openlan/pkg/schema" "github.com/urfave/cli/v2" ) @@ -29,9 +27,7 @@ func (u Config) List(c *cli.Context) error { if err := clt.GetJSON(url, cfg); err == nil { name := c.String("network") format := c.String("format") - if format == "yaml" { - cfg.FormatNetworks() - } + cfg.RemarshalNetworks(format) if len(name) > 0 { obj := cfg.GetNetwork(name) return u.Out(obj, format, "") @@ -43,126 +39,6 @@ func (u Config) List(c *cli.Context) error { } } -func (u Config) Check(c *cli.Context) error { - out := u.Log() - dir := c.String("dir") - // Check proxy configurations. - out.Info("%15s: %s", "check", "proxy") - file := filepath.Join(dir, "proxy.json") - if err := libol.FileExist(file); err == nil { - obj := &config.Proxy{} - if err := libol.UnmarshalLoad(obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - // Check OLAP configurations. - out.Info("%15s: %s", "check", "access") - file = filepath.Join(dir, "access", "access.json") - if err := libol.FileExist(file); err == nil { - obj := &config.Access{} - if err := libol.UnmarshalLoad(obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - // Check OLSW configurations. - out.Info("%15s: %s", "check", "switch") - file = filepath.Join(dir, "switch", "switch.json") - if err := libol.FileExist(file); err == nil { - obj := &config.Switch{} - if err := libol.UnmarshalLoad(obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - // Check network configurations. - out.Info("%15s: %s", "check", "network") - pattern := filepath.Join(dir, "switch", "network", "*.json") - if files, err := filepath.Glob(pattern); err == nil { - for _, file := range files { - obj := &config.Network{} - if err := libol.UnmarshalLoad(obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - } - - out.Info("%15s: %s", "check", "qos") - pattern = filepath.Join(dir, "switch", "qos", "*.json") - if files, err := filepath.Glob(pattern); err == nil { - for _, file := range files { - obj := &config.Qos{} - if err := libol.UnmarshalLoad(obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - } - - // Check ACL configurations. - out.Info("%15s: %s", "check", "acl") - pattern = filepath.Join(dir, "switch", "acl", "*.json") - if files, err := filepath.Glob(pattern); err == nil { - for _, file := range files { - obj := &config.ACL{} - if err := libol.UnmarshalLoad(obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - } - // Check links configurations. - out.Info("%15s: %s", "check", "link") - pattern = filepath.Join(dir, "switch", "link", "*.json") - if files, err := filepath.Glob(pattern); err == nil { - for _, file := range files { - var obj []config.Access - if err := libol.UnmarshalLoad(&obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - } - // Check routes configurations. - out.Info("%15s: %s", "check", "route") - pattern = filepath.Join(dir, "switch", "route", "*.json") - if files, err := filepath.Glob(pattern); err == nil { - for _, file := range files { - var obj []config.PrefixRoute - if err := libol.UnmarshalLoad(&obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - } - - // Check output config - out.Info("%15s: %s", "check", "output") - pattern = filepath.Join(dir, "switch", "output", "*.json") - if files, err := filepath.Glob(pattern); err == nil { - for _, file := range files { - var obj []config.Output - if err := libol.UnmarshalLoad(&obj, file); err != nil { - out.Warn("%15s: %s", filepath.Base(file), err) - } else { - out.Info("%15s: %s", filepath.Base(file), "success") - } - } - } - - return nil -} - func (u Config) Reload(c *cli.Context) error { url := u.Url(c.String("url"), "reload") clt := u.NewHttp(c.String("token")) @@ -201,15 +77,6 @@ func (u Config) Commands(app *api.App) { }, Action: u.List, }, - { - Name: "check", - Usage: "Check all configuration", - Aliases: []string{"co"}, - Flags: []cli.Flag{ - &cli.StringFlag{Name: "dir", Value: "/etc/openlan"}, - }, - Action: u.Check, - }, { Name: "reload", Usage: "Reload configuration", diff --git a/dist/rootfs/var/openlan/script/switch.sh b/dist/rootfs/var/openlan/script/switch.sh index 436d829..d6895ca 100755 --- a/dist/rootfs/var/openlan/script/switch.sh +++ b/dist/rootfs/var/openlan/script/switch.sh @@ -14,13 +14,10 @@ sysctl -p /etc/sysctl.d/90-openlan.conf /usr/bin/env find /var/openlan/openvpn -name '*client.ovpn' -delete /usr/bin/env find /var/openlan/openvpn -name '*client.tmpl' -delete -if [ ! -e "/etc/openlan/switch/switch.json" ]; then -cat >> /etc/openlan/switch/switch.json << EOF -{ - "crypt": { - "secret": "cb2ff088a34d" - } -} +if [ ! -e "/etc/openlan/switch/switch.yaml" ]; then +cat >> /etc/openlan/switch/switch.yaml << EOF +crypt + secret: cb2ff088a34d EOF fi diff --git a/docs/central.md b/docs/central.md index 29f5ef9..2a3f5a7 100755 --- a/docs/central.md +++ b/docs/central.md @@ -64,10 +64,10 @@ Add three access users on central network: ``` -[root@switch ~]# openlan us add --name admin@central --role admin -[root@switch ~]# openlan us add --name access1@central -[root@switch ~]# openlan us add --name access2@central -[root@switch ~]# openlan us add --name access3@central +[root@switch ~]# openlan user add --name admin@central --role admin +[root@switch ~]# openlan user add --name access1@central +[root@switch ~]# openlan user add --name access2@central +[root@switch ~]# openlan user add --name access3@central ``` diff --git a/docs/install.md b/docs/install.md index 5a35ac8..86e2f5c 100755 --- a/docs/install.md +++ b/docs/install.md @@ -27,7 +27,7 @@ OpenLAN软件包含下面部分: ``` $ cd /etc/openlan/switch $ cp ./switch.yaml.example ./switch.yaml - $ vim ./switch.yaml ## 编辑switch.yaml配置文件 + $ vim ./switch.yaml protocol: tcp crypt: algorithm: aes-128 @@ -39,7 +39,7 @@ OpenLAN软件包含下面部分: $ cd ./network $ cp ./network.yaml.example ./example.yaml $ vim ./example.yaml - name: example, + name: example bridge: address: 172.32.10.10/24 subnet: @@ -58,10 +58,8 @@ OpenLAN软件包含下面部分: ``` 3. 添加一个新的接入认证的用户; ``` - $ openlan us add --name hi@example + $ openlan user add --name hi@example hi@example l6llot97yx guest - - $ openlan us rm --name hi@example ``` 4. 导出OpenVPN的客户端配置文件; @@ -104,7 +102,7 @@ OpenLAN软件包含下面部分: 3. 配置Access Point服务自启动; ``` $ systemctl enable --now openlan-access@example - $ journalctl -u openlan-access@example ## 查看日志信息 + $ journalctl -u openlan-access@example ``` 4. 检测网络是否可达; ``` diff --git a/docs/multiarea.md b/docs/multiarea.md index 9690b51..4aec3c1 100755 --- a/docs/multiarea.md +++ b/docs/multiarea.md @@ -106,9 +106,9 @@ EOF Add three access users on private network: ``` -[root@switch-sh ~]# openlan us add --name admin@private --role admin -[root@switch-sh ~]# openlan us add --name access3@private -[root@switch-sh ~]# openlan us add --name access4@private -[root@switch-sh ~]# openlan us add --name access5@private +[root@switch-sh ~]# openlan user add --name admin@private --role admin +[root@switch-sh ~]# openlan user add --name access3@private +[root@switch-sh ~]# openlan user add --name access4@private +[root@switch-sh ~]# openlan user add --name access5@private ``` diff --git a/docs/proxy.md b/docs/proxy.md index bff8ece..69d78fd 100755 --- a/docs/proxy.md +++ b/docs/proxy.md @@ -34,7 +34,7 @@ root@openlan::/opt/openlan/etc/openlan# docker restart openlan_proxy_1 root@openlan:/opt/openlan/etc/openlan# cd /opt/openlan/etc/openlan root@openlan:/opt/openlan/etc/openlan# cat > proxy.yaml << EOF tcp: -- listen: 192.168.1.66:11082, +- listen: 192.168.1.66:11082 target: [192.168.1.88:11082 EOF diff --git a/pkg/api/network.go b/pkg/api/network.go index b14cf57..acdea05 100755 --- a/pkg/api/network.go +++ b/pkg/api/network.go @@ -65,7 +65,7 @@ func (h Network) Post(w http.ResponseWriter, r *http.Request) { return } - cs.CorrectNetwork(obj) + cs.CorrectNetwork(obj, "json") if obj := cs.GetNetwork(obj.Name); obj != nil { h.Switcher.AddNetwork(obj.Name) } else { diff --git a/pkg/config/switch.go b/pkg/config/switch.go index 280296b..01caf2c 100755 --- a/pkg/config/switch.go +++ b/pkg/config/switch.go @@ -158,7 +158,7 @@ func (s *Switch) Dir(elem0, elem1 string) string { var file string if elem1 == "" { - return filepath.Join(elem0) + return filepath.Join(s.ConfDir, elem0) } if s.IsYaml() { @@ -170,16 +170,33 @@ func (s *Switch) Dir(elem0, elem1 string) string { return filepath.Join(s.ConfDir, elem0, file) } -func (s *Switch) formatNetwork(obj *Network) { - context := obj.Specifies - obj.NewSpecifies() +func (s *Switch) RemarshalNetwork(obj *Network, format string) { if obj.Specifies == nil { return } - if data, err := libol.Marshal(context, true); err != nil { - libol.Warn("Switch.Format %s", err) - } else if err := libol.Unmarshal(obj.Specifies, data); err != nil { - libol.Warn("Switch.Format %s", err) + + context := obj.Specifies + obj.NewSpecifies() + + if format == "" { + format = "json" + if s.IsYaml() { + format = "yaml" + } + } + + if format == "yaml" { + if data, err := libol.MarshalYaml(context); err != nil { + libol.Warn("Switch.Format %s", err) + } else if err := libol.UnmarshalYaml(obj.Specifies, data); err != nil { + libol.Warn("Switch.Format %s", err) + } + } else { + if data, err := libol.Marshal(context, true); err != nil { + libol.Warn("Switch.Format %s", err) + } else if err := libol.Unmarshal(obj.Specifies, data); err != nil { + libol.Warn("Switch.Format %s", err) + } } } @@ -210,11 +227,19 @@ func (s *Switch) UnmarshalNetwork(data []byte) (*Network, error) { obj.LoadRoute() obj.LoadOutput() obj.LoadFindHop() + s.Network[obj.Name] = obj return obj, nil } -func (s *Switch) correctNetwork(obj *Network) { +func (s *Switch) RemarshalNetworks(format string) { + for _, obj := range s.Network { + s.RemarshalNetwork(obj, format) + } +} + +func (s *Switch) CorrectNetwork(obj *Network, format string) { + s.RemarshalNetwork(obj, format) for _, link := range obj.Links { link.Correct() } @@ -239,20 +264,9 @@ func (s *Switch) correctNetwork(obj *Network) { } } -func (s *Switch) FormatNetworks() { - for _, obj := range s.Network { - s.formatNetwork(obj) - } -} - -func (s *Switch) CorrectNetwork(obj *Network) { - s.formatNetwork(obj) - s.correctNetwork(obj) -} - func (s *Switch) CorrectNetworks() { for _, obj := range s.Network { - s.CorrectNetwork(obj) + s.CorrectNetwork(obj, "") } } diff --git a/pkg/socks5/socks5.go b/pkg/socks5/socks5.go index da8bcae..b62ce56 100644 --- a/pkg/socks5/socks5.go +++ b/pkg/socks5/socks5.go @@ -60,7 +60,7 @@ type Config struct { TlsConfig *tls.Config } -// Server is reponsible for accepting connections and handling +// Server is responsible for accepting connections and handling // the details of the SOCKS5 protocol type Server struct { config *Config diff --git a/pkg/switch/switch_linux.go b/pkg/switch/switch_linux.go index 49b6db8..fc662c2 100755 --- a/pkg/switch/switch_linux.go +++ b/pkg/switch/switch_linux.go @@ -428,8 +428,8 @@ func (v *Switch) Stop() { } w.Stop() } - v.out.Info("Switch.Stop left Accesss") - // notify leave to Access. + v.out.Info("Switch.Stop left access") + // notify leave to access. for p := range cache.Access.List() { if p == nil { break