411 lines
12 KiB
Go
411 lines
12 KiB
Go
// Code generated by zi2go from websocket.zi. DO NOT EDIT.
|
|
package main
|
|
|
|
import (
|
|
bufio "bufio"
|
|
sha1 "crypto/sha1"
|
|
base64 "encoding/base64"
|
|
io "io"
|
|
net "net"
|
|
http "net/http"
|
|
strings "strings"
|
|
)
|
|
|
|
// #import std_go_types
|
|
type Connection = net.Conn
|
|
|
|
type BufferedReader = bufio.Reader
|
|
|
|
type BufferedConnection = bufio.ReadWriter
|
|
|
|
type AcceptedSocket struct {
|
|
Connection Connection
|
|
Buffer *BufferedConnection
|
|
Error Error
|
|
}
|
|
|
|
type Frame struct {
|
|
Opcode uint8
|
|
Payload []uint8
|
|
Error Error
|
|
}
|
|
|
|
type zir_6cdc28462cfc0851_Hijacker = http.Hijacker
|
|
|
|
type zir_cc9dcb6f3173cd4e_HijackerResult struct {
|
|
Value zir_6cdc28462cfc0851_Hijacker
|
|
Present bool
|
|
}
|
|
|
|
type zir_50b2af4b05b6a4ca_Encoding = base64.Encoding
|
|
|
|
type zir_71ad4052a732d3cb_BytesResult struct {
|
|
Value []uint8
|
|
Error Error
|
|
}
|
|
|
|
type zir_f5c2242e7b75ab47_WriteResult struct {
|
|
Count int
|
|
Error Error
|
|
}
|
|
|
|
type zir_79d7080a4fb882c3_PrefixResult struct {
|
|
Value string
|
|
Present bool
|
|
}
|
|
|
|
func Websocket_CutPrefix(Value string, Prefix string) zir_79d7080a4fb882c3_PrefixResult {
|
|
var result zir_79d7080a4fb882c3_PrefixResult
|
|
result.Value, result.Present = strings.CutPrefix(Value, Prefix)
|
|
return result
|
|
}
|
|
|
|
func Websocket_StandardEncoding() *zir_50b2af4b05b6a4ca_Encoding {
|
|
return base64.StdEncoding
|
|
}
|
|
|
|
func Websocket_Decode(Encoding *zir_50b2af4b05b6a4ca_Encoding, Value string) zir_71ad4052a732d3cb_BytesResult {
|
|
var result zir_71ad4052a732d3cb_BytesResult
|
|
result.Value, result.Error = (*base64.Encoding).DecodeString(Encoding, Value)
|
|
return result
|
|
}
|
|
|
|
func Websocket_AsHijacker(Value Any) zir_cc9dcb6f3173cd4e_HijackerResult {
|
|
var result zir_cc9dcb6f3173cd4e_HijackerResult
|
|
result.Value, result.Present = Value.(zir_6cdc28462cfc0851_Hijacker)
|
|
return result
|
|
}
|
|
|
|
func Websocket_Hijack(Value zir_6cdc28462cfc0851_Hijacker) AcceptedSocket {
|
|
var result AcceptedSocket
|
|
result.Connection, result.Buffer, result.Error = (http.Hijacker).Hijack(Value)
|
|
return result
|
|
}
|
|
|
|
func Websocket_WriteString(Buffer *BufferedConnection, Value string) zir_f5c2242e7b75ab47_WriteResult {
|
|
var result zir_f5c2242e7b75ab47_WriteResult
|
|
result.Count, result.Error = (*bufio.ReadWriter).WriteString(Buffer, Value)
|
|
return result
|
|
}
|
|
|
|
func Websocket_WriteBytes(Connection Connection, Value []uint8) zir_f5c2242e7b75ab47_WriteResult {
|
|
var result zir_f5c2242e7b75ab47_WriteResult
|
|
result.Count, result.Error = (net.Conn).Write(Connection, Value)
|
|
return result
|
|
}
|
|
|
|
func Websocket_ReadFull(Reader *BufferedReader, Value []uint8) zir_f5c2242e7b75ab47_WriteResult {
|
|
var result zir_f5c2242e7b75ab47_WriteResult
|
|
result.Count, result.Error = io.ReadFull(Reader, Value)
|
|
return result
|
|
}
|
|
|
|
func Websocket_EOF() Error {
|
|
return io.EOF
|
|
}
|
|
|
|
const GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
|
|
|
func Websocket_Failure(error Error) AcceptedSocket {
|
|
var result AcceptedSocket = AcceptedSocket{}
|
|
result.Error = error
|
|
return result
|
|
}
|
|
|
|
func Websocket_FrameFailure(error Error) Frame {
|
|
var result Frame = Frame{}
|
|
result.Error = error
|
|
return result
|
|
}
|
|
|
|
func Websocket_Accept(writer ResponseWriter, request *Request) AcceptedSocket {
|
|
headers := StdHttpGo_Headers(request)
|
|
var value_0 bool = strings.EqualFold(StdHttpGo_HeaderValue(headers, "Upgrade"), "websocket")
|
|
var value_1 bool = !value_0
|
|
if !value_1 {
|
|
var value_2 bool = Websocket_HeaderContainsToken(StdHttpGo_HeaderValue(headers, "Connection"), "upgrade")
|
|
value_1 = !value_2
|
|
}
|
|
if value_1 {
|
|
Response_Error(writer, int(400), "websocket upgrade required")
|
|
return Websocket_Failure(StdErrorsGo_New("missing upgrade"))
|
|
}
|
|
key := strings.TrimSpace(StdHttpGo_HeaderValue(headers, "Sec-WebSocket-Key"))
|
|
var value_3 bool = Websocket_ValidKey(key)
|
|
var value_4 bool = !value_3
|
|
if !value_4 {
|
|
var value_5 bool = StdHttpGo_HeaderValue(headers, "Sec-WebSocket-Version") != "13"
|
|
value_4 = value_5
|
|
}
|
|
if value_4 {
|
|
Response_Error(writer, int(400), "invalid websocket handshake")
|
|
return Websocket_Failure(StdErrorsGo_New("invalid handshake"))
|
|
}
|
|
asserted := Websocket_AsHijacker(writer)
|
|
if !asserted.Present {
|
|
Response_Error(writer, int(500), "websocket unsupported")
|
|
return Websocket_Failure(StdErrorsGo_New("hijack unsupported"))
|
|
}
|
|
hijacked := Websocket_Hijack(asserted.Value)
|
|
if hijacked.Error != nil {
|
|
return Websocket_Failure(hijacked.Error)
|
|
}
|
|
var value_6 [2]string
|
|
var value_7 string = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: "
|
|
value_6[0] = value_7
|
|
var value_8 string = Websocket_AcceptKey(key)
|
|
value_6[1] = value_8
|
|
parts := value_6
|
|
_ = parts
|
|
reply := strings.Join(parts[0:2:2], "")
|
|
protocol := StdHttpGo_HeaderValue(headers, "Sec-WebSocket-Protocol")
|
|
if Websocket_ProtocolRequested(protocol, "daochi-sync-v1") {
|
|
reply = Websocket_AddText(reply, "\r\nSec-WebSocket-Protocol: daochi-sync-v1")
|
|
} else if Websocket_ProtocolRequested(protocol, "ksync-sync-v1") {
|
|
reply = Websocket_AddText(reply, "\r\nSec-WebSocket-Protocol: ksync-sync-v1")
|
|
} else if Websocket_ProtocolRequested(protocol, "inbe-sync-v1") {
|
|
reply = Websocket_AddText(reply, "\r\nSec-WebSocket-Protocol: inbe-sync-v1")
|
|
}
|
|
reply = Websocket_AddText(reply, "\r\n\r\n")
|
|
written := Websocket_WriteString(hijacked.Buffer, reply)
|
|
if written.Error != nil {
|
|
(net.Conn).Close(hijacked.Connection)
|
|
return Websocket_Failure(written.Error)
|
|
}
|
|
error := (*bufio.ReadWriter).Flush(hijacked.Buffer)
|
|
if error != nil {
|
|
(net.Conn).Close(hijacked.Connection)
|
|
return Websocket_Failure(error)
|
|
}
|
|
return hijacked
|
|
}
|
|
|
|
func Websocket_BearerToken(header string) string {
|
|
{
|
|
var value_0 []string = strings.Split(header, ",")
|
|
if int64(0) < 0 || int64(int64(len(value_0))) < int64(0) || int64(int64(len(value_0))) > int64(len(value_0)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_0 := value_0[0:int64(len(value_0)):int64(len(value_0))]
|
|
loop_count_0 := int64(len(loop_view_0))
|
|
var loop_cursor_0 int64 = 0
|
|
for loop_cursor_0 < loop_count_0 {
|
|
loop_index_0 := loop_cursor_0
|
|
part := loop_view_0[loop_index_0]
|
|
token := Websocket_CutPrefix(strings.TrimSpace(part), "bearer.")
|
|
if token.Present && token.Value != "" {
|
|
return token.Value
|
|
}
|
|
loop_cursor_0++
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func Websocket_ProtocolRequested(header string, protocol string) bool {
|
|
{
|
|
var value_0 []string = strings.Split(header, ",")
|
|
if int64(0) < 0 || int64(int64(len(value_0))) < int64(0) || int64(int64(len(value_0))) > int64(len(value_0)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_0 := value_0[0:int64(len(value_0)):int64(len(value_0))]
|
|
loop_count_0 := int64(len(loop_view_0))
|
|
var loop_cursor_0 int64 = 0
|
|
for loop_cursor_0 < loop_count_0 {
|
|
loop_index_0 := loop_cursor_0
|
|
part := loop_view_0[loop_index_0]
|
|
var value_2 string = strings.TrimSpace(part)
|
|
if value_2 == protocol {
|
|
return true
|
|
}
|
|
loop_cursor_0++
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func Websocket_HeaderContainsToken(header string, token string) bool {
|
|
{
|
|
var value_0 []string = strings.Split(header, ",")
|
|
if int64(0) < 0 || int64(int64(len(value_0))) < int64(0) || int64(int64(len(value_0))) > int64(len(value_0)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_0 := value_0[0:int64(len(value_0)):int64(len(value_0))]
|
|
loop_count_0 := int64(len(loop_view_0))
|
|
var loop_cursor_0 int64 = 0
|
|
for loop_cursor_0 < loop_count_0 {
|
|
loop_index_0 := loop_cursor_0
|
|
part := loop_view_0[loop_index_0]
|
|
var value_2 string = strings.TrimSpace(part)
|
|
if strings.EqualFold(value_2, token) {
|
|
return true
|
|
}
|
|
loop_cursor_0++
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func Websocket_ValidKey(key string) bool {
|
|
decoded := Websocket_Decode(Websocket_StandardEncoding(), key)
|
|
return decoded.Error == nil && int64(len(decoded.Value)) == 16
|
|
}
|
|
|
|
func Websocket_AcceptKey(key string) string {
|
|
sum := sha1.Sum(StdTextGo_ToBytes(Websocket_AddText(key, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")))
|
|
_ = sum
|
|
var value_0 *zir_50b2af4b05b6a4ca_Encoding = Websocket_StandardEncoding()
|
|
return (*base64.Encoding).EncodeToString(value_0, sum[0:20:20])
|
|
}
|
|
|
|
func Websocket_AddText(left string, right string) string {
|
|
var value_0 [2]string
|
|
value_0[0] = left
|
|
value_0[1] = right
|
|
parts := value_0
|
|
_ = parts
|
|
return strings.Join(parts[0:2:2], "")
|
|
}
|
|
|
|
func Websocket_WriteJSON(connection Connection, value Any) Error {
|
|
encoded := StdJsonGo_Marshal(value)
|
|
if encoded.Error != nil {
|
|
return encoded.Error
|
|
}
|
|
return Websocket_WriteFrame(connection, 1, encoded.Value)
|
|
}
|
|
|
|
func Websocket_WriteFrame(connection Connection, opcode uint8, payload []uint8) Error {
|
|
header := make([]uint8, int(int(2)))
|
|
header[0] = 128 | opcode
|
|
if int64(len(payload)) < 126 {
|
|
header[1] = uint8(int64(len(payload)))
|
|
} else if int64(len(payload)) <= 65535 {
|
|
header[1] = 126
|
|
header = append(header, uint8(uint8((int64(len(payload)) >> 8))))
|
|
header = append(header, uint8(uint8(int64(len(payload)))))
|
|
} else {
|
|
header[1] = 127
|
|
size := uint64(int64(len(payload)))
|
|
for index := int64(0); index <= 7; index++ {
|
|
header = append(header, uint8(uint8((uint64(integerOp(uint64(size), uint64(uint64(((7 - index) * 8))), 64, false, 7))))))
|
|
}
|
|
}
|
|
written := Websocket_WriteBytes(connection, header)
|
|
if written.Error != nil {
|
|
return written.Error
|
|
}
|
|
var value_0 zir_f5c2242e7b75ab47_WriteResult = Websocket_WriteBytes(connection, payload)
|
|
return value_0.Error
|
|
}
|
|
|
|
func Websocket_ReadFrame(reader *BufferedReader) Frame {
|
|
return Websocket_ReadMaskedFrame(reader, false)
|
|
}
|
|
|
|
func Websocket_ReadClientFrame(reader *BufferedReader) Frame {
|
|
return Websocket_ReadMaskedFrame(reader, true)
|
|
}
|
|
|
|
func Websocket_ReadMaskedFrame(reader *BufferedReader, requireMask bool) Frame {
|
|
var first [2]uint8
|
|
var value_0 zir_f5c2242e7b75ab47_WriteResult = Websocket_ReadFull(reader, first[0:2:2])
|
|
error := value_0.Error
|
|
if error != nil {
|
|
return Websocket_FrameFailure(error)
|
|
}
|
|
finished := (first[0] & 128) != 0
|
|
if (first[0] & 112) != 0 {
|
|
return Websocket_FrameFailure(StdErrorsGo_New("websocket reserved bits set"))
|
|
}
|
|
opcode := first[0] & 15
|
|
var value_1 bool = Websocket_ValidOpcode(opcode)
|
|
if !value_1 {
|
|
return Websocket_FrameFailure(StdErrorsGo_New("websocket unsupported opcode"))
|
|
}
|
|
masked := (first[1] & 128) != 0
|
|
if requireMask && !masked {
|
|
return Websocket_FrameFailure(StdErrorsGo_New("websocket client frame not masked"))
|
|
}
|
|
length := uint64((first[1] & 127))
|
|
if length == 126 {
|
|
var extended [2]uint8
|
|
_ = extended
|
|
var value_2 zir_f5c2242e7b75ab47_WriteResult = Websocket_ReadFull(reader, extended[0:2:2])
|
|
error = value_2.Error
|
|
if error != nil {
|
|
return Websocket_FrameFailure(error)
|
|
}
|
|
length = (uint64(extended[0]) << 8) | uint64(extended[1])
|
|
} else if length == 127 {
|
|
var extended [8]uint8
|
|
_ = extended
|
|
var value_3 zir_f5c2242e7b75ab47_WriteResult = Websocket_ReadFull(reader, extended[0:8:8])
|
|
error = value_3.Error
|
|
if error != nil {
|
|
return Websocket_FrameFailure(error)
|
|
}
|
|
length = 0
|
|
for it_index := int64(0); it_index < 8; it_index++ {
|
|
byte := extended[it_index]
|
|
_ = byte
|
|
length = (length << 8) | uint64(byte)
|
|
}
|
|
if (length & 9223372036854775808) != 0 {
|
|
return Websocket_FrameFailure(StdErrorsGo_New("websocket invalid length"))
|
|
}
|
|
}
|
|
if opcode >= 8 && (!finished || length > 125) {
|
|
return Websocket_FrameFailure(StdErrorsGo_New("websocket invalid control frame"))
|
|
}
|
|
if !finished {
|
|
return Websocket_FrameFailure(StdErrorsGo_New("websocket fragmented frames unsupported"))
|
|
}
|
|
if length > 1048576 {
|
|
return Websocket_FrameFailure(StdErrorsGo_New("websocket frame too large"))
|
|
}
|
|
var mask [4]uint8
|
|
if masked {
|
|
var value_4 zir_f5c2242e7b75ab47_WriteResult = Websocket_ReadFull(reader, mask[0:4:4])
|
|
error = value_4.Error
|
|
if error != nil {
|
|
return Websocket_FrameFailure(error)
|
|
}
|
|
}
|
|
payload := make([]uint8, int(int(length)))
|
|
var value_5 zir_f5c2242e7b75ab47_WriteResult = Websocket_ReadFull(reader, payload)
|
|
error = value_5.Error
|
|
if error != nil {
|
|
return Websocket_FrameFailure(error)
|
|
}
|
|
if masked {
|
|
{
|
|
if int64(0) < 0 || int64(int64(len(payload))) < int64(0) || int64(int64(len(payload))) > int64(len(payload)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_62 := payload[0:int64(len(payload)):int64(len(payload))]
|
|
loop_count_62 := int64(len(loop_view_62))
|
|
var loop_cursor_62 int64 = 0
|
|
for loop_cursor_62 < loop_count_62 {
|
|
loop_index_62 := loop_cursor_62
|
|
byte := loop_view_62[loop_index_62]
|
|
_ = byte
|
|
index := loop_index_62
|
|
payload[index] = byte ^ mask[(index%4)]
|
|
loop_cursor_62++
|
|
}
|
|
}
|
|
}
|
|
var result Frame = Frame{}
|
|
result.Opcode = opcode
|
|
result.Payload = payload
|
|
if opcode == 8 {
|
|
result.Error = Websocket_EOF()
|
|
}
|
|
return result
|
|
}
|
|
|
|
func Websocket_ValidOpcode(opcode uint8) bool {
|
|
return opcode == 0 || opcode == 1 || opcode == 2 || opcode == 8 || opcode == 9 || opcode == 10
|
|
}
|