Skip to content

Commit

Permalink
Add socks proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
cokeBeer committed Apr 11, 2023
1 parent e64bf0a commit c34fcf9
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ReconAIzer v0.3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from javax.swing import JMenuItem, JPanel, JTextArea, JScrollPane, ScrollPaneConstants, JTextField, JButton, JLabel
from java.awt import BorderLayout
from java.util import ArrayList
from java.net import URL, HttpURLConnection
from java.net import URL, HttpURLConnection, Proxy, InetSocketAddress;
from java.io import BufferedReader, InputStreamReader, DataOutputStream
from org.python.core.util import StringUtil
from java.lang import Runnable, Thread
Expand Down Expand Up @@ -114,6 +114,9 @@ def redact_sensitive_headers(self, request_string):
def send_request_to_openai(self, text, prompt_type):
API_KEY = "[YOUR OPENAI API KEY]"
OPENAI_API_URL = "https://api.openai.com/v1/chat/completions"
# Use proxy if SOCKS_PROXY_URL is set, e.g. 127.0.0.1
SOCKS_PROXY_URL = ""
SOCKS_PROXY_PROT = 7890

headers = {
"Content-Type": "application/json",
Expand Down Expand Up @@ -142,7 +145,7 @@ def send_request_to_openai(self, text, prompt_type):
retry_delay = 2

for attempt in range(max_retries):
connection = self.send_post_request(OPENAI_API_URL, headers, json.dumps(data))
connection = self.send_post_request(OPENAI_API_URL, headers, json.dumps(data), proxy_url = SOCKS_PROXY_URL, proxy_port = SOCKS_PROXY_PROT)
response_code = connection.getResponseCode()

if response_code == 429:
Expand All @@ -158,9 +161,13 @@ def send_request_to_openai(self, text, prompt_type):

raise Exception("Exceeded maximum retries for API request")

def send_post_request(self, url, headers, data):
def send_post_request(self, url, headers, data, proxy_url = "", proxy_port = 7890):
java_url = URL(url)
connection = java_url.openConnection()
if proxy_url !="":
proxy = Proxy(Proxy.Type.SOCKS, InetSocketAddress(proxy_url, proxy_port))
connection = java_url.openConnection(proxy)
else:
connection = java_url.openConnection()
connection.setDoOutput(True)
connection.setRequestMethod("POST")
for key, value in headers.items():
Expand Down Expand Up @@ -194,4 +201,4 @@ def __init__(self):
self.add(scroll_pane, BorderLayout.CENTER)

def update_text(self, text):
self._text_area.setText(text)
self._text_area.setText(text)

0 comments on commit c34fcf9

Please sign in to comment.