yaroslavyaroslav / OpenAI-sublime-text

First class Sublime Text AI assistant with GPT-4o and llama.cpp support!
MIT License
130 stars 11 forks source link

OSError: Tunnel connection failed: 407 Proxy Authentication Required #25

Closed finalblast closed 9 months ago

finalblast commented 9 months ago

Please add support for private proxy.

yaroslavyaroslav commented 9 months ago

Hi, do you mean the one with a password?

finalblast commented 9 months ago

Yes, that's correct. Proxy with username and password.

yaroslavyaroslav commented 9 months ago

gpt-4 provides me with following code, could you validate it before it being included in the sources?

from http.client import HTTPSConnection
from base64 import b64encode

proxy_host = "proxy_host"
proxy_port = "proxy_port"
proxy_username = "username"
proxy_password = "password"

# Encode proxy username and password into base64
proxy_auth = b64encode(bytes(f'{proxy_username}:{proxy_password}', 'utf-8')).strip().decode('ascii')

# Set up original connection
conn = HTTPSConnection(proxy_host, proxy_port)

# Set up tunnel over the proxy to the actual address
conn.set_tunnel('www.somehost.com', headers={
    'Proxy-Authorization': f'Basic {proxy_auth}'
})

# Now perform request as usual
conn.request('GET', '/index.html')

res = conn.getresponse()
data = res.read()

I relied on http.client for a network level implementation, which is such a pain in the ass for any unusual movement (and mostly usual though), I planed to migrate to a requests one, but it's not a subject of a nearest future, Q1 2024 most likely I believe.

finalblast commented 9 months ago

{proxy_username}:{proxy_password} should be username and password in proxy configs. I think all should be good.

yaroslavyaroslav commented 9 months ago

Great, thank you. Would you test a separate branch with these changes applied there to report whether it work or not?

finalblast commented 9 months ago

@yaroslavyaroslav Yes, it works with these changes:

proxy_settings = self.settings.get('proxy')
        if isinstance(proxy_settings, dict):
            address = proxy_settings.get('address')
            port = proxy_settings.get('port')
            proxy_username = proxy_settings.get('username')
            proxy_password = proxy_settings.get('password')
            proxy_auth = b64encode(bytes(f'{proxy_username}:{proxy_password}', 'utf-8')).strip().decode('ascii')
            if address and len(address) > 0 and port:
                self.connection = HTTPSConnection(
                    host=address,
                    port=port
                )
                self.connection.set_tunnel("api.openai.com", headers={
                    'Proxy-Authorization': f'Basic {proxy_auth}'
                })
            else:
                self.connection = HTTPSConnection("api.openai.com")
finalblast commented 9 months ago

Don't forget to import at the top

from base64 import b64encode
yaroslavyaroslav commented 9 months ago

I'll make this enhancement kinda soonish

yaroslavyaroslav commented 9 months ago

@finalblast please review changes in #27.

finalblast commented 9 months ago

Changes tested and all good.