From ea13390abee55b2afec5be8d77a33d4d56caed57 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Fri, 31 May 2019 10:07:40 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 + .gitlab-ci.yml | 36 +++++++++++++ README.md | 4 ++ cmd/root.go | 26 +++++++++ cmd/serve.go | 107 ++++++++++++++++++++++++++++++++++++++ component/all/main.go | 6 +++ component/main.go | 36 +++++++++++++ component/threema/main.go | 15 ++++++ component/xmpp.go | 38 ++++++++++++++ config_example.toml | 15 ++++++ main.go | 7 +++ models/account_threema.go | 19 +++++++ models/jid.go | 21 ++++++++ 13 files changed, 332 insertions(+) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 README.md create mode 100644 cmd/root.go create mode 100644 cmd/serve.go create mode 100644 component/all/main.go create mode 100644 component/main.go create mode 100644 component/threema/main.go create mode 100644 component/xmpp.go create mode 100644 config_example.toml create mode 100644 main.go create mode 100644 models/account_threema.go create mode 100644 models/jid.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a71c6c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +thrempp.db +config.toml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..ba4814f --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,36 @@ +image: golang:latest +stages: + - build + - test + +before_script: + - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh + - mkdir -p /go/src/dev.sum7.eu/genofire/ + - cp -R /builds/freifunkbremen/thrempp /go/src/dev.sum7.eu/genofire/thrempp + - cd /go/src/dev.sum7.eu/genofire/thrempp + - dep ensure + +build-my-project: + stage: build + script: + - go install -ldflags "-X dev.sum7.eu/genofire/thrempp/cmd.VERSION=`git -C $GOPATH/src/dev.sum7.eu/genofire/thrempp rev-parse HEAD`" dev.sum7.eu/genofire/thrempp + - mkdir /builds/freifunkbremen/thrempp/bin/ + - cp /go/bin/thrempp /builds/freifunkbremen/thrempp/bin/thrempp + artifacts: + paths: + - bin/thrempp + - config_example.toml + - config-respondd_example.toml + +test-my-project: + stage: test + script: + - ./.circleci/check-gofmt + - ./.circleci/check-testfiles + - go test $(go list ./... | grep -v /vendor/) -v -coverprofile .testCoverage.txt + - go tool cover -func=.testCoverage.txt + +test-race-my-project: + stage: test + script: + - go test -race ./... diff --git a/README.md b/README.md new file mode 100644 index 0000000..c286048 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Threempp +Threema XMPP - Transport + + diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..ec76178 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "os" + + "github.com/bdlm/log" + "github.com/spf13/cobra" +) + +var ( + timestamps bool +) + +// RootCmd represents the base command when called without any subcommands +var RootCmd = &cobra.Command{ + Use: "thrempp", +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + if err := RootCmd.Execute(); err != nil { + log.Fatal(err) + os.Exit(1) + } +} diff --git a/cmd/serve.go b/cmd/serve.go new file mode 100644 index 0000000..7903fac --- /dev/null +++ b/cmd/serve.go @@ -0,0 +1,107 @@ +package cmd + +import ( + "os" + "os/signal" + "syscall" + + "github.com/bdlm/log" + "github.com/bdlm/std/logger" + "github.com/spf13/cobra" + + "dev.sum7.eu/genofire/golang-lib/database" + "dev.sum7.eu/genofire/golang-lib/file" + + "dev.sum7.eu/genofire/thrempp/component" + // need for database init + _ "dev.sum7.eu/genofire/thrempp/component/all" + _ "dev.sum7.eu/genofire/thrempp/models" +) + +type Config struct { + LogLevel logger.Level `toml:"log_level"` + Database database.Config `toml:"database"` + Components []component.Config `toml:"component"` +} + +var configPath string + +// serveCmd represents the serve command +var serveCmd = &cobra.Command{ + Use: "serve", + Short: "Run xmpp transport", + Example: "yanic serve --config /etc/thrempp.toml", + Run: func(cmd *cobra.Command, args []string) { + config := &Config{} + if err := file.ReadTOML(configPath, config); err != nil { + log.Panicf("open config file: %s", err) + } + + log.SetLevel(config.LogLevel) + + if err := database.Open(config.Database); err != nil { + log.Panicf("no database connection: %s", err) + } + defer database.Close() + component.Load(config.Components) + + // Wait for INT/TERM + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + sig := <-sigs + log.Infof("received %s", sig) + + /* + server := o3.ThreemaRest{} + + var thrAccount models.AccountThreema + if err := database.Read.First(&thrAccount).Error; err != nil { + id, _ := server.CreateIdentity() + thrAccount.TID = make([]byte, len(id.ID)) + thrAccount.LSK = make([]byte, len(id.LSK)) + copy(thrAccount.TID, id.ID[:]) + copy(thrAccount.LSK, id.LSK[:]) + database.Write.Create(&thrAccount) + } + + log.Warnf("%s", thrAccount.TID) + var lsk [32]byte + copy(lsk[:], thrAccount.LSK[:]) + tid, err := o3.NewThreemaID(string(thrAccount.TID), lsk, o3.AddressBook{}) + tid.Nick = o3.NewPubNick("xmpp:geno@fireorbit.de") + + ctx := o3.NewSessionContext(tid) + + // let the session begin + log.Info("Starting session") + sendMsgChan, receiveMsgChan, err := ctx.Run() + if err != nil { + log.Fatal(err) + } + // handle incoming messages + for receivedMessage := range receiveMsgChan { + if receivedMessage.Err != nil { + log.Errorf("Error Receiving Message: %s\n", receivedMessage.Err) + continue + } + switch msg := receivedMessage.Msg.(type) { + case o3.TextMessage: + if tid.String() == msg.Sender().String() { + continue + } + qoute := fmt.Sprintf("> %s: %s\n%s", msg.Sender(), msg.Text(), "Exactly!") + err = ctx.SendTextMessage(msg.Sender().String(), qoute, sendMsgChan) + if err != nil { + log.Fatal(err) + } + + } + } + */ + }, +} + +func init() { + RootCmd.AddCommand(serveCmd) + serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.toml", "Path to configuration file") +} diff --git a/component/all/main.go b/component/all/main.go new file mode 100644 index 0000000..8f39aa4 --- /dev/null +++ b/component/all/main.go @@ -0,0 +1,6 @@ +package all + +import ( + // import all implementations + _ "dev.sum7.eu/genofire/thrempp/component/threema" +) diff --git a/component/main.go b/component/main.go new file mode 100644 index 0000000..0919671 --- /dev/null +++ b/component/main.go @@ -0,0 +1,36 @@ +package component + +import ( + "github.com/bdlm/log" + "gosrc.io/xmpp" +) + +type Component interface { + Connect() (chan xmpp.Packet, error) + Send(xmpp.Packet) +} + +// Connect function with config to get DB connection interface +type Connect func(config map[string]interface{}) (Component, error) + +var components = map[string]Connect{} + +func AddComponent(name string, c Connect) { + components[name] = c +} + +func Load(configs []Config) { + for _, config := range configs { + f, ok := components[config.Type] + if !ok { + log.Warnf("it was not possible to find a component with type '%s'", config.Type) + continue + } + comp, err := f(config.Special) + if err != nil { + log.WithField("type", config.Type).Panic(err) + } + config.comp = comp + log.WithField("type", config.Type).Infof("component for %s started", config.Host) + } +} diff --git a/component/threema/main.go b/component/threema/main.go new file mode 100644 index 0000000..0616aac --- /dev/null +++ b/component/threema/main.go @@ -0,0 +1,15 @@ +package threema + +import "dev.sum7.eu/genofire/thrempp/component" + +type Threema struct { + component.Component +} + +func NewThreema(config map[string]interface{}) (component.Component, error) { + return &Threema{}, nil +} + +func init() { + component.AddComponent("threema", NewThreema) +} diff --git a/component/xmpp.go b/component/xmpp.go new file mode 100644 index 0000000..02a4ca6 --- /dev/null +++ b/component/xmpp.go @@ -0,0 +1,38 @@ +package component + +import ( + "gosrc.io/xmpp" +) + +type Config struct { + Type string + Host string + Connection string + Secret string + Special map[string]interface{} + + xmpp *xmpp.Component + comp Component +} + +func (c *Config) Start() error { + c.xmpp = &xmpp.Component{Host: c.Host, Secret: c.Secret} + err := c.xmpp.Connect(c.Connection) + if err != nil { + return err + } + out, err := c.comp.Connect() + if err != nil { + return err + } + + go c.recieve(out) + go c.sender() + + return nil +} + +func (c *Config) recieve(chan xmpp.Packet) { +} +func (c *Config) sender() { +} diff --git a/config_example.toml b/config_example.toml new file mode 100644 index 0000000..0e52875 --- /dev/null +++ b/config_example.toml @@ -0,0 +1,15 @@ +log_level = 50 + +[[component]] +type = "threema" +host = "threema.chat.sum7.eu" +connection = "localhost:10001" +secret = "change_me" + +[database] +type = "sqlite3" +logging = true +connection = "./thrempp.db" +# For Master-Slave cluster +# read_connection = "" + diff --git a/main.go b/main.go new file mode 100644 index 0000000..d54f0b9 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "dev.sum7.eu/genofire/thrempp/cmd" + +func main() { + cmd.Execute() +} diff --git a/models/account_threema.go b/models/account_threema.go new file mode 100644 index 0000000..82fcfba --- /dev/null +++ b/models/account_threema.go @@ -0,0 +1,19 @@ +package models + +import ( + "github.com/jinzhu/gorm" + + "dev.sum7.eu/genofire/golang-lib/database" +) + +type AccountThreema struct { + gorm.Model + XMPPID uint + XMPP JID + TID []byte + LSK []byte +} + +func init() { + database.AddModel(&AccountThreema{}) +} diff --git a/models/jid.go b/models/jid.go new file mode 100644 index 0000000..b42a2aa --- /dev/null +++ b/models/jid.go @@ -0,0 +1,21 @@ +package models + +import ( + "github.com/jinzhu/gorm" + + "dev.sum7.eu/genofire/golang-lib/database" +) + +type JID struct { + gorm.Model + Local string + Domain string +} + +func (j *JID) TableName() string { + return "jid" +} + +func init() { + database.AddModel(&JID{}) +}