Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC-web proxy #8077

Merged
merged 39 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
6aa0309
init
aleem1314 Dec 3, 2020
93a277f
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into al…
aleem1314 Dec 4, 2020
efaa0b4
WIP config
aleem1314 Dec 4, 2020
939eb79
WIP add proxy server
aleem1314 Dec 4, 2020
f4f98eb
fmt
aleem1314 Dec 4, 2020
e3b7ab1
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into al…
aleem1314 Dec 4, 2020
758b4bb
WIP
aleem1314 Dec 5, 2020
34c4eb6
setup proxy server
aleem1314 Dec 7, 2020
e737111
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into al…
aleem1314 Dec 7, 2020
8ff60e3
clean go.mod
aleem1314 Dec 7, 2020
d2cabcd
lint
aleem1314 Dec 7, 2020
a5d90bc
lint
aleem1314 Dec 7, 2020
dd6a000
lint
aleem1314 Dec 8, 2020
222d1cf
custom codec
aleem1314 Dec 10, 2020
a5e7dbf
lint
aleem1314 Dec 14, 2020
34cbea8
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into al…
aleem1314 Dec 14, 2020
e2e1704
add comments
aleem1314 Dec 15, 2020
3e1e495
change grpc-proxy port
aleem1314 Dec 15, 2020
95a71ac
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into al…
aleem1314 Dec 15, 2020
aaddeca
add grpc-web
aleem1314 Dec 16, 2020
2473fc8
Merge branch 'master' into aleem/7345-grpc-web-proxy
aleem1314 Dec 16, 2020
a29f2a4
update server/start.go
aleem1314 Dec 21, 2020
805aa20
Merge branch 'master' into aleem/7345-grpc-web-proxy
aleem1314 Dec 21, 2020
72f0ce1
add tests
aleem1314 Dec 22, 2020
7d3bfe0
Merge branch 'aleem/7345-grpc-web-proxy' of https://github.com/cosmos…
aleem1314 Dec 22, 2020
c074ece
Merge branch 'master' into aleem/7345-grpc-web-proxy
aleem1314 Dec 22, 2020
d57513d
add test with client
aleem1314 Dec 22, 2020
4ffc71b
Merge branch 'aleem/7345-grpc-web-proxy' of https://github.com/cosmos…
aleem1314 Dec 22, 2020
4debd21
Update server/start.go
aleem1314 Dec 23, 2020
72f4aeb
Merge branch 'master' into aleem/7345-grpc-web-proxy
aleem1314 Dec 23, 2020
b8bb16b
Update server/start.go
aleem1314 Jan 4, 2021
f3bc9c6
Update server/start.go
aleem1314 Jan 4, 2021
6e2f070
review changes
aleem1314 Jan 4, 2021
5468ca0
review changes
aleem1314 Jan 13, 2021
260276a
Merge branch 'master' into aleem/7345-grpc-web-proxy
anilcse Jan 13, 2021
9ba7192
Merge branch 'master' into aleem/7345-grpc-web-proxy
alexanderbez Jan 14, 2021
24e8f9e
Update server/start.go
alexanderbez Jan 14, 2021
8b14c71
Merge branch 'master' into aleem/7345-grpc-web-proxy
mergify[bot] Jan 18, 2021
c5e7fc6
Merge branch 'master' into aleem/7345-grpc-web-proxy
mergify[bot] Jan 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/iavl v0.15.0-rc5
github.com/cosmos/ledger-cosmos-go v0.11.1
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/enigmampc/btcutil v1.0.3-0.20200723161021-e2fb6adb2a25
Expand All @@ -26,8 +27,11 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/golang-lru v0.5.4
github.com/improbable-eng/grpc-web v0.13.0
github.com/magiconair/properties v1.8.4
github.com/mattn/go-isatty v0.0.12
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f
github.com/mwitkow/grpc-proxy v0.0.0-20181017164139-0f1106ef9c76
github.com/otiai10/copy v1.2.0
github.com/pelletier/go-toml v1.8.0 // indirect
github.com/pkg/errors v0.9.1
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f h1:U5y3Y5UE0w7amNe7Z5G/twsBW0KEalRQXZzf8ufSh9I=
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f/go.mod h1:xH/i4TFMt8koVQZ6WFms69WAsDWr2XsYL3Hkl7jkoLE=
github.com/dgraph-io/badger/v2 v2.2007.1/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
github.com/dgraph-io/badger/v2 v2.2007.2 h1:EjjK0KqwaFMlPin1ajhP943VPENHJdEz1KLIegjaI3k=
github.com/dgraph-io/badger/v2 v2.2007.2/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
Expand Down Expand Up @@ -250,6 +252,7 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de
github.com/grpc-ecosystem/go-grpc-middleware v1.2.1/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 h1:FlFbCRLd5Jr4iYXZufAvgWN6Ao0JrI5chLINnUXDDr0=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.8.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
Expand Down Expand Up @@ -296,6 +299,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
github.com/improbable-eng/grpc-web v0.13.0 h1:7XqtaBWaOCH0cVGKHyvhtcuo6fgW32Y10yRKrDHFHOc=
github.com/improbable-eng/grpc-web v0.13.0/go.mod h1:6hRR09jOEG81ADP5wCQju1z71g6OL4eEvELdran/3cs=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
Expand All @@ -305,6 +310,7 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht
github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U=
github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand All @@ -324,6 +330,7 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/kkdai/bstream v1.0.0/go.mod h1:FDnDOHt5Yx4p3FaHcioFT0QjDOtgUpvjeZqAs+NVZZA=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -369,7 +376,10 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
github.com/mtibben/percent v0.2.1 h1:5gssi8Nqo8QU/r2pynCm+hBQHpkB/uNK7BJCFogWdzs=
github.com/mtibben/percent v0.2.1/go.mod h1:KG9uO+SZkUp+VkRHsCdYQV3XSZrrSpR3O9ibNBTZrns=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/grpc-proxy v0.0.0-20181017164139-0f1106ef9c76 h1:0xuRacu/Zr+jX+KyLLPPktbwXqyOvnOPUQmMLzX1jxU=
github.com/mwitkow/grpc-proxy v0.0.0-20181017164139-0f1106ef9c76/go.mod h1:x5OoJHDHqxHS801UIuhqGl6QdSAEJvtausosHSdazIo=
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
github.com/nats-io/nats-server/v2 v2.1.2/go.mod h1:Afk+wRZqkMQs/p45uXdrVLuab3gwv3Z8C4HTBu8GD/k=
Expand Down Expand Up @@ -495,6 +505,7 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
Expand Down
51 changes: 51 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const (

// DefaultGRPCAddress is the default address the gRPC server binds to.
DefaultGRPCAddress = "0.0.0.0:9090"

// DefaultGRPCAddress is the address to bind the server to.
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
DefaultBindAddress = "0.0.0.0"
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved

// TCP port to listen on for HTTP1.1 calls.
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
DefaultGRPCProxyPort = 9091
)

// BaseConfig defines the server's basic configuration
Expand Down Expand Up @@ -105,6 +111,33 @@ type GRPCConfig struct {

// Address defines the API server to listen on
Address string `mapstructure:"address"`

// GRPCWebProxy defines the list of flags for grpc-proxy.
GRPCWebProxy GRPCProxy `mapstructure:"grpc-proxy"`
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
}

// GRPProxy defines configuration for the gRPC server.
type GRPCProxy struct {
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
// Enable defines if the proxy should be enabled.
Enable bool `mapstructure:"enable"`

// EnableHTTPServer defines if the HTTP should be enabled.
EnableHTTPServer bool `mapstructure:"enable-http-server"`

// AllowAllOrigins defines allow requests from any origin.
AllowAllOrigins bool `mapstructure:"allow-all-origins"`

// HTTPPort defines TCP port to listen on for HTTP1.1 debug calls.
HTTPPort int `mapstructure:"http-port"`

// BindAddress defines address to bind the server to.
BindAddress string `mapstructure:"bind-address"`

// AllowedOrigins defines list of origin URLs which are allowed to make cross-origin requests.
AllowedOrigins []string `mapstructure:"allowed-origins"`

// AllowedHeaders defines list of headers which are allowed to propagate to the gRPC backend.
AllowedHeaders []string `mapstructure:"allowed-headers"`
}

// StateSyncConfig defines the state sync snapshot configuration.
Expand Down Expand Up @@ -184,6 +217,15 @@ func DefaultConfig() *Config {
GRPC: GRPCConfig{
Enable: true,
Address: DefaultGRPCAddress,
GRPCWebProxy: GRPCProxy{
Enable: true,
AllowAllOrigins: true,
EnableHTTPServer: true,
AllowedOrigins: []string{DefaultGRPCAddress},
AllowedHeaders: make([]string, 0),
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
BindAddress: DefaultBindAddress,
HTTPPort: DefaultGRPCProxyPort,
},
},
StateSync: StateSyncConfig{
SnapshotInterval: 0,
Expand Down Expand Up @@ -238,6 +280,15 @@ func GetConfig(v *viper.Viper) Config {
GRPC: GRPCConfig{
Enable: v.GetBool("grpc.enable"),
Address: v.GetString("grpc.address"),
GRPCWebProxy: GRPCProxy{
Enable: v.GetBool("grpc.grpc-proxy.enable"),
AllowAllOrigins: v.GetBool("grpc.grpc-proxy.allow-all-origins"),
AllowedHeaders: v.GetStringSlice("grpc.grpc-proxy.allowed-headers"),
AllowedOrigins: v.GetStringSlice("grpc.grpc-proxy.allowed-origins"),
EnableHTTPServer: v.GetBool("grpc.grpc-proxy.enable-http-server"),
BindAddress: v.GetString("grpc.grpc-proxy.bind-address"),
HTTPPort: v.GetInt("grpc.grpc-proxy.http-port"),
},
},
StateSync: StateSyncConfig{
SnapshotInterval: v.GetUint64("state-sync.snapshot-interval"),
Expand Down
22 changes: 22 additions & 0 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ enable = {{ .GRPC.Enable }}
# Address defines the gRPC server address to bind to.
address = "{{ .GRPC.Address }}"

[grpc.grpc-proxy]
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
# GRPCWebProxyEnable defines if the gRPC-web-proxy should be enabled.
enable = {{ .GRPC.GRPCWebProxy.Enable }}

# BindAddress defines address to bind the server to.
bind-address = "{{ .GRPC.GRPCWebProxy.BindAddress }}"

# HTTPPort defines TCP port to listen on for HTTP1.1 debug calls.
http-port = {{ .GRPC.GRPCWebProxy.HTTPPort }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to bind to the same port as the grpc-gateway rest server?


# AllowAllOrigins defines allow requests from any origin.
allow-all-origins = {{ .GRPC.GRPCWebProxy.AllowAllOrigins }}

# AllowedOrigins defines list of origin URLs which are allowed to make cross-origin requests.
allowed-origins = [{{ range $k, $v := .GRPC.GRPCWebProxy.AllowedOrigins }} "{{ $v }}", {{ end }}]

# AllowedHeaders defines list of headers which are allowed to propagate to the gRPC backend.
allowed-headers = [{{ range $k, $v := .GRPC.GRPCWebProxy.AllowedHeaders }} "{{ $v }}", {{ end }}]

# EnableHTTPServer defines if the HTTP should be enabled.
enable-http-server = {{ .GRPC.GRPCWebProxy.EnableHTTPServer }}
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved

###############################################################################
### State Sync Configuration ###
###############################################################################
Expand Down
171 changes: 171 additions & 0 deletions server/grpc/grpcweb_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package grpc

import (
"context"
"fmt"
"net"
"net/http"
"time"

"github.com/cosmos/cosmos-sdk/server/config"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/mwitkow/go-conntrack"
"github.com/mwitkow/grpc-proxy/proxy"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

var (
flagHTTPMaxReadTimeout = 10 * time.Second
flagHTTPMaxWriteTimeout = 10 * time.Second
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
)

type allowedOrigins struct {
origins map[string]struct{}
}

// StartGRPCProxyServer starts a gRPC-proxy server on the given config.
// https://github.com/improbable-eng/grpc-web/tree/master/go/grpcwebproxy used as a reference.
func StartGRPCProxyServer(grpcConfig config.GRPCConfig) (*http.Server, error) {
proxyFlags := grpcConfig.GRPCWebProxy

grpcSrv, err := buildGrpcProxyServer(grpcConfig.Address)
if err != nil {
return nil, err
}

allowedOrigins := makeAllowedOrigins(proxyFlags.AllowedOrigins)

options := []grpcweb.Option{
grpcweb.WithCorsForRegisteredEndpointsOnly(false),
grpcweb.WithOriginFunc(makeHTTPOriginFunc(allowedOrigins, proxyFlags.AllowAllOrigins)),
}

wrappedGrpc := grpcweb.WrapServer(grpcSrv, options...)

if !proxyFlags.EnableHTTPServer {
return nil, fmt.Errorf("run_http_server is set to false. enable for grpcweb proxy to function correctly")
}

proxyServer := buildServer(wrappedGrpc)
listener, err := buildListenerOrFail("http", proxyFlags.BindAddress, proxyFlags.HTTPPort)

if err != nil {
return nil, err
}
errCh := make(chan error)

go func() {
err = proxyServer.Serve(listener)
if err != nil {
errCh <- fmt.Errorf("failed to serve: %w", err)
}
}()

select {
case err := <-errCh:
return nil, err
case <-time.After(5 * time.Second): // assume server started successfully
return proxyServer, nil
}

}

// nolint:interfacer
func buildServer(wrappedGrpc *grpcweb.WrappedGrpcServer) *http.Server {
return &http.Server{
ReadTimeout: flagHTTPMaxReadTimeout,
WriteTimeout: flagHTTPMaxWriteTimeout,
Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
wrappedGrpc.ServeHTTP(resp, req)
}),
}
}

func buildGrpcProxyServer(host string) (*grpc.Server, error) {
backendConn, err := dialBackendOrFail(host)
if err != nil {
return nil, err
}

director := func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
md, _ := metadata.FromIncomingContext(ctx)
outCtx, _ := context.WithCancel(ctx) //nolint:govet
mdCopy := md.Copy()
delete(mdCopy, "user-agent")
// If this header is present in the request from the web client,
// the actual connection to the backend will not be established.
// https://github.com/improbable-eng/grpc-web/issues/568
delete(mdCopy, "connection")
outCtx = metadata.NewOutgoingContext(outCtx, mdCopy)
return outCtx, backendConn, nil
}
// TODO: handle grpc.CustomCodec is deprecated. Waiting on https://github.com/mwitkow/grpc-proxy/pull/36.
// nolint:staticcheck
return grpc.NewServer(
grpc.CustomCodec(proxy.Codec()), // needed for proxy to function.
grpc.UnknownServiceHandler(proxy.TransparentHandler(director)),
grpc_middleware.WithUnaryServerChain(),
grpc_middleware.WithStreamServerChain(),
), nil
}

func buildListenerOrFail(name string, host string, port int) (net.Listener, error) {
addr := fmt.Sprintf("%s:%d", host, port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, fmt.Errorf("failed listening for '%v' on %v: %v", name, port, err)
}

return conntrack.NewListener(listener,
conntrack.TrackWithName(name),
conntrack.TrackWithTcpKeepAlive(20*time.Second),
conntrack.TrackWithTracing(),
), nil
}

func makeHTTPOriginFunc(allowedOrigins *allowedOrigins, allowAllOrigins bool) func(origin string) bool {
if allowAllOrigins {
return func(origin string) bool {
return true
}
}

return allowedOrigins.IsAllowed
}

func makeAllowedOrigins(origins []string) *allowedOrigins {
o := map[string]struct{}{}
for _, allowedOrigin := range origins {
o[allowedOrigin] = struct{}{}
}

return &allowedOrigins{
origins: o,
}
}

func (a *allowedOrigins) IsAllowed(origin string) bool {
_, ok := a.origins[origin]
return ok
}

func dialBackendOrFail(host string) (*grpc.ClientConn, error) {
if host == "" {
return nil, fmt.Errorf("host cannot be empty")
}

opt := []grpc.DialOption{
grpc.WithInsecure(),
}
// TODO: handle grpc.WithCodec is deprecated. Waiting on https://github.com/mwitkow/grpc-proxy/pull/36
// nolint:staticcheck
opt = append(opt, grpc.WithCodec(proxy.Codec()))

cc, err := grpc.Dial(host, opt...)
if err != nil {
return nil, fmt.Errorf("failed dialing backend: %v", err)
}
return cc, nil
}
Loading