Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 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 | |
| 18 | type service struct { |
| 19 | iana *provider.IANA |
| 20 | providers map[provider.IRR]provider.Provider |
| 21 | } |
| 22 | |
| 23 | func 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 Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 32 | iana: provider.NewIANA(2), |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 33 | providers: map[provider.IRR]provider.Provider{ |
Sergiusz Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 34 | provider.IRR_RIPE: provider.NewRIPE(10), |
| 35 | provider.IRR_ARIN: provider.NewARIN(2), |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 36 | }, |
| 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. |
| 49 | func (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 Bazanski | 2316ac0 | 2019-08-03 23:49:19 +0200 | [diff] [blame] | 71 | return nil, status.Errorf(codes.Unimplemented, "AS belongs to unhandled IRR %s", irr.String()) |
Sergiusz Bazanski | 6eaaaf9 | 2019-08-02 01:25:31 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | res, err := prov.Query(ctx, asn) |
| 75 | return res, err |
| 76 | } |