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 символів.

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