mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-05 16:28:17 +00:00
40 lines
733 B
Go
40 lines
733 B
Go
package thoth
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
iptoasnv1 "github.com/TecharoHQ/thoth-proto/gen/techaro/thoth/iptoasn/v1"
|
|
)
|
|
|
|
type ASNChecker struct {
|
|
iptoasn iptoasnv1.IpToASNServiceClient
|
|
asns map[uint32]struct{}
|
|
hash string
|
|
}
|
|
|
|
func (asnc *ASNChecker) Check(r *http.Request) (bool, error) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 500*time.Millisecond)
|
|
defer cancel()
|
|
|
|
ipInfo, err := asnc.iptoasn.Lookup(ctx, &iptoasnv1.LookupRequest{
|
|
IpAddress: r.Header.Get("X-Real-Ip"),
|
|
})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if !ipInfo.GetAnnounced() {
|
|
return false, nil
|
|
}
|
|
|
|
_, ok := asnc.asns[uint32(ipInfo.GetAsNumber())]
|
|
|
|
return ok, nil
|
|
}
|
|
|
|
func (asnc *ASNChecker) Hash() string {
|
|
return asnc.hash
|
|
}
|