Skip to content

Commit 7e3a8d3

Browse files
yuhan6665RPRX
authored andcommitted
Add separate host config for websocket
1 parent e2302b4 commit 7e3a8d3

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

infra/conf/transport_internet.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (c *TCPConfig) Build() (proto.Message, error) {
146146
}
147147

148148
type WebSocketConfig struct {
149+
Host string `json:"host"`
149150
Path string `json:"path"`
150151
Headers map[string]string `json:"headers"`
151152
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
@@ -154,10 +155,6 @@ type WebSocketConfig struct {
154155
// Build implements Buildable.
155156
func (c *WebSocketConfig) Build() (proto.Message, error) {
156157
path := c.Path
157-
header := make(map[string]string);
158-
for key, value := range c.Headers {
159-
header[key] = value;
160-
}
161158
var ed uint32
162159
if u, err := url.Parse(path); err == nil {
163160
if q := u.Query(); q.Get("ed") != "" {
@@ -168,18 +165,27 @@ func (c *WebSocketConfig) Build() (proto.Message, error) {
168165
path = u.String()
169166
}
170167
}
168+
// If http host is not set in the Host field, but in headers field, we add it to Host Field here.
169+
// If we don't do that, http host will be overwritten as address.
170+
// Host priority: Host field > headers field > address.
171+
if c.Host == "" && c.Headers["host"] != "" {
172+
c.Host = c.Headers["host"]
173+
} else if c.Host == "" && c.Headers["Host"] != "" {
174+
c.Host = c.Headers["Host"]
175+
}
171176
config := &websocket.Config{
172177
Path: path,
173-
Header: header,
178+
Host: c.Host,
179+
Header: c.Headers,
174180
AcceptProxyProtocol: c.AcceptProxyProtocol,
175181
Ed: ed,
176182
}
177183
return config, nil
178184
}
179185

180186
type HttpUpgradeConfig struct {
181-
Path string `json:"path"`
182187
Host string `json:"host"`
188+
Path string `json:"path"`
183189
Headers map[string]string `json:"headers"`
184190
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
185191
}

transport/internet/websocket/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func (c *Config) GetRequestHeader() http.Header {
2525
for k, v := range c.Header {
2626
header.Add(k, v)
2727
}
28+
header.Set("Host", c.Host)
2829
return header
2930
}
3031

transport/internet/websocket/hub.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
)
2222

2323
type requestHandler struct {
24+
host string
2425
path string
2526
ln *Listener
2627
}
@@ -37,6 +38,10 @@ var upgrader = &websocket.Upgrader{
3738
}
3839

3940
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
41+
if len(h.host) > 0 && request.Host != h.host {
42+
writer.WriteHeader(http.StatusNotFound)
43+
return
44+
}
4045
if request.URL.Path != h.path {
4146
writer.WriteHeader(http.StatusNotFound)
4247
return
@@ -125,6 +130,7 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSet
125130

126131
l.server = http.Server{
127132
Handler: &requestHandler{
133+
host: wsSettings.Host,
128134
path: wsSettings.GetNormalizedPath(),
129135
ln: l,
130136
},

0 commit comments

Comments
 (0)