route/internal/server/backend.go

60 lines
1.2 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"
"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) {
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) {
if err := b.ts.KillBackend(bid.Id); err != nil {
return nil, err
}
return &proto.Nil{}, nil
}