MCPcopy
hub / github.com/coyove/goflyway / dialUpstreamAndBridgeWS

Method dialUpstreamAndBridgeWS

proxy/client.go:266–326  ·  view source on GitHub ↗
(downstreamConn net.Conn, host string, resp []byte, extra, dialStyle byte)

Source from the content-addressed store, hash-verified

264}
265
266func (proxy *ProxyClient) dialUpstreamAndBridgeWS(downstreamConn net.Conn, host string, resp []byte, extra, dialStyle byte) net.Conn {
267 upstreamConn, err := proxy.dialUpstream(dialStyle)
268 if err != nil {
269 proxy.Logger.E("Dial", "Error", err)
270 if downstreamConn != nil {
271 downstreamConn.Close()
272 }
273 return nil
274 }
275
276 r := proxy.Cipher.newRequest()
277 r.Opt = Options(doConnect | doWebSocket | extra)
278 r.Auth = proxy.UserAuth
279 if proxy.Partial {
280 r.Opt.Set(doPartial)
281 }
282
283 var pl buffer
284 if proxy.URLHeader == "" {
285 pl.Writes("GET /", proxy.encryptHost(host, r), " HTTP/1.1\r\nHost: ", proxy.genHost(), "\r\n")
286 } else {
287 pl.Writes("GET http://", proxy.Upstream, "/ HTTP/1.1\r\nHost: ", proxy.Upstream, "\r\n",
288 proxy.URLHeader, ": http://", proxy.genHost(), "/", proxy.encryptHost(host, r), "\r\n")
289 }
290
291 wsKey := [20]byte{}
292 proxy.Cipher.Rand.Read(wsKey[:])
293
294 pl.Writes("Upgrade: websocket\r\nConnection: Upgrade\r\n",
295 "Sec-WebSocket-Key: ", base64.StdEncoding.EncodeToString(wsKey[:]), "\r\nSec-WebSocket-Version: 13\r\n\r\n")
296
297 upstreamConn.Write(pl.Bytes())
298
299 buf, err := readUntil(upstreamConn, "\r\n\r\n")
300 if err != nil || !strings.HasPrefix(string(buf), "HTTP/1.1 101 Switching Protocols") {
301 if err != nil {
302 proxy.Logger.E("Client", "Invalid response", host, err)
303 }
304
305 upstreamConn.Close()
306 if downstreamConn != nil {
307 downstreamConn.Close()
308 }
309 return nil
310 }
311
312 if extra == doMuxWS {
313 // we return here and handle the connection to tcpmux
314 return upstreamConn
315 }
316
317 if resp != nil {
318 downstreamConn.Write(resp)
319 }
320
321 go proxy.Cipher.IO.Bridge(downstreamConn, upstreamConn, &r.iv, IOConfig{
322 Partial: proxy.Partial,
323 WSCtrl: wsClient,

Callers 3

handleUDPtoTCPMethod · 0.95
DialUpstreamMethod · 0.95
NewClientFunction · 0.95

Calls 12

dialUpstreamMethod · 0.95
WritesMethod · 0.95
encryptHostMethod · 0.95
genHostMethod · 0.95
OptionsTypeAlias · 0.85
readUntilFunction · 0.85
newRequestMethod · 0.80
SetMethod · 0.80
CloseMethod · 0.45
ReadMethod · 0.45
WriteMethod · 0.45
BridgeMethod · 0.45

Tested by

no test coverage detected