Skip to content

Commit

Permalink
Fix sending headers server->client (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doridian authored Mar 23, 2023
1 parent e6361d2 commit d61db50
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 29 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/kr/pretty v0.3.0 // indirect
github.com/onsi/ginkgo/v2 v2.2.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-18 v0.2.0 // indirect
github.com/quic-go/qtls-go1-19 v0.2.1 // indirect
github.com/quic-go/qtls-go1-20 v0.1.1 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
Expand Down
24 changes: 0 additions & 24 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/Doridian/gopacket v1.2.1 h1:z0Iu5zplIq01nGNwKoreAhc/RMIUqu6vZLxLsHjpO48=
github.com/Doridian/gopacket v1.2.1/go.mod h1:16EwY3JsEHp3TFeSRcmSC9yOdG8GkFAWImZaL13kOGc=
github.com/Doridian/water v1.4.0 h1:RMqGN/CxDk//9oMNG+ud4OqWyv9DueyxCSclMTQC8o8=
github.com/Doridian/water v1.4.0/go.mod h1:284mGeO2q1U9zosywrswN961pYz9N+uA1TQclNoNpH0=
github.com/Doridian/water v1.4.1 h1:SkRESscwXeubvl9kv+w6EfPFvuFybhfpJKrFaJHw8oI=
github.com/Doridian/water v1.4.1/go.mod h1:C9cNWty0c4/LFl8VO6dTbTeokbhtua8qmUaxz80cjmM=
github.com/GehirnInc/crypt v0.0.0-20200316065508-bb7000b8a962 h1:KeNholpO2xKjgaaSyd+DyQRrsQjhbSeS7qe4nEw8aQw=
Expand Down Expand Up @@ -34,8 +32,6 @@ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/golang-lru/v2 v2.0.2 h1:Dwmkdr5Nc/oBiXgJS3CDHNhJtIHkuZ3DZF5twqnfBdU=
github.com/hashicorp/golang-lru/v2 v2.0.2/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand All @@ -55,22 +51,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/qtls-go1-18 v0.2.0 h1:5ViXqBZ90wpUcZS0ge79rf029yx0dYB0McyPJwqqj7U=
github.com/quic-go/qtls-go1-18 v0.2.0/go.mod h1:moGulGHK7o6O8lSPSZNoOwcLvJKJ85vVNc7oJFD65bc=
github.com/quic-go/qtls-go1-19 v0.2.0 h1:Cvn2WdhyViFUHoOqK52i51k4nDX8EwIh5VJiVM4nttk=
github.com/quic-go/qtls-go1-19 v0.2.0/go.mod h1:ySOI96ew8lnoKPtSqx2BlI5wCpUVPT05RMAlajtnyOI=
github.com/quic-go/qtls-go1-19 v0.2.1 h1:aJcKNMkH5ASEJB9FXNeZCyTEIHU1J7MmHyz1Q1TSG1A=
github.com/quic-go/qtls-go1-19 v0.2.1/go.mod h1:ySOI96ew8lnoKPtSqx2BlI5wCpUVPT05RMAlajtnyOI=
github.com/quic-go/qtls-go1-20 v0.1.0 h1:d1PK3ErFy9t7zxKsG3NXBJXZjp/kMLoIb3y/kV54oAI=
github.com/quic-go/qtls-go1-20 v0.1.0/go.mod h1:JKtK6mjbAVcUTN/9jZpvLbGxvdWIKS8uT7EiStoU1SM=
github.com/quic-go/qtls-go1-20 v0.1.1 h1:KbChDlg82d3IHqaj2bn6GfKRj84Per2VGf5XV3wSwQk=
github.com/quic-go/qtls-go1-20 v0.1.1/go.mod h1:JKtK6mjbAVcUTN/9jZpvLbGxvdWIKS8uT7EiStoU1SM=
github.com/quic-go/quic-go v0.32.0 h1:lY02md31s1JgPiiyfqJijpu/UX/Iun304FI3yUqX7tA=
github.com/quic-go/quic-go v0.32.0/go.mod h1:/fCsKANhQIeD5l76c2JFU+07gVE3KaA0FP+0zMWwfwo=
github.com/quic-go/quic-go v0.33.0 h1:ItNoTDN/Fm/zBlq769lLJc8ECe9gYaW40veHCCco7y0=
github.com/quic-go/quic-go v0.33.0/go.mod h1:YMuhaAV9/jIu0XclDXwZPAsP/2Kgr5yMYhe9oxhhOFA=
github.com/quic-go/webtransport-go v0.5.1 h1:1eVb7WDWCRoaeTtFHpFBJ6WDN1bSrPrRoW6tZgSw0Ow=
github.com/quic-go/webtransport-go v0.5.1/go.mod h1:OhmmgJIzTTqXK5xvtuX0oBpLV2GkLWNDA+UeTGJXErU=
github.com/quic-go/webtransport-go v0.5.2 h1:GA6Bl6oZY+g/flt00Pnu0XtivSD8vukOu3lYhJjnGEk=
github.com/quic-go/webtransport-go v0.5.2/go.mod h1:OhmmgJIzTTqXK5xvtuX0oBpLV2GkLWNDA+UeTGJXErU=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
Expand All @@ -95,8 +81,6 @@ golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -105,8 +89,6 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -125,8 +107,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -136,16 +116,12 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
13 changes: 13 additions & 0 deletions server/cli/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -98,6 +99,18 @@ func reloadConfig(configPtr *string, server *servers.Server, initialConfig bool)
}
server.APIUsers = apiUsers

srvHeaders := http.Header{}
for name, values := range config.Server.Headers {
for _, value := range values {
srvHeaders.Add(name, value)
}
}

if srvHeaders.Get("Server") == "" {
srvHeaders.Set("Server", fmt.Sprintf("wsvpn/%s", shared.Version))
}
server.SetHeaders(srvHeaders)

err = server.SetMTU(config.Tunnel.MTU)
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions server/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
_ "embed" // Required for go:embed
"log"
"net/http"
"strings"

"github.com/Doridian/wsvpn/shared"
Expand Down Expand Up @@ -37,8 +38,9 @@ type Config struct {
Scripts shared.EventConfig `yaml:"scripts"`

Server struct {
Listen string `yaml:"listen"`
EnableHTTP3 bool `yaml:"enable-http3"`
Listen string `yaml:"listen"`
EnableHTTP3 bool `yaml:"enable-http3"`
Headers http.Header `yaml:"headers"`
TLS struct {
ClientCA string `yaml:"client-ca"`
Certificate string `yaml:"certificate"`
Expand Down
8 changes: 8 additions & 0 deletions server/cli/server.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ server:
listen: 127.0.0.1:9000
enable-http3: false
website-directory: "" # Serve normal HTTP(S) requests from this folder, disabled if blank

headers: # Map of headers (string key to *list* of string values)
# X-Some-Host:
# - example.com
# X-Other-Header:
# - value1
# - value2

tls:
client-ca: "" # Filename of CA for mTLS
certificate: "" # Filename of certificate for TLS
Expand Down
7 changes: 7 additions & 0 deletions server/servers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"log"
"net/http"
"sync"

"github.com/Doridian/wsvpn/server/authenticators"
Expand Down Expand Up @@ -46,6 +47,7 @@ type Server struct {
APIEnabled bool
APIUsers map[string]bool
PreauthorizeSecret []byte
headers http.Header

upgraders []upgraders.SocketUpgrader
slotMutex *sync.Mutex
Expand Down Expand Up @@ -87,6 +89,11 @@ func NewServer() *Server {
}
}

func (s *Server) SetHeaders(headers http.Header) {
s.headers = headers
s.setUpgraderHeaders()
}

func (s *Server) SetServerID(serverID string) {
s.serverID = serverID
shared.UpdateLogger(s.log, "SERVER", s.serverID)
Expand Down
11 changes: 9 additions & 2 deletions server/servers/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
const ReadHeaderTimeout = time.Duration(10) * time.Second

func (s *Server) listenUpgraders() {
for _, upgraderLoop := range s.upgraders {
for _, upgrader := range s.upgraders {
s.serveWaitGroup.Add(1)
upgrader.SetHeaders(s.headers)
go func(upgrader upgraders.SocketUpgrader) {
defer s.serveWaitGroup.Done()
err := upgrader.ListenAndServe()
s.setServeError(err)
}(upgraderLoop)
}(upgrader)
}
}

Expand All @@ -28,6 +29,12 @@ func (s *Server) addUpgrader(upgrader upgraders.SocketUpgrader) {
s.addCloser(upgrader)
}

func (s *Server) setUpgraderHeaders() {
for _, upgrader := range s.upgraders {
upgrader.SetHeaders(s.headers)
}
}

func (s *Server) listenPlaintext(httpHandlerFunc http.HandlerFunc) {
if s.HTTP3Enabled {
s.setServeError(errors.New("HTTP/3 requires TLS"))
Expand Down
6 changes: 6 additions & 0 deletions server/servers/sockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func (s *Server) serveSocket(w http.ResponseWriter, r *http.Request) {
clientID := clientUUID.String()
clientLogger := shared.MakeLogger("CLIENT", clientID)

for key, values := range s.headers {
for _, value := range values {
w.Header().Add(key, value)
}
}

tlsConnectionState := r.TLS

http3Hijacker, ok := w.(http3.Hijacker)
Expand Down
1 change: 1 addition & 0 deletions server/upgraders/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type SocketUpgrader interface {
io.Closer

SetHeaders(headers http.Header)
Upgrade(w http.ResponseWriter, r *http.Request) (adapters.SocketAdapter, error)
ListenAndServe() error
Matches(r *http.Request) bool
Expand Down
4 changes: 4 additions & 0 deletions server/upgraders/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func NewWebSocketUpgrader() *WebSocketUpgrader {
}
}

func (u *WebSocketUpgrader) SetHeaders(headers http.Header) {
u.upgrader.Header = headers
}

func (u *WebSocketUpgrader) Upgrade(w http.ResponseWriter, r *http.Request) (adapters.SocketAdapter, error) {
serializationType := handleHTTPSerializationHeaders(w, r)

Expand Down
4 changes: 4 additions & 0 deletions server/upgraders/webtransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func NewWebTransportUpgrader(quicServer *QuicServerConfig) *WebTransportUpgrader
}
}

func (u *WebTransportUpgrader) SetHeaders(headers http.Header) {
// Nothing to do here
}

func (u *WebTransportUpgrader) SetQuicHeaders(header http.Header) error {
return u.server.H3.SetQuicHeaders(header)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/bins.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ def __init__(self, proj: str) -> None:
LAST_PORT += 1
self.cfg["server"]["listen"] = f"127.0.0.1:{self.port}"
self.cfg["tunnel"]["subnet"] = None
self.cfg["server"]["headers"] = {
"X-Test-Header": ["test-header-value"]
}
else:
self.port = None
self.ip = None
self.cfg["client"]["headers"] = {
"X-Test-Header": ["test-header-value"]
}

self.proc_wait_cond = Condition()
self.is_ready_or_done = False
Expand Down

0 comments on commit d61db50

Please sign in to comment.