Search code examples
httptcpluaenvoyproxy

How to make proxy http to tcp to send only raw "body" using envoy?


I want to send http request to envoy and after that envoy send only body to tcp server

My steps:

  • Create tcp listen port nc -l 13370
  • deploy envoy in docker on same host (:10000 port)
  • Execute command curl -d "Hello" -X POST localhost:10000 I use this envoy settings
static_resources:

  listeners:
    - name: listener_0
      address:
        socket_address:
          address: 0.0.0.0
          port_value: 10000
      filter_chains:
        - filters:
            - name: envoy.filters.network.http_connection_manager
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
                stat_prefix: ingress_http
                access_log:
                  - name: envoy.access_loggers.stdout
                    typed_config:
                      "@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
                http_filters:
                  - name: lua_filter_cutting_head
                    typed_config:
                      "@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
                      default_source_code:
                        inline_string:
                          function envoy_on_request(request_handle)
                            request_handle:headers():remove("host")
                            request_handle:headers():remove("user-agent")
                            request_handle:headers():remove("x-envoy-expected-rq-timeout-ms")
                          end
                  - name: envoy.filters.http.router
                    typed_config:
                      "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
                route_config:
                  name: local_route
                  virtual_hosts:
                    - name: local_service
                      domains: ["*"]
                      routes:
                        - match:
                            prefix: "/"
                          route:
                            cluster: cluster_0

  clusters:
    - name: cluster_0
      type: STATIC
      dns_lookup_family: V4_ONLY
      load_assignment:
        cluster_name: cluster_0
        endpoints:
          - lb_endpoints:
              - endpoint:
                  address:
                    socket_address:
                      address: 192.168.0.23
                      port_value: 13370

Client --(http)-- envoy --(tcp)-- tcp_server (receive only "Hello)

So i want to see on my tcp_server only "Hello", but i see

POST / HTTP/1.1
accept: */*
content-length: 5
content-type: application/x-www-form-urlencoded
x-forwarded-proto: http
x-request-id: 8cc7670c-faf5-4ca8-aa97-b3674d81c28a
x-envoy-expected-rq-timeout-ms: 15000

Hello

Solution

  • Working config

    admin:
      address:
        socket_address:
          protocol: TCP
          address: 127.0.0.1
          port_value: 9902
    static_resources:
      listeners:
        - name: listener_0
          address:
            socket_address:
              address: 0.0.0.0
              port_value: 10000
          filter_chains:
            - filters:
                - name: envoy.filters.network.http_connection_manager
                  typed_config:
                    "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
                    stat_prefix: ingress_http
                    route_config:
                      name: local_route
                      virtual_hosts:
                        - name: local_service
                          domains:
                            - "*"
                          routes:
                            - match:
                                prefix: "/"
                                headers:
                                  - name: ":method"
                                    string_match:
                                      exact: "POST"
                              route:
                                cluster: service
                                upgrade_configs:
                                  - upgrade_type: CONNECT
                                    connect_config:
                                      allow_post: true
                    http_filters:
                      - name: envoy.filters.http.router
                        typed_config:
                          "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
                    http2_protocol_options:
                      allow_connect: true
      clusters:
        - name: service
          connect_timeout: 0.25s
          type: LOGICAL_DNS
          dns_lookup_family: V4_ONLY
          lb_policy: ROUND_ROBIN
          load_assignment:
            cluster_name: service
            endpoints:
              - lb_endpoints:
                  - endpoint:
                      address:
                        socket_address:
                          address: 192.168.0.31
                          port_value: 13370
        ```