Skip to content

Commit

Permalink
fea: ipsec tunnel cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldin95 committed Jun 18, 2024
1 parent 7c27b06 commit 8799004
Show file tree
Hide file tree
Showing 20 changed files with 486 additions and 369 deletions.
8 changes: 1 addition & 7 deletions cmd/api/v5/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,15 @@ func Commands(app *api.App) {
app.Before = Before
User{}.Commands(app)
ACL{}.Commands(app)
Qos{}.Commands(app)
Device{}.Commands(app)
Lease{}.Commands(app)
Config{}.Commands(app)
Point{}.Commands(app)
VPNClient{}.Commands(app)
Link{}.Commands(app)
Server{}.Commands(app)
Network{}.Commands(app)
PProf{}.Commands(app)
VxLAN{}.Commands(app)
IPSec{}.Commands(app)
Version{}.Commands(app)
Log{}.Commands(app)
Guest{}.Commands(app)
Knock{}.Commands(app)
Output{}.Commands(app)
Route{}.Commands(app)
}
121 changes: 121 additions & 0 deletions cmd/api/v5/ipsec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package v5

import (
"github.com/luscis/openlan/cmd/api"
"github.com/luscis/openlan/pkg/schema"
"github.com/urfave/cli/v2"
)

type IPSec struct {
Cmd
}

func (o IPSec) Commands(app *api.App) {
tunnel := IPSecTunnel{}
app.Command(&cli.Command{
Name: "ipsec",
Usage: "IPSec configuration",
Subcommands: []*cli.Command{
tunnel.Commands(),
},
})
}

type IPSecTunnel struct {
Cmd
}

func (o IPSecTunnel) Url(prefix string) string {
return prefix + "/api/network/ipsec/tunnel"
}

func (o IPSecTunnel) Add(c *cli.Context) error {
output := &schema.IPSecTunnel{
Right: c.String("remote"),
Secret: c.String("secret"),
Transport: c.String("transport"),
LeftId: c.String("localid"),
RightId: c.String("remoteid"),
LeftPort: c.Int("localport"),
RightPort: c.Int("remoteport"),
}
url := o.Url(c.String("url"))
clt := o.NewHttp(c.String("token"))
if err := clt.PostJSON(url, output, nil); err != nil {
return err
}
return nil
}

func (o IPSecTunnel) Remove(c *cli.Context) error {
output := &schema.IPSecTunnel{
Right: c.String("remote"),
Transport: c.String("transport"),
}
url := o.Url(c.String("url"))
clt := o.NewHttp(c.String("token"))
if err := clt.DeleteJSON(url, output, nil); err != nil {
return err
}
return nil
}

func (o IPSecTunnel) Tmpl() string {
return `# total {{ len . }}
{{ps -15 "Right"}} {{ps -15 "Transport"}} {{ps -15 "Secret"}} {{ps -15 "Port"}} {{ps -15 "Connection"}}
{{- range . }}
{{ps -15 .Right}} {{ps -15 .Transport }} {{ps -15 .Secret}} {{.LeftPort}}-{{.RightPort}} {{.LeftId}}-{{.RightId}}
{{- end }}
`
}

func (o IPSecTunnel) List(c *cli.Context) error {
url := o.Url(c.String("url"))
clt := o.NewHttp(c.String("token"))
var items []schema.IPSecTunnel
if err := clt.GetJSON(url, &items); err != nil {
return err
}
return o.Out(items, c.String("format"), o.Tmpl())
}

func (o IPSecTunnel) Commands() *cli.Command {
return &cli.Command{
Name: "tunnel",
Aliases: []string{"tun"},
Usage: "IPSec Tunnel configuration",
Subcommands: []*cli.Command{
{
Name: "add",
Usage: "Add a tunnel for the network",
Flags: []cli.Flag{
&cli.StringFlag{Name: "remote", Required: true},
&cli.StringFlag{Name: "remoteid"},
&cli.IntFlag{Name: "remoteport"},
&cli.StringFlag{Name: "transport", Required: true},
&cli.StringFlag{Name: "secret", Required: true},
&cli.StringFlag{Name: "localid"},
&cli.IntFlag{Name: "localport"},
},
Action: o.Add,
},
{
Name: "remove",
Usage: "Remove a tunnel from the network",
Aliases: []string{"rm"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "remote", Required: true},
&cli.StringFlag{Name: "transport", Required: true},
},
Action: o.Remove,
},
{
Name: "list",
Usage: "Display all tunnel of the network",
Aliases: []string{"ls"},
Flags: []cli.Flag{},
Action: o.List,
},
},
}
}
19 changes: 7 additions & 12 deletions cmd/api/v5/link.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v5

import (
"github.com/luscis/openlan/cmd/api"
"github.com/luscis/openlan/pkg/schema"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -34,7 +33,7 @@ func (u Link) List(c *cli.Context) error {
if err := clt.GetJSON(url, &items); err != nil {
return err
}
name := c.String("network")
name := c.String("name")
if len(name) > 0 {
tmp := items[:0]
for _, obj := range items {
Expand All @@ -47,21 +46,17 @@ func (u Link) List(c *cli.Context) error {
return u.Out(items, c.String("format"), u.Tmpl())
}

func (u Link) Commands(app *api.App) {
app.Command(&cli.Command{
Name: "link",
Aliases: []string{"ln"},
Usage: "Link connect to others",
func (u Link) Commands() *cli.Command {
return &cli.Command{
Name: "link",
Usage: "Link connect to others",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "Display all links",
Aliases: []string{"ls"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "network"},
},
Action: u.List,
Action: u.List,
},
},
})
}
}
69 changes: 18 additions & 51 deletions cmd/api/v5/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,26 @@ func (u Network) Save(c *cli.Context) error {
}

func (u Network) Commands(app *api.App) {
point := Point{}
client := VPNClient{}
route := Route{}
link := Link{}
openvpn := OpenVpn{}
output := Output{}
qos := Qos{}
app.Command(&cli.Command{
Name: "network",
Aliases: []string{"net"},
Usage: "Logical network",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: ""},
},
Usage: "Logical network",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "Display all network",
Aliases: []string{"ls"},
Action: u.List,
Flags: []cli.Flag{
&cli.StringFlag{Name: "name"},
},
},
{
Name: "add",
Expand All @@ -113,60 +119,21 @@ func (u Network) Commands(app *api.App) {
Name: "remove",
Usage: "Remove the network",
Aliases: []string{"rm"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "name"},
},
Action: u.Remove,
Action: u.Remove,
},
{
Name: "save",
Usage: "Save the network",
Aliases: []string{"sa"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: ""},
},
Action: u.Save,
Action: u.Save,
},
point.Commands(),
qos.Commands(),
client.Commands(),
openvpn.Commands(),
output.Commands(),
route.Commands(),
link.Commands(),
},
})
}

type OpenVpn struct {
Cmd
}

func (o OpenVpn) Url(prefix, name string) string {
return prefix + "/api/network/" + name + "/openvpn/restart"
}

func (o OpenVpn) Restart(c *cli.Context) error {
network := c.String("network")
url := o.Url(c.String("url"), network)

clt := o.NewHttp(c.String("token"))
if err := clt.PostJSON(url, nil, nil); err != nil {
return err
}

return nil
}

func (o OpenVpn) Commands() *cli.Command {
return &cli.Command{
Name: "openvpn",
Usage: "control openvpn",
Aliases: []string{"ov"},
Subcommands: []*cli.Command{
{
Name: "restart",
Usage: "restart openvpn for the network",
Aliases: []string{"ro"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "network", Required: true},
},
Action: o.Restart,
},
},
}
}
56 changes: 43 additions & 13 deletions cmd/api/v5/openvpn.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v5

import (
"github.com/luscis/openlan/cmd/api"
"github.com/luscis/openlan/pkg/schema"
"github.com/urfave/cli/v2"
)
Expand All @@ -28,13 +27,13 @@ func (u VPNClient) Tmpl() string {
}

func (u VPNClient) List(c *cli.Context) error {
url := u.Url(c.String("url"), c.String("network"))
url := u.Url(c.String("url"), c.String("name"))
clt := u.NewHttp(c.String("token"))
var items []schema.VPNClient
if err := clt.GetJSON(url, &items); err != nil {
return err
}
name := c.String("network")
name := c.String("name")
if len(name) > 0 {
tmp := items[:0]
for _, obj := range items {
Expand All @@ -47,21 +46,52 @@ func (u VPNClient) List(c *cli.Context) error {
return u.Out(items, c.String("format"), u.Tmpl())
}

func (u VPNClient) Commands(app *api.App) {
app.Command(&cli.Command{
Name: "client",
Aliases: []string{"cl"},
Usage: "Connected client by OpenVPN",
func (u VPNClient) Commands() *cli.Command {
return &cli.Command{
Name: "client",
Usage: "Clients by OpenVPN",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "Display all clients",
Aliases: []string{"ls"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "network"},
},
Action: u.List,
Action: u.List,
},
},
})
}
}

type OpenVpn struct {
Cmd
}

func (o OpenVpn) Url(prefix, name string) string {
return prefix + "/api/network/" + name + "/openvpn/restart"
}

func (o OpenVpn) Restart(c *cli.Context) error {
network := c.String("name")
url := o.Url(c.String("url"), network)

clt := o.NewHttp(c.String("token"))
if err := clt.PostJSON(url, nil, nil); err != nil {
return err
}

return nil
}

func (o OpenVpn) Commands() *cli.Command {
return &cli.Command{
Name: "openvpn",
Usage: "Control OpenVPN",
Subcommands: []*cli.Command{
{
Name: "restart",
Usage: "restart openvpn for the network",
Aliases: []string{"ro"},
Action: o.Restart,
},
},
}
}
Loading

0 comments on commit 8799004

Please sign in to comment.