route/internal/server/backend.go

80 lines
1.9 KiB
Go
Raw Normal View History

2017-09-30 16:47:47 +00:00
package server
import (
"git.xeserv.us/xena/route/lib/tun2"
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"
2017-10-01 15:06:27 +00:00
"golang.org/x/net/trace"
2017-09-30 16:47:47 +00:00
)
// 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) {
2017-10-01 15:06:27 +00:00
ctx, clitok, err := b.getAuth(ctx, "backend list", "backend:list")
if err != nil {
return nil, handleError(ctx, clitok, err, ln.F{"action": "authentication for backend list"})
}
tr, _ := trace.FromContext(ctx)
defer tr.Finish()
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) {
2017-10-01 15:06:27 +00:00
ctx, clitok, err := b.getAuth(ctx, "backend list", "backend:list")
if err != nil {
return nil, handleError(ctx, clitok, err, ln.F{"action": "authentication for backend list"})
}
tr, _ := trace.FromContext(ctx)
defer tr.Finish()
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
}