1
#pragma once
2

            
3
#include <chrono>
4
#include <cstdint>
5
#include <memory>
6
#include <string>
7
#include <vector>
8

            
9
#include "envoy/buffer/buffer.h"
10
#include "envoy/common/random_generator.h"
11
#include "envoy/common/time.h"
12
#include "envoy/event/dispatcher.h"
13
#include "envoy/http/request_id_extension.h"
14
#include "envoy/server/factory_context.h"
15
#include "envoy/stats/stats_macros.h" // IWYU pragma: keep
16

            
17
#include "source/common/common/logger.h"
18
#include "source/common/protobuf/protobuf.h" // IWYU pragma: keep
19

            
20
#include "absl/strings/string_view.h"
21
#include "cilium/accesslog.h"
22
#include "cilium/api/accesslog.pb.h"
23
#include "cilium/api/websocket.pb.h"
24

            
25
namespace Envoy {
26
namespace Cilium {
27
namespace WebSocket {
28

            
29
/**
30
 * All WebSocket filter stats. @see stats_macros.h
31
 */
32
// clang-format off
33
#define ALL_WEBSOCKET_STATS(COUNTER)		\
34
24
  COUNTER(access_denied)			\
35
24
  COUNTER(protocol_error)			\
36
24
  COUNTER(handshake_timeout)			\
37
24
  COUNTER(handshake_not_http)			\
38
24
  COUNTER(handshake_too_large)			\
39
24
  COUNTER(handshake_parse_error)      		\
40
24
  COUNTER(handshake_invalid_http_version)	\
41
24
  COUNTER(handshake_invalid_http_status)	\
42
24
  COUNTER(handshake_invalid_websocket_request)	\
43
24
  COUNTER(handshake_invalid_websocket_response)	\
44
24
  COUNTER(handshake_write_error)		\
45
24
  COUNTER(ping_sent_count)                                                                                \
46
// clang-format on
47

            
48
/**
49
 * Struct definition for all WebSocket filter stats. @see stats_macros.h
50
 */
51
struct FilterStats {
52
  ALL_WEBSOCKET_STATS(GENERATE_COUNTER_STRUCT)
53
};
54

            
55
/**
56
 * Per listener configuration for Cilium HTTP filter. This
57
 * is accessed by multiple working thread instances of the filter.
58
 */
59
class Config : public Logger::Loggable<Logger::Id::config> {
60
public:
61
  Config(Server::Configuration::FactoryContext& context, bool client,
62
         const std::string& access_log_path, const std::string& host, const std::string& path,
63
         const std::string& key, const std::string& version, const std::string& origin,
64
         const ProtobufWkt::Duration& handshake_timeout, const ProtobufWkt::Duration& ping_interval,
65
         bool ping_when_idle);
66
  Config(const ::cilium::WebSocketClient& config, Server::Configuration::FactoryContext& context);
67
  Config(const ::cilium::WebSocketServer& config, Server::Configuration::FactoryContext& context);
68

            
69
  static std::string keyResponse(absl::string_view key);
70

            
71
  void log(Cilium::AccessLog::Entry&, ::cilium::EntryType);
72

            
73
  TimeSource& time_source_;
74
  Event::Dispatcher& dispatcher_;
75
  FilterStats stats_;
76
  Random::RandomGenerator& random_;
77
  Http::RequestIDExtensionSharedPtr request_id_extension_;
78
  bool client_;
79

            
80
  std::string host_;
81
  std::string path_;
82
  std::string key_;
83
  std::string version_;
84
  std::string origin_;
85
  std::chrono::milliseconds handshake_timeout_;
86
  std::chrono::milliseconds ping_interval_;
87
  bool ping_when_idle_;
88

            
89
  static std::vector<uint8_t> getSha1Digest(const Buffer::Instance&);
90

            
91
private:
92
  Cilium::AccessLogSharedPtr access_log_;
93
};
94

            
95
using ConfigSharedPtr = std::shared_ptr<Config>;
96

            
97
} // namespace WebSocket
98
} // namespace Cilium
99
} // namespace Envoy