Support modules
This commit is contained in:
parent
3491a47811
commit
6948f5a30d
@ -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.
|
||||
|
26
config.go
26
config.go
@ -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
14
main.go
@ -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
|
||||
}
|
||||
|
25
modules/example/example.go
Normal file
25
modules/example/example.go
Normal 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
21
modules/modules.go
Normal 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,
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user