Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

streamcipher.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package wrappers
  2. import (
  3. "crypto/cipher"
  4. "net"
  5. "github.com/juju/errors"
  6. )
  7. type StreamCipher struct {
  8. encryptor cipher.Stream
  9. decryptor cipher.Stream
  10. conn StreamReadWriteCloser
  11. }
  12. func (s *StreamCipher) Read(p []byte) (int, error) {
  13. n, err := s.conn.Read(p)
  14. if err != nil {
  15. return 0, errors.Annotate(err, "Cannot read stream ciphered data")
  16. }
  17. s.decryptor.XORKeyStream(p, p[:n])
  18. return n, nil
  19. }
  20. func (s *StreamCipher) Write(p []byte) (int, error) {
  21. encrypted := make([]byte, len(p))
  22. s.encryptor.XORKeyStream(encrypted, p)
  23. return s.conn.Write(encrypted)
  24. }
  25. func (s *StreamCipher) LogDebug(msg string, data ...interface{}) {
  26. s.conn.LogDebug(msg, data...)
  27. }
  28. func (s *StreamCipher) LogInfo(msg string, data ...interface{}) {
  29. s.conn.LogInfo(msg, data...)
  30. }
  31. func (s *StreamCipher) LogWarn(msg string, data ...interface{}) {
  32. s.conn.LogWarn(msg, data...)
  33. }
  34. func (s *StreamCipher) LogError(msg string, data ...interface{}) {
  35. s.conn.LogError(msg, data...)
  36. }
  37. func (s *StreamCipher) LocalAddr() *net.TCPAddr {
  38. return s.conn.LocalAddr()
  39. }
  40. func (s *StreamCipher) RemoteAddr() *net.TCPAddr {
  41. return s.conn.RemoteAddr()
  42. }
  43. func (s *StreamCipher) Close() error {
  44. return s.conn.Close()
  45. }
  46. func NewStreamCipher(conn StreamReadWriteCloser, encryptor, decryptor cipher.Stream) StreamReadWriteCloser {
  47. return &StreamCipher{
  48. conn: conn,
  49. encryptor: encryptor,
  50. decryptor: decryptor,
  51. }
  52. }