Skip to content

Commit

Permalink
👋 Added Proxy Support #69
Browse files Browse the repository at this point in the history
  • Loading branch information
soranoo committed Mar 19, 2024
1 parent d46433b commit 0843f8a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Give me a ⭐ if you like it.
### Portal ↠ [Installation](docs/gettingstarted.md#installing-python-package) · [Usage](docs/gettingstarted.md#setting-up-tradingview-alert) · [Cloud Versions](docs/cloud-versions/cloud-versions.md) · [Videos](https://www.youtube.com/playlist?list=PLOHaKcov3Nkt0LIK1joPYgFnZY24zf_Wo)

## :newspaper: NEWS
###### <<< - [Mar 19, 2024] - >>>
Added proxy support for all versions. Check out the config file for more details.

###### <<< - 🎄 [Dec 24, 2023] 🎁 - >>>
Added Docker Version ([Read Docs](docs/gettingstarted.md#2-ngrok-version))

Expand Down Expand Up @@ -75,6 +78,11 @@ For example,
* You may send a webhook to [3commas](https://3commas.io/) for auto trading.
* You may send a webhook to [Discord](https://discord.com/) for sharing the signal.

## :question: FAQ
* **Q: Some exchnage banned my IP. What should I do?**
- A: You may use a proxy to bypass the IP ban. You should see there is a proxy option in the config file. Related issue: [#69](https://github.com/soranoo/TradingView-Free-Webhook-Alerts/issues/69)


## ⭐ TODO
* N/A

Expand Down
9 changes: 8 additions & 1 deletion cloud-versions/pipedream.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#r"https://yourwebhook3.com",
]

proxy_url = "" # eg. http://username:password@0.0.0.0:8080

# vv for logging (fill in the webhook URL if you need)
discord_webhook_url = r""

Expand All @@ -30,6 +32,11 @@

show_welcome_msg = False

proxies = {
"http": proxy_url,
"https": proxy_url
} if proxy_url else None

url_regex = "https?://([A-Za-z_0-9.-]+).*"

dc_embeds_sample = [{
Expand Down Expand Up @@ -101,7 +108,7 @@ def post_request(webhook_url:str, payload:str or json, auto_json_dumps:bool = Tr
session.mount('http://', adapter)
session.mount('https://', adapter)
# send request
response = session.post(webhook_url, data=data, headers=headers)
response = session.post(webhook_url, data=data, headers=headers, proxies=proxies)
return response

def send_msg_to_dc(message, embeds=None):
Expand Down
4 changes: 3 additions & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ webhook_urls = [
"Webhook URL",
]

proxy_url = "" # eg. http://username:password@0.0.0.0:8080

# ---------------* Dicord *---------------
discord_log = false
discord_webhook_url = ""
Expand All @@ -41,4 +43,4 @@ log_time_zone = false
log_full_color = false

# ----------------* don't touch *----------------
config_version = "1.0.0"
config_version = "1.0.1"
19 changes: 16 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# from bs4 import BeautifulSoup

from src import EmailListener
from src import config, send_post_request, StoppableThread
from src import config, send_post_request, StoppableThread, is_url_valid
from src import log , DiscordEmbed, create_logger
from src import project_main_directory
from src import event_subscribe, event_unsubscribe, event_post
Expand All @@ -37,6 +37,7 @@
ngrok_auth_token = config.get("ngrok_auth_token")

webhook_urls = config.get("webhook_urls")
proxy_url = config.get("proxy_url")

discord_log = config.get("discord_log")
discord_webhook_url = config.get("discord_webhook_url")
Expand All @@ -54,9 +55,21 @@

# ---------------* Main *---------------
__version__ = "2.6.3"
expect_config_version = "1.0.0"
expect_config_version = "1.0.1"
github_config_toml_url = "https://github.com/soranoo/TradingView-Free-Webhook-Alerts/blob/main/config.example.toml"

proxies = {
"http": proxy_url,
"https": proxy_url
} if proxy_url else None

if proxies != None:
if is_valid_url := is_url_valid(proxy_url):
log.info(f"Using proxy: {proxy_url}")
else:
log.error(f"Invalid proxy URL: {proxy_url}")
exit()

if not mode_traditional:
try_import("pyngrok")
from pyngrok import ngrok, conf as ngrok_conf
Expand Down Expand Up @@ -312,7 +325,7 @@ def send_webhook(payload:str or dict):
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11"
}
for webhook_url in webhook_urls:
res = send_post_request(webhook_url, payload, headers)
res = send_post_request(webhook_url, payload, headers, proxies=proxies)
if res.status_code in [200, 201, 202, 203, 204]:
log.ok(f"Sent webhook to {webhook_url} successfully, response code: {res.status_code}")
elif retry_after := res.headers.get("Retry-After"):
Expand Down
2 changes: 1 addition & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import toml as _toml
import os as _os

from .network import send_post_request
from .network import send_post_request, is_url_valid
from .event import subscribe as event_subscribe, unsubscribe as event_unsubscribe, post_event as event_post
from .logger import logger as log, add_logging_level, Colorcode, create_logger
from .http_status import http_status
Expand Down
5 changes: 4 additions & 1 deletion src/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ def extract_url_domain(url:str) -> bool or str:
"""
return m[1] if (m := re.search(URL_REGEX, url)) else None

def send_post_request(url:str, payload:str or dict, headers:dict = None) -> requests.models.Response:
def send_post_request(url:str, payload:str or dict, headers:dict = None, proxies:dict = None) -> requests.models.Response:
"""
### Description ###
Send HTTP POST request
### Parameters ###
- `url` (str): URL
- `payload` (str or dict): Payload
- `headers` (dict): Headers
- `proxies` (dict): Proxies
### Returns ###
- (requests.models.Response): Response
Expand All @@ -71,4 +73,5 @@ def send_post_request(url:str, payload:str or dict, headers:dict = None) -> requ
url,
data=payload,
headers=POST_REQUEST_HEADERS if headers is None else headers,
proxies=proxies
)

0 comments on commit 0843f8a

Please sign in to comment.