Coder Social home page Coder Social logo

spacecase123 / aiosocks2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ultrafunkamsterdam/aiosocks2

0.0 0.0 0.0 112 KB

Revived abandoned aiosocks project into aiosocks2, a SOCKS(4/5) proxy client for asyncio

License: Apache License 2.0

Python 100.00%

aiosocks2's Introduction

SOCKS proxy client for asyncio and aiohttp

image

image

image

Dependencies

python 3.5+ aiohttp 2.3.2+

Features

  • Fixed original library to use in 2021 onwards
  • SOCKS4, SOCKS4a and SOCKS5 version
  • ProxyConnector for aiohttp
  • SOCKS "CONNECT" command

TODO

  • UDP associate
  • TCP port binding

Installation

You can install it using Pip:

pip install aiosocks2

If you want the latest development version, you can install it from source:

git clone [email protected]:ultrafunkamsterdam/aiosocks2.git
cd aiosocks2
python setup.py install

Usage

direct usage

import asyncio
import aiosocks2


async def connect():
  socks5_addr = aiosocks2.Socks5Addr('127.0.0.1', 1080)
  socks4_addr = aiosocks2.Socks4Addr('127.0.0.1', 1080)

  socks5_auth = aiosocks2.Socks5Auth('login', 'pwd')
  socks4_auth = aiosocks2.Socks4Auth('ident')

  dst = ('github.com', 80)

  # socks5 connect
  transport, protocol = await aiosocks2.create_connection(
      lambda: Protocol, proxy=socks5_addr, proxy_auth=socks5_auth, dst=dst)

  # socks4 connect
  transport, protocol = await aiosocks2.create_connection(
      lambda: Protocol, proxy=socks4_addr, proxy_auth=socks4_auth, dst=dst)

  # socks4 without auth and local domain name resolving
  transport, protocol = await aiosocks2.create_connection(
      lambda: Protocol, proxy=socks4_addr, proxy_auth=None, dst=dst, remote_resolve=False)

  # use socks protocol
  transport, protocol = await aiosocks2.create_connection(
      None, proxy=socks4_addr, proxy_auth=None, dst=dst)

if __name__ == '__main__':
  loop = asyncio.get_event_loop()
  loop.run_until_complete(connect())
  loop.close()

A wrapper for create_connection() returning a (reader, writer) pair

# StreamReader, StreamWriter
reader, writer = await aiosocks2.open_connection(
    proxy=socks5_addr, proxy_auth=socks5_auth, dst=dst, remote_resolve=True)

data = await reader.read(10)
writer.write('data')

error handling

SocksError is a base class for:
  • NoAcceptableAuthMethods
  • LoginAuthenticationFailed
  • InvalidServerVersion
  • InvalidServerReply
try:
  transport, protocol = await aiosocks2.create_connection(
      lambda: Protocol, proxy=socks5_addr, proxy_auth=socks5_auth, dst=dst)
except aiosocks2.SocksConnectionError:
  # connection error
except aiosocks2.LoginAuthenticationFailed:
  # auth failed
except aiosocks2.NoAcceptableAuthMethods:
  # All offered SOCKS5 authentication methods were rejected
except (aiosocks2.InvalidServerVersion, aiosocks2.InvalidServerReply):
  # something wrong
except aiosocks2.SocksError:
  # something other

or

try:
  transport, protocol = await aiosocks2.create_connection(
      lambda: Protocol, proxy=socks5_addr, proxy_auth=socks5_auth, dst=dst)
except aiosocks2.SocksConnectionError:
    # connection error
except aiosocks2.SocksError:
    # socks error

aiohttp usage

import asyncio
import aiohttp
import aiosocks2
from aiosocks2.connector import ProxyConnector, ProxyClientRequest


async def load_github_main():
  auth5 = aiosocks2.Socks5Auth('proxyuser1', password='pwd')
  auth4 = aiosocks2.Socks4Auth('proxyuser1')
  ba = aiohttp.BasicAuth('login')

  # remote resolve
  conn = ProxyConnector(remote_resolve=True)

  # or locale resolve
  conn = ProxyConnector(remote_resolve=False)

  try:
    with aiohttp.ClientSession(connector=conn, request_class=ProxyClientRequest) as session:
      # socks5 proxy
      async with session.get('http://github.com/', proxy='socks5://127.0.0.1:1080',
                             proxy_auth=auth5) as resp:
        if resp.status == 200:
          print(await resp.text())

      # socks4 proxy
      async with session.get('http://github.com/', proxy='socks4://127.0.0.1:1081',
                             proxy_auth=auth4) as resp:
        if resp.status == 200:
          print(await resp.text())

      # http proxy
      async with session.get('http://github.com/', proxy='http://127.0.0.1:8080',
                             proxy_auth=ba) as resp:
        if resp.status == 200:
          print(await resp.text())
  except aiohttp.ClientProxyConnectionError:
    # connection problem
  except aiohttp.ClientConnectorError:
    # ssl error, certificate error, etc
  except aiosocks2.SocksError:
    # communication problem


if __name__ == '__main__':
  loop = asyncio.get_event_loop()
  loop.run_until_complete(load_github_main())
  loop.close()

aiosocks2's People

Contributors

nibrag avatar nibragimov-alytics avatar resource-not-found-blank avatar ultrafunkamsterdam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.