Files
portmaster/profile/endpoints/endpoint-country.go
2020-10-15 11:48:27 +02:00

49 lines
942 B
Go

package endpoints
import (
"context"
"regexp"
"strings"
"github.com/safing/portmaster/intel"
)
var (
countryRegex = regexp.MustCompile(`^[A-Z]{2}$`)
)
// EndpointCountry matches countries.
type EndpointCountry struct {
EndpointBase
Country string
}
// Matches checks whether the given entity matches this endpoint definition.
func (ep *EndpointCountry) Matches(ctx context.Context, entity *intel.Entity) (EPResult, Reason) {
country, ok := entity.GetCountry(ctx)
if !ok {
return Undeterminable, nil
}
if country == ep.Country {
return ep.match(ep, entity, country, "IP is located in")
}
return NoMatch, nil
}
func (ep *EndpointCountry) String() string {
return ep.renderPPP(ep.Country)
}
func parseTypeCountry(fields []string) (Endpoint, error) {
if countryRegex.MatchString(fields[1]) {
ep := &EndpointCountry{
Country: strings.ToUpper(fields[1]),
}
return ep.parsePPP(ep, fields)
}
return nil, nil
}