102 lines
2.1 KiB
Go
102 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Simple TOML parser, only handles basic formats we need
|
|
func parseConfig(filename string) (*Config, error) {
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
config := &Config{
|
|
DomainsOfInterest: []string{},
|
|
}
|
|
|
|
// Set default values
|
|
config.Proxy.Port = 8080
|
|
config.Dump.OutputDir = "traffic_dumps"
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
var currentSection string
|
|
|
|
for scanner.Scan() {
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
// Skip empty lines and comments
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
|
|
// Check if it's a section title
|
|
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
|
|
currentSection = strings.Trim(line, "[]")
|
|
continue
|
|
}
|
|
|
|
// Parse key-value pairs
|
|
parts := strings.SplitN(line, "=", 2)
|
|
if len(parts) != 2 {
|
|
continue
|
|
}
|
|
|
|
key := strings.TrimSpace(parts[0])
|
|
value := strings.TrimSpace(parts[1])
|
|
|
|
// Handle array values
|
|
if strings.HasPrefix(value, "[") && strings.HasSuffix(value, "]") {
|
|
arrayStr := strings.Trim(value, "[]")
|
|
if key == "domains_of_interest" {
|
|
if arrayStr != "" {
|
|
items := strings.Split(arrayStr, ",")
|
|
for _, item := range items {
|
|
item = strings.TrimSpace(item)
|
|
item = strings.Trim(item, "\"")
|
|
if item != "" {
|
|
config.DomainsOfInterest = append(config.DomainsOfInterest, item)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Handle string values
|
|
value = strings.Trim(value, "\"")
|
|
|
|
// Set values based on current section
|
|
switch currentSection {
|
|
case "proxy":
|
|
switch key {
|
|
case "port":
|
|
if port, err := strconv.Atoi(value); err == nil {
|
|
config.Proxy.Port = port
|
|
}
|
|
}
|
|
case "dump":
|
|
switch key {
|
|
case "output_dir":
|
|
config.Dump.OutputDir = value
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return config, nil
|
|
}
|
|
|
|
func (c *Config) String() string {
|
|
return fmt.Sprintf("Config{DomainsOfInterest: %v, Proxy: {Port: %d}, Dump: {OutputDir: %s}}",
|
|
c.DomainsOfInterest, c.Proxy.Port, c.Dump.OutputDir)
|
|
}
|