-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (76 loc) · 2 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
package main
import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/hashicorp/go-hclog"
"github.com/netauth/netauth/pkg/netauth"
"github.com/netauth/netradius/radius"
"github.com/spf13/viper"
)
func main() {
var appLogger hclog.Logger
llevel := os.Getenv("NETAUTH_LOGLEVEL")
if llevel != "" {
appLogger = hclog.New(&hclog.LoggerOptions{
Name: "netradius",
Level: hclog.LevelFromString(llevel),
})
} else {
appLogger = hclog.NewNullLogger()
}
// Take over the built in logger and set it up for Trace level
// priority. The only thing that logs at this priority are
// protocol messages from the underlying ldap server mux.
log.SetOutput(appLogger.Named("radius.protocol").
StandardWriter(
&hclog.StandardLoggerOptions{
ForceLevel: hclog.Trace,
},
),
)
viper.SetConfigName("config")
viper.AddConfigPath("/etc/netauth/")
viper.AddConfigPath("$HOME/.netauth/")
viper.AddConfigPath(".")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("NETAUTH")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
appLogger.Error("Error loading config", "error", err)
os.Exit(5)
}
nacl, err := netauth.NewWithLog(appLogger.Named("netauth"))
if err != nil {
appLogger.Error("Error initializing client", "error", err)
os.Exit(2)
}
nacl.SetServiceName("netradius")
srvr, err := radius.New(
radius.WithLogger(appLogger),
radius.WithNetAuth(nacl),
radius.WithSecret(os.Getenv("NETAUTH_RADIUS_SECRET")),
)
if err != nil {
appLogger.Error("Error initializing", "error", err)
os.Exit(1)
}
go func() {
if err := srvr.Serve(); err != nil {
appLogger.Error("Error serving", "error", err)
os.Exit(1)
}
}()
// Sit here and wait for a signal to shutdown.
ch := make(chan os.Signal, 5)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
appLogger.Info("Shutting down...")
if err := srvr.Shutdown(); err != nil {
appLogger.Error("Error during shutdown", "error", err)
os.Exit(2)
}
appLogger.Info("Goodbye!")
}