proxy.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from asyncio import get_event_loop, sleep
  21. from pyrogram import filters
  22. from pyrogram.types import CallbackQuery, Message
  23. from wbb import app, arq
  24. from wbb.core.keyboard import ikb
  25. __MODULE__ = "Proxy"
  26. __HELP__ = (
  27. "/proxy - Get socks5 proxy which you can"
  28. + " use with telegram or other things"
  29. )
  30. proxies = []
  31. async def get_proxies():
  32. global proxies
  33. proxies = (await arq.proxy()).result
  34. loop = get_event_loop()
  35. loop.create_task(get_proxies())
  36. def url_from_proxy(proxy: str) -> str:
  37. creds, proxy = proxy.split("//")[1:][0].split("@")
  38. user, passwd = creds.split(":")
  39. host, port = proxy.split(":")
  40. return (
  41. f"https://t.me/socks?server={host}&port="
  42. + f"{port}&user={user}&pass={passwd}"
  43. )
  44. @app.on_message(filters.command("proxy") & ~filters.edited)
  45. async def proxy_func(_, message: Message):
  46. if len(proxies) == 0:
  47. await sleep(0.5)
  48. location = proxies[0].location
  49. proxy = proxies[0].proxy
  50. url = url_from_proxy(proxy)
  51. keyb = ikb(
  52. {
  53. "←": "proxy_arq_-1",
  54. "→": "proxy_arq_1",
  55. "Connect": url,
  56. }
  57. )
  58. await message.reply_text(
  59. f"""
  60. **Proxy:** {proxy}
  61. **Location**: {location}
  62. **POWERED BY [ARQ](http://t.me/ARQUpdates)**""",
  63. reply_markup=keyb,
  64. disable_web_page_preview=True,
  65. )
  66. @app.on_callback_query(filters.regex(r"proxy_arq_"))
  67. async def proxy_callback_func(_, cq: CallbackQuery):
  68. data = cq.data
  69. index = int(data.split("_")[-1])
  70. location = proxies[index].location
  71. proxy = proxies[index].proxy
  72. url = url_from_proxy(proxy)
  73. keyb = ikb(
  74. {
  75. "←": f"proxy_arq_{index-1}",
  76. "→": f"proxy_arq_{index+1}",
  77. "Connect": url,
  78. }
  79. )
  80. await cq.message.edit(
  81. f"""
  82. **Proxy:** {proxy}
  83. **Location**: {location}
  84. **POWERED BY [ARQ](http://t.me/ARQUpdates)**""",
  85. reply_markup=keyb,
  86. disable_web_page_preview=True,
  87. )