From 49e2b06b00e1f8b61d03f0baeb68f86dbcb4502b Mon Sep 17 00:00:00 2001 From: Geno Date: Sun, 5 Sep 2021 19:20:27 +0200 Subject: [PATCH] init distributor dbus --- distributor/const.go | 11 ++++++++ distributor/dbus.go | 65 ++++++++++++++++++++++++++++++++++++++++++++ distributor/go.mod | 9 ++++++ distributor/go.sum | 13 +++++++++ distributor/main.go | 36 ++++++++++++++++++++++-- 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 distributor/const.go create mode 100644 distributor/dbus.go create mode 100644 distributor/go.sum diff --git a/distributor/const.go b/distributor/const.go new file mode 100644 index 0000000..ca1033b --- /dev/null +++ b/distributor/const.go @@ -0,0 +1,11 @@ +package main + +const ( + DBUSName = "org.unifiedpush.Distributor.xmpp" + + DBUSDistributorPath = "/org/unifiedpush/Distributor" + DBUSDistributorInterface = "org.unifiedpush.Distributor1" + + DBUSConnectorPath = "/org/unifiedpush/Connector" + DBUSConnectorInterface = "org.unifiedpush.Connector1" +) diff --git a/distributor/dbus.go b/distributor/dbus.go new file mode 100644 index 0000000..14c54f6 --- /dev/null +++ b/distributor/dbus.go @@ -0,0 +1,65 @@ +package main + +import ( + "github.com/godbus/dbus/v5" + "github.com/bdlm/log" +) + + +type Distributor struct { + dbus *dbus.Conn +} + +func NewDistributor(dbus *dbus.Conn) *Distributor { + return &Distributor{ + dbus: dbus, + } +} + +func (d Distributor) Register(name, token string) (thing, reason string, err *dbus.Error) { + logger:= log.WithFields(map[string]interface{}{ + "name": name, + "token": token, + }) + + endpoint := "https://up.chat.sum7.eu/UP?appid="+name+"&token="+token + + c := NewConector(d.dbus, name) + if err := c.NewEndpoint(token, endpoint); err != nil { + logger.Errorf("distributor-register error on NewEndpoint: %v", err) + return "REGISTRATION_FAILED", err.Error(), nil + } + + logger.Info("distributor-register") + return "NEW_ENDPOINT", "", nil +} + +func (d Distributor) Unregister(token string) *dbus.Error { + log.WithFields(map[string]interface{}{ + "token": token, + }).Info("distributor-unregister") + return nil +} + +type Connector struct { + obj dbus.BusObject +} + +func NewConector(dbus *dbus.Conn,appid string) *Connector { + obj := dbus.Object(appid, DBUSConnectorPath) + return &Connector{ + obj: obj, + } +} + +func (c Connector) Message(token, contents, id string) error { + return c.obj.Call(DBUSConnectorInterface+".Message", dbus.FlagNoReplyExpected, token, contents, id).Err +} + +func (c Connector) NewEndpoint(token, endpoint string) error { + return c.obj.Call(DBUSConnectorInterface+".NewEndpoint", dbus.FlagNoReplyExpected, token, endpoint).Err +} + +func (c Connector) Unregistered(token string) error { + return c.obj.Call(DBUSConnectorInterface+".Unregistered", dbus.FlagNoReplyExpected, token).Err +} diff --git a/distributor/go.mod b/distributor/go.mod index 849fcbd..0a7e463 100644 --- a/distributor/go.mod +++ b/distributor/go.mod @@ -1,3 +1,12 @@ module dev.sum7.eu/genofire/unified-push-xmpp/distributor go 1.17 + +require ( + github.com/bdlm/log v0.1.20 // indirect + github.com/bdlm/std v1.0.1 // indirect + github.com/godbus/dbus/v5 v5.0.4 // indirect + golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect + golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect + golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect +) diff --git a/distributor/go.sum b/distributor/go.sum new file mode 100644 index 0000000..fc003d1 --- /dev/null +++ b/distributor/go.sum @@ -0,0 +1,13 @@ +github.com/bdlm/log v0.1.20 h1:fSxBuBSHz+DkxPSFlaVcPiep20mCYUJZ5azUynkjhfA= +github.com/bdlm/log v0.1.20/go.mod h1:30V5Zwc5Vt5ePq5rd9KJ6JQ/A5aFUcKzq5fYtO7c9qc= +github.com/bdlm/std v1.0.1 h1:USdxays+0tgB3BJCEQ9z942tmTWmzpVPC7jCvczsj/I= +github.com/bdlm/std v1.0.1/go.mod h1:dittT3gnvbHQ4P+1UbkdSwkHFHVl1gx8qYu4zIFyB+Q= +github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg= +golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/distributor/main.go b/distributor/main.go index 4e9805e..d6e351b 100644 --- a/distributor/main.go +++ b/distributor/main.go @@ -1,9 +1,41 @@ package main import ( - "fmt" + "os" + "os/signal" + "syscall" + + "github.com/bdlm/log" + "github.com/godbus/dbus/v5" ) func main() { - fmt.Println("startup") + conn, err := dbus.ConnectSessionBus() + if err != nil { + log.Panicf("on dbus connection: %v", err) + } + defer conn.Close() + log.Debug("connect to dbus") + + rp, err := conn.RequestName(DBUSName, dbus.NameFlagReplaceExisting) + if err != nil { + log.Panicf("register name on dbus: %v", err) + } + if rp != dbus.RequestNameReplyPrimaryOwner { + log.Panicf("a other dbus service is running with %s: %v", DBUSName, rp) + } + log.Debug("register name to dbus") + + d := NewDistributor(conn) + if err := conn.ExportAll(d, DBUSDistributorPath, DBUSDistributorInterface); err != nil { + log.Panicf("export distributor on %s: %v", DBUSDistributorPath, err) + } + + log.Info("startup") + + // Wait for INT/TERM + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + sig := <-sigs + log.Infof("received %s", sig) }