route/cmd/routed/backend.go

73 lines
1.8 KiB
Go
Raw Normal View History

2017-12-15 18:18:13 +00:00
package main
2017-09-30 16:47:47 +00:00
import (
2017-10-01 15:23:08 +00:00
"git.xeserv.us/xena/route/internal/tun2"
2017-09-30 16:47:47 +00:00
proto "git.xeserv.us/xena/route/proto"
2017-10-01 15:06:27 +00:00
"github.com/Xe/ln"
2017-09-30 16:47:47 +00:00
"golang.org/x/net/context"
)
// Backend implements proto.BackendsServer for gRPC.
type Backend struct {
*Server
}
// List returns a list of backends given filtering parameters.
func (b *Backend) List(ctx context.Context, sel *proto.BackendSelector) (*proto.BackendList, error) {
clitok, err := b.getAuth(ctx, "backend list", "backend:list")
2017-10-01 15:06:27 +00:00
if err != nil {
return nil, handleError(ctx, clitok, err, ln.F{"action": "authentication for backend list"})
}
2017-09-30 16:47:47 +00:00
bl := map[string]tun2.Backend{}
switch {
case sel.Domain != "":
for _, bk := range b.ts.GetBackendsForDomain(sel.Domain) {
bl[bk.ID] = bk
}
case sel.User != "":
for _, bk := range b.ts.GetBackendsForUser(sel.User) {
bl[bk.ID] = bk
}
default:
for _, bk := range b.ts.GetAllBackends() {
bl[bk.ID] = bk
}
}
resp := &proto.BackendList{
Bs: sel,
}
for _, bk := range bl {
resp.Backends = append(resp.Backends, &proto.Backend{
Id: bk.ID,
Proto: bk.Proto,
User: bk.User,
Domain: bk.Domain,
Phi: bk.Phi,
Host: bk.Host,
2017-09-30 17:33:19 +00:00
Usable: bk.Usable,
2017-09-30 16:47:47 +00:00
})
}
return resp, nil
}
// Kill removes a backend's connection by ID.
func (b *Backend) Kill(ctx context.Context, bid *proto.BackendID) (*proto.Nil, error) {
clitok, err := b.getAuth(ctx, "backend list", "backend:list")
2017-10-01 15:06:27 +00:00
if err != nil {
return nil, handleError(ctx, clitok, err, ln.F{"action": "authentication for backend list"})
}
2017-09-30 16:47:47 +00:00
if err := b.ts.KillBackend(bid.Id); err != nil {
2017-10-01 15:06:27 +00:00
return nil, handleError(ctx, clitok, err, ln.F{"action": "killing backend", "backend_id": bid.Id})
2017-09-30 16:47:47 +00:00
}
2017-10-01 15:06:27 +00:00
ln.Log(ctx, clitok, ln.Action("backend killed"), ln.F{"backend_id": bid.Id})
2017-09-30 16:47:47 +00:00
return &proto.Nil{}, nil
}