Add list support
This commit is contained in:
parent
751e312862
commit
8dde47dfe3
3 changed files with 79 additions and 3 deletions
24
LICENSE
Normal file
24
LICENSE
Normal file
|
@ -0,0 +1,24 @@
|
|||
Copyright (c) 2019 @crosbymichael
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use, copy,
|
||||
modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED,
|
||||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
33
main.go
33
main.go
|
@ -38,6 +38,7 @@ import (
|
|||
|
||||
v1 "github.com/crosbymichael/guard/api/v1"
|
||||
"github.com/getsentry/raven-go"
|
||||
"github.com/gogo/protobuf/types"
|
||||
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
@ -79,6 +80,7 @@ func main() {
|
|||
app.Commands = []cli.Command{
|
||||
createCommand,
|
||||
deleteCommand,
|
||||
listCommand,
|
||||
serverCommand,
|
||||
}
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
|
@ -158,7 +160,36 @@ var createCommand = cli.Command{
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(r.Tunnel)
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(r.Tunnel)
|
||||
},
|
||||
}
|
||||
|
||||
var listCommand = cli.Command{
|
||||
Name: "list",
|
||||
Description: "list all tunnels",
|
||||
Action: func(clix *cli.Context) error {
|
||||
conn, err := grpc.Dial(clix.GlobalString("address"), grpc.WithInsecure())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "dial server")
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
var (
|
||||
ctx = cancelContext()
|
||||
client = v1.NewWireguardClient(conn)
|
||||
)
|
||||
r, err := client.List(ctx, &types.Empty{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(r.Tunnels) == 0 {
|
||||
return nil
|
||||
}
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
return enc.Encode(r.Tunnels)
|
||||
},
|
||||
}
|
||||
|
||||
|
|
25
server.go
25
server.go
|
@ -31,9 +31,11 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
v1 "github.com/crosbymichael/guard/api/v1"
|
||||
"github.com/gogo/protobuf/types"
|
||||
|
@ -126,7 +128,26 @@ func (s *server) Delete(ctx context.Context, r *v1.DeleteRequest) (*types.Empty,
|
|||
}
|
||||
|
||||
func (s *server) List(ctx context.Context, _ *types.Empty) (*v1.ListResponse, error) {
|
||||
return nil, nil
|
||||
fi, err := ioutil.ReadDir(s.dir)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "read config dir")
|
||||
}
|
||||
var r v1.ListResponse
|
||||
for _, f := range fi {
|
||||
if !f.IsDir() {
|
||||
continue
|
||||
}
|
||||
data, err := ioutil.ReadFile(filepath.Join(s.dir, f.Name(), "tunnel.json"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "read %s", f.Name())
|
||||
}
|
||||
var t v1.Tunnel
|
||||
if err := json.Unmarshal(data, &t); err != nil {
|
||||
return nil, errors.Wrap(err, "unmarshal tunnel")
|
||||
}
|
||||
r.Tunnels = append(r.Tunnels, &t)
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (s *server) saveConf(t *v1.Tunnel) error {
|
||||
|
@ -159,7 +180,7 @@ func newPrivateKey(ctx context.Context) (string, error) {
|
|||
if err != nil {
|
||||
return "", errors.Wrapf(err, "%s", data)
|
||||
}
|
||||
return string(data), nil
|
||||
return strings.TrimSuffix(string(data), "\n"), nil
|
||||
}
|
||||
|
||||
func wireguard(ctx context.Context, args ...string) ([]byte, error) {
|
||||
|
|
Loading…
Reference in a new issue