Commit Graph

64 Commits

Author SHA1 Message Date
Abhinav Singh aadcc10813
`EventManager` is also a context manager (#709)
* `EventManager` is also a context manager

* unused

* Rename `EventManager.event_queue` to `EventManager.queue`
2021-11-08 23:02:36 +05:30
Abhinav Singh c6eaacefce
Move pid file write/remove within `AcceptorPool` (#708)
* Move pid file write/remove within AcceptorPool

* Remove unused
2021-11-08 22:22:23 +05:30
Abhinav Singh db8da4f398
Fix `--enable-dashboard` flags (#707)
* Expose within __all__

* Enable `--numprocesses=auto` for `pytest.ini`

* make lib-lint

* Also consider `--plugins` flag when bootstrapping plugins

* Add `from .dashboard import ProxyDashboard` in top-level `__init__.py` to make `ProxyDashboard` flags auto discoverable

* Move `--enable-dashboard` to top-level

* Move logging utility within `Logger` class

* Consider comma separated --plugin and --plugins during discover_plugins

* Refactor plugin related utilities in Plugins module

* mypy and lint

* Fix unused import

* Safe to use tempdir on Github actions to avoid race conditions???

* pki (generically disk based file) based tests are flaky on macOS under parallel execution
2021-11-08 21:42:43 +05:30
Abhinav Singh a6d7fae0b3
Disable static web server test on GHA environment (flaky on Ubuntu) (#700) 2021-11-08 02:03:24 +05:30
Abhinav Singh 3cb0a62583
Add a `--unix-socket-path` flag (#697)
* Add a `--unix-socket-path` flag.

When available `--hostname` and `--port` flags are ignored.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* `print` statement is allowed only in `flags.py` and `version-check.py`.  All other places must use a `logger` instance

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add guard for `AF_UNIX` on Windows

* Comment out assertion on Windows for now

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-11-08 01:11:46 +05:30
Abhinav Singh b830f7b238
Rename `Proxy.initialize` as `FlagParser.initialize` (#696)
* Move Proxy.initialize within FlagParser.initialize.  Also move other staticmethods from within proxy class into utils

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* remove unused imports

* Fix `import-outside-toplevel` error

* add `make lib-flake8` and `make lib-mypy` targets

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-11-07 23:02:34 +05:30
Abhinav Singh f48771fb41
Top-level notion of work not client (#695)
* Top-level notion of work not client

* Update ssl echo server example
2021-11-07 21:43:38 +05:30
Abhinav Singh d3cee32909
Pool (#694)
* Refactor pool

* mypy fixes

* Fix import (relative)

* Add WebScraper example skeleton & ConnectionPool skeleton

* Add ConnectionPool class

* Integrate ConnectionPool with proxy server (experimental)

* Lint fixes

* Remove unused imports. TODO: Put pool behind a flag. Default to false for now

* Make ConnectionPool multiprocess safe.  Later we want to make it safe but without using locks

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove unused imports

* Return created flag from acquire

* Guard connection pool behind --enable-conn-pool flag

* Flag belongs within connection pool class

* spelling

* self.upstream = None only for pool config

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-11-07 21:06:41 +05:30
Abhinav Singh fd838ca64e
DEFAULT_CA_FILE is now certifi/cacert.pem (#691)
* Add FAQ: OSError when wrapping client for TLS Interception

* Silence exception log for several valid "cert verification failed" by client during tls interception

* Lint checks

* Move exception handling within wrap_server/wrap_client methods

* Lint fixes

* Use certifi/cacert.pem as default --ca-file flag value

* Address tests after DEFAULT_CA_FILE change

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-11-07 02:50:11 +05:30
Abhinav Singh d9200fd082
Response bodies can be empty or missing + HttpParser refactoring (#688)
* Responses can have None body, remove assertions, update modify chunk plugin to not modify chunks for responses with no content

* Address mypy warning after removing assertion

* Reusable get_body_or_chunks

* Order methods by public/private, mark private ones with _ prefix

* HttpParser.url deprecation notice (renamed to _url).  Add zero-copy todo
2021-11-06 22:07:11 +05:30
Abhinav Singh 25b7952b1d
Fix python 3.10 @ ubuntu pytest ignore (#685)
* Use suggested fix in #683 to remove pytest ignore

* Handle should not flush now which can block, instead let base tcp handler do its magic

* test speed up, doc string, logging enhancements

* Move macOS to the end of workflows

* Fix mypy warnings
2021-11-06 17:25:32 +05:30
Sviatoslav Sydorenko 423e9ce0a4
Add a pytest-based test for catching import loops (#678)
Co-authored-by: Abhinav Singh <mailsforabhinav@gmail.com>
2021-11-06 14:37:08 +05:30
Abhinav Singh 628de71ca8
Adopt BaseTcpServerHandler within HttpProtocolHandler (#681)
* Rename .server to .upstream

* Lint fixes

* Mark internal methods with _ prefix

* Fix broken test

* lint changes

* Wah, double client :D

* Avoid selector initialization for threadless mode

* remove unused imports

* Now HttpProtocolHandler implements BaseTcpServerHandler

* Consistent return and guard againt upstream.closed

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* AcceptorPool as context manager

* Group multiprocessing imports together

* Use com.jaxl bundle identifier as proxy.py will eventually move under jaxl org

* revisit devtools integration :)

* Emit all necessary events for devtools integration

* Lint fixes

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-11-06 14:01:13 +05:30
Abhinav Singh 880c3c876e
Add tests for missing core modules (#674)
* Rename is_py3 to is_py2 for more logical guard

* Add stubs for missing tests, add few more tests for core modules

* Lint fixes

* Line too long fix

* Remove unnecessary KeyboardInterrupt

* Consistent workflow names

* Update homebrew formulae.  Doesnt seems to work now

* test_enable_dashboard and test_enable_events

* test_enable_dashboard and test_enable_events

* Fix problem where empty plugin string was passed as plugin module

* test_enable_devtools and remove redundant guards for None and "" which was there due to a bug
2021-11-05 13:50:50 +05:30
Abhinav Singh bf4ee90e21
CustomDnsResolver plugin, CloudflareDnsResolver plugin, Allow plugins to configure network interface (#671)
* Add CustomDnsResolver plugin.  Addresses #535 and #664

* Add cloudflare DNS resolver plugin

* Lint fixes
2021-11-05 07:36:02 +05:30
Abhinav Singh 2a9db3a2dc
Update outdated sections of the codebase (#670)
* Remove autopep8, is redundant now after recent CI changes

* Add pyenv .python-version to .gitignore

* Update year

* Add lib-pytest target so that pytest can run in isolation

* Add git-push hook which will also run the lint.

By default now git-pre-commit hook will only run pytest.

* Update outdated sections of README

* Update requirement to match setup.cfg install_requires

* Deprecate proxy.start and TestCase.PROXY_PORT

Proxy port during test is now available as self.PROXY.pool.flags.port.
Also now TestCase utilize ephemeral port strategy instead of
calling get_available_port utility method.

* Rename to git-pre-push

* Ideally public repo dont require CODECOV_TOKEN but codecov integration is broken since introduction of codecov-action@v2 (instead of codecov binary invocation)

* Issue is possibly with codecov@v2 action, fallback to codecov.  See https://github.com/abhinavsingh/proxy.py/runs/4110423084\?check_suite_focus\=true and https://github.com/codecov/uploader/issues/223

* Revert back to v2
2021-11-05 03:11:22 +05:30
Sviatoslav Sydorenko 05a78567ac
Enable the `add-trailing-comma` pre-commit fixer (#661)
Co-authored-by: Abhinav Singh <mailsforabhinav@gmail.com>
2021-11-04 16:58:36 +05:30
Sviatoslav Sydorenko 1bd97678c8
Correct spelling mistakes caught by `codespell` (#656)
Co-authored-by: Abhinav Singh <mailsforabhinav@gmail.com>
2021-11-03 17:33:05 +05:30
Abhinav Singh d4ee4fa15c
Async proxy pool, Event manager, Custom access log, Expose loop to plugins (#645)
* Async proxy pool

* Async proxy pool

* Late upstream initialization and exception guards

* Close upstream proxy connection on client connection close

* Refactor into EventManager

* Fix tests accounting in the event manager

* Ensure each process initializes logger

* pragma no cover

* Teardown connection when proxy pool upstream proxy closes

* Add ability to customize access log format and add additional context to it

* Maintain total size for response bytes in access logs

* Fix tests broken due to new plugin methods missing mock

* Update pubsub_eventing to use EventManager to avoid entire bootstrapping step
2021-11-01 01:19:19 +05:30
Abhinav Singh 7448c44cc6
v3.4.0 (#638)
* Build docker from 3.10-alpine

* Bump version to 3.4.0

* Add instructions for how to run dashboard

* Order of menu

* Override dashboard png path until submitted

* Add some doc string for top-level Proxy class.  Also some TODOs and warnings regarding PID file overwrite

* Allow HttpProxyBasePlugin implementations to register custom descriptors for read/write events

* Remove hardcoded adblock regex into json config. Update upstream filter to block facebook, not google

* ProxyPoolPlugin and ReverseProxyPlugin must now be updated to use get/read/write descriptor APIs

* Add get/read/write descriptor API for HttpWebServerBasePlugin too

* Surface actual listening port via flags.port
2021-10-30 04:32:05 +05:30
Aksh Gupta c09fc86277
DeepSource: Code quality issues (#523) 2021-03-23 07:29:11 +05:30
Abhinav Singh 094cd2c585
Add proxy auth test coverage (#496)
* Cover all proxy auth scenarios

* Add test_proxy_auth_works_with_mixed_case_basic_string
2021-02-03 17:08:45 +05:30
Abhinav Singh 137ce457bb
Separate basic auth plugin outside of core server (#453)
* Separate basic auth plugin outside of core

* Put basic auth plugin at top
2020-10-14 20:00:29 +05:30
Abhinav Singh 0744cd8e7f
Go flagless to allow custom user defined flags. (#452)
* Go flagless to allow custom user defined flags. Fixes #301

* Add --cache-dir flag for cache plugin (when used with on-disk store)

* Enable discovery of flags from external plugins, example those that reside outside of proxy.py package and loaded on demand.  This also allows external flags to surface in --help section

* Define --filtered-client-ips flag for FilterByClientIpPlugin
2020-10-14 10:51:56 +05:30
Abhinav Singh a48319e32d
Relax proxy auth requirement to allow mixed case for the auth type e.g. "basic", "Basic", "BaSiC" are all allowed (#451) 2020-10-13 20:56:23 +05:30
Abhinav Singh 1038bb841d
More examples (#444)
* Refactor into BaseServerHandler and BaseEchoServerHandler classes

* Add connect tunnel example
2020-10-06 22:27:19 +05:30
Abhinav Singh 8cc349be48
Allow plugins to add custom command line flags (#438)
* Allow plugins to add custom command line flags.  Addresses #301

* Reduce dependency over Flags class.  This will be deprecated so that adhoc flags can be added without any additional manual configuration

* Fix: Argument 1 to "mock_default_args" of "TestMain" has incompatible type "Namespace"; expected "Mock"

* Reduce Flags class to just the initializer.

* Store list of action dest in FlagParser
2020-10-03 16:25:43 +05:30
Pascal COMBES e3a1b7f963 Ensure plugins are loaded only once.
Also changed module name for plugins passed by type.
2020-08-11 22:14:51 +02:00
Pascal COMBES 46ec410610 Tests for Flags.load_plugins method. 2020-08-11 22:11:50 +02:00
Pascal COMBES 1b8d9a8e67 Pass a list plugin class objects or bytes to proxy when used in embeded mode.
No automated tests for the feature yet.
2020-08-11 16:28:32 +02:00
Mike aedf5933f2
Add plugin "FilterByURLRegexPlugin" (#397)
* Initial draft of filter_by_url_regex.py

* Add FilterByURLRegexPlugin

* Fix dictionary key & add logging

* Add proper logging

* Add better logging

* Add logging

* move code to handle_client_request

* development logging

* development

* development

* development

* dev

* dev

* dev

* dev

* dev

* dev

* dev

* dev

* dev

* dev

* dev

* Fix blocked log

* Add to FILTER_LIST, some tidy up

* Update FILTER_LIST

* dev

* remove scheme from url

* Add to FILTER_LIST

* Add to FILTER_LIST

* Update FILTER_LIST

* commenting

* Update FILTER_LIST

* After autopep8

* Fix Anomalous backslash in string (pep8)

* Address code quality checks - flake8 F401 & W605

* Address flake8 errors

* Attempt to fix flake8 errors

* Fix linting issues

* Address flake8 W292

* Attempt to create tests

* Add FilterByURLRegexPlugin

* Rename test

* Work on tests

* Work on tests

* Work on tests

Co-authored-by: Abhinav Singh <mailsforabhinav@gmail.com>
2020-07-13 10:40:34 +05:30
Abhinav Singh af3bdc23e7
Move codecov.yml to top level directory (#400) 2020-07-12 20:36:34 +05:30
Abhinav Singh 682114e9e0
Decouple SSL wrap logic into connection classes (#394)
* Move wrap functionality within respective connection classes. Also decouple websocket client handshake method

* Add a TCP echo client example that works with TCP echo server example
2020-07-08 13:11:12 +05:30
Abhinav Singh c884338f42
Core acceptor pool doc, cleanup and standalone example (#393)
* Better document acceptor module and add a TCP Echo Server example

* autopep8 formating

* Rename ThreadlessWork --> Work class

* Make initialize, is_inactive and shutdown as optional interface methods.

Also introduce Readables & Writables custom types.

* Move websocket code into its own module

* Add websocket client example

* Cleanup websocket client
2020-07-07 18:01:49 +05:30
Abhinav Singh 1b0ed923d7
Refactor plugin base classes for plugin specific flags (#388)
* Update to latest code signing recommendations

* Move HttpProtocolHandlerPlugin into separate file

* Dont add subject attributes if not provided by upstream. Also handle subprocess.TimeoutExpired raised during certificate generation.  Instead of retries, we simply close the connection on timeout

* Remove plugin specific flag initialization methods for now
2020-07-04 18:17:11 +05:30
Abhinav Singh 1b2966140c
Copy certificate subject from peercertificate, use ssl.PROTOCOL_TLSv1_2 for client wrap and allow TLSv1_1 for server wrap (#370) 2020-06-13 21:42:12 +05:30
Abhinav Singh e7aa8a28f7
TLS Interception Cert Generation (#362)
* Use common.pki for interception certificate generation

* Fix tests

* Dont use certificate fields that we dont need, it leads to certificate generation error on Ubuntu

* Prepare for v2.2.0

* npm audit fix
2020-06-09 12:07:00 +05:30
Abhinav Singh 7a67d0ffe4
urlsplit compliance for https CONNECT (#360)
* Add fix to be complaint with urlparse.urlsplit semantics which expects a fully qualified URL

* Log exception stacktrace on OSError
2020-06-06 19:10:41 +05:30
Abhinav Singh 08824869c2
Fix & Re-enable VCR testing on Linux (#323)
* Fix/Re-enable VCR test on Linux

* Disable tests for Windows due to weird permission issues
2020-03-26 12:31:00 +05:30
Abhinav Singh ab1198268c
Add flag to specify custom system CA Path (#321)
* Fixes #320

* Update README and add codecov.yml

* Update codecov.yml
2020-03-25 13:30:19 +05:30
Abhinav Singh 87a54a0781
Fixes #267 (#277)
* Fixes #267

* Prepare for v2.1.1
2020-01-30 21:54:02 -08:00
Abhinav Singh ee4e4ce41e
Add py.typed marker, add version checker, remove deprecated methods (#255)
* Add py.typed marker

* Add version-check.py

* Remove deprecated assertDictContainsSubset usage
2020-01-06 21:23:38 -08:00
Abhinav Singh e84c212465
Integration testing (#243)
* Add tests for public/private/csr generation

* Add integration testing skeleton for mac and ubuntu

* Merge integration within lib test to avoid too many workflows

* Disable integration testing on windows for now

* Use sudo to start integration test script as lsof fails on MacOS.

lsof: WARNING: can't stat() vmhgfs file system

* Add basic integration testing for now to assert proxy works as expected when started out of develop branch

* Add a call to inbuilt http server to verify it works

* wait for server to accept requests
2019-12-25 16:39:18 -08:00
Abhinav Singh 63e6d22566
Optionally initialize manager in main thread and use the same for EventQueue initialization (#239) 2019-12-20 17:10:35 -08:00
Abhinav Singh c6c09395f9
Share lock to acceptors via pool (#238)
* Move manager initialization outside of top level scope.  Fixes #233

* Share lock to acceptor via pool
2019-12-20 15:13:46 -08:00
Abhinav Singh ccaf868921
Response parser now reaches COMPLETE even when no body is expected (#220)
* Stash current changes

* Refactor into connection module

* Response parser state complete when no body expect

* Raise NotImplementedError if invalid state reached within parser
2019-12-02 19:18:43 -08:00
Abhinav Singh 269484df2e
Make HTTP handler constructor free of socket file number (#219)
* Refactor into acceptor module

* Add tunnel doc

* Make fileno free

* Autopep8
2019-12-02 15:55:08 -08:00
Abhinav Singh 6137fd6f82
Refactor (#213)
* Add DEFAULT_HTTP_PORT constant

* Use DEFAULT_HTTP_PORT in tests

* Refactor into exception module

* Refactor into inspector module

* Refactor into server module

* Refactor into proxy module
2019-12-01 22:46:00 -08:00
Abhinav Singh 4713ad6ea7
Homebrew formula (#209)
* Add homebrew formula

* Build PyPi package and Homebrew installation verification

* Check develop

* bdist_wheel reported as error: invalid command "bdist_wheel"

* Move under stable/develop folders to keep Proxy class name same

* uff

* develop installs proxy not proxy.py binary

* Prepend site-packages

* Install typing-extensions explicitly with brew

* Use find_packages

* Most likely failing due to lack of find_packages in current develop branch

* Fix windows setup.py build

* test_static_web_server_serves seems flaky on Ubuntu python 3.8

* Add instructions to install using homebrew

* Disable test_static_web_server_serves on GitHub actions, seems flaky
2019-12-01 19:01:28 -08:00
Abhinav Singh bfbbc5a82a
Test Dashboard backend (#206)
* Update shortlink gif name

* Conditionally run workflows as necessary

* Use pytest

* It works but github workflow is not reporting any status :(

* Separate out badges

* Add python_requires to setup.py
2019-11-30 22:54:20 -08:00