package wsutilimport ()// CipherReader implements io.Reader that applies xor-cipher to the bytes read// from source.// It could help to unmask WebSocket frame payload on the fly.typeCipherReaderstruct { r io.Reader mask [4]byte pos int}// NewCipherReader creates xor-cipher reader from r with given mask.func ( io.Reader, [4]byte) *CipherReader {return &CipherReader{, , 0}}// Reset resets CipherReader to read from r with given mask.func ( *CipherReader) ( io.Reader, [4]byte) { .r = .mask = .pos = 0}// Read implements io.Reader interface. It applies mask given during// initialization to every read byte.func ( *CipherReader) ( []byte) ( int, error) { , = .r.Read()ws.Cipher([:], .mask, .pos) .pos += return , }// CipherWriter implements io.Writer that applies xor-cipher to the bytes// written to the destination writer. It does not modify the original bytes.typeCipherWriterstruct { w io.Writer mask [4]byte pos int}// NewCipherWriter creates xor-cipher writer to w with given mask.func ( io.Writer, [4]byte) *CipherWriter {return &CipherWriter{, , 0}}// Reset reset CipherWriter to write to w with given mask.func ( *CipherWriter) ( io.Writer, [4]byte) { .w = .mask = .pos = 0}// Write implements io.Writer interface. It applies masking during// initialization to every sent byte. It does not modify original slice.func ( *CipherWriter) ( []byte) ( int, error) { := pbytes.GetLen(len())deferpbytes.Put()copy(, )ws.Cipher(, .mask, .pos) , = .w.Write() .pos += return , }
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.