github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/websocket/x/websocket.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package websocket implements a client and server for the WebSocket protocol 6 // as specified in RFC 6455. 7 // 8 // This package currently lacks some features found in an alternative 9 // and more actively maintained WebSocket package: 10 // 11 // https://pkg.go.dev/nhooyr.io/websocket 12 package websocket // import "golang.org/x/net/websocket" 13 14 import ( 15 "crypto/sha1" 16 "encoding/base64" 17 "unsafe" 18 ) 19 20 const ( 21 SupportedProtocolVersion = "13" 22 ) 23 24 const ( 25 websocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" 26 27 closeStatusNormal = 1000 28 closeStatusGoingAway = 1001 29 closeStatusProtocolError = 1002 30 closeStatusUnsupportedData = 1003 31 closeStatusFrameTooLarge = 1004 32 closeStatusNoStatusRcvd = 1005 33 closeStatusAbnormalClosure = 1006 34 closeStatusBadMessageData = 1007 35 closeStatusPolicyViolation = 1008 36 closeStatusTooBigData = 1009 37 closeStatusExtensionMismatch = 1010 38 39 maxControlFramePayloadLength = 125 40 ) 41 42 var ( 43 ErrUnsupportedExtensions = &ProtocolError{"unsupported extensions"} 44 45 handshakeHeader = map[string]bool{ 46 "Host": true, 47 "Upgrade": true, 48 "Connection": true, 49 "Sec-Websocket-Key": true, 50 "Sec-Websocket-Origin": true, 51 "Sec-Websocket-Version": true, 52 "Sec-Websocket-Protocol": true, 53 "Sec-Websocket-Accept": true, 54 } 55 ) 56 57 // ProtocolError represents WebSocket protocol errors. 58 type ProtocolError struct { 59 ErrorString string 60 } 61 62 func (err *ProtocolError) Error() string { return err.ErrorString } 63 64 var ( 65 ErrBadProtocolVersion = &ProtocolError{"bad protocol version"} 66 ErrBadScheme = &ProtocolError{"bad scheme"} 67 ErrBadStatus = &ProtocolError{"bad status"} 68 ErrBadUpgrade = &ProtocolError{"missing or bad upgrade"} 69 ErrBadWebSocketOrigin = &ProtocolError{"missing or bad WebSocket-Origin"} 70 ErrBadWebSocketLocation = &ProtocolError{"missing or bad WebSocket-Location"} 71 ErrBadWebSocketProtocol = &ProtocolError{"missing or bad WebSocket-Protocol"} 72 ErrBadWebSocketVersion = &ProtocolError{"missing or bad WebSocket Version"} 73 ErrChallengeResponse = &ProtocolError{"mismatch challenge/response"} 74 ErrBadFrame = &ProtocolError{"bad frame"} 75 ErrBadFrameBoundary = &ProtocolError{"not on frame boundary"} 76 ErrNotWebSocket = &ProtocolError{"not websocket protocol"} 77 ErrBadRequestMethod = &ProtocolError{"bad method"} 78 ErrNotSupported = &ProtocolError{"not supported"} 79 ) 80 81 // getNonceAccept computes the base64-encoded SHA-1 of the concatenation of 82 // the nonce ("Sec-WebSocket-Key" value) with the websocket GUID string. 83 func getNonceAccept(nonce string) string { 84 h := sha1.New() 85 h.Write(unsafe.Slice(unsafe.StringData(nonce), len(nonce))) 86 h.Write([]byte(websocketGUID)) 87 return base64.StdEncoding.EncodeToString(h.Sum(nil)) 88 }