iptoasn.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package thothmock
  2. import (
  3. "context"
  4. "net/netip"
  5. iptoasnv1 "github.com/TecharoHQ/thoth-proto/gen/techaro/thoth/iptoasn/v1"
  6. "google.golang.org/grpc"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/status"
  9. )
  10. func MockIpToASNService() *IpToASNService {
  11. responses := map[string]*iptoasnv1.LookupResponse{
  12. "127.0.0.1": {Announced: false},
  13. "::1": {Announced: false},
  14. "10.10.10.10": {
  15. Announced: true,
  16. AsNumber: 13335,
  17. Cidr: []string{"1.1.1.0/24"},
  18. CountryCode: "US",
  19. Description: "Cloudflare",
  20. },
  21. "2.2.2.2": {
  22. Announced: true,
  23. AsNumber: 420,
  24. Cidr: []string{"2.2.2.0/24"},
  25. CountryCode: "CA",
  26. Description: "test canada",
  27. },
  28. "1.1.1.1": {
  29. Announced: true,
  30. AsNumber: 13335,
  31. Cidr: []string{"1.1.1.0/24"},
  32. CountryCode: "US",
  33. Description: "Cloudflare",
  34. },
  35. }
  36. return &IpToASNService{Responses: responses}
  37. }
  38. type IpToASNService struct {
  39. iptoasnv1.UnimplementedIpToASNServiceServer
  40. Responses map[string]*iptoasnv1.LookupResponse
  41. }
  42. func (ip2asn *IpToASNService) Lookup(ctx context.Context, lr *iptoasnv1.LookupRequest, opts ...grpc.CallOption) (*iptoasnv1.LookupResponse, error) {
  43. if _, err := netip.ParseAddr(lr.GetIpAddress()); err != nil {
  44. return nil, err
  45. }
  46. resp, ok := ip2asn.Responses[lr.GetIpAddress()]
  47. if !ok {
  48. return nil, status.Error(codes.NotFound, "IP address not found in mock")
  49. }
  50. return resp, nil
  51. }