git » chasquid » commit ddd1b6d

chasquid: Run a localrpc server

author Alberto Bertogli
2023-07-29 21:46:15 UTC
committer Alberto Bertogli
2023-07-30 12:21:07 UTC
parent 360ac13a731e66c45f1c446635063a03a3191512

chasquid: Run a localrpc server

This patch makes chasquid run a localrpc server, exporting two methods:
alias resolve, and domaininfo clear.

They will be used by chasquid-util in later patches.

chasquid.go +3 -0
internal/smtpsrv/server.go +30 -0

diff --git a/chasquid.go b/chasquid.go
index 33f8df6..35aa99b 100644
--- a/chasquid.go
+++ b/chasquid.go
@@ -20,6 +20,7 @@ import (
 	"blitiri.com.ar/go/chasquid/internal/courier"
 	"blitiri.com.ar/go/chasquid/internal/domaininfo"
 	"blitiri.com.ar/go/chasquid/internal/dovecot"
+	"blitiri.com.ar/go/chasquid/internal/localrpc"
 	"blitiri.com.ar/go/chasquid/internal/maillog"
 	"blitiri.com.ar/go/chasquid/internal/normalize"
 	"blitiri.com.ar/go/chasquid/internal/smtpsrv"
@@ -172,6 +173,8 @@ func main() {
 		log.Fatalf("No address to listen on")
 	}
 
+	go localrpc.DefaultServer.ListenAndServe(conf.DataDir + "/localrpc-v1")
+
 	go signalHandler(dinfo, s)
 
 	s.ListenAndServe()
diff --git a/internal/smtpsrv/server.go b/internal/smtpsrv/server.go
index 54c1c32..d8d6a6f 100644
--- a/internal/smtpsrv/server.go
+++ b/internal/smtpsrv/server.go
@@ -4,8 +4,10 @@ package smtpsrv
 import (
 	"crypto/tls"
 	"flag"
+	"fmt"
 	"net"
 	"net/http"
+	"net/url"
 	"path"
 	"time"
 
@@ -13,9 +15,11 @@ import (
 	"blitiri.com.ar/go/chasquid/internal/auth"
 	"blitiri.com.ar/go/chasquid/internal/courier"
 	"blitiri.com.ar/go/chasquid/internal/domaininfo"
+	"blitiri.com.ar/go/chasquid/internal/localrpc"
 	"blitiri.com.ar/go/chasquid/internal/maillog"
 	"blitiri.com.ar/go/chasquid/internal/queue"
 	"blitiri.com.ar/go/chasquid/internal/set"
+	"blitiri.com.ar/go/chasquid/internal/trace"
 	"blitiri.com.ar/go/chasquid/internal/userdb"
 	"blitiri.com.ar/go/log"
 )
@@ -162,6 +166,29 @@ func (s *Server) InitQueue(path string, localC, remoteC courier.Courier) {
 		})
 }
 
+func (s *Server) aliasResolveRPC(tr *trace.Trace, req url.Values) (url.Values, error) {
+	rcpts, err := s.aliasesR.Resolve(tr, req.Get("Address"))
+	if err != nil {
+		return nil, err
+	}
+
+	v := url.Values{}
+	for _, rcpt := range rcpts {
+		v.Add(string(rcpt.Type), rcpt.Addr)
+	}
+
+	return v, nil
+}
+
+func (s *Server) dinfoClearRPC(tr *trace.Trace, req url.Values) (url.Values, error) {
+	domain := req.Get("Domain")
+	exists := s.dinfo.Clear(tr, domain)
+	if !exists {
+		return nil, fmt.Errorf("does not exist")
+	}
+	return nil, nil
+}
+
 // periodicallyReload some of the server's information that can be changed
 // without the server knowing, such as aliases and the user databases.
 func (s *Server) periodicallyReload() {
@@ -200,6 +227,9 @@ func (s *Server) ListenAndServe() {
 		log.Fatalf("At least one valid certificate is needed")
 	}
 
+	localrpc.DefaultServer.Register("AliasResolve", s.aliasResolveRPC)
+	localrpc.DefaultServer.Register("DomaininfoClear", s.dinfoClearRPC)
+
 	go s.periodicallyReload()
 
 	for m, addrs := range s.addrs {