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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timeoutrwc.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package wrappers
  2. import (
  3. "net"
  4. "time"
  5. "github.com/9seconds/mtg/config"
  6. )
  7. type TimeoutReadWriteCloserWithAddr struct {
  8. conn net.Conn
  9. publicIPv4 net.IP
  10. publicIPv6 net.IP
  11. }
  12. func (t *TimeoutReadWriteCloserWithAddr) Read(p []byte) (int, error) {
  13. t.conn.SetReadDeadline(time.Now().Add(config.TimeoutRead))
  14. return t.conn.Read(p)
  15. }
  16. func (t *TimeoutReadWriteCloserWithAddr) Write(p []byte) (int, error) {
  17. t.conn.SetWriteDeadline(time.Now().Add(config.TimeoutWrite))
  18. return t.conn.Write(p)
  19. }
  20. func (t *TimeoutReadWriteCloserWithAddr) Close() error {
  21. return t.conn.Close()
  22. }
  23. func (t *TimeoutReadWriteCloserWithAddr) RemoteAddr() *net.TCPAddr {
  24. return t.conn.RemoteAddr().(*net.TCPAddr)
  25. }
  26. func (t *TimeoutReadWriteCloserWithAddr) LocalAddr() *net.TCPAddr {
  27. addr := t.conn.LocalAddr().(*net.TCPAddr)
  28. newAddr := *addr
  29. if t.RemoteAddr().IP.To4() != nil {
  30. if t.publicIPv4 != nil {
  31. newAddr.IP = t.publicIPv4
  32. }
  33. } else if t.publicIPv6 != nil {
  34. newAddr.IP = t.publicIPv6
  35. }
  36. return &newAddr
  37. }
  38. func NewTimeoutRWC(conn net.Conn, ipv4, ipv6 net.IP) ReadWriteCloserWithAddr {
  39. return &TimeoutReadWriteCloserWithAddr{
  40. conn: conn,
  41. publicIPv4: ipv4,
  42. publicIPv6: ipv6,
  43. }
  44. }