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).
|
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:
|
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
|
package main
|
||||||
@ -48,4 +49,6 @@ proxyInstance := pndp.NewProxy(iface1 string, iface2 string, filter []*net.IPNet
|
|||||||
proxyInstance.Start()
|
proxyInstance.Start()
|
||||||
proxyInstance.Stop()
|
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.
|
Pull requests are welcome for any functionality you add.
|
||||||
|
26
config.go
26
config.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"pndpd/modules"
|
||||||
"pndpd/pndp"
|
"pndpd/pndp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -40,7 +41,7 @@ func readConfig(dest string) {
|
|||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
if strings.HasPrefix(line, "//") {
|
if strings.HasPrefix(line, "//") || strings.TrimSpace(line) == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(line, "debug") {
|
if strings.HasPrefix(line, "debug") {
|
||||||
@ -50,6 +51,7 @@ func readConfig(dest string) {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(line, "responder") && strings.Contains(line, "{") {
|
if strings.HasPrefix(line, "responder") && strings.Contains(line, "{") {
|
||||||
obj := configResponder{}
|
obj := configResponder{}
|
||||||
filter := ""
|
filter := ""
|
||||||
@ -75,8 +77,7 @@ func readConfig(dest string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
allResponders = append(allResponders, &obj)
|
allResponders = append(allResponders, &obj)
|
||||||
}
|
} else if strings.HasPrefix(line, "proxy") && strings.Contains(line, "{") {
|
||||||
if strings.HasPrefix(line, "proxy") && strings.Contains(line, "{") {
|
|
||||||
obj := configProxy{}
|
obj := configProxy{}
|
||||||
filter := ""
|
filter := ""
|
||||||
for {
|
for {
|
||||||
@ -106,6 +107,25 @@ func readConfig(dest string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
allProxies = append(allProxies, &obj)
|
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"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"pndpd/modules"
|
||||||
"pndpd/pndp"
|
"pndpd/pndp"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
@ -18,6 +19,13 @@ func WaitForSignal() {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("PNDPD Version 1.0 - Kioubit 2021")
|
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 {
|
if len(os.Args) <= 2 {
|
||||||
printUsage()
|
printUsage()
|
||||||
@ -47,6 +55,12 @@ func main() {
|
|||||||
case "config":
|
case "config":
|
||||||
readConfig(os.Args[2])
|
readConfig(os.Args[2])
|
||||||
default:
|
default:
|
||||||
|
for i := range modules.ModuleList {
|
||||||
|
if (*modules.ModuleList[i]).Option == os.Args[1] {
|
||||||
|
(*modules.ModuleList[i]).CommandLineCallback(os.Args)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
printUsage()
|
printUsage()
|
||||||
return
|
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,
|
||||||
|
})
|
||||||
|
}
|
@ -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 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)
|
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
|
<-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 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)
|
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
|
<-obj.stopChan
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user