iplistget/main.go

99 lines
2.5 KiB
Go

// iplistget project main.go
// Production build: go build -ldflags="-w" -o iplistget_release.exe iplistget
package main
import (
"flag"
"fmt"
"git.veenee.ru/veenee/iplistget/common"
_ "git.veenee.ru/veenee/iplistget/sources"
_ "git.veenee.ru/veenee/iplistget/targets"
"net"
"os"
)
const (
APP_NAME = "iplistget network lists operator"
VERSION = "0.1-dev"
)
var (
// Application flags
showVersion bool // Show version and exit
//sudo bool // Use sudo for privileged operations
listmodules bool // List available sources
source string // Module name to fetch the list
target string // Module name to supply this list to (iproute, ipset, file, stdout)
destination string // Optional parameter where to apply (routing table, ipset list, filename)
sourceurl string // Override the default source url
)
func init() {
flag.BoolVar(&showVersion, "version", false, "Show version and exit")
flag.BoolVar(&listmodules, "modules", false, "List all available modules and exit")
flag.StringVar(&source, "source", "", "Use specified source module")
flag.StringVar(&sourceurl, "url", "", "Override the default URL for source module")
flag.StringVar(&target, "target", "", "Use specified target module")
flag.StringVar(&destination, "destination", "", "Override the default destination (table name, list name, filename)")
//flag.BoolVar(&sudo, "sudo", false, "Use sudo for privileged operations")
}
func main() {
flag.Parse()
if showVersion {
fmt.Printf("%s version %s\n", APP_NAME, VERSION)
return
}
if listmodules {
lsmodules()
return
}
if source == "" {
fmt.Println("No source module specified")
os.Exit(1)
}
if target == "" {
fmt.Println("No destination module provided")
os.Exit(1)
}
srcmod, srcok := common.Sources[source]
if srcok != true {
fmt.Printf("Error. No such source module `%v`!\n", source)
os.Exit(1)
}
tgtmod, tgtok := common.Targets[target]
if tgtok != true {
fmt.Printf("Error. No such target module `%v`!\n", target)
os.Exit(1)
}
var iplist []net.IPNet
iplist, geterr := srcmod.Get(sourceurl)
if geterr != nil {
fmt.Printf("Error. Source module failed. %v\n", geterr)
}
if err := tgtmod.Set(destination, iplist); err != nil {
fmt.Printf("Error. Target module failed. %v\n", err)
}
}
func lsmodules() {
fmt.Print("Available sources: ")
for src := range common.Sources {
fmt.Print(src, " ")
}
fmt.Print("\nAvailable targets: ")
for tgt := range common.Targets {
fmt.Print(tgt, " ")
}
fmt.Println()
}