Support modules

This commit is contained in:
Kioubit 2021-12-25 09:28:59 -05:00
parent 3491a47811
commit 6948f5a30d
6 changed files with 89 additions and 6 deletions

View File

@ -32,7 +32,8 @@ pndpd proxy <interface1> <interface2> <optional whitelist of CIDRs separated by
````
More options and additional documentation in the example config file (pndpd.conf).
### Developing
## Developing
### Adding Modules
It is easy to add functionality to PNDPD. For additions outside the core functionality you only need to keep the following methods in mind:
````
package main
@ -48,4 +49,6 @@ proxyInstance := pndp.NewProxy(iface1 string, iface2 string, filter []*net.IPNet
proxyInstance.Start()
proxyInstance.Stop()
````
New functionality should be implemented as a module. You will find an example module under ``modules/example/``.
Pull requests are welcome for any functionality you add.

View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"pndpd/modules"
"pndpd/pndp"
"strings"
)
@ -40,7 +41,7 @@ func readConfig(dest string) {
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "//") {
if strings.HasPrefix(line, "//") || strings.TrimSpace(line) == "" {
continue
}
if strings.HasPrefix(line, "debug") {
@ -50,6 +51,7 @@ func readConfig(dest string) {
}
continue
}
if strings.HasPrefix(line, "responder") && strings.Contains(line, "{") {
obj := configResponder{}
filter := ""
@ -75,8 +77,7 @@ func readConfig(dest string) {
}
allResponders = append(allResponders, &obj)
}
if strings.HasPrefix(line, "proxy") && strings.Contains(line, "{") {
} else if strings.HasPrefix(line, "proxy") && strings.Contains(line, "{") {
obj := configProxy{}
filter := ""
for {
@ -106,6 +107,25 @@ func readConfig(dest string) {
}
}
allProxies = append(allProxies, &obj)
} else if strings.Contains(line, "{") {
option := strings.TrimSuffix(strings.TrimSpace(line), "{")
option = strings.TrimSpace(option)
if modules.ModuleList != nil {
for i := range modules.ModuleList {
if (*modules.ModuleList[i]).Option == option {
var lines []string
for {
scanner.Scan()
line = strings.TrimSpace(scanner.Text())
lines = append(lines, line)
if strings.HasPrefix(line, "}") {
break
}
}
(*modules.ModuleList[i]).ConfigCallback(lines)
}
}
}
}
}

14
main.go
View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/signal"
"pndpd/modules"
"pndpd/pndp"
"syscall"
)
@ -18,6 +19,13 @@ func WaitForSignal() {
func main() {
fmt.Println("PNDPD Version 1.0 - Kioubit 2021")
if modules.ModuleList != nil {
fmt.Print("Loaded Modules: ")
for i := range modules.ModuleList {
fmt.Print((*modules.ModuleList[i]).Name + " ")
}
fmt.Println()
}
if len(os.Args) <= 2 {
printUsage()
@ -47,6 +55,12 @@ func main() {
case "config":
readConfig(os.Args[2])
default:
for i := range modules.ModuleList {
if (*modules.ModuleList[i]).Option == os.Args[1] {
(*modules.ModuleList[i]).CommandLineCallback(os.Args)
return
}
}
printUsage()
return
}

View File

@ -0,0 +1,25 @@
package example
import (
"fmt"
"pndpd/modules"
)
// This is an example module that is not imported by the main program
func init() {
modules.RegisterModule("Example", "example", "example <parameter 1> <parameter 2>", commandLineRead, configRead)
}
func configRead(s []string) {
// Prints out the contents of the config file that are relevant for this module (that are inside the example{} option)
for _, n := range s {
fmt.Println(n)
}
}
func commandLineRead(s []string) {
// Prints out the command line options given to the program if the command starts with "example"
for _, n := range s {
fmt.Println(n)
}
}

21
modules/modules.go Normal file
View File

@ -0,0 +1,21 @@
package modules
var ModuleList []*Module
type Module struct {
Name string
Option string
OptionDescription string
CommandLineCallback func([]string)
ConfigCallback func([]string)
}
func RegisterModule(name string, option string, description string, commandLineCallback func([]string), configCallback func([]string)) {
ModuleList = append(ModuleList, &Module{
Name: name,
Option: option,
OptionDescription: description,
CommandLineCallback: commandLineCallback,
ConfigCallback: configCallback,
})
}

View File

@ -57,7 +57,7 @@ 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.Println("Started responder instance on interface", obj.iface)
<-obj.stopChan
}
@ -128,7 +128,7 @@ 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.Println("Started Proxy instance for interfaces:", obj.iface1, "and", obj.iface2)
<-obj.stopChan
}