Initial commit

This commit is contained in:
Martin/Geno 2019-05-31 10:07:40 +02:00
commit ea13390abe
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
13 changed files with 332 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
thrempp.db
config.toml

36
.gitlab-ci.yml Normal file
View File

@ -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 ./...

4
README.md Normal file
View File

@ -0,0 +1,4 @@
# Threempp
Threema XMPP - Transport

26
cmd/root.go Normal file
View File

@ -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)
}
}

107
cmd/serve.go Normal file
View File

@ -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")
}

6
component/all/main.go Normal file
View File

@ -0,0 +1,6 @@
package all
import (
// import all implementations
_ "dev.sum7.eu/genofire/thrempp/component/threema"
)

36
component/main.go Normal file
View File

@ -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)
}
}

15
component/threema/main.go Normal file
View File

@ -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)
}

38
component/xmpp.go Normal file
View File

@ -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() {
}

15
config_example.toml Normal file
View File

@ -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 = ""

7
main.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "dev.sum7.eu/genofire/thrempp/cmd"
func main() {
cmd.Execute()
}

19
models/account_threema.go Normal file
View File

@ -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{})
}

21
models/jid.go Normal file
View File

@ -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{})
}