Skip to content

Commit

Permalink
add dsl (test)
Browse files Browse the repository at this point in the history
  • Loading branch information
3JoB committed Aug 28, 2023
1 parent 13a61be commit 255ebd1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
55 changes: 29 additions & 26 deletions analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ func (t *Teler) Analyze(c *atreugo.RequestCtx) error {
return err
}

func (t *Teler) setMmdb(c *atreugo.RequestCtx) (*ASN, *City) {
// Get the client's IP address
clientIP := t.env.GetRequestValue("IP")

// Check if the client's IP address is in the cache
if _, ok := t.getCache(clientIP); ok {
return nil, nil
}

if (t.opt.MaxMind != MaxMind{}) {
if t.opt.MaxMind.Install {
nip := net.ParseIP(clientIP)

var (
asn ASN
city City
)

t.threat.MaxM.City.Lookup(nip, &city)
t.threat.MaxM.ASN.Lookup(nip, &asn)
return &asn, &city
// They are temporary Debug methods and will be removed soon.
//t.log.Info().Any("ASN", asn).Msg("asn msg")
//t.log.Info().Any("City", city).Msg("city msg")
}
}
return nil, nil
}

/*
analyzeRequest checks an incoming HTTP request for certain types of threats or vulnerabilities.
If a threat is detected, the function returns an error and the request is stopped from continuing through the middleware chain.
Expand Down Expand Up @@ -117,32 +146,6 @@ func (t *Teler) checkCustomRules(c *atreugo.RequestCtx) error {
uri := t.env.GetRequestValue("URI")
body := t.env.GetRequestValue("Body")

// Get the client's IP address
clientIP := t.env.GetRequestValue("IP")

// Check if the client's IP address is in the cache
if err, ok := t.getCache(clientIP); ok {
return err
}

if (t.opt.MaxMind != MaxMind{}) {
if t.opt.MaxMind.Install {
nip := net.ParseIP(clientIP)

var (
asn ASN
city City
)

t.threat.MaxM.City.Lookup(nip, &city)
t.threat.MaxM.ASN.Lookup(nip, &asn)

// They are temporary Debug methods and will be removed soon.
t.log.Info().Any("ASN", asn).Msg("asn msg")
t.log.Info().Any("City", city).Msg("city msg")
}
}

// Check if the request is in cache
key := headers + uri + body
if err, ok := t.getCache(key); ok {
Expand Down
18 changes: 18 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,26 @@ func (t *Teler) setDSLRequestEnv(c *atreugo.RequestCtx) {
"Headers": headers,
"Body": body,
"Method": unsafeConvert.StringSlice(c.Method()),
"Remote": unsafeConvert.StringSlice(c.Request.Header.Peek("Remote-Host")),
"IP": realip.FromRequest(c),
}

if (t.opt.MaxMind != MaxMind{}) {
if t.opt.MaxMind.Install {
asn, city := t.setMmdb(c)
if city != nil {
t.env.Requests["DB"] = map[string]any{
"City": city.City.Names,
"Country": city.Country,
"Continent": city.Continent,
"ASN": map[string]any{
"Code": asn.AutonomousSystemNumber,
"Org": asn.AutonomousSystemOrganization,
},
}
}
}
}
}

// headersToRawString converts a map of http.Header to
Expand Down

0 comments on commit 255ebd1

Please sign in to comment.