-
Notifications
You must be signed in to change notification settings - Fork 3
/
devldap.go
115 lines (92 loc) · 2.42 KB
/
devldap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/signal"
"syscall"
"github.com/Jeffail/gabs"
ldap "github.com/butonic/ldapserver"
"github.com/fsnotify/fsnotify"
)
var jsonParsed *gabs.Container
func loadData(file string) error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
log.Printf("loaded %s", file)
var tmpJson *gabs.Container
tmpJson, err = gabs.ParseJSON(data)
if err != nil {
return err
}
jsonParsed = tmpJson
log.Printf("parsed JSON")
return nil
}
func main() {
hostAndPort := flag.String("l", "127.0.0.1:10389", "host and port to listen for connections")
dataFile := flag.String("d", "data.json", "json file with data to serve")
flag.Parse()
err := loadData(*dataFile)
if err != nil {
log.Printf("ERROR: %v", err)
os.Exit(1)
}
// create a new file watcher, based on https://medium.com/@skdomino/watch-this-file-watching-in-go-5b5a247cf71f
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Println("ERROR", err)
}
defer watcher.Close()
//nch := make(chan bool)
go func() {
for {
select {
// watch for events
case event := <-watcher.Events:
// TODO wait a sec before reparsing and ignore additional notifications in that time
if event.Op&fsnotify.Write == fsnotify.Write {
log.Printf("modified file: %v, %v, %#v", event.Name, event.Op, event)
// reload json
err := loadData(*dataFile)
if err != nil {
log.Printf("ERROR: %v", err)
}
}
// watch for errors
case err := <-watcher.Errors:
log.Println("ERROR", err)
}
}
}()
// out of the box fsnotify can watch a single file, or a single directory
if err := watcher.Add(*dataFile); err != nil {
log.Println("ERROR", err)
}
//<-nch
//Create a new LDAP Server
server := ldap.NewServer()
//Create routes bindings
routes := ldap.NewRouteMux()
routes.NotFound(handleNotFound)
routes.Abandon(handleAbandon)
routes.Bind(handleBind)
routes.Extended(handleWhoAmI).
RequestName(ldap.NoticeOfWhoAmI).Label("Ext - WhoAmI")
routes.Extended(handleExtended).Label("Ext - Generic")
routes.Search(handleSearch).Label("Search - Generic")
//Attach routes to server
server.Handle(routes)
// listen on 10389 and serve
go server.ListenAndServe(*hostAndPort)
// When CTRL+C, SIGINT and SIGTERM signal occurs
// Then stop server gracefully
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
close(ch)
server.Stop()
}