-
Notifications
You must be signed in to change notification settings - Fork 0
/
radiusTool.go
130 lines (106 loc) · 2.8 KB
/
radiusTool.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
//radiusTool.go, Radius Client Tool
package main
import (
"bytes"
"context"
"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"`
Port int `toml:"port"`
Secret string `toml:"secret"`
}
type user struct {
Usr string `toml:"usr"`
Pwd string `toml:"pwd"`
}
var applName string
var config tomlConfig
var err error
var buf bytes.Buffer
var fileSet string
var fileLog string
var remote string
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")
remote = config.Server.Ip + ":" + strconv.Itoa(config.Server.Port)
buf.WriteString(fmt.Sprintln("Remote:", remote))
buf.WriteString(fmt.Sprintln("Secret:", config.Server.Secret))
buf.WriteString(fmt.Sprintln("User:", config.User.Usr))
buf.WriteString(fmt.Sprintln("Pass:", config.User.Pwd))
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)
}
}
func executeSettings() {
if config.Enable.AccEn {
}
packet := radius.New(radius.CodeAccessRequest, []byte(config.Server.Secret))
rfc2865.UserName_SetString(packet, config.User.Usr)
rfc2865.UserPassword_SetString(packet, config.User.Pwd)
response, err := radius.Exchange(context.Background(), packet, remote)
if err == nil {
buf.WriteString(fmt.Sprintln("Code:", response.Code))
} else {
buf.WriteString(fmt.Sprintln("%v", err))
log.Printf("%v\n", err)
}
log.Println("Code:", response.Code)
}
const strDefaultSettings =
`title = "Default Settings for Radius Client Tool"
[enable]
logAntoGenEnabled = false
accountingEnabled = false
[server]
ip = "127.0.0.1"
port = 1812
secret = "secret1234"
[user]
usr = "peter"
pwd = "Password1234"
`