-
-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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>
- Loading branch information
1 parent
da23c7f
commit d3cee32
Showing
13 changed files
with
414 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
proxy.py | ||
~~~~~~~~ | ||
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on | ||
Network monitoring, controls & Application development, testing, debugging. | ||
:copyright: (c) 2013-present by Abhinav Singh and contributors. | ||
:license: BSD, see LICENSE for more details. | ||
""" | ||
import time | ||
import socket | ||
|
||
from typing import Dict | ||
|
||
from proxy.proxy import Proxy | ||
from proxy.core.acceptor import Work, AcceptorPool | ||
from proxy.common.types import Readables, Writables | ||
|
||
|
||
class WebScraper(Work): | ||
"""Demonstrates how to orchestrate a generic work acceptors and executors | ||
workflow using proxy.py core. | ||
By default, `WebScraper` expects to receive work from a file on disk. | ||
Each line in the file must be a URL to scrape. Received URL is scrapped | ||
by the implementation in this class. | ||
After scrapping, results are published to the eventing core. One or several | ||
result subscriber can then handle the result as necessary. Currently, result | ||
subscribers consume the scrapped response and write discovered URL in the | ||
file on the disk. This creates a feedback loop. Allowing WebScraper to | ||
continue endlessly. | ||
NOTE: No loop detection is performed currently. | ||
NOTE: File descriptor need not point to a file on disk. | ||
Example, file descriptor can be a database connection. | ||
For simplicity, imagine a Redis server connection handling | ||
only PUBSUB protocol. | ||
""" | ||
|
||
def get_events(self) -> Dict[socket.socket, int]: | ||
"""Return sockets and events (read or write) that we are interested in.""" | ||
return {} | ||
|
||
def handle_events( | ||
self, | ||
readables: Readables, | ||
writables: Writables, | ||
) -> bool: | ||
"""Handle readable and writable sockets. | ||
Return True to shutdown work.""" | ||
return False | ||
|
||
|
||
if __name__ == '__main__': | ||
with AcceptorPool( | ||
flags=Proxy.initialize( | ||
port=12345, | ||
num_workers=1, | ||
threadless=True, | ||
keyfile='https-key.pem', | ||
certfile='https-signed-cert.pem', | ||
), | ||
work_klass=WebScraper, | ||
) as pool: | ||
while True: | ||
time.sleep(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.