From e154997153f893211001fd5bdc7704db8236cb1b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Al=D0=B5x=D0=B0nder=20Lysik=D0=BEv?=
 <alexander.lysikov@demlabs.net>
Date: Tue, 2 Jun 2020 14:33:08 +0500
Subject: [PATCH] fixed bug with a foreign continent in the server "auto"

---
 .../dap_chain_net_srv_vpn_cdb_server_list.c   | 55 +++++++++++--------
 1 file changed, 33 insertions(+), 22 deletions(-)

diff --git a/modules/service/vpn/dap_chain_net_srv_vpn_cdb_server_list.c b/modules/service/vpn/dap_chain_net_srv_vpn_cdb_server_list.c
index 80fe55c56b..b03956319f 100644
--- a/modules/service/vpn/dap_chain_net_srv_vpn_cdb_server_list.c
+++ b/modules/service/vpn/dap_chain_net_srv_vpn_cdb_server_list.c
@@ -156,7 +156,7 @@ static void s_http_simple_proc(dap_http_simple_t *a_http_simple, void *a_arg)
     dap_string_t *l_reply_str = dap_string_new("[\n");
 
 
-    char *l_client_ip = a_http_simple->http->client->s_ip;//"77.222.110.44"
+    char *l_client_ip = a_http_simple->http->client->s_ip;//"64.225.61.216"
     geoip_info_t *l_geoip_info = chain_net_geoip_get_ip_info(l_client_ip);
 
     log_it(L_DEBUG, "Have %zd chain networks for cdb lists", s_cdb_net_count );
@@ -232,28 +232,39 @@ static void s_http_simple_proc(dap_http_simple_t *a_http_simple, void *a_arg)
 
             int8_t l_client_continent = l_geoip_info ? dap_chain_net_srv_order_continent_to_num(l_geoip_info->continent) : 0;
             // random node on client's continent
-			if (l_client_continent) {
-				int l_count = 0;
-				while (l_orders_num > 0) {
-					size_t k = rand() % l_continents_numbers[l_client_continent];
-					dap_chain_net_srv_order_t *l_order = l_orders_pos[k];
-					const char *country_code = dap_chain_net_srv_order_get_country_code(l_order);
-					if (country_code) {
-						// only for other countries
-						if (dap_strcmp(l_geoip_info->country_code, country_code)){
-							if (!order_info_print(l_reply_str, l_net, l_order, "Auto", -1)) {
-								dap_string_append_printf(l_reply_str, ",\n");
-								break;
-							}
-						}
-					}
-					if (l_count > 20)
-						break;
-					l_count++;
-				}
+            if(l_client_continent > 0 && l_continents_numbers[l_client_continent] > 1) {
+                int l_count = 0;
+                while(l_orders_num > 0) {
+                    size_t k = rand() % l_continents_numbers[l_client_continent];
+                    size_t l_node_pos = -1;
+                    for(size_t j2 = 0; j2 <= l_orders_num; j2++) {
+                        if(k == l_node_numbering[l_client_continent][j2]) {
+                            l_node_pos = j2;
+                            break;
+                        }
+                    }
+                    if(l_node_pos == -1) {
+                        // random node for the whole world
+                        l_node_pos = rand() % l_orders_num;
+                    }
+                    dap_chain_net_srv_order_t *l_order = l_orders_pos[l_node_pos];
+                    const char *country_code = dap_chain_net_srv_order_get_country_code(l_order);
+                    if(country_code) {
+                        // only for other countries
+                        if(dap_strcmp(l_geoip_info->country_code, country_code)) {
+                            if(!order_info_print(l_reply_str, l_net, l_order, "Auto", -1)) {
+                                dap_string_append_printf(l_reply_str, ",\n");
+                                break;
+                            }
+                        }
+                    }
+                    if(l_count > 20)
+                        break;
+                    l_count++;
+                }
 
-			}
-			// random node for the whole world
+            }
+            // random node for the whole world
 			else {
 				int l_count = 0;
 				while(l_orders_num > 0) {
-- 
GitLab