package obfuscated2

import (
	

	

	
)

// Obfuscated2 implements obfuscated2 obfuscation protocol.
type Obfuscated2 struct {
	rand io.Reader
	conn io.ReadWriter

	keys
}

// NewObfuscated2 creates new Obfuscated2.
func ( io.Reader,  io.ReadWriter) *Obfuscated2 {
	return &Obfuscated2{
		rand: ,
		conn: ,
	}
}

// Handshake sends obfuscated2 header.
func ( *Obfuscated2) ( [4]byte,  int,  mtproxy.Secret) error {
	,  := generateKeys(.rand, , .Secret, )
	if  != nil {
		return errors.Wrap(, "generate keys")
	}
	.keys = 

	if ,  := .conn.Write(.header);  != nil {
		return errors.Wrap(, "write obfuscated header")
	}

	return nil
}

// Write implements io.Writer.
func ( *Obfuscated2) ( []byte) ( int,  error) {
	 := append([]byte(nil), ...)
	.encrypt.XORKeyStream(, )
	return .conn.Write()
}

// Read implements io.Reader.
func ( *Obfuscated2) ( []byte) (int, error) {
	,  := .conn.Read()
	if  != nil {
		return , 
	}
	if  > 0 {
		.decrypt.XORKeyStream(, )
	}
	return , 
}