add ipinfo middleware
This commit is contained in:
parent
b26ef7439e
commit
6fd78737dc
5 changed files with 278 additions and 0 deletions
23
README.md
23
README.md
|
|
@ -49,3 +49,26 @@ import "git.juancwu.dev/juancwu/lightmux-contrib/recoverer"
|
|||
|
||||
mux.Use(recoverer.New())
|
||||
```
|
||||
|
||||
### `ipinfo`
|
||||
|
||||
Looks up the client IP against the [ipinfo.io](https://ipinfo.io) API via the [official Go SDK](https://github.com/ipinfo/go) and attaches the `*ipinfo.Core` result to the request context. Pair with `realip` upstream so the lookup uses the originating client IP rather than the proxy peer.
|
||||
|
||||
```go
|
||||
import (
|
||||
sdk "github.com/ipinfo/go/v2/ipinfo"
|
||||
"git.juancwu.dev/juancwu/lightmux-contrib/ipinfo"
|
||||
"git.juancwu.dev/juancwu/lightmux-contrib/realip"
|
||||
)
|
||||
|
||||
client := sdk.NewClient(nil, nil, "YOUR_TOKEN")
|
||||
mux.Use(realip.New(), ipinfo.New(client, nil))
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if info, ok := ipinfo.From(r.Context()); ok {
|
||||
fmt.Fprintf(w, "hello from %s, %s\n", info.City, info.Country)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Loopback, private, link-local, and unspecified addresses are skipped to preserve API quota. Lookup failures are logged at warn level via [splinter](https://git.juancwu.dev/juancwu/splinter) (pass `nil` for `splinter.Default()` resolved at request time, or supply a custom `*splinter.Logger`) and let the request through with no context value — handlers should treat the `From` lookup as optional.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue