From: nshero.lee Date: Tue, 23 Jan 2018 09:25:23 +0000 (+0900) Subject: Add proxy setting function X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bb136eac09bc466d5e554bf87ecf11a35d2430b5;p=platform%2Fcore%2Fsystem%2Fsetup-adaptor.git Add proxy setting function In order to set the proxy value in specific file to read from sytemd. It has to parse the proxy and save it into /etc/dockzen/environment the file is reading when systemd is started for dockzen-launcher services It would be effect to the docker too. Change-Id: Icc59eacd4aa08b02e6d557e95d8f14dbb7a86765 Signed-off-by: nshero.lee --- diff --git a/packaging/setup-adaptor.spec b/packaging/setup-adaptor.spec index a4aa32d..3b758f0 100644 --- a/packaging/setup-adaptor.spec +++ b/packaging/setup-adaptor.spec @@ -1,6 +1,6 @@ Name: setup-adaptor Summary: Provide network and system data setting daemon -Version: 0.3.0 +Version: 0.4.0 Release: 0 Group: System & System Tools ExclusiveArch: armv7l diff --git a/src/input_file.c b/src/input_file.c index 9238a77..175c2f7 100644 --- a/src/input_file.c +++ b/src/input_file.c @@ -30,6 +30,12 @@ #define CONFIG_FILE "/opt/etc/setup-adaptor/config.json" #define CONFIG_FOLDER "/opt/etc/setup-adaptor" #define CONFIG_NAME "config.json" + +#define ENVIRONMENT_PROXY_FILE "/etc/dockzen/environment" + +#define FLAG_FILE_SYSTEM "/opt/etc/setup-adaptor/system_executed" +#define FLAG_FILE_ETH "/opt/etc/setup-adaptor/ethernet_executed" +#define FLAG_FILE_WIFI "/opt/etc/setup-adaptor/wifi_executed" #define EVENT_NAME_MAX 256 #define EVENT_SIZE (sizeof(struct inotify_event)) #define EVENT_BUF_LEN (512 * (EVENT_SIZE + EVENT_NAME_MAX)) @@ -249,10 +255,8 @@ static void print_network_config(sa_network_s * network) static void print_system_config(sa_system_s * systemData) { if (systemData != NULL) { - if (systemData->proxy != NULL) { - _D("systemData::httpProxyHost [%s]", systemData->proxy->httpProxyHost); - _D("systemData::HttpProxyPort [%d]", systemData->proxy->httpProxyPort); - } + if (systemData->proxy != NULL) + _D("systemData::httpProxy [%s]", systemData->proxy->httpProxy); } } @@ -443,22 +447,20 @@ static int __parse_network_data(json_object * inputObj, sa_config_s * setupConfi static int __parse_system_data(json_object * inputObj, sa_config_s * setupConfig) { int ret = 0; - char *httpProxyHost = NULL; + char *httpProxy = NULL; + if (inputObj == NULL) { _D("__parse_system_data input error"); return -1; } - //httpProxyHost - httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST); - if (httpProxyHost != NULL) { + //httpProxy + httpProxy = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXY); + if (httpProxy != NULL) { setupConfig->systemData = (sa_system_s *) malloc(sizeof(sa_system_s)); if (setupConfig->systemData != NULL) { setupConfig->systemData->proxy = (sa_proxy_s *) malloc(sizeof(sa_proxy_s)); if (setupConfig->systemData->proxy != NULL) { - memcpy(setupConfig->systemData->proxy->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(setupConfig->systemData->proxy->httpProxyHost) - 1)); - //httpProxyPort - setupConfig->systemData->proxy->httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT); - + memcpy(setupConfig->systemData->proxy->httpProxy, httpProxy, MIN(strlen(httpProxy), sizeof(setupConfig->systemData->proxy->httpProxy) - 1)); } else { ret = -1; _D("malloc fail setupConfig->systemData->proxy"); @@ -467,9 +469,11 @@ static int __parse_system_data(json_object * inputObj, sa_config_s * setupConfig ret = -1; _D("malloc fail setupConfig->systemData"); } + } - free(httpProxyHost); - httpProxyHost = NULL; + if (httpProxy != NULL) { + free(httpProxy); + httpProxy = NULL; } return ret; @@ -647,10 +651,6 @@ void sa_inputfile_release_resource(sa_config_s * config) } } -#define FLAG_FILE_SYSTEM "/opt/etc/setup-adaptor/system_executed" -#define FLAG_FILE_ETH "/opt/etc/setup-adaptor/ethernet_executed" -#define FLAG_FILE_WIFI "/opt/etc/setup-adaptor/wifi_executed" - void sa_inputfile_remove(void) { char buff[256]; @@ -735,3 +735,42 @@ gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type) else return FALSE; } + +sa_error_e sa_inputfile_set_proxy_env(sa_proxy_s * proxy) +{ + int fd = 0; + int len = 0; + sa_error_e ret = SA_ERROR_NONE; + char value[256] = {0, }; + char writeBuff[512] = {0, }; + + if (proxy == NULL) { + _E("input parameter is null"); + return SA_ERROR_INVALID_PARAMETER; + } + + // HTTP_PROXY + _D("Proxy[%s]", proxy->httpProxy); + + // make string + if (strlen(proxy->httpProxy) > 0 && strlen(proxy->httpProxy) < (sizeof(proxy->httpProxy)-1)) + snprintf(writeBuff, strlen(SA_CONFIG_SERVICE_HTTP_PROXY) + strlen(SA_CONFIG_SERVICE_OPERATOR) + strlen(proxy->httpProxy) + 1, "%s%s%s", SA_CONFIG_SERVICE_HTTP_PROXY, SA_CONFIG_SERVICE_OPERATOR, proxy->httpProxy); + else { + ret = SA_ERROR_NOT_AVAILABLE; + _E("Not available to set due to buffer overflow"); + } + + //write data into file + if (ret == SA_ERROR_NONE) { + fd = open(ENVIRONMENT_PROXY_FILE, O_WRONLY | O_CREAT, 0644); + if (fd > 0) { + write(fd, writeBuff, strlen(writeBuff)); + close(fd); + } else { + ret = SA_ERROR_NOT_AVAILABLE; + _E("Cannot open file"); + } + } + + return ret; +} diff --git a/src/input_file.h b/src/input_file.h index 501c9f3..0e979d7 100644 --- a/src/input_file.h +++ b/src/input_file.h @@ -82,4 +82,11 @@ void sa_inputfile_set_completion_flag(sa_file_config_e config_type); */ gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type); +/** + * @fn sa_error_e sa_inputfile_set_proxy_env + * @brief This function to set proxy value in environment file + * @param sa_proxy_s for proxy information + * @return sa_file_state_e return of state + */ +sa_error_e sa_inputfile_set_proxy_env(sa_proxy_s * proxy); #endif /* __INPUT_FILE_H__ */ diff --git a/src/sa_types.h b/src/sa_types.h index 4c05371..f42e20d 100644 --- a/src/sa_types.h +++ b/src/sa_types.h @@ -34,8 +34,12 @@ #define SA_CONFIG_NETWORKDATA_PRIMARYDNSSERVER "primaryDnsServer" #define SA_CONFIG_NETWORKDATA_SECONDARYDNSSERVER "secondaryDnsServer" -#define SA_CONFIG_NETWORKDATA_HTTPPROXYHOST "httpProxyHost" -#define SA_CONFIG_NETWORKDATA_HTTPPROXYPORT "httpProxyPort" +#define SA_CONFIG_NETWORKDATA_HTTPPROXY "httpProxy" + +#define SA_CONFIG_SERVICE_ENVIRONMENTFILE "EnvironmentFile" +#define SA_CONFIG_SERVICE_HTTP_PROXY "HTTP_PROXY" +#define SA_CONFIG_SERVICE_OPERATOR "=" + /** * @brief This enum contains setup-adaptor error information @@ -157,12 +161,11 @@ typedef struct { * @struct sa_proxy_s * @brief This struct contains proxy information * - * The sa_network_s struct encapsulate httpproxyHost and httpProxyPort information in the one data + * The sa_network_s struct encapsulate httpProxy information in the one data * */ typedef struct { - char httpProxyHost[255 + 1]; - int httpProxyPort; + char httpProxy[255 + 1]; } sa_proxy_s; /** diff --git a/src/setup_system.c b/src/setup_system.c index 7d09c86..4c51357 100644 --- a/src/setup_system.c +++ b/src/setup_system.c @@ -16,37 +16,14 @@ #include #include "sa_common.h" #include "sa_types.h" +#include "input_file.h" #include "setup_system.h" -static sa_error_e system_set_proxy(sa_proxy_s * info) -{ - return SA_ERROR_NOT_SUPPORTED; -} - static sa_error_e system_get_proxy(sa_proxy_s * info) { return SA_ERROR_NOT_SUPPORTED; } -static int compare_proxy_info(sa_proxy_s * devProxy, sa_proxy_s * newProxy) -{ - // comapre proxy - if (devProxy != NULL && newProxy != NULL) { - if (strlen(devProxy->httpProxyHost) > 0) { - if (strcmp(devProxy->httpProxyHost, newProxy->httpProxyHost)) { - // proxy info is different - return 1; - } else { - // Host address is the same, but port is different - if (devProxy->httpProxyPort != newProxy->httpProxyPort) - return 1; - } - } - } - - return 0; -} - sa_error_e sa_setup_system(sa_system_s * system) { sa_proxy_s *devProxy = NULL; @@ -58,11 +35,8 @@ sa_error_e sa_setup_system(sa_system_s * system) } if (system->proxy != NULL) { - ret = system_get_proxy(devProxy); - if (ret == SA_ERROR_NONE && compare_proxy_info(devProxy, system->proxy)) { - // set proxy - ret = system_set_proxy(system->proxy); - } + // set proxy + ret = sa_inputfile_set_proxy_env(system->proxy); } return ret; diff --git a/test/config.json b/test/config.json index bf0ac9a..7ebc29d 100644 --- a/test/config.json +++ b/test/config.json @@ -2,6 +2,5 @@ "version":"0.1", "wifi":{"ssid":"docker","password":"dockerdocker","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"}, "ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"}, -"httpProxyHost":"127.0.0.1", -"httpProxyPort":8080 +"httpProxy":"127.0.0.1:8080" } \ No newline at end of file