Skip to content

Commit d094385

Browse files
committed
Initial working version of pod
Signed-off-by: Matan Schatzman <[email protected]>
1 parent 32864e3 commit d094385

File tree

8 files changed

+530
-0
lines changed

8 files changed

+530
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Go workspace file
12+
go.work

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM golang:1.19
2+
3+
COPY . /app
4+
WORKDIR /app
5+
RUN go build -o kubevirt-performance-pod .
6+
7+
ENTRYPOINT [ "/app/kubevirt-performance-pod"]

go.mod

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module github.com/kubevirt-ui/kubevirt-proxy-data
2+
3+
go 1.20
4+
5+
require (
6+
github.com/gin-gonic/gin v1.9.0
7+
github.com/gorilla/websocket v1.5.0
8+
)
9+
10+
require (
11+
github.com/bytedance/sonic v1.8.0 // indirect
12+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
13+
github.com/gin-contrib/sse v0.1.0 // indirect
14+
github.com/go-playground/locales v0.14.1 // indirect
15+
github.com/go-playground/universal-translator v0.18.1 // indirect
16+
github.com/go-playground/validator/v10 v10.11.2 // indirect
17+
github.com/goccy/go-json v0.10.0 // indirect
18+
github.com/json-iterator/go v1.1.12 // indirect
19+
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
20+
github.com/leodido/go-urn v1.2.1 // indirect
21+
github.com/mattn/go-isatty v0.0.17 // indirect
22+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
23+
github.com/modern-go/reflect2 v1.0.2 // indirect
24+
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
25+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
26+
github.com/ugorji/go/codec v1.2.9 // indirect
27+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
28+
golang.org/x/crypto v0.5.0 // indirect
29+
golang.org/x/net v0.7.0 // indirect
30+
golang.org/x/sys v0.5.0 // indirect
31+
golang.org/x/text v0.7.0 // indirect
32+
google.golang.org/protobuf v1.28.1 // indirect
33+
gopkg.in/yaml.v3 v3.0.1 // indirect
34+
)

go.sum

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
2+
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
3+
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
4+
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
5+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
6+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
7+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
11+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
12+
github.com/gin-gonic/gin v1.9.0 h1:OjyFBKICoexlu99ctXNR2gg+c5pKrKMuyjgARg9qeY8=
13+
github.com/gin-gonic/gin v1.9.0/go.mod h1:W1Me9+hsUSyj3CePGrd1/QrKJMSJ1Tu/0hFEH89961k=
14+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
15+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
16+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
17+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
18+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
19+
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
20+
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
21+
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
22+
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
23+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
24+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
25+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
26+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
27+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
28+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
29+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
30+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
31+
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
32+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
33+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
34+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
35+
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
36+
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
37+
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
38+
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
39+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
40+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
41+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
42+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
43+
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
44+
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
45+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
47+
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
48+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
49+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
50+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
51+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
52+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
53+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
54+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
55+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
56+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
57+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
58+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
59+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
60+
github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU=
61+
github.com/ugorji/go/codec v1.2.9/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
62+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
63+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
64+
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
65+
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
66+
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
67+
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
68+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
69+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
70+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
71+
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
72+
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
73+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
74+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
75+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
76+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
77+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
78+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
79+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
80+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
81+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
82+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
83+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

handlers/handlers.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package handlers
2+
3+
import (
4+
"crypto/tls"
5+
"encoding/json"
6+
"io"
7+
"log"
8+
"net/http"
9+
10+
"github.com/gin-gonic/gin"
11+
"github.com/kubevirt-ui/kubevirt-proxy-data/proxy"
12+
)
13+
14+
var API_SERVER_URL string = "kubernetes.default.svc"
15+
var PROTOCOL string = "https"
16+
var ORIGIN = "http://localhost"
17+
18+
func HealthHandler(c *gin.Context) {
19+
c.String(200, "OK")
20+
}
21+
22+
func RequestHandler(c *gin.Context) {
23+
if c.Request.URL.Scheme == "" {
24+
c.Request.URL.Scheme = "https"
25+
}
26+
27+
if c.Request.URL.Host == "" {
28+
c.Request.URL.Host = API_SERVER_URL
29+
}
30+
31+
tlsConf := &tls.Config{InsecureSkipVerify: true}
32+
33+
proxy := &proxy.Proxy{
34+
Config: &proxy.Config{
35+
TLSClientConfig: tlsConf,
36+
Endpoint: c.Request.URL,
37+
Origin: ORIGIN,
38+
},
39+
}
40+
41+
c.Request.Header.Set("Origin", ORIGIN)
42+
43+
if c.IsWebsocket() {
44+
proxy.ServeHTTP(c.Writer, c.Request)
45+
return
46+
}
47+
48+
tr := &http.Transport{
49+
TLSClientConfig: tlsConf, // TODO: add a check for PROD / DEV mode
50+
}
51+
52+
cr := func(req *http.Request, via []*http.Request) error {
53+
return http.ErrUseLastResponse
54+
}
55+
56+
httpClient := http.Client{Transport: tr, CheckRedirect: cr}
57+
58+
c.Request.RequestURI = ""
59+
resp, err := httpClient.Do(c.Request)
60+
if err != nil {
61+
log.Println("Failed to initiate call to kube api server: ", err.Error())
62+
}
63+
64+
bodyBytes, err := io.ReadAll(resp.Body)
65+
66+
if err != nil {
67+
log.Println("Failed to read response body: ", err.Error())
68+
}
69+
70+
defer resp.Body.Close()
71+
bodyJson := map[string]interface{}{}
72+
err = json.Unmarshal(bodyBytes, &bodyJson)
73+
74+
if err != nil {
75+
log.Println("Unable to transform response body to json)")
76+
}
77+
78+
c.JSON(http.StatusOK, bodyJson)
79+
80+
}

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/kubevirt-ui/kubevirt-proxy-data/handlers"
8+
)
9+
10+
func main() {
11+
server := gin.Default()
12+
server.GET("/health", handlers.HealthHandler)
13+
server.GET("/apis/*path", handlers.RequestHandler)
14+
15+
log.Println("listening for server 8080")
16+
17+
err := server.Run() // listen and serve on 0.0.0.0:8080
18+
19+
if err != nil {
20+
log.Println("Failed to start server: ", err.Error())
21+
}
22+
}

proxy/proxy.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package proxy
2+
3+
import (
4+
"crypto/tls"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"net/url"
9+
"sync"
10+
11+
"github.com/gorilla/websocket"
12+
"github.com/kubevirt-ui/kubevirt-proxy-data/util"
13+
)
14+
15+
type Config struct {
16+
Endpoint *url.URL
17+
TLSClientConfig *tls.Config
18+
Origin string
19+
}
20+
21+
type Proxy struct {
22+
Config *Config
23+
}
24+
25+
func (p *Proxy) createUpgrader(subProtocol string) websocket.Upgrader {
26+
upgrader := &websocket.Upgrader{
27+
Subprotocols: []string{subProtocol},
28+
CheckOrigin: func(r *http.Request) bool {
29+
origin := r.Header["Origin"]
30+
if p.Config.Origin == "" {
31+
log.Printf("CheckOrigin: Proxy has no configured Origin. Allowing origin %v to %v", origin, r.URL)
32+
return true
33+
}
34+
if len(origin) == 0 {
35+
log.Printf("CheckOrigin: No origin header. Denying request to %v", r.URL)
36+
return false
37+
}
38+
if p.Config.Origin == origin[0] {
39+
return true
40+
}
41+
log.Printf("CheckOrigin '%v' != '%v'", p.Config.Origin, origin[0])
42+
return false
43+
},
44+
}
45+
return *upgrader
46+
}
47+
48+
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
49+
50+
// Block scripts from running in proxied content for browsers that support Content-Security-Policy.
51+
w.Header().Set("Content-Security-Policy", "sandbox;")
52+
// Add `X-Content-Security-Policy` for IE11 and older browsers.
53+
w.Header().Set("X-Content-Security-Policy", "sandbox;")
54+
55+
for _, h := range util.HeaderBlacklist {
56+
r.Header.Del(h)
57+
}
58+
59+
// Include `system:authenticated` when impersonating groups so that basic requests that all
60+
// users can run like self-subject access reviews work.
61+
if len(r.Header["Impersonate-Group"]) > 0 {
62+
r.Header.Add("Impersonate-Group", "system:authenticated")
63+
}
64+
65+
r.Host = p.Config.Endpoint.Host
66+
r.URL.Host = p.Config.Endpoint.Host
67+
r.URL.Scheme = p.Config.Endpoint.Scheme
68+
69+
if r.URL.Scheme == "https" {
70+
r.URL.Scheme = "wss"
71+
} else {
72+
r.URL.Scheme = "ws"
73+
}
74+
75+
proxiedHeader, subProtocol, err := util.CreateProxyHeaders(w, r)
76+
77+
if err != nil {
78+
log.Println("Proxy header failed to create: ", err)
79+
}
80+
81+
// NOTE (ericchiang): K8s might not enforce this but websockets requests are
82+
// required to supply an origin.
83+
dialer := &websocket.Dialer{
84+
TLSClientConfig: p.Config.TLSClientConfig,
85+
}
86+
87+
backend, resp, err := dialer.Dial(r.URL.String(), proxiedHeader)
88+
89+
if err != nil {
90+
errMsg := fmt.Sprintf("Failed to dial backend: '%v'", err)
91+
statusCode := http.StatusBadGateway
92+
if resp == nil || resp.StatusCode == 0 {
93+
log.Println(errMsg)
94+
} else {
95+
statusCode = resp.StatusCode
96+
if resp.Request == nil {
97+
log.Printf("%s Status: '%v' (no request object)", errMsg, resp.Status)
98+
} else {
99+
log.Printf("%s Status: '%v' URL: '%v' , r.URL: %v", errMsg, resp.Status, resp.Request.URL, r.URL.String())
100+
}
101+
}
102+
http.Error(w, errMsg, statusCode)
103+
return
104+
}
105+
106+
upgrader := p.createUpgrader(subProtocol)
107+
108+
frontend, err := upgrader.Upgrade(w, r, nil)
109+
if err != nil {
110+
log.Printf("Failed to upgrade websocket to client: '%v'", err)
111+
return
112+
}
113+
114+
var writeMutex sync.Mutex // Needed because ticker & copy are writing to frontend in separate goroutines
115+
116+
defer func() {
117+
backend.Close()
118+
frontend.Close()
119+
}()
120+
121+
errc := make(chan error, 2)
122+
123+
// Can't just use io.Copy here since browsers care about frame headers.
124+
go func() { errc <- util.CopyMsgs(nil, frontend, backend) }()
125+
go func() { errc <- util.CopyMsgs(&writeMutex, backend, frontend) }()
126+
go func() { errc <- util.KeepAlive(&writeMutex, frontend) }()
127+
128+
for {
129+
select {
130+
case <-errc:
131+
// Only wait for a single error and let the defers close both connections.
132+
return
133+
}
134+
}
135+
}

0 commit comments

Comments
 (0)