-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
119 lines (102 loc) · 2.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"flag"
"fmt"
"log"
"maksimkurb/keenetic-pbr/lib"
"os"
"path/filepath"
)
// Command represents the subcommand to execute
type Command string
const (
Download Command = "download"
Apply Command = "apply"
GenRoutingConfig Command = "gen-routing-config"
)
// Config holds the application configuration
type Config struct {
// Configuration fields will be defined in config package
}
// CLI represents command line arguments
type CLI struct {
configPath string
command Command
ipFamily uint8
}
func parseFlags() *CLI {
cli := &CLI{}
// Define flags
flag.StringVar(&cli.configPath, "config", "/opt/etc/keenetic-pbr/keenetic-pbr.conf", "Path to configuration file")
// Custom usage message
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Keenetic Policy-Based Routing Manager\n\n")
fmt.Fprintf(os.Stderr, "Usage: %s [options] <command>\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Commands:\n")
fmt.Fprintf(os.Stderr, " download Download lists\n")
fmt.Fprintf(os.Stderr, " apply Import lists to ipset and update dnsmasq lists\n")
fmt.Fprintf(os.Stderr, " gen-routing-config Gen IPv4 configuration for routing scripts (ipset, iface_name, fwmark, table, priority)\n\n")
fmt.Fprintf(os.Stderr, " gen-routing-config-ipv6 Gen IPv6 configuration for routing scripts (ipset, iface_name, fwmark, table, priority)\n\n")
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
}
flag.Parse()
// Process command
args := flag.Args()
if len(args) != 1 {
flag.Usage()
os.Exit(1)
}
switch args[0] {
case "download":
cli.command = Download
case "apply":
cli.command = Apply
case "gen-routing-config":
cli.command = GenRoutingConfig
cli.ipFamily = 4
case "gen-routing-config-ipv6":
cli.command = GenRoutingConfig
cli.ipFamily = 6
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", args[0])
flag.Usage()
os.Exit(1)
}
return cli
}
func init() {
// Setup logging
if os.Getenv("LOG_LEVEL") == "" {
os.Setenv("LOG_LEVEL", "info")
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
cli := parseFlags()
// Ensure config directory exists
configDir := filepath.Dir(cli.configPath)
if err := os.MkdirAll(configDir, 0755); err != nil {
log.Fatalf("Failed to create config directory: %v", err)
}
// Load configuration
config, err := lib.LoadConfig(cli.configPath)
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
// Execute command
switch cli.command {
case Download:
if err := lib.DownloadLists(config); err != nil {
log.Fatalf("Failed to download lists: %v", err)
}
case Apply:
if err := lib.ApplyLists(config); err != nil {
log.Fatalf("Failed to apply configuration: %v", err)
}
case GenRoutingConfig:
if err := lib.GenRoutingConfig(config, cli.ipFamily); err != nil {
log.Fatalf("Failed to apply configuration: %v", err)
}
}
}