@@ -1096,18 +1096,19 @@ void udp_remote_on_dying(struct udp_remote_ctx_t *remote_ctx, void*p) {
10961096 (void )remote_ctx ;
10971097}
10981098
1099- void do_normal_response (struct tunnel_ctx * tunnel ) {
1099+ void do_normal_response (struct tunnel_ctx * tunnel , const char * domain ) {
11001100 struct server_ctx * ctx = (struct server_ctx * )tunnel -> data ;
11011101 struct server_config * config = ctx -> env -> config ;
11021102 struct socket_ctx * incoming = tunnel -> incoming ;
1103- char * http_ok = ws_normal_response (& malloc , config -> over_tls_server_domain );
1103+ char * http_ok = ws_normal_response (& malloc , domain );
11041104
11051105 ASSERT (config -> over_tls_enable );
11061106
11071107 tunnel_socket_ctx_write (tunnel , incoming , http_ok , strlen (http_ok ));
11081108 free (http_ok );
11091109
11101110 ctx -> stage = tunnel_stage_normal_response ;
1111+ (void )config ;
11111112}
11121113
11131114static void do_tls_init_package (struct tunnel_ctx * tunnel , struct socket_ctx * socket ) {
@@ -1117,6 +1118,8 @@ static void do_tls_init_package(struct tunnel_ctx *tunnel, struct socket_ctx *so
11171118 struct buffer_t * proto_confirm = NULL ;
11181119 struct buffer_t * result = NULL ;
11191120 struct http_headers * hdrs = NULL ;
1121+ char * domain = NULL ;
1122+ #define SAFE_GET_DOMAIN () (domain ? domain : config->over_tls_server_domain)
11201123 do {
11211124 uint8_t * indata = (uint8_t * )socket -> buf -> base ;
11221125 size_t len = (size_t )socket -> result ;
@@ -1136,11 +1139,22 @@ static void do_tls_init_package(struct tunnel_ctx *tunnel, struct socket_ctx *so
11361139 ctx -> tcp_mss = tcp_mss ;
11371140
11381141 hdrs = http_headers_parse (true, indata , len );
1142+ {
1143+ const char * h = http_headers_get_field_val (hdrs , HOST_STR );
1144+ if (h ) {
1145+ char * port ;
1146+ domain = strdup (h );
1147+ port = strchr (domain , ':' );
1148+ if (port ) {
1149+ * port = '\0' ;
1150+ }
1151+ }
1152+ }
11391153 {
11401154 const char * key = http_headers_get_field_val (hdrs , SEC_WEBSOKET_KEY );
11411155 const char * url = http_headers_get_url (hdrs );
11421156 if (key == NULL || url == NULL || 0 != strcmp (url , config -> over_tls_path )) {
1143- do_normal_response (tunnel );
1157+ do_normal_response (tunnel , SAFE_GET_DOMAIN () );
11441158 break ;
11451159 }
11461160 string_safe_assign (& ctx -> sec_websocket_key , key );
@@ -1150,12 +1164,12 @@ static void do_tls_init_package(struct tunnel_ctx *tunnel, struct socket_ctx *so
11501164 size_t p_len = 0 ;
11511165 const char * addr_field = http_headers_get_field_val (hdrs , TARGET_ADDRESS_STR );
11521166 if (addr_field == NULL ) {
1153- do_normal_response (tunnel );
1167+ do_normal_response (tunnel , SAFE_GET_DOMAIN () );
11541168 break ;
11551169 }
11561170 addr_p = std_base64_decode_alloc (addr_field , & malloc , & p_len );
11571171 if (addr_p == NULL ) {
1158- do_normal_response (tunnel );
1172+ do_normal_response (tunnel , SAFE_GET_DOMAIN () );
11591173 break ;
11601174 }
11611175 result = buffer_create_from (addr_p , p_len );
@@ -1178,7 +1192,7 @@ static void do_tls_init_package(struct tunnel_ctx *tunnel, struct socket_ctx *so
11781192 addr_p = url_safe_base64_decode_alloc (udp_field , & malloc , & p_len );
11791193 if (socks5_address_parse (addr_p , p_len , & target_addr , NULL ) == false) {
11801194 free (addr_p );
1181- do_normal_response (tunnel );
1195+ do_normal_response (tunnel , SAFE_GET_DOMAIN () );
11821196 break ;
11831197 }
11841198 free (addr_p );
@@ -1196,7 +1210,7 @@ static void do_tls_init_package(struct tunnel_ctx *tunnel, struct socket_ctx *so
11961210 }
11971211
11981212 if (result == NULL || is_legal_header (result ) == false) {
1199- do_normal_response (tunnel );
1213+ do_normal_response (tunnel , SAFE_GET_DOMAIN () );
12001214 break ;
12011215 }
12021216 buffer_replace (ctx -> target_address_with_data_pkg , result );
@@ -1206,6 +1220,8 @@ static void do_tls_init_package(struct tunnel_ctx *tunnel, struct socket_ctx *so
12061220 } while (0 );
12071221 http_headers_destroy (hdrs );
12081222 buffer_release (result );
1223+ free (domain );
1224+ #undef SAFE_GET_DOMAIN
12091225}
12101226
12111227static size_t _tls_get_read_size (struct tunnel_ctx * tunnel , struct socket_ctx * socket , size_t suggested_size ) {
0 commit comments