-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
161 lines (133 loc) · 3.82 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"context"
_ "expvar"
"net"
"net/http"
"time"
"github.com/coreos/go-systemd/daemon"
"github.com/davecgh/go-spew/spew"
"github.com/fdurand/arp"
cache "github.com/fdurand/go-cache"
"github.com/go-errors/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/inverse-inc/packetfence/go/log"
"github.com/inverse-inc/packetfence/go/timedlock"
dhcp "github.com/krolaw/dhcp4"
)
var DHCPConfig *Interfaces
var GlobalIpCache *cache.Cache
var GlobalMacCache *cache.Cache
var GlobalTransactionCache *cache.Cache
var GlobalTransactionLock *timedlock.RWLock
var RequestGlobalTransactionCache *cache.Cache
var VIP map[string]bool
var VIPIp map[string]net.IP
var ctx = context.Background()
var intNametoInterface map[string]*Interface
const FreeMac = "00:00:00:00:00:00"
const FakeMac = "ff:ff:ff:ff:ff:ff"
func main() {
log.SetProcessName("godhcp")
ctx = log.LoggerNewContext(ctx)
arp.AutoRefresh(30 * time.Second)
// Default http timeout
http.DefaultClient.Timeout = 10 * time.Second
// Initialize IP cache
GlobalIpCache = cache.New(5*time.Minute, 10*time.Minute)
// Initialize Mac cache
GlobalMacCache = cache.New(5*time.Minute, 10*time.Minute)
// Initialize transaction cache
GlobalTransactionCache = cache.New(5*time.Minute, 10*time.Minute)
GlobalTransactionLock = timedlock.NewRWLock()
RequestGlobalTransactionCache = cache.New(5*time.Minute, 10*time.Minute)
VIP = make(map[string]bool)
VIPIp = make(map[string]net.IP)
// Read pfconfig
DHCPConfig = newDHCPConfig()
DHCPConfig.readConfig()
// Queue value
var (
maxQueueSize = 100
maxWorkers = 100
)
// create job channel
jobs := make(chan job, maxQueueSize)
// create workers
for i := 1; i <= maxWorkers; i++ {
go func(i int) {
for j := range jobs {
doWork(i, j)
}
}(i)
}
intNametoInterface = make(map[string]*Interface)
// Unicast listener
for _, v := range DHCPConfig.intsNet {
v := v
// Create a channel for each interfaces
intNametoInterface[v.Name] = &v
go func() {
v.runUnicast(ctx, jobs)
}()
}
// Broadcast listener
for _, v := range DHCPConfig.intsNet {
v := v
go func() {
v.run(ctx, jobs)
}()
}
// Api
router := mux.NewRouter()
router.HandleFunc("/api/v1/dhcp/mac/{mac:(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}}", handleMac2Ip).Methods("GET")
router.HandleFunc("/api/v1/dhcp/mac/{mac:(?:[0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}}", handleReleaseIP).Methods("DELETE")
router.HandleFunc("/api/v1/dhcp/ip/{ip:(?:[0-9]{1,3}.){3}(?:[0-9]{1,3})}", handleIP2Mac).Methods("GET")
router.HandleFunc("/api/v1/dhcp/stats", handleAllStats).Methods("GET")
router.HandleFunc("/api/v1/dhcp/stats/{int:.*}/{network:(?:[0-9]{1,3}.){3}(?:[0-9]{1,3})}", handleStats).Methods("GET")
router.HandleFunc("/api/v1/dhcp/stats/{int:.*}", handleStats).Methods("GET")
router.HandleFunc("/api/v1/dhcp/debug/{int:.*}/{role:(?:[^/]*)}", handleDebug).Methods("GET")
srv := &http.Server{
Addr: "127.0.0.1:22227",
IdleTimeout: 5 * time.Second,
Handler: router,
}
// Systemd
daemon.SdNotify(false, "READY=1")
go func() {
interval, err := daemon.SdWatchdogEnabled(false)
if err != nil || interval == 0 {
return
}
cli := &http.Client{}
for {
req, err := http.NewRequest("GET", "http://127.0.0.1:22227", nil)
if err != nil {
log.LoggerWContext(ctx).Error(err.Error())
continue
}
req.Close = true
resp, err := cli.Do(req)
time.Sleep(100 * time.Millisecond)
if err != nil {
log.LoggerWContext(ctx).Error(err.Error())
continue
}
defer resp.Body.Close()
if err == nil {
daemon.SdNotify(false, "WATCHDOG=1")
}
time.Sleep(interval / 3)
}
}()
srv.ListenAndServe()
}
func recoverName(options dhcp.Options) {
if r := recover(); r != nil {
fmt.Println("recovered from ", r)
fmt.Println(errors.Wrap(r, 2).ErrorStack())
spew.Dump(options)
}
}