2018-07-09 16:41:21 +00:00
< img src = "internal/radius.svg" width = "250" align = "right" >
2018-10-03 16:55:26 +00:00
# radius
2017-02-07 21:04:27 +00:00
a Go (golang) [RADIUS ](https://tools.ietf.org/html/rfc2865 ) client and server implementation
2018-10-03 16:55:26 +00:00
[![GoDoc ](https://godoc.org/layeh.com/radius?status.svg )](https://godoc.org/layeh.com/radius)
[![CircleCI ](https://circleci.com/gh/layeh/radius/tree/master.svg?style=shield )](https://circleci.com/gh/layeh/radius/tree/master)
2017-02-07 21:04:27 +00:00
## Installation
go get -u layeh.com/radius
2017-09-05 22:06:47 +00:00
## Client example
```go
package main
import (
"context"
2018-10-03 16:55:26 +00:00
"log"
2017-09-05 22:06:47 +00:00
"layeh.com/radius"
2019-04-12 15:51:37 +00:00
"layeh.com/radius/rfc2865"
2017-09-05 22:06:47 +00:00
)
func main() {
packet := radius.New(radius.CodeAccessRequest, []byte(`secret`))
2019-04-12 15:51:37 +00:00
rfc2865.UserName_SetString(packet, "tim")
rfc2865.UserPassword_SetString(packet, "12345")
2017-09-05 22:06:47 +00:00
response, err := radius.Exchange(context.Background(), packet, "localhost:1812")
if err != nil {
2018-10-03 16:55:26 +00:00
log.Fatal(err)
}
log.Println("Code:", response.Code)
}
```
## Server example
```go
package main
import (
"log"
"layeh.com/radius"
2019-04-12 15:51:37 +00:00
"layeh.com/radius/rfc2865"
2018-10-03 16:55:26 +00:00
)
func main() {
handler := func(w radius.ResponseWriter, r *radius.Request) {
2019-04-12 15:51:37 +00:00
username := rfc2865.UserName_GetString(r.Packet)
password := rfc2865.UserPassword_GetString(r.Packet)
2018-10-03 16:55:26 +00:00
var code radius.Code
if username == "tim" & & password == "12345" {
code = radius.CodeAccessAccept
} else {
code = radius.CodeAccessReject
}
log.Printf("Writing %v to %v", code, r.RemoteAddr)
w.Write(r.Response(code))
2017-09-05 22:06:47 +00:00
}
2018-10-03 16:55:26 +00:00
server := radius.PacketServer{
Handler: radius.HandlerFunc(handler),
SecretSource: radius.StaticSecretSource([]byte(`secret`)),
}
log.Printf("Starting server on :1812")
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
2017-09-05 22:06:47 +00:00
}
```
## RADIUS Dictionaries
Included in this package is the command line program `radius-dict-gen` . It can be installed with:
go get -u layeh.com/radius/cmd/radius-dict-gen
2018-02-11 00:29:52 +00:00
Given a FreeRADIUS dictionary, the program will generate helper functions and types for reading and manipulating RADIUS attributes in a packet. It is recommended that generated code be used for any RADIUS dictionary you would like to consume.
2017-09-05 22:06:47 +00:00
Included in this repository are sub-packages of generated helpers for commonly used RADIUS attributes, including [`rfc2865` ](https://godoc.org/layeh.com/radius/rfc2865 ) and [`rfc2866` ](https://godoc.org/layeh.com/radius/rfc2866 ).
2017-02-07 21:04:27 +00:00
## License
MPL 2.0
2018-07-06 16:09:34 +00:00
## Author
Tim Cooper (< tim.cooper @ layeh . com > )