@@ -95,33 +95,41 @@ def load_config_from_yaml(self, config_yaml):
95
95
return False
96
96
97
97
async def proxy_dispatcher (self , proxy_web_socket , path ):
98
- self .logger .log ("Connection established with CLIENT" )
98
+ self .logger .log ("Connection established with CLIENT at " + path )
99
99
100
100
connection = WebSocketConnection ()
101
101
102
- if not self .is_forced_url_no_password_server ():
102
+ if self .requires_authentication ():
103
103
connection .credentials = await self .get_credentials (proxy_web_socket )
104
104
105
105
if self .authenticate (connection ):
106
- authenticated_message = "Authenticated " + self .get_post_authentication_directions ()
107
- await proxy_web_socket .send (get_json_status_response ("ok" , f"{ authenticated_message } '" ))
108
- if self .is_open_url_server ():
109
- proxied_url_value = await self .get_proxy_url_from_client (proxy_web_socket )
110
- if proxied_url_value is None :
111
- return
112
- else :
113
- proxied_url_value = self .proxied_url
114
-
115
- proxied_web_socket = await self .connect_to_proxy_server (proxied_url_value , proxy_web_socket )
116
- await self .process_arbitrary_requests (proxy_web_socket , proxied_web_socket , connection )
106
+ await self .handle_authenticated_connection (connection , proxy_web_socket )
117
107
else :
118
- auth_failed_message = "Authentication failed. Password invalid [" + connection .credentials + "]"
119
- await proxy_web_socket .send (get_json_status_response ("error" , auth_failed_message + "'}" ))
120
- self .logger .log ("CLIENT authentication credentials [%s] rejected." , connection .credentials )
108
+ await self .handle_failed_authentication (connection , proxy_web_socket )
109
+ else :
110
+ await self .handle_connection_without_authentication (connection , proxy_web_socket )
111
+
112
+ async def handle_authenticated_connection (self , connection , proxy_web_socket ):
113
+ authenticated_message = "Authenticated " + self .get_post_authentication_directions ()
114
+ await proxy_web_socket .send (get_json_status_response ("ok" , f"{ authenticated_message } '" ))
115
+ if self .is_open_url_server ():
116
+ proxied_url_value = await self .get_proxy_url_from_client (proxy_web_socket )
117
+ if proxied_url_value is None :
118
+ return
121
119
else :
122
120
proxied_url_value = self .proxied_url
123
- proxied_web_socket = await self .connect_to_proxy_server (proxied_url_value , proxy_web_socket )
124
- await self .process_arbitrary_requests (proxy_web_socket , proxied_web_socket , connection )
121
+ proxied_web_socket = await self .connect_to_proxy_server (proxied_url_value , proxy_web_socket )
122
+ await self .process_arbitrary_requests (proxy_web_socket , proxied_web_socket , connection )
123
+
124
+ async def handle_connection_without_authentication (self , connection , proxy_web_socket ):
125
+ proxied_url_value = self .proxied_url
126
+ proxied_web_socket = await self .connect_to_proxy_server (proxied_url_value , proxy_web_socket )
127
+ await self .process_arbitrary_requests (proxy_web_socket , proxied_web_socket , connection )
128
+
129
+ async def handle_failed_authentication (self , connection , proxy_web_socket ):
130
+ auth_failed_message = "Authentication failed. Password invalid [" + connection .credentials + "]"
131
+ await proxy_web_socket .send (get_json_status_response ("error" , auth_failed_message + "'}" ))
132
+ self .logger .log ("CLIENT authentication credentials [%s] rejected." , connection .credentials )
125
133
126
134
async def respond_with_proxy_connect_error (self , proxied_url_value , proxy_web_socket ):
127
135
error_message = "Unable to connect with proxied url [" + proxied_url_value + "]. Connection closed."
@@ -238,6 +246,9 @@ async def connect_to_proxy_server(self, proxied_url_value, proxy_web_socket):
238
246
239
247
return proxied_web_socket
240
248
249
+ def requires_authentication (self ):
250
+ return not self .is_forced_url_no_password_server ();
251
+
241
252
242
253
class WebSocketConnection :
243
254
request_count = 0
0 commit comments