1
#pragma once
2

            
3
#include "envoy/config/typed_config.h"
4
#include "envoy/network/address.h"
5
#include "envoy/protobuf/message_validator.h"
6
#include "envoy/server/factory_context.h"
7

            
8
#include "absl/container/flat_hash_set.h"
9

            
10
namespace Envoy {
11
namespace Geolocation {
12

            
13
// Actual result of the lookup. Each entry in the map represents the geolocation header (entry key)
14
// for which the lookup was invoked mapped to a lookup result from the database.
15
using LookupResult = const absl::flat_hash_map<std::string, std::string>;
16

            
17
// Async callbacks used for geolocation provider lookups.
18
using LookupGeoHeadersCallback = std::function<void(LookupResult&&)>;
19

            
20
class LookupRequest {
21
public:
22
10
  LookupRequest() = default;
23
  LookupRequest(Network::Address::InstanceConstSharedPtr&& remote_address)
24
107
      : remote_address_(std::move(remote_address)) {};
25

            
26
106
  const Network::Address::InstanceConstSharedPtr remoteAddress() const { return remote_address_; }
27

            
28
private:
29
  Network::Address::InstanceConstSharedPtr remote_address_;
30
};
31

            
32
class Driver {
33
public:
34
117
  virtual ~Driver() = default;
35

            
36
  /**
37
   *  Performs asynchronous lookup in the geolocation database.
38
   *
39
   *  @param request containing geolocation headers to lookup in the database.
40
   *  @param cb supplies the filter callbacks to notify when lookup is complete.
41
   */
42
  virtual void lookup(LookupRequest&& request, LookupGeoHeadersCallback&& cb) const PURE;
43
};
44

            
45
using DriverSharedPtr = std::shared_ptr<Driver>;
46

            
47
/**
48
 * Implemented by each geolocation provider and registered via Registry::registerFactory() or the
49
 * convenience class RegisterFactory.
50
 */
51
class GeoipProviderFactory : public Config::TypedFactory {
52
public:
53
47
  ~GeoipProviderFactory() override = default;
54

            
55
  /**
56
   * Create a particular geolocation provider implementation. If the implementation is unable to
57
   * produce a geolocation provider with the provided parameters, it should throw an EnvoyException
58
   * in the case of general error or a Json::Exception if the json configuration is erroneous. The
59
   * returned pointer should always be valid.
60
   *
61
   *
62
   * @param config supplies the proto configuration for the geolocation provider
63
   * @param context supplies the factory context
64
   */
65
  virtual DriverSharedPtr
66
  createGeoipProviderDriver(const Protobuf::Message& config, const std::string& stat_prefix,
67
                            Server::Configuration::FactoryContext& context) PURE;
68

            
69
30
  std::string category() const override { return "envoy.geoip_providers"; }
70
};
71

            
72
} // namespace Geolocation
73
} // namespace Envoy