Skip to content

Commit

Permalink
feat: yaml config & edit config
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtdeh committed Nov 4, 2023
1 parent 23f4118 commit 1ccb9e1
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 71 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ require (
)

require (
github.com/goccy/go-yaml v1.11.2 // indirect
github.com/klauspost/compress v1.11.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
golang.org/x/sys v0.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/elastic/go-lumber v0.1.1 h1:aae5rSBnwBvdB0aShJ7AbOYPyvP1/wS/JIOC1A4D1
github.com/elastic/go-lumber v0.1.1/go.mod h1:DMVoFv7YM71enE9X5vWJWWv7wvQNtzXh7bPeKukDccY=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/goccy/go-yaml v1.11.2 h1:joq77SxuyIs9zzxEjgyLBugMQ9NEgTWxXfz2wVqwAaQ=
github.com/goccy/go-yaml v1.11.2/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/compress v1.11.2 h1:MiK62aErc3gIiVEtyzKfeOHgW7atJb5g/KNX5m3c2nQ=
Expand All @@ -13,6 +15,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -25,6 +29,10 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23 changes: 10 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/mrtdeh/testeps/pkg/core"
tls_config "github.com/mrtdeh/testeps/pkg/tls_config"
)

var (
Expand All @@ -18,7 +17,7 @@ var (
delay *int64
threads *int

showSources *bool
showSources, editSources *bool

tlsConfig *tls.Config
SendDelay time.Duration
Expand All @@ -37,6 +36,7 @@ func main() {
threads = flag.Int("t", 1, "threads count")

showSources = flag.Bool("show-sources", false, "show sources list")
editSources = flag.Bool("edit-sources", false, "edit sources")

ca = flag.String("ca", "", "ca certificate")
cert = flag.String("cert", "", "cert certificate")
Expand All @@ -47,24 +47,21 @@ func main() {
core.PrintSources()
return
}
SendDelay = time.Duration(*delay) * time.Millisecond
tlsConfig, err = tls_config.LoadTLSCredentials(tls_config.Config{
CAPath: *ca,
CertPath: *cert,
KeyPath: *key,
})
if err != nil {
// log.Println("tls config : ", err.Error())

if *editSources {
core.EditSources()
return
}
SendDelay = time.Duration(*delay) * time.Millisecond

var incs []string
if *sources != "" {
incs = strings.Split(*sources, ",")
}
core.Run(core.Config{
Sources: incs,
SendDelay: SendDelay,
TLSConfig: tlsConfig,
Sources: incs,
SendDelay: SendDelay,
// TLSConfig: tlsConfig,
Inifity: *inifity,
ThreadsCount: *threads,
DestinationIp: *dest,
Expand Down
4 changes: 2 additions & 2 deletions packaging/nfpm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ contents:
type: symlink
- src: ./Logs
dst: /usr/share/logrator/Logs
- src: ./sources.json
dst: /usr/share/logrator/sources.json
- src: ./sources.yaml
dst: /usr/share/logrator/sources.yaml



Expand Down
163 changes: 109 additions & 54 deletions pkg/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,44 @@ package core

import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net"
"os"
"os/exec"
"sort"
"strings"
"sync"
"time"

"github.com/fatih/color"
"github.com/goccy/go-yaml"
"github.com/mrtdeh/testeps/pkg/lumber"
"github.com/mrtdeh/testeps/pkg/tls_config"
"github.com/rodaine/table"
)

type JsonLogs struct {
Name string `json:"name"`
Port int `json:"port"`
Protocol string `json:"protocol"`
Secure bool `json:"tls"`
Logs []string `json:"logs"`
Name string `yaml:"name"`
Port int `yaml:"port"`
Protocol string `yaml:"proto"`
Secure bool `yaml:"tls"`
Path string `yaml:"path"`

CA string `yaml:"ssl_ca"`
Cert string `yaml:"ssl_cert"`
Key string `yaml:"ssl_key"`

logs []string
}

type Sources struct {
Default struct {
CA string `yaml:"ssl_ca"`
Cert string `yaml:"ssl_cert"`
Key string `yaml:"ssl_key"`
} `yaml:"default"`
Sources []JsonLogs `yaml:"sources"`
}

type LogsMap map[string]JsonLogs
Expand All @@ -40,6 +57,44 @@ var (
logsMap LogsMap = make(LogsMap)
)

func LoadSetting() error {
logsMap = LogsMap{}

data, err := os.ReadFile("./sources.yaml")
if err != nil {
return fmt.Errorf("sources.yaml not exist an current directory!")
}

var j Sources
// err = json.Unmarshal(data, &j)
err = yaml.Unmarshal(data, &j)
if err != nil {
return err
}

if j.Sources != nil {
for _, s := range j.Sources {
s.logs = load(s.Path)
if s.CA == "" {
s.CA = j.Default.CA
}
if s.Cert == "" {
s.Cert = j.Default.Cert
}
if s.Key == "" {
s.Key = j.Default.Key
}

logsMap[s.Name] = s
}
} else {
return fmt.Errorf("parse failed : sources not found")
}

return nil

}

func Run(cnf Config) {
incs := cnf.Sources

Expand All @@ -56,7 +111,7 @@ func Run(cnf Config) {
go func(i string) {
// var count int
defer wg.Done()
for j := 0; j <= cnf.ThreadsCount; j++ {
for j := 0; j < cnf.ThreadsCount; j++ {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down Expand Up @@ -95,7 +150,7 @@ func (c *Config) sendBeatsLogs(mylog JsonLogs) {
defer lc.Close()

for {
for _, msg := range mylog.Logs {
for _, msg := range mylog.logs {
// Convert message to beat log
m := lumber.M2(msg)
// Overwrite timestamp field
Expand All @@ -112,7 +167,7 @@ func (c *Config) sendBeatsLogs(mylog JsonLogs) {
log.Fatalf("Failed to send log to Beat: %v", err)
}
}
log.Printf("send %d logs from datasource %s to server %s", len(mylog.Logs), mylog.Name, addr)
log.Printf("send %d logs from datasource %s to server %s", len(mylog.logs), mylog.Name, addr)
time.Sleep(c.SendDelay)

if !*&c.Inifity {
Expand Down Expand Up @@ -141,13 +196,13 @@ func (c *Config) sendSyslogLogs(mylog JsonLogs) {
defer conn.Close()

for {
for _, msg := range mylog.Logs {
for _, msg := range mylog.logs {
_, err := fmt.Fprintln(conn, msg)
if err != nil {
log.Fatalf("Failed to send log to Beat: %v", err)
}
}
log.Printf("send %d logs from datasource %s to server %s", len(mylog.Logs), mylog.Name, addr)
log.Printf("send %d logs from datasource %s to server %s", len(mylog.logs), mylog.Name, addr)
time.Sleep(c.SendDelay)

if !*&c.Inifity {
Expand All @@ -156,11 +211,21 @@ func (c *Config) sendSyslogLogs(mylog JsonLogs) {
}
}

func (c *Config) send(log JsonLogs) {
if log.Protocol == "tcp" || log.Protocol == "udp" {
c.sendSyslogLogs(log)
} else if log.Protocol == "beats" {
c.sendBeatsLogs(log)
func (c *Config) send(mylog JsonLogs) {
tlsConfig, err := tls_config.LoadTLSCredentials(tls_config.Config{
CAPath: mylog.CA,
CertPath: mylog.Cert,
KeyPath: mylog.Key,
})
if err != nil {
log.Fatal("tls config : ", err.Error())
}
c.TLSConfig = tlsConfig

if mylog.Protocol == "tcp" || mylog.Protocol == "udp" {
c.sendSyslogLogs(mylog)
} else if mylog.Protocol == "beats" {
c.sendBeatsLogs(mylog)
}
}

Expand All @@ -173,43 +238,6 @@ func load(path string) []string {
return lines
}

func LoadSetting() error {
logsMap = LogsMap{}

data, err := os.ReadFile("./sources.json")
if err != nil {
return fmt.Errorf("sources.json not exist an current directory!")
}

var j map[string]interface{}
err = json.Unmarshal(data, &j)
if err != nil {
return err
}

if sources, ok := j["sources"]; ok {
if ss, ok := sources.([]interface{}); ok {
for _, s := range ss {
tmp := s.(map[string]interface{})
logsMap[tmp["name"].(string)] = JsonLogs{
Name: tmp["name"].(string),
Port: int(tmp["port"].(float64)),
Protocol: tmp["proto"].(string),
Secure: tmp["tls"].(bool),
Logs: load(tmp["path"].(string)),
}
}
} else {
return fmt.Errorf("parse failed : sources must be a array")
}
} else {
return fmt.Errorf("parse failed : sources not found")
}

return nil

}

func PrintSources() {
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
Expand All @@ -230,8 +258,35 @@ func PrintSources() {
if v.Secure {
s = "Yes"
}
tbl.AddRow(v.Name, v.Protocol, v.Port, s, len(v.Logs))
tbl.AddRow(v.Name, v.Protocol, v.Port, s, len(v.logs))
}

tbl.Print()
}

func EditSources() error {
fpath := "/usr/share/logrator/sources.yaml"
f, err := os.Open(fpath)
if err != nil {
log.Fatal(err)
}
f.Close()

cmd := exec.Command("nano", fpath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
err = cmd.Wait()
if err != nil {
log.Printf("Error while editing. Error: %v\n", err)
} else {
log.Printf("Successfully edited.")
}

return nil

}
27 changes: 27 additions & 0 deletions sources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


# default settings
default:
# default path of ca for all sources
ssl_ca: ./certs/ca.crt
# default path of cert for all sources
ssl_cert: ./certs/logstash.crt
# default path of key for all sources
ssl_key: ./certs/logstash.key

# list of sources
sources:
- name: windows
port: 5044
proto: beats
path: ./Logs/windows.json

- name: windows-tls
port: 15044
ssl: true
ssl_ca: /opt/certs/ca.crt
ssl_cert: /opt/certs/logstash.crt
ssl_key: /opt/certs/logstash.key
proto: beats
path: ./Logs/windows.json

0 comments on commit 1ccb9e1

Please sign in to comment.