diff --git a/gifs/main.go b/gifs/main.go index 7161391..f07f7c4 100644 --- a/gifs/main.go +++ b/gifs/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/micro/services/gifs/handler" pb "github.com/micro/services/gifs/proto" + "github.com/micro/services/pkg/tracing" "github.com/micro/micro/v3/service" "github.com/micro/micro/v3/service/logger" @@ -17,6 +18,8 @@ func main() { // Register handler pb.RegisterGifsHandler(srv.Server(), handler.New()) + traceCloser := tracing.SetupOpentracing("gifs") + defer traceCloser.Close() // Run service if err := srv.Run(); err != nil { diff --git a/go.mod b/go.mod index a4fb073..6f05ba5 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/Masterminds/semver/v3 v3.1.1 github.com/PuerkitoBio/goquery v1.6.1 github.com/SlyMarbo/rss v1.0.1 + github.com/Teamwork/spamc v0.0.0-20200109085853-a4e0c5c3f7a0 github.com/asim/mq v0.1.0 github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 // indirect github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10 @@ -33,7 +34,7 @@ require ( github.com/mattheath/base62 v0.0.0-20150408093626-b80cdc656a7a // indirect github.com/mattheath/kala v0.0.0-20171219141654-d6276794bf0e github.com/micro/micro-go v0.0.0-20211101221015-79ab982f8163 - github.com/micro/micro/v3 v3.6.1-0.20211110104311-614fde05be0c + github.com/micro/micro/v3 v3.7.1-0.20211111170433-1ebb8328e280 github.com/miekg/dns v1.1.31 // indirect github.com/oschwald/geoip2-golang v1.5.0 github.com/patrickmn/go-cache v2.1.0+incompatible @@ -45,6 +46,8 @@ require ( github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/stoewer/go-strcase v1.2.0 github.com/stretchr/testify v1.7.0 + github.com/teamwork/test v0.0.0-20200108114543-02621bae84ad // indirect + github.com/teamwork/utils v0.0.0-20211103135549-f7e7a68ba696 // indirect github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf github.com/tkuchiki/go-timezone v0.2.2 github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect diff --git a/go.sum b/go.sum index 14917eb..6cc8bbb 100644 --- a/go.sum +++ b/go.sum @@ -72,6 +72,10 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/SlyMarbo/rss v1.0.1 h1:fiaIU5UhcXauVOniHOIocWG7uj8Ej6pHNarMGPJilzA= github.com/SlyMarbo/rss v1.0.1/go.mod h1:JNF+T33oj4m5WLCQXpBTCgO+SxRbYVgdiiimHNgzcbA= +github.com/Strum355/go-difflib v1.1.0 h1:+rR2X3UuvIbe1Jmhx8WA7gkgjMNRscFWbHchk2RB8I4= +github.com/Strum355/go-difflib v1.1.0/go.mod h1:r1cVg1JkGsTWkaR7At56v7hfuMgiUL8meTLwxFzOmvE= +github.com/Teamwork/spamc v0.0.0-20200109085853-a4e0c5c3f7a0 h1:OH7eTFVtLj/DJYM2EgWZE+GIq1T5xg/m+JiKH9tV9pc= +github.com/Teamwork/spamc v0.0.0-20200109085853-a4e0c5c3f7a0/go.mod h1:q7zbrHu00GwyX+EVlv0bAfmzlbhI0tRpznyCZ96VyN4= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= github.com/akamai/AkamaiOPEN-edgegrid-golang v0.9.0/go.mod h1:zpDJeKyp9ScW4NNrbdr+Eyxvry3ilGPewKoXw3XGN1k= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -499,6 +503,14 @@ github.com/micro/micro-go v0.0.0-20211101221015-79ab982f8163 h1:kNngAyoUre7ahqYW github.com/micro/micro-go v0.0.0-20211101221015-79ab982f8163/go.mod h1:o4fTExNn5LlnQRB/WiW3RChsohPwQTJ1AKdNCz2YEYA= github.com/micro/micro/v3 v3.6.1-0.20211110104311-614fde05be0c h1:9+w31dXDHVUD11x1St5LiXiBQLNSUxgeH9GI9+sKv0M= github.com/micro/micro/v3 v3.6.1-0.20211110104311-614fde05be0c/go.mod h1:NqYnFOGrnc0Apk912w49oX9qIk1YDJcCaO+y+CLaAXA= +github.com/micro/micro/v3 v3.7.1-0.20211111155628-5c83aaf82619 h1:SRfAA7YcpHh1P/F1HY8p7QprED5o6WOlovGZoZcuHxU= +github.com/micro/micro/v3 v3.7.1-0.20211111155628-5c83aaf82619/go.mod h1:NqYnFOGrnc0Apk912w49oX9qIk1YDJcCaO+y+CLaAXA= +github.com/micro/micro/v3 v3.7.1-0.20211111162246-1f3d81ada4a9 h1:1jn/GQhmv87dxGaoOOWou+LfJWc+McSFlg+8GQ78Xf0= +github.com/micro/micro/v3 v3.7.1-0.20211111162246-1f3d81ada4a9/go.mod h1:NqYnFOGrnc0Apk912w49oX9qIk1YDJcCaO+y+CLaAXA= +github.com/micro/micro/v3 v3.7.1-0.20211111164609-a157fe0c3185 h1:7CjYMqI5ACQAL276dJGoleoHeZRIkLmMM0a9rSio8Kk= +github.com/micro/micro/v3 v3.7.1-0.20211111164609-a157fe0c3185/go.mod h1:NqYnFOGrnc0Apk912w49oX9qIk1YDJcCaO+y+CLaAXA= +github.com/micro/micro/v3 v3.7.1-0.20211111170433-1ebb8328e280 h1:SOthdbPABgdxgsi9d7lXBDE8WzlTvj+2owHR51vW2AA= +github.com/micro/micro/v3 v3.7.1-0.20211111170433-1ebb8328e280/go.mod h1:NqYnFOGrnc0Apk912w49oX9qIk1YDJcCaO+y+CLaAXA= github.com/miekg/dns v1.1.15/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo= @@ -637,6 +649,11 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/teamwork/test v0.0.0-20190410143529-8897d82f8d46/go.mod h1:TIbx7tx6WHBjQeLRM4eWQZBL7kmBZ7/KI4x4v7Y5YmA= +github.com/teamwork/test v0.0.0-20200108114543-02621bae84ad h1:25sEr0awm0ZPancg5W5H5VvN7PWsJloUBpii10a9isw= +github.com/teamwork/test v0.0.0-20200108114543-02621bae84ad/go.mod h1:TIbx7tx6WHBjQeLRM4eWQZBL7kmBZ7/KI4x4v7Y5YmA= +github.com/teamwork/utils v0.0.0-20211103135549-f7e7a68ba696 h1:G6Hc6/KxUmHxZsCgkXOyxp6UzBrkSA/zQjZWNBWzL3A= +github.com/teamwork/utils v0.0.0-20211103135549-f7e7a68ba696/go.mod h1:3Fn0qxFeRNpvsg/9T1+btOOOKkd1qG2nPYKKcOmNpcs= github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf h1:Z2X3Os7oRzpdJ75iPqWZc0HeJWFYNCvKsfpQwFpRNTA= github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf/go.mod h1:M8agBzgqHIhgj7wEn9/0hJUZcrvt9VY+Ln+S1I5Mha0= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= diff --git a/qr/main.go b/qr/main.go index f88f1ed..9ea87ca 100644 --- a/qr/main.go +++ b/qr/main.go @@ -1,6 +1,7 @@ package main import ( + "github.com/micro/services/pkg/tracing" "github.com/micro/services/qr/handler" pb "github.com/micro/services/qr/proto" @@ -17,6 +18,8 @@ func main() { // Register handler pb.RegisterQrHandler(srv.Server(), handler.New()) + traceCloser := tracing.SetupOpentracing("qr") + defer traceCloser.Close() // Run service if err := srv.Run(); err != nil { diff --git a/spam/.gitignore b/spam/.gitignore new file mode 100644 index 0000000..7930b8d --- /dev/null +++ b/spam/.gitignore @@ -0,0 +1,2 @@ + +spam diff --git a/spam/Dockerfile b/spam/Dockerfile new file mode 100644 index 0000000..22cb851 --- /dev/null +++ b/spam/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD spam /spam +ENTRYPOINT [ "/spam" ] diff --git a/spam/Makefile b/spam/Makefile new file mode 100644 index 0000000..0d2aafa --- /dev/null +++ b/spam/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/spam.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/spam.proto + +.PHONY: build +build: + go build -o spam *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t spam:latest diff --git a/spam/README.md b/spam/README.md new file mode 100644 index 0000000..4af0f9c --- /dev/null +++ b/spam/README.md @@ -0,0 +1,7 @@ +Check if an email is spam + +# Spam Service + +An API to classify emails against spam detection rules. + +Powered by [SpamAssassin](http://spamassassin.apache.org/). diff --git a/spam/examples.json b/spam/examples.json new file mode 100644 index 0000000..30bd5d4 --- /dev/null +++ b/spam/examples.json @@ -0,0 +1,23 @@ +{ + "classify": [ + { + "title": "Classify an email", + "run_check": true, + "request": { + "subject": "Welcome", + "email_body": "Hi there,\n\nWelcome to M3O.\n\nThanks\nM3O team", + "from": "noreply@m3o.com", + "to": "hello@example.com" + }, + "response": { + "is_spam": false, + "score": 0.1, + "details": [ + "NO_RELAYS, Informational: message was not relayed via SMTP, -0", + "NO_RECEIVED, Informational: message has no Received headers, -0", + "MISSING_MID, Missing Message-Id: header, 0.1" + ] + } + } + ] +} diff --git a/spam/generate.go b/spam/generate.go new file mode 100644 index 0000000..96f431a --- /dev/null +++ b/spam/generate.go @@ -0,0 +1,2 @@ +package main +//go:generate make proto diff --git a/spam/handler/spam.go b/spam/handler/spam.go new file mode 100644 index 0000000..bfb42fa --- /dev/null +++ b/spam/handler/spam.go @@ -0,0 +1,75 @@ +package handler + +import ( + "bufio" + "bytes" + "context" + "fmt" + "net/textproto" + "time" + + "github.com/Teamwork/spamc" + "github.com/micro/micro/v3/service/config" + "github.com/micro/micro/v3/service/errors" + log "github.com/micro/micro/v3/service/logger" + spam "github.com/micro/services/spam/proto" +) + +type conf struct { + SpamdAddress string `json:"spamd_address"` +} + +type Spam struct { + c conf + client *spamc.Client +} + +func New() *Spam { + val, err := config.Get("micro.spam") + if err != nil { + log.Fatalf("Failed to load config") + } + c := conf{} + if err := val.Scan(&c); err != nil { + log.Fatalf("Failed to load config") + } + return &Spam{ + c: c, + client: spamc.New(c.SpamdAddress, nil), + } +} + +func (s *Spam) Classify(ctx context.Context, request *spam.ClassifyRequest, response *spam.ClassifyResponse) error { + if len(request.EmailBody) == 0 { + return errors.BadRequest("spam.Classify", "missing email_body") + } + + bf := bytes.NewBufferString("") + tp := textproto.NewWriter(bufio.NewWriter(bf)) + + if len(request.To) > 0 { + tp.PrintfLine("To: %v", request.To) + } + if len(request.From) > 0 { + tp.PrintfLine("From: %v", request.From) + } + if len(request.Subject) > 0 { + tp.PrintfLine("Subject: %v", request.Subject) + } + tp.PrintfLine("Date: %s", time.Now().Format(time.RFC1123Z)) + tp.PrintfLine("") + tp.PrintfLine("%v", request.EmailBody) + rc, err := s.client.Report(ctx, bf, spamc.Header{}.Set("Content-Length", fmt.Sprintf("%d", bf.Len()))) + if err != nil { + log.Errorf("Error checking spamd %s", err) + return errors.InternalServerError("spam.Classify", "Error classifying email") + } + response.IsSpam = rc.IsSpam + response.Score = rc.Score + + response.Details = []string{} + for _, v := range rc.Report.Table { + response.Details = append(response.Details, fmt.Sprintf("%s, %s, %v", v.Rule, v.Description, v.Points)) + } + return nil +} diff --git a/spam/main.go b/spam/main.go new file mode 100644 index 0000000..460e0a6 --- /dev/null +++ b/spam/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "github.com/micro/services/pkg/tracing" + "github.com/micro/services/spam/handler" + pb "github.com/micro/services/spam/proto" + + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/logger" +) + +func main() { + // Create service + srv := service.New( + service.Name("spam"), + service.Version("latest"), + ) + + // Register handler + pb.RegisterSpamHandler(srv.Server(), handler.New()) + + traceCloser := tracing.SetupOpentracing("spam") + defer traceCloser.Close() + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/spam/micro.mu b/spam/micro.mu new file mode 100644 index 0000000..2573a70 --- /dev/null +++ b/spam/micro.mu @@ -0,0 +1 @@ +service spam diff --git a/spam/proto/spam.pb.go b/spam/proto/spam.pb.go new file mode 100644 index 0000000..a33cfbd --- /dev/null +++ b/spam/proto/spam.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.5 +// source: proto/spam.proto + +package spam + +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) +) + +// Check whether an email is likely to be spam based on its attributes +type ClassifyRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // The body of the email + EmailBody string `protobuf:"bytes,1,opt,name=email_body,json=emailBody,proto3" json:"email_body,omitempty"` + // The email address it is being sent to + To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"` + // The email address it has been sent from + From string `protobuf:"bytes,3,opt,name=from,proto3" json:"from,omitempty"` + // The subject of the email + Subject string `protobuf:"bytes,4,opt,name=subject,proto3" json:"subject,omitempty"` +} + +func (x *ClassifyRequest) Reset() { + *x = ClassifyRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_spam_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ClassifyRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ClassifyRequest) ProtoMessage() {} + +func (x *ClassifyRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_spam_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 ClassifyRequest.ProtoReflect.Descriptor instead. +func (*ClassifyRequest) Descriptor() ([]byte, []int) { + return file_proto_spam_proto_rawDescGZIP(), []int{0} +} + +func (x *ClassifyRequest) GetEmailBody() string { + if x != nil { + return x.EmailBody + } + return "" +} + +func (x *ClassifyRequest) GetTo() string { + if x != nil { + return x.To + } + return "" +} + +func (x *ClassifyRequest) GetFrom() string { + if x != nil { + return x.From + } + return "" +} + +func (x *ClassifyRequest) GetSubject() string { + if x != nil { + return x.Subject + } + return "" +} + +type ClassifyResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // Is it spam? Returns true if its score is > 5 + IsSpam bool `protobuf:"varint,1,opt,name=is_spam,json=isSpam,proto3" json:"is_spam,omitempty"` + // The score evaluated for this email. A higher number means it is more likely to be spam + Score float64 `protobuf:"fixed64,2,opt,name=score,proto3" json:"score,omitempty"` + // The rules that have contributed to this score + Details []string `protobuf:"bytes,3,rep,name=details,proto3" json:"details,omitempty"` +} + +func (x *ClassifyResponse) Reset() { + *x = ClassifyResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_spam_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ClassifyResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ClassifyResponse) ProtoMessage() {} + +func (x *ClassifyResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_spam_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 ClassifyResponse.ProtoReflect.Descriptor instead. +func (*ClassifyResponse) Descriptor() ([]byte, []int) { + return file_proto_spam_proto_rawDescGZIP(), []int{1} +} + +func (x *ClassifyResponse) GetIsSpam() bool { + if x != nil { + return x.IsSpam + } + return false +} + +func (x *ClassifyResponse) GetScore() float64 { + if x != nil { + return x.Score + } + return 0 +} + +func (x *ClassifyResponse) GetDetails() []string { + if x != nil { + return x.Details + } + return nil +} + +var File_proto_spam_proto protoreflect.FileDescriptor + +var file_proto_spam_proto_rawDesc = []byte{ + 0x0a, 0x10, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x70, 0x61, 0x6d, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x12, 0x04, 0x73, 0x70, 0x61, 0x6d, 0x22, 0x6e, 0x0a, 0x0f, 0x43, 0x6c, 0x61, 0x73, + 0x73, 0x69, 0x66, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x65, + 0x6d, 0x61, 0x69, 0x6c, 0x5f, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x09, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x42, 0x6f, 0x64, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x74, 0x6f, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x72, + 0x6f, 0x6d, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x12, 0x18, + 0x0a, 0x07, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x07, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x22, 0x5b, 0x0a, 0x10, 0x43, 0x6c, 0x61, 0x73, + 0x73, 0x69, 0x66, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x17, 0x0a, 0x07, + 0x69, 0x73, 0x5f, 0x73, 0x70, 0x61, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x69, + 0x73, 0x53, 0x70, 0x61, 0x6d, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x73, 0x63, 0x6f, 0x72, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x64, + 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x64, 0x65, + 0x74, 0x61, 0x69, 0x6c, 0x73, 0x32, 0x43, 0x0a, 0x04, 0x53, 0x70, 0x61, 0x6d, 0x12, 0x3b, 0x0a, + 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x66, 0x79, 0x12, 0x15, 0x2e, 0x73, 0x70, 0x61, 0x6d, + 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x66, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x16, 0x2e, 0x73, 0x70, 0x61, 0x6d, 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x69, 0x66, 0x79, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0e, 0x5a, 0x0c, 0x2e, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x73, 0x70, 0x61, 0x6d, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_proto_spam_proto_rawDescOnce sync.Once + file_proto_spam_proto_rawDescData = file_proto_spam_proto_rawDesc +) + +func file_proto_spam_proto_rawDescGZIP() []byte { + file_proto_spam_proto_rawDescOnce.Do(func() { + file_proto_spam_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_spam_proto_rawDescData) + }) + return file_proto_spam_proto_rawDescData +} + +var file_proto_spam_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_proto_spam_proto_goTypes = []interface{}{ + (*ClassifyRequest)(nil), // 0: spam.ClassifyRequest + (*ClassifyResponse)(nil), // 1: spam.ClassifyResponse +} +var file_proto_spam_proto_depIdxs = []int32{ + 0, // 0: spam.Spam.Classify:input_type -> spam.ClassifyRequest + 1, // 1: spam.Spam.Classify:output_type -> spam.ClassifyResponse + 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_spam_proto_init() } +func file_proto_spam_proto_init() { + if File_proto_spam_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_spam_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ClassifyRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_spam_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ClassifyResponse); 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_spam_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_spam_proto_goTypes, + DependencyIndexes: file_proto_spam_proto_depIdxs, + MessageInfos: file_proto_spam_proto_msgTypes, + }.Build() + File_proto_spam_proto = out.File + file_proto_spam_proto_rawDesc = nil + file_proto_spam_proto_goTypes = nil + file_proto_spam_proto_depIdxs = nil +} diff --git a/spam/proto/spam.pb.micro.go b/spam/proto/spam.pb.micro.go new file mode 100644 index 0000000..17a8ae5 --- /dev/null +++ b/spam/proto/spam.pb.micro.go @@ -0,0 +1,93 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/spam.proto + +package spam + +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 Spam service + +func NewSpamEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Spam service + +type SpamService interface { + Classify(ctx context.Context, in *ClassifyRequest, opts ...client.CallOption) (*ClassifyResponse, error) +} + +type spamService struct { + c client.Client + name string +} + +func NewSpamService(name string, c client.Client) SpamService { + return &spamService{ + c: c, + name: name, + } +} + +func (c *spamService) Classify(ctx context.Context, in *ClassifyRequest, opts ...client.CallOption) (*ClassifyResponse, error) { + req := c.c.NewRequest(c.name, "Spam.Classify", in) + out := new(ClassifyResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Spam service + +type SpamHandler interface { + Classify(context.Context, *ClassifyRequest, *ClassifyResponse) error +} + +func RegisterSpamHandler(s server.Server, hdlr SpamHandler, opts ...server.HandlerOption) error { + type spam interface { + Classify(ctx context.Context, in *ClassifyRequest, out *ClassifyResponse) error + } + type Spam struct { + spam + } + h := &spamHandler{hdlr} + return s.Handle(s.NewHandler(&Spam{h}, opts...)) +} + +type spamHandler struct { + SpamHandler +} + +func (h *spamHandler) Classify(ctx context.Context, in *ClassifyRequest, out *ClassifyResponse) error { + return h.SpamHandler.Classify(ctx, in, out) +} diff --git a/spam/proto/spam.proto b/spam/proto/spam.proto new file mode 100644 index 0000000..f1ba376 --- /dev/null +++ b/spam/proto/spam.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; + +package spam; + +option go_package = "./proto;spam"; + +service Spam { + rpc Classify(ClassifyRequest) returns (ClassifyResponse) {} +} + +// Check whether an email is likely to be spam based on its attributes +message ClassifyRequest { + // The body of the email + string email_body = 1; + // The email address it is being sent to + string to = 2; + // The email address it has been sent from + string from = 3; + // The subject of the email + string subject = 4; +} + +message ClassifyResponse { + // Is it spam? Returns true if its score is > 5 + bool is_spam = 1; + // The score evaluated for this email. A higher number means it is more likely to be spam + double score = 2; + // The rules that have contributed to this score + repeated string details = 3; +} diff --git a/spam/publicapi.json b/spam/publicapi.json new file mode 100644 index 0000000..cd7697c --- /dev/null +++ b/spam/publicapi.json @@ -0,0 +1,6 @@ +{ + "name": "spam", + "icon": "🚫", + "category": "communication", + "display_name": "Spam" +} diff --git a/spam/setup.md b/spam/setup.md new file mode 100644 index 0000000..94caf8b --- /dev/null +++ b/spam/setup.md @@ -0,0 +1,8 @@ +The spam API is powered by http://spamassassin.apache.org/. Specifically we run spamd which we then communicate with to classify emails. + +We run this as a separate container in the same k8s namespace. Simply run +```shell +kubectl apply -f spamassassin-k8s.yaml +``` + +Then deploy the spam service as normal. diff --git a/spam/spamassassin-k8s.yaml b/spam/spamassassin-k8s.yaml new file mode 100644 index 0000000..feaf5a8 --- /dev/null +++ b/spam/spamassassin-k8s.yaml @@ -0,0 +1,35 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: spamd-latest + labels: + app: spamd +spec: + replicas: 1 + selector: + matchLabels: + app: spamd + template: + metadata: + labels: + app: spamd + spec: + containers: + - name: spamd + image: instantlinux/spamassassin:latest + ports: + - containerPort: 783 + +--- + +apiVersion: v1 +kind: Service +metadata: + name: spamd +spec: + selector: + app: spamd + ports: + - protocol: TCP + port: 783 + targetPort: 783