-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c27b06
commit d17f64f
Showing
15 changed files
with
377 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package v5 | ||
|
||
import ( | ||
"github.com/luscis/openlan/cmd/api" | ||
"github.com/luscis/openlan/pkg/libol" | ||
"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", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{Name: "network", Required: true}, | ||
}, | ||
Subcommands: []*cli.Command{ | ||
tunnel.Commands(), | ||
}, | ||
}) | ||
} | ||
|
||
type IPSecTunnel struct { | ||
Cmd | ||
} | ||
|
||
func (o IPSecTunnel) Url(prefix, name string) string { | ||
return prefix + "/api/network/" + name + "/tunnel" | ||
} | ||
|
||
func (o IPSecTunnel) Add(c *cli.Context) error { | ||
network := c.String("network") | ||
if len(network) == 0 { | ||
return libol.NewErr("invalid network") | ||
} | ||
output := &schema.IPSecTunnel{ | ||
Right: c.String("remote"), | ||
Secret: c.String("secret"), | ||
Transport: c.String("protocol"), | ||
} | ||
url := o.Url(c.String("url"), network) | ||
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 { | ||
network := c.String("network") | ||
if len(network) == 0 { | ||
return libol.NewErr("invalid network") | ||
} | ||
output := &schema.IPSecTunnel{ | ||
Right: c.String("remote"), | ||
Secret: c.String("secret"), | ||
Transport: c.String("protocol"), | ||
} | ||
url := o.Url(c.String("url"), network) | ||
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 -24 "network"}} {{ps -15 "protocol"}} {{ps -15 "Right"}} {{ps -15 "Transport"}} {{ps -15 "RightId"}} | ||
{{- range . }} | ||
{{ps -24 .Network}} {{ps -15 .Protocol}} {{ps -15 .Right}} {{pi -15 .Transport }} {{ps -15 .RightId}} | ||
{{- end }} | ||
` | ||
} | ||
|
||
func (o IPSecTunnel) List(c *cli.Context) error { | ||
url := o.Url(c.String("url"), c.String("network")) | ||
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: "transport", Required: true}, | ||
&cli.StringFlag{Name: "secret", Required: true}, | ||
}, | ||
Action: o.Add, | ||
}, | ||
{ | ||
Name: "remove", | ||
Usage: "Remove a tunnel from the network", | ||
Aliases: []string{"rm"}, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{Name: "remote", Required: true}, | ||
}, | ||
Action: o.Remove, | ||
}, | ||
{ | ||
Name: "list", | ||
Usage: "Display all tunnel of the network", | ||
Aliases: []string{"ls"}, | ||
Flags: []cli.Flag{}, | ||
Action: o.List, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.