Skip to content

Commit

Permalink
add example server code
Browse files Browse the repository at this point in the history
Fixes layeh#37
  • Loading branch information
Tim Cooper committed Sep 26, 2018
1 parent 639444c commit 7f0e5b5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package main

import (
"context"
"fmt"
"log"

"layeh.com/radius"
. "layeh.com/radius/rfc2865"
Expand All @@ -30,10 +30,49 @@ func main() {
UserPassword_SetString(packet, "12345")
response, err := radius.Exchange(context.Background(), packet, "localhost:1812")
if err != nil {
panic(err)
log.Fatal(err)
}

fmt.Println("Code:", response.Code)
log.Println("Code:", response.Code)
}
```

## Server example

```go
package main

import (
"log"

"layeh.com/radius"
. "layeh.com/radius/rfc2865"
)

func main() {
handler := func(w radius.ResponseWriter, r *radius.Request) {
username := UserName_GetString(r.Packet)
password := UserPassword_GetString(r.Packet)

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))
}

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)
}
}
```

Expand Down
6 changes: 3 additions & 3 deletions client-example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package radius_test

import (
"context"
"fmt"
"log"

"layeh.com/radius"
. "layeh.com/radius/rfc2865"
Expand All @@ -19,8 +19,8 @@ func Example_client() {
UserPassword_SetString(packet, Password)
response, err := radius.Exchange(context.Background(), packet, "localhost:1812")
if err != nil {
panic(err)
log.Fatal(err)
}

fmt.Println("Code:", response.Code)
log.Println("Code:", response.Code)
}
39 changes: 39 additions & 0 deletions server-example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package radius_test

import (
"log"

"layeh.com/radius"
. "layeh.com/radius/rfc2865"
)

var (
Username = "tim"
Password = "12345"
)

func Example_packetServer() {
handler := func(w radius.ResponseWriter, r *radius.Request) {
username := UserName_GetString(r.Packet)
password := UserPassword_GetString(r.Packet)

var code radius.Code
if username == Username && password == Password {
code = radius.CodeAccessAccept
} else {
code = radius.CodeAccessReject
}
log.Printf("Writing %v to %v", code, r.RemoteAddr)
w.Write(r.Response(code))
}

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)
}
}

0 comments on commit 7f0e5b5

Please sign in to comment.