-
Notifications
You must be signed in to change notification settings - Fork 0
/
radiusServer.go
145 lines (115 loc) · 3.26 KB
/
radiusServer.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
//radiusServer.go, Radius Server Simulator
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/BurntSushi/toml"
"layeh.com/radius"
"layeh.com/radius/rfc2865"
)
type tomlConfig struct {
Title string `toml:"title"`
Enable enable `toml:"enable"`
Server server `toml:"server"`
User user `toml:"user"`
}
type enable struct {
LogEn bool `toml:"logAntoGenEnabled"`
AccEn bool `toml:"accountingEnabled"`
}
type server struct {
Ip string `toml:"ip"`
AutPort int `toml:"autPort"`
AccPort int `toml:"accPort"`
AutSecret string `toml:"autSecret"`
AccSecret string `toml:"accSecret"`
}
type user struct {
Allowlist [][2]string `toml:"allowlist"`
Blocklist [][2]string `toml:"blocklist"`
}
var applName string
var config tomlConfig
var err error
var buf bytes.Buffer
var fileSet string
var fileLog string
var autServer radius.PacketServer
var accServer radius.PacketServer
func init() {
strApp := path.Base(os.Args[0])
strExt := path.Ext(strApp)
applName = strings.TrimSuffix(strApp, strExt)
fileSet = applName + ".settings"
if _, err = os.Stat(fileSet); os.IsNotExist(err) {
data := []byte(strDefaultSettings)
ioutil.WriteFile(fileSet, data, 0644)
}
}
func main() {
if _, err = toml.DecodeFile(fileSet, &config); err != nil {
log.Println(err)
return
}
strTime := time.Now().Format(time.RFC3339)
buf.WriteString(fmt.Sprintln("Application :", applName))
buf.WriteString(fmt.Sprintln("Configuration:", fileSet))
buf.WriteString(fmt.Sprintln("Config Title :", config.Title))
buf.WriteString(fmt.Sprintln("Date Time :", strTime, "LIHUI"))
buf.WriteString("\n")
executeSettings()
fmt.Printf(buf.String())
if config.Enable.LogEn {
strTmp := strings.Replace(strTime, ":", "", -1)
fileLog = applName +"_" + strTmp + ".log"
ioutil.WriteFile(fileLog, buf.Bytes(), 0644)
}
if err = autServer.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func executeSettings() {
handler1 := func(w radius.ResponseWriter, r *radius.Request) {
usr := rfc2865.UserName_GetString(r.Packet)
pwd := rfc2865.UserPassword_GetString(r.Packet)
var code radius.Code = radius.CodeAccessReject
for _, v := range config.User.Allowlist {
if usr == v[0] && pwd == v[1] {
code = radius.CodeAccessAccept
}
}
log.Printf("Writing %v to %v %v %v", code, r.RemoteAddr, usr, pwd)
w.Write(r.Response(code))
}
autServer.Addr = config.Server.Ip + ":" +strconv.Itoa(config.Server.AutPort)
autServer.SecretSource = radius.StaticSecretSource([]byte(config.Server.AutSecret))
autServer.Handler = radius.HandlerFunc(handler1)
buf.WriteString(fmt.Sprintln("Authen radius server listening on:",autServer.Addr))
buf.WriteString(fmt.Sprintln("Shared secret:", config.Server.AutSecret))
if config.Enable.AccEn {
}
}
const strDefaultSettings =
`title = "Default Settings for Radius Server Simulator"
[enable]
logAntoGenEnabled = false
accountingEnabled = false
[server]
ip = "0.0.0.0"
autPort = 1812
accPort = 1813
autSecret = "secret1234"
accSecret = "secret5678"
[user]
allowlist = [["peter", "Password1234"],
["lihui", "Password5678"]]
blocklist = [["PETER", "Password1234"],
["lihui", "Password5678"]]
`