Better user input validation, always clear filter slice
This commit is contained in:
parent
a7eb52c0c5
commit
49c6c333e9
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"pndpd/modules"
|
||||
"pndpd/pndp"
|
||||
@ -13,7 +12,8 @@ import (
|
||||
func readConfig(dest string) {
|
||||
file, err := os.Open(dest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Println("Error:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
_ = file.Close()
|
||||
|
4
main.go
4
main.go
@ -16,7 +16,7 @@ import (
|
||||
var Version = "Development"
|
||||
|
||||
func main() {
|
||||
fmt.Println("PNDPD Version", Version, "- Kioubit 2021")
|
||||
fmt.Println("PNDPD Version", Version, "- Kioubit 2022")
|
||||
|
||||
if len(os.Args) <= 2 {
|
||||
printUsage()
|
||||
@ -53,7 +53,7 @@ func printUsage() {
|
||||
for i := range modules.ModuleList {
|
||||
for d := range (*modules.ModuleList[i]).Commands {
|
||||
if (*modules.ModuleList[i]).Commands[d].CommandLineEnabled {
|
||||
fmt.Println("pndpd", (*modules.ModuleList[i]).Commands[d].Description)
|
||||
fmt.Println((*modules.ModuleList[i]).Commands[d].Description)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,13 +11,13 @@ import (
|
||||
// This is an example module
|
||||
func init() {
|
||||
commands := []modules.Command{{
|
||||
CommandText: "command1",
|
||||
CommandText: "pndpd command1",
|
||||
Description: "This is the usage description for command1",
|
||||
BlockTerminate: true,
|
||||
CommandLineEnabled: true,
|
||||
ConfigEnabled: true,
|
||||
}, {
|
||||
CommandText: "command2",
|
||||
CommandText: "pndpd command2",
|
||||
Description: "This is the usage description for command2",
|
||||
BlockTerminate: false,
|
||||
CommandLineEnabled: false,
|
||||
|
@ -5,6 +5,7 @@ package userInterface
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"pndpd/modules"
|
||||
"pndpd/pndp"
|
||||
"strings"
|
||||
@ -13,19 +14,19 @@ import (
|
||||
func init() {
|
||||
commands := []modules.Command{{
|
||||
CommandText: "proxy",
|
||||
Description: "proxy <interface1> <interface2> <optional whitelist of CIDRs separated by a semicolon applied to interface2>",
|
||||
Description: "pndpd proxy <interface1> <interface2> <optional whitelist of CIDRs separated by a semicolon applied to interface2>",
|
||||
BlockTerminate: true,
|
||||
ConfigEnabled: true,
|
||||
CommandLineEnabled: true,
|
||||
}, {
|
||||
CommandText: "responder",
|
||||
Description: "responder <interface> <optional whitelist of CIDRs separated by a semicolon>",
|
||||
Description: "pndpd responder <interface> <optional whitelist of CIDRs separated by a semicolon>",
|
||||
BlockTerminate: true,
|
||||
ConfigEnabled: true,
|
||||
CommandLineEnabled: true,
|
||||
}, {
|
||||
CommandText: "modules",
|
||||
Description: "modules available - list available modules",
|
||||
Description: "pndpd modules available - list available modules",
|
||||
BlockTerminate: false,
|
||||
ConfigEnabled: false,
|
||||
CommandLineEnabled: true,
|
||||
@ -114,14 +115,23 @@ func initCallback(callback modules.CallbackInfo) {
|
||||
if strings.HasPrefix(n, "filter") {
|
||||
filter += strings.TrimSpace(strings.TrimPrefix(n, "filter")) + ";"
|
||||
if strings.Contains(n, ";") {
|
||||
panic("Invalid config file syntax")
|
||||
showError("config: the use of semicolons is not allowed in the filter arguments")
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(n, "autosense") {
|
||||
obj.autosense = strings.TrimSpace(strings.TrimPrefix(n, "autosense"))
|
||||
}
|
||||
if strings.Contains(n, "//") {
|
||||
showError("config: comments are not allowed after arguments")
|
||||
}
|
||||
}
|
||||
obj.Filter = strings.TrimSuffix(filter, ";")
|
||||
if obj.autosense != "" && obj.Filter != "" {
|
||||
showError("config: cannot have both a filter and autosense enabled on a proxy object")
|
||||
}
|
||||
if obj.Iface2 == "" || obj.Iface1 == "" {
|
||||
showError("config: two interfaces need to be specified in the config file for a proxy object. (iface1 and iface2 parameters)")
|
||||
}
|
||||
allProxies = append(allProxies, &obj)
|
||||
case "responder":
|
||||
obj := configResponder{}
|
||||
@ -133,13 +143,21 @@ func initCallback(callback modules.CallbackInfo) {
|
||||
if strings.HasPrefix(n, "filter") {
|
||||
filter += strings.TrimSpace(strings.TrimPrefix(n, "filter")) + ";"
|
||||
if strings.Contains(n, ";") {
|
||||
panic("Invalid config file syntax")
|
||||
showError("config: the use of semicolons is not allowed in the filter arguments")
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(n, "autosense") {
|
||||
obj.autosense = strings.TrimSpace(strings.TrimPrefix(n, "autosense"))
|
||||
}
|
||||
|
||||
if obj.autosense != "" && obj.Filter != "" {
|
||||
showError("config: cannot have both a filter and autosense enabled on a responder object")
|
||||
}
|
||||
if obj.Iface == "" {
|
||||
showError("config: interface not specified in the responder object. (iface parameter)")
|
||||
}
|
||||
if strings.Contains(n, "//") {
|
||||
showError("config: comments are not allowed after arguments")
|
||||
}
|
||||
}
|
||||
obj.Filter = strings.TrimSuffix(filter, ";")
|
||||
allResponders = append(allResponders, &obj)
|
||||
@ -169,3 +187,9 @@ func shutdownCallback() {
|
||||
n.instance.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
func showError(error string) {
|
||||
fmt.Println(error)
|
||||
fmt.Println("Exiting due to error")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ type ProxyObj struct {
|
||||
// With the optional autosenseInterface argument, the whitelist is configured based on the addresses assigned to the interface specified. This works even if the IP addresses change frequently.
|
||||
// Start() must be called on the object to actually start responding
|
||||
func NewResponder(iface string, filter []*net.IPNet, autosenseInterface string) *ResponderObj {
|
||||
if filter == nil {
|
||||
if filter == nil && autosenseInterface == "" {
|
||||
fmt.Println("WARNING: You should use a whitelist for the responder unless you really know what you are doing")
|
||||
}
|
||||
var s sync.WaitGroup
|
||||
@ -59,7 +59,8 @@ func (obj *ResponderObj) start() {
|
||||
}()
|
||||
go respond(obj.iface, requests, ndp_ADV, nil, obj.filter, obj.autosense, obj.stopWG, obj.stopChan)
|
||||
go listen(obj.iface, requests, ndp_SOL, obj.stopWG, obj.stopChan)
|
||||
fmt.Println("Started responder instance on interface", obj.iface)
|
||||
fmt.Printf("Started responder instance on interface %s", obj.iface)
|
||||
fmt.Println()
|
||||
<-obj.stopChan
|
||||
}
|
||||
|
||||
@ -130,7 +131,8 @@ func (obj *ProxyObj) start() {
|
||||
go listen(obj.iface2, req_iface2_adv_iface1, ndp_ADV, obj.stopWG, obj.stopChan)
|
||||
go respond(obj.iface1, req_iface2_adv_iface1, ndp_ADV, out_iface2_sol_questions_iface1_adv, nil, "", obj.stopWG, obj.stopChan)
|
||||
|
||||
fmt.Println("Started Proxy instance for interfaces:", obj.iface1, "and", obj.iface2)
|
||||
fmt.Printf("Started Proxy instance on interfaces %s and %s (if enabled, the whitelist is applied on %s)", obj.iface1, obj.iface2, obj.iface2)
|
||||
fmt.Println()
|
||||
<-obj.stopChan
|
||||
}
|
||||
|
@ -73,6 +73,7 @@ func respond(iface string, requests chan *ndpRequest, respondType ndpType, ndpQu
|
||||
// Auto-sense
|
||||
if autoSense != "" {
|
||||
//TODO Future work: Use another sub goroutine to monitor the interface instead of checking here
|
||||
filter = make([]*net.IPNet, 0)
|
||||
result = selectSourceIP(respondIface)
|
||||
autoiface, err := net.InterfaceByName(autoSense)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user