Initial commit

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

8
.ci/check-gofmt Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
result="$(gofmt -s -l . | grep -v '^vendor/' )"
if [ -n "$result" ]; then
echo "Go code is not formatted, run 'gofmt -s -w .'" >&2
echo "$result"
exit 1
fi

25
.ci/check-testfiles Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python
# checks if every desired package has test files
import os
import re
import sys
source_re = re.compile(".*\.go")
test_re = re.compile(".*_test\.go")
missing = False
for root, dirs, files in os.walk("."):
# ignore some paths
if root == "." or root.startswith("./vendor") or root.startswith("./."):
continue
# source files but not test files?
if len(filter(source_re.match, files)) > 0 and len(filter(test_re.match, files)) == 0:
print("no test files for {}".format(root))
missing = True
if missing:
sys.exit(1)
else:
print("every package has test files")

2
.gitignore vendored Normal file
View File

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

33
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,33 @@
image: golang:latest
stages:
- build
- test
before_script:
- mkdir -p /go/src/dev.sum7.eu/$CI_PROJECT_NAMESPACE/
- cp -R $CI_PROJECT_DIR /go/src/dev.sum7.eu/$CI_PROJECT_NAMESPACE
- cd /go/src/dev.sum7.eu/$CI_PROJECT_PATH
- go get -v dev.sum7.eu/$CI_PROJECT_PATH
build-my-project:
stage: build
script:
- mkdir $CI_PROJECT_DIR/bin/
- cp /go/bin/$CI_PROJECT_NAME $CI_PROJECT_DIR/bin/$CI_PROJECT_NAME
artifacts:
paths:
- bin/thrempp
- config_example.toml
test-my-project:
stage: test
script:
- ./.ci/check-gofmt
- ./.ci/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 ./...

50
README.md Normal file
View File

@ -0,0 +1,50 @@
# Thrempp [![pipeline status](https://dev.sum7.eu/genofire/thrempp/badges/master/pipeline.svg)](https://dev.sum7.eu/genofire/thrempp/pipelines) [![coverage report](https://dev.sum7.eu/genofire/thrempp/badges/master/coverage.svg)](https://dev.sum7.eu/genofire/thrempp/pipelines)
XMPP - Transport
ATM planned support for Threema only
## Get thrempp
#### Download
Latest Build binary from ci here:
[Download All](https://dev.sum7.eu/genofire/thrempp/-/jobs/artifacts/master/download/?job=build-my-project) (with config example)
[Download Binary](https://dev.sum7.eu/genofire/thrempp/-/jobs/artifacts/master/raw/bin/thrempp?inline=false&job=build-my-project)
#### Build
```bash
go get -u dev.sum7.eu/genofire/thrempp
```
## Configure
see `config_example.conf`
## Start / Boot
_/lib/systemd/system/thrempp.service_ :
```
[Unit]
Description=thrempp
After=network.target
# After=ejabberd.service
# After=prosody.service
[Service]
Type=simple
# User=notRoot
ExecStart=/opt/go/bin/thrempp serve --config /etc/thrempp.conf
Restart=always
RestartSec=5sec
[Install]
WantedBy=multi-user.target
```
Start: `systemctl start thrempp`
Autostart: `systemctl enable thrempp`

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

1
cmd/root_test.go Normal file
View File

@ -0,0 +1 @@
package cmd

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

View File

@ -0,0 +1 @@
package all

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

1
component/main_test.go Normal file
View File

@ -0,0 +1 @@
package component

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

View File

@ -0,0 +1 @@
package threema

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:5347"
secret = "change_me"
[database]
type = "sqlite3"
logging = false
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{})
}

1
models/main_test.go Normal file
View File

@ -0,0 +1 @@
package models