Skip to content

Commit

Permalink
foo
Browse files Browse the repository at this point in the history
  • Loading branch information
johanix committed May 22, 2024
1 parent 7fe395e commit b89d6b7
Showing 1 changed file with 0 additions and 170 deletions.
170 changes: 0 additions & 170 deletions sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"bytes"
"crypto/ecdsa"
"encoding/gob"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -251,175 +250,6 @@ func (td *TemData) Reaper(full bool) error {
return nil
}

func (td *TemData) ParseSourcesOG() error {
sources := viper.GetStringSlice("sources.active")
log.Printf("Defined policy sources: %v", sources)

td.mu.Lock()
td.Lists["whitelist"]["white_catchall"] =
&tapir.WBGlist{
Name: "white_catchall",
Description: "Whitelist consisting of white names found in black- or greylist sources",
Type: "whitelist",
SrcFormat: "none",
Format: "map",
Datasource: "Data misplaced in other sources",
Names: map[string]tapir.TapirName{},
ReaperData: map[time.Time]map[string]bool{},
}
td.Lists["greylist"]["grey_catchall"] =
&tapir.WBGlist{
Name: "grey_catchall",
Description: "Greylist consisting of grey names found in whitelist sources",
Type: "greylist",
SrcFormat: "none",
Format: "map",
Datasource: "Data misplaced in other sources",
Names: map[string]tapir.TapirName{},
ReaperData: map[time.Time]map[string]bool{},
}
td.mu.Unlock()

srcs := viper.GetStringMap("sources")
td.Logger.Printf("*** ParseSources: there are %d items in spec.", len(srcs))

threads := 0

var rptchan = make(chan string, 5)
var mqttdetails = tapir.MqttDetails{
ValidatorKeys: make(map[string]*ecdsa.PublicKey),
}

if td.MqttEngine == nil {
td.mu.Lock()
err := td.CreateMqttEngine(viper.GetString("mqtt.clientid"), td.MqttLogger)
if err != nil {
TEMExiter("Error creating MQTT Engine: %v", err)
}
td.mu.Unlock()
}

for name, src := range srcs {
switch src.(type) {
case map[string]any:
s := src.(map[string]any)
if _, exist := s["active"]; !exist {
td.Logger.Printf("*** Source \"%s\" is not active. Ignored.", name)
continue
}

switch s["active"].(type) {
case bool:
if s["active"].(bool) == false {
td.Logger.Printf("*** Source \"%s\" is not active (%v). Ignored.",
name, s["active"])
continue
}
default:
td.Logger.Printf("*** [should not happen] Source \"%s\" active key is of type %t. Ignored.",
name, s["active"])
continue
}

td.Logger.Printf("=== Source: %s (%s) will be used (list type %s)",
name, s["name"], s["type"])

var params = map[string]string{}

for _, key := range []string{"name", "upstream", "filename", "zone", "topic", "validatorkey"} {
if tmp, ok := s[key].(string); ok {
params[key] = tmp
} else {
params[key] = ""
}
}

threads++
newsource := tapir.WBGlist{
Name: params["name"],
Description: s["description"].(string),
Type: s["type"].(string),
SrcFormat: s["format"].(string),
Datasource: s["source"].(string),
Names: map[string]tapir.TapirName{},
ReaperData: map[time.Time]map[string]bool{},
Filename: params["filename"],
RpzUpstream: params["upstream"],
RpzZoneName: dns.Fqdn(params["zone"]),
}

if newsource.Datasource == "mqtt" {
key, err := tapir.FetchMqttValidatorKey(params["topic"], params["validatorkey"])
if err != nil {
td.Logger.Printf("ParseSources: Error fetching MQTT validator key for topic %s: %v",
params["topic"], err)
} else {
mqttdetails.ValidatorKeys[params["topic"]] = key
}
}

var err error

go func(name string, threads int) {
td.Logger.Printf("Thread: parsing source \"%s\"", name)
switch s["source"] {
case "mqtt":
err = td.MqttEngine.AddTopic(params["topic"], mqttdetails.ValidatorKeys[params["topic"]])
if err != nil {
TEMExiter("Error adding topic %s to MQTT Engine: %v", params["topic"], err)
}

newsource.Format = "map" // for now
// td.Greylists[newsource.Name] = &newsource
td.mu.Lock()
td.Lists["greylist"][newsource.Name] = &newsource
td.Logger.Printf("Created list [greylist][%s]", newsource.Name)
td.mu.Unlock()
td.Logger.Printf("*** MQTT sources are only managed via RefreshEngine.")
rptchan <- name
case "file":
err = td.ParseLocalFile(name, &newsource, rptchan)
case "xfr":
err = td.ParseRpzFeed(name, &newsource, rptchan)
}
if err != nil {
log.Printf("Error parsing source %s (datasource %s): %v",
name, s["source"], err)
}
}(name, threads)

default:
td.Logger.Printf("*** ParseSources: Error: failed to parse source \"%s\": %v",
name, src)
}
}

if td.MqttEngine != nil && !td.TapirMqttEngineRunning {
err := td.StartMqttEngine(td.MqttEngine)
if err != nil {
TEMExiter("Error starting MQTT Engine: %v", err)
}
}

for {
tmp := <-rptchan
threads--
td.Logger.Printf("ParseSources: source \"%s\" is now complete. %d remaining", tmp, threads)
if threads == 0 {
break
}
}

td.Logger.Printf("ParseSources: static sources done.")

err := td.GenerateRpzAxfr()
if err != nil {
td.Logger.Printf("ParseSources: Error from GenerateRpzAxfr(): %v", err)
}

return nil
}

type SrcFoo struct {
Src struct {
Style string `yaml:"style"`
Expand Down

0 comments on commit b89d6b7

Please sign in to comment.