diff --git a/README.md b/README.md index b8ed061..7e39b84 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,6 @@ -# Micro Services +IP to geolocation lookup -The canonical source for Micro services. +## IP Service -## Overview - -Services provides a home for real world reusable Micro services. - -- [routing](routing) - etas, routes and turn by turn directions -- [geocoding](geocoding) - address to gps location and reverse -- [location](location) - gps point location tracking - -## Usage - -Run a service from source - -``` -micro run github.com/micro/services/helloworld -``` - -## Contributing - -Feel free to contribute by PR and signoff. - -## Documentation - -Documentation for this repo is autogenerated and appears on [services.m3o.com](https://services.m3o.com) - -Read this [doc](cmd/docgen/README.md) on how to write documentation for these services. - -## License - -[Polyform Strict](https://polyformproject.org/licenses/strict/1.0.0/) +The IP or ip2geo service provides IP to geolocation lookup. It includes asn, city, country and lat/long. diff --git a/ip/.gitignore b/ip/.gitignore new file mode 100644 index 0000000..1d8417b --- /dev/null +++ b/ip/.gitignore @@ -0,0 +1,2 @@ + +ip diff --git a/ip/Dockerfile b/ip/Dockerfile new file mode 100644 index 0000000..17ce198 --- /dev/null +++ b/ip/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD ip /ip +ENTRYPOINT [ "/ip" ] diff --git a/ip/Makefile b/ip/Makefile new file mode 100644 index 0000000..e467883 --- /dev/null +++ b/ip/Makefile @@ -0,0 +1,28 @@ + +GOPATH:=$(shell go env GOPATH) +.PHONY: init +init: + go get -u github.com/golang/protobuf/proto + go get -u github.com/golang/protobuf/protoc-gen-go + go get github.com/micro/micro/v3/cmd/protoc-gen-micro + go get github.com/micro/micro/v3/cmd/protoc-gen-openapi + +.PHONY: api +api: + protoc --openapi_out=. --proto_path=. proto/ip.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/ip.proto + +.PHONY: build +build: + go build -o ip *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t ip:latest diff --git a/ip/README.md b/ip/README.md new file mode 100644 index 0000000..e1a8da8 --- /dev/null +++ b/ip/README.md @@ -0,0 +1,23 @@ +# Ip Service + +This is the Ip service + +Generated with + +``` +micro new ip +``` + +## Usage + +Generate the proto code + +``` +make proto +``` + +Run the service + +``` +micro run . +``` \ No newline at end of file diff --git a/ip/config.md b/ip/config.md new file mode 100644 index 0000000..fdff5b2 --- /dev/null +++ b/ip/config.md @@ -0,0 +1,11 @@ +# IP Config + +The ip service depends on the maxmind geolite2 dataset. You must configure its on disk location. + +``` +micro config set ip.city.database /tmp/GeoLite2-City.mmdb +micro config set ip.asn.database /tmp/GeoLite2-ASN.mmdb +``` + +In the event the config is not found it will attempt to read these two files from the local directory. +If the config value is prefixed with `blob://` it will attempt to read it from the blob store and store on disk. diff --git a/ip/examples.json b/ip/examples.json new file mode 100644 index 0000000..c4179b8 --- /dev/null +++ b/ip/examples.json @@ -0,0 +1,18 @@ +{ + "lookup": [{ + "title": "Lookup IP info", + "description": "Lookup the location info for an IP address", + "request": { + "ip", "93.148.214.31" + }, + "response": { + "ip": "93.148.214.31", + "asn": "30722", + "city": "Reggiolo", + "country": "Italy", + "latitude": 44.9201, + "longitude": 10.8075, + "timezone": "Europe/Rome" + } + }] +} diff --git a/ip/generate.go b/ip/generate.go new file mode 100644 index 0000000..7d9db91 --- /dev/null +++ b/ip/generate.go @@ -0,0 +1,3 @@ +package main + +//go:generate make proto diff --git a/ip/handler/ip.go b/ip/handler/ip.go new file mode 100644 index 0000000..f7572e0 --- /dev/null +++ b/ip/handler/ip.go @@ -0,0 +1,56 @@ +package handler + +import ( + "context" + "net" + + "github.com/micro/micro/v3/service/errors" + "github.com/micro/micro/v3/service/logger" + pb "github.com/micro/services/ip/proto" + geoip2 "github.com/oschwald/geoip2-golang" +) + +type Ip struct { + ASNReader *geoip2.Reader + CityReader *geoip2.Reader +} + +func (i *Ip) Lookup(ctx context.Context, req *pb.LookupRequest, rsp *pb.LookupResponse) error { + if len(req.Ip) == 0 { + return errors.BadRequest("ip.lookup", "missing ip") + } + + // get the ip + ip := net.ParseIP(req.Ip) + + // only if the asn reader exists + if i.ASNReader != nil { + asn, err := i.ASNReader.ASN(ip) + if err != nil { + logger.Errorf("failed to lookup asn for %v: %v", req.Ip, err) + return errors.InternalServerError("ip.lookup", "failed to lookup ip") + } + // set asp + rsp.Asn = int64(asn.AutonomousSystemNumber) + } + + info, err := i.CityReader.City(ip) + if err != nil { + logger.Errorf("failed to lookup city for %v: %v", req.Ip, err) + return errors.InternalServerError("ip.lookup", "failed to lookup ip") + } + + // set ip + rsp.Ip = req.Ip + // set city + rsp.City = info.City.Names["en"] + // set countr + rsp.Country = info.Country.Names["en"] + // latitude/longitude + rsp.Latitude = info.Location.Latitude + rsp.Longitude = info.Location.Longitude + // set timezone + rsp.Timezone = info.Location.TimeZone + + return nil +} diff --git a/ip/main.go b/ip/main.go new file mode 100644 index 0000000..55682b8 --- /dev/null +++ b/ip/main.go @@ -0,0 +1,97 @@ +package main + +import ( + "io" + "os" + "path" + "strings" + + "github.com/micro/services/ip/handler" + pb "github.com/micro/services/ip/proto" + + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/config" + "github.com/micro/micro/v3/service/logger" + "github.com/micro/micro/v3/service/store" + "github.com/oschwald/geoip2-golang" +) + +// loadFile from the blob store +func loadFile(p string) (string, error) { + name := path.Base(p) + + f, err := os.Create("./" + name) + if err != nil { + return "", err + } + defer f.Close() + + reader, err := store.DefaultBlobStore.Read(p) + if err != nil { + return "", err + } + + _, err = io.Copy(f, reader) + return "./" + name, err +} + +func main() { + // Create service + srv := service.New( + service.Name("ip"), + service.Version("latest"), + ) + + // get the ip city database + v, err := config.Get("ip.city.database") + if err != nil { + logger.Fatalf("failed to get config: %v", err) + } + path := v.String("./GeoLite2-City.mmdb") + + // load from blob store if specified + if strings.HasPrefix(path, "blob://") { + f, err := loadFile(strings.TrimPrefix(path, "blob://")) + if err != nil { + logger.Fatal("failed to load db: %v", err) + } + + path = f + } + + // load the ip city database + cr, err := geoip2.Open(path) + if err != nil { + logger.Fatalf("failed to open ip db: %v", err) + } + + // get the asn database + v, err = config.Get("ip.asn.database") + if err != nil { + logger.Fatalf("failed to get config: %v", err) + } + path = v.String("./GeoLite2-ASN.mmdb") + + // load from blob store if specified + if strings.HasPrefix(path, "blob://") { + f, err := loadFile(strings.TrimPrefix(path, "blob://")) + if err != nil { + logger.Fatal("failed to load db: %v", err) + } + + path = f + } + + ar, err := geoip2.Open(path) + if err != nil { + logger.Fatalf("failed to open ip db: %v", err) + } + + // Register handler + pb.RegisterIpHandler(srv.Server(), &handler.Ip{CityReader: cr, ASNReader: ar}) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/ip/micro.mu b/ip/micro.mu new file mode 100644 index 0000000..369d7ff --- /dev/null +++ b/ip/micro.mu @@ -0,0 +1 @@ +service ip diff --git a/ip/proto/ip.pb.go b/ip/proto/ip.pb.go new file mode 100644 index 0000000..b4a4411 --- /dev/null +++ b/ip/proto/ip.pb.go @@ -0,0 +1,267 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.15.6 +// source: proto/ip.proto + +package ip + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// Lookup the geolocation information for an IP address +type LookupRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"` +} + +func (x *LookupRequest) Reset() { + *x = LookupRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_ip_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *LookupRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LookupRequest) ProtoMessage() {} + +func (x *LookupRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_ip_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LookupRequest.ProtoReflect.Descriptor instead. +func (*LookupRequest) Descriptor() ([]byte, []int) { + return file_proto_ip_proto_rawDescGZIP(), []int{0} +} + +func (x *LookupRequest) GetIp() string { + if x != nil { + return x.Ip + } + return "" +} + +type LookupResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ip string `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"` + Asn int64 `protobuf:"varint,2,opt,name=asn,proto3" json:"asn,omitempty"` + City string `protobuf:"bytes,3,opt,name=city,proto3" json:"city,omitempty"` + Country string `protobuf:"bytes,4,opt,name=country,proto3" json:"country,omitempty"` + Latitude float64 `protobuf:"fixed64,5,opt,name=latitude,proto3" json:"latitude,omitempty"` + Longitude float64 `protobuf:"fixed64,6,opt,name=longitude,proto3" json:"longitude,omitempty"` + Timezone string `protobuf:"bytes,7,opt,name=timezone,proto3" json:"timezone,omitempty"` +} + +func (x *LookupResponse) Reset() { + *x = LookupResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_ip_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *LookupResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LookupResponse) ProtoMessage() {} + +func (x *LookupResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_ip_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use LookupResponse.ProtoReflect.Descriptor instead. +func (*LookupResponse) Descriptor() ([]byte, []int) { + return file_proto_ip_proto_rawDescGZIP(), []int{1} +} + +func (x *LookupResponse) GetIp() string { + if x != nil { + return x.Ip + } + return "" +} + +func (x *LookupResponse) GetAsn() int64 { + if x != nil { + return x.Asn + } + return 0 +} + +func (x *LookupResponse) GetCity() string { + if x != nil { + return x.City + } + return "" +} + +func (x *LookupResponse) GetCountry() string { + if x != nil { + return x.Country + } + return "" +} + +func (x *LookupResponse) GetLatitude() float64 { + if x != nil { + return x.Latitude + } + return 0 +} + +func (x *LookupResponse) GetLongitude() float64 { + if x != nil { + return x.Longitude + } + return 0 +} + +func (x *LookupResponse) GetTimezone() string { + if x != nil { + return x.Timezone + } + return "" +} + +var File_proto_ip_proto protoreflect.FileDescriptor + +var file_proto_ip_proto_rawDesc = []byte{ + 0x0a, 0x0e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x69, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x12, 0x02, 0x69, 0x70, 0x22, 0x1f, 0x0a, 0x0d, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x02, 0x69, 0x70, 0x22, 0xb6, 0x01, 0x0a, 0x0e, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x73, 0x6e, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x61, 0x73, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x69, + 0x74, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x69, 0x74, 0x79, 0x12, 0x18, + 0x0a, 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x6c, 0x61, 0x74, 0x69, + 0x74, 0x75, 0x64, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x6c, 0x61, 0x74, 0x69, + 0x74, 0x75, 0x64, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, + 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x01, 0x52, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, + 0x64, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x74, 0x69, 0x6d, 0x65, 0x7a, 0x6f, 0x6e, 0x65, 0x18, 0x07, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x74, 0x69, 0x6d, 0x65, 0x7a, 0x6f, 0x6e, 0x65, 0x32, 0x37, + 0x0a, 0x02, 0x49, 0x70, 0x12, 0x31, 0x0a, 0x06, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x12, 0x11, + 0x2e, 0x69, 0x70, 0x2e, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x12, 0x2e, 0x69, 0x70, 0x2e, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0c, 0x5a, 0x0a, 0x2e, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x3b, 0x69, 0x70, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_ip_proto_rawDescOnce sync.Once + file_proto_ip_proto_rawDescData = file_proto_ip_proto_rawDesc +) + +func file_proto_ip_proto_rawDescGZIP() []byte { + file_proto_ip_proto_rawDescOnce.Do(func() { + file_proto_ip_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_ip_proto_rawDescData) + }) + return file_proto_ip_proto_rawDescData +} + +var file_proto_ip_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_proto_ip_proto_goTypes = []interface{}{ + (*LookupRequest)(nil), // 0: ip.LookupRequest + (*LookupResponse)(nil), // 1: ip.LookupResponse +} +var file_proto_ip_proto_depIdxs = []int32{ + 0, // 0: ip.Ip.Lookup:input_type -> ip.LookupRequest + 1, // 1: ip.Ip.Lookup:output_type -> ip.LookupResponse + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_proto_ip_proto_init() } +func file_proto_ip_proto_init() { + if File_proto_ip_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_ip_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*LookupRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_ip_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*LookupResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_ip_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_ip_proto_goTypes, + DependencyIndexes: file_proto_ip_proto_depIdxs, + MessageInfos: file_proto_ip_proto_msgTypes, + }.Build() + File_proto_ip_proto = out.File + file_proto_ip_proto_rawDesc = nil + file_proto_ip_proto_goTypes = nil + file_proto_ip_proto_depIdxs = nil +} diff --git a/ip/proto/ip.pb.micro.go b/ip/proto/ip.pb.micro.go new file mode 100644 index 0000000..b062871 --- /dev/null +++ b/ip/proto/ip.pb.micro.go @@ -0,0 +1,93 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/ip.proto + +package ip + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +import ( + context "context" + api "github.com/micro/micro/v3/service/api" + client "github.com/micro/micro/v3/service/client" + server "github.com/micro/micro/v3/service/server" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// Reference imports to suppress errors if they are not otherwise used. +var _ api.Endpoint +var _ context.Context +var _ client.Option +var _ server.Option + +// Api Endpoints for Ip service + +func NewIpEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Ip service + +type IpService interface { + Lookup(ctx context.Context, in *LookupRequest, opts ...client.CallOption) (*LookupResponse, error) +} + +type ipService struct { + c client.Client + name string +} + +func NewIpService(name string, c client.Client) IpService { + return &ipService{ + c: c, + name: name, + } +} + +func (c *ipService) Lookup(ctx context.Context, in *LookupRequest, opts ...client.CallOption) (*LookupResponse, error) { + req := c.c.NewRequest(c.name, "Ip.Lookup", in) + out := new(LookupResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Ip service + +type IpHandler interface { + Lookup(context.Context, *LookupRequest, *LookupResponse) error +} + +func RegisterIpHandler(s server.Server, hdlr IpHandler, opts ...server.HandlerOption) error { + type ip interface { + Lookup(ctx context.Context, in *LookupRequest, out *LookupResponse) error + } + type Ip struct { + ip + } + h := &ipHandler{hdlr} + return s.Handle(s.NewHandler(&Ip{h}, opts...)) +} + +type ipHandler struct { + IpHandler +} + +func (h *ipHandler) Lookup(ctx context.Context, in *LookupRequest, out *LookupResponse) error { + return h.IpHandler.Lookup(ctx, in, out) +} diff --git a/ip/proto/ip.proto b/ip/proto/ip.proto new file mode 100644 index 0000000..6b700cc --- /dev/null +++ b/ip/proto/ip.proto @@ -0,0 +1,25 @@ +syntax = "proto3"; + +package ip; + +option go_package = "./proto;ip"; + +service Ip { + rpc Lookup(LookupRequest) returns (LookupResponse) {} +} + +// Lookup the geolocation information for an IP address +message LookupRequest { + string ip = 1; +} + +message LookupResponse { + string ip = 1; + int64 asn = 2; + string city = 3; + string country = 4; + double latitude = 5; + double longitude = 6; + string timezone = 7; +} + diff --git a/ip/publicapi.json b/ip/publicapi.json new file mode 100644 index 0000000..9d94ddc --- /dev/null +++ b/ip/publicapi.json @@ -0,0 +1,8 @@ +{ + "name": "ip", + "icon": "🗺️", + "category": "location", + "pricing": { + "Ip.Location": 100 + } +}