blob: 90061fb36066da090c1ab66a75025f7b1e7f6b7a [file] [log] [blame]
Sergiusz Bazanski6eaaaf92019-08-02 01:25:31 +02001package main
2
3import (
4 "context"
5 "flag"
6 "strconv"
7 "strings"
8
9 "github.com/golang/glog"
10 "google.golang.org/grpc/codes"
11 "google.golang.org/grpc/status"
12
13 "code.hackerspace.pl/hscloud/bgpwtf/cccampix/irr/provider"
14 pb "code.hackerspace.pl/hscloud/bgpwtf/cccampix/proto"
15 "code.hackerspace.pl/hscloud/go/mirko"
16)
17
18type service struct {
19 iana *provider.IANA
20 providers map[provider.IRR]provider.Provider
21}
22
23func main() {
24 flag.Parse()
25 mi := mirko.New()
26
27 if err := mi.Listen(); err != nil {
28 glog.Exitf("Listen failed: %v", err)
29 }
30
31 s := &service{
Sergiusz Bazanski2316ac02019-08-03 23:49:19 +020032 iana: provider.NewIANA(2),
Sergiusz Bazanski6eaaaf92019-08-02 01:25:31 +020033 providers: map[provider.IRR]provider.Provider{
Sergiusz Bazanski2316ac02019-08-03 23:49:19 +020034 provider.IRR_RIPE: provider.NewRIPE(10),
35 provider.IRR_ARIN: provider.NewARIN(2),
Sergiusz Bazanski6eaaaf92019-08-02 01:25:31 +020036 },
37 }
38 pb.RegisterIRRServer(mi.GRPC(), s)
39
40 if err := mi.Serve(); err != nil {
41 glog.Exitf("Serve failed: %v", err)
42 }
43
44 <-mi.Done()
45}
46
47// Query returns parsed RPSL data for a given aut-num objects in any of the
48// supported IRRs.
49func (s *service) Query(ctx context.Context, req *pb.IRRQueryRequest) (*pb.IRRQueryResponse, error) {
50 if req.As == "" {
51 return nil, status.Error(codes.InvalidArgument, "as must be given")
52 }
53
54 req.As = strings.ToLower(req.As)
55 if strings.HasPrefix(req.As, "as") {
56 req.As = req.As[2:]
57 }
58
59 asn, err := strconv.ParseUint(req.As, 10, 64)
60 if err != nil {
61 return nil, status.Error(codes.InvalidArgument, "as is invalid")
62 }
63
64 irr, err := s.iana.Who(ctx, asn)
65 if err != nil {
66 return nil, status.Errorf(codes.Unavailable, "could not find AS block delegation from IANA: %v", err)
67 }
68
69 prov, ok := s.providers[irr]
70 if !ok {
Sergiusz Bazanski2316ac02019-08-03 23:49:19 +020071 return nil, status.Errorf(codes.Unimplemented, "AS belongs to unhandled IRR %s", irr.String())
Sergiusz Bazanski6eaaaf92019-08-02 01:25:31 +020072 }
73
74 res, err := prov.Query(ctx, asn)
75 return res, err
76}