1*dd0948b3SAndroid Build Coastguard Worker /* 2*dd0948b3SAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project 3*dd0948b3SAndroid Build Coastguard Worker * 4*dd0948b3SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*dd0948b3SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*dd0948b3SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*dd0948b3SAndroid Build Coastguard Worker * 8*dd0948b3SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*dd0948b3SAndroid Build Coastguard Worker * 10*dd0948b3SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*dd0948b3SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*dd0948b3SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*dd0948b3SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*dd0948b3SAndroid Build Coastguard Worker * limitations under the License. 15*dd0948b3SAndroid Build Coastguard Worker */ 16*dd0948b3SAndroid Build Coastguard Worker 17*dd0948b3SAndroid Build Coastguard Worker #include <libProxyConfig/libProxyConfig.h> 18*dd0948b3SAndroid Build Coastguard Worker 19*dd0948b3SAndroid Build Coastguard Worker #include <json/json.h> 20*dd0948b3SAndroid Build Coastguard Worker 21*dd0948b3SAndroid Build Coastguard Worker #include <fstream> 22*dd0948b3SAndroid Build Coastguard Worker #include <map> 23*dd0948b3SAndroid Build Coastguard Worker #include <mutex> 24*dd0948b3SAndroid Build Coastguard Worker 25*dd0948b3SAndroid Build Coastguard Worker namespace android::automotive::proxyconfig { 26*dd0948b3SAndroid Build Coastguard Worker 27*dd0948b3SAndroid Build Coastguard Worker static std::map<std::string, Service> serviceConfigs; 28*dd0948b3SAndroid Build Coastguard Worker std::once_flag flag; 29*dd0948b3SAndroid Build Coastguard Worker 30*dd0948b3SAndroid Build Coastguard Worker static std::string_view proxyConfig = 31*dd0948b3SAndroid Build Coastguard Worker "/etc/automotive/proxy_config.json"; 32*dd0948b3SAndroid Build Coastguard Worker setProxyConfigFile(std::string_view configFile)33*dd0948b3SAndroid Build Coastguard Workervoid setProxyConfigFile(std::string_view configFile) { 34*dd0948b3SAndroid Build Coastguard Worker proxyConfig = configFile; 35*dd0948b3SAndroid Build Coastguard Worker } 36*dd0948b3SAndroid Build Coastguard Worker readVmService(Json::Value service)37*dd0948b3SAndroid Build Coastguard Workerstatic Service readVmService(Json::Value service) { 38*dd0948b3SAndroid Build Coastguard Worker Service vmService; 39*dd0948b3SAndroid Build Coastguard Worker 40*dd0948b3SAndroid Build Coastguard Worker vmService.name = service["name"].asString(); 41*dd0948b3SAndroid Build Coastguard Worker vmService.port = service["port"].asInt(); 42*dd0948b3SAndroid Build Coastguard Worker 43*dd0948b3SAndroid Build Coastguard Worker return vmService; 44*dd0948b3SAndroid Build Coastguard Worker } 45*dd0948b3SAndroid Build Coastguard Worker getAllVmProxyConfigs()46*dd0948b3SAndroid Build Coastguard Workerstd::vector<VmProxyConfig> getAllVmProxyConfigs() { 47*dd0948b3SAndroid Build Coastguard Worker std::ifstream file(std::string(proxyConfig).c_str()); 48*dd0948b3SAndroid Build Coastguard Worker Json::Value jsonConfig; 49*dd0948b3SAndroid Build Coastguard Worker file >> jsonConfig; 50*dd0948b3SAndroid Build Coastguard Worker std::vector<VmProxyConfig> vmConfigs; 51*dd0948b3SAndroid Build Coastguard Worker 52*dd0948b3SAndroid Build Coastguard Worker for (const auto& vm: jsonConfig) { 53*dd0948b3SAndroid Build Coastguard Worker VmProxyConfig vmConfig; 54*dd0948b3SAndroid Build Coastguard Worker vmConfig.cid = vm["CID"].asInt(); 55*dd0948b3SAndroid Build Coastguard Worker for (const auto& service: vm["Services"]) { 56*dd0948b3SAndroid Build Coastguard Worker vmConfig.services.push_back(readVmService(service)); 57*dd0948b3SAndroid Build Coastguard Worker } 58*dd0948b3SAndroid Build Coastguard Worker vmConfigs.push_back(vmConfig); 59*dd0948b3SAndroid Build Coastguard Worker } 60*dd0948b3SAndroid Build Coastguard Worker return vmConfigs; 61*dd0948b3SAndroid Build Coastguard Worker } 62*dd0948b3SAndroid Build Coastguard Worker lazyLoadConfig()63*dd0948b3SAndroid Build Coastguard Workerstatic void lazyLoadConfig() { 64*dd0948b3SAndroid Build Coastguard Worker std::call_once(flag, [](){ 65*dd0948b3SAndroid Build Coastguard Worker std::vector<VmProxyConfig> vmConfigs = getAllVmProxyConfigs(); 66*dd0948b3SAndroid Build Coastguard Worker for (const auto& vmConfig: vmConfigs) { 67*dd0948b3SAndroid Build Coastguard Worker for (const auto& service: vmConfig.services) { 68*dd0948b3SAndroid Build Coastguard Worker serviceConfigs.insert({service.name, service}); 69*dd0948b3SAndroid Build Coastguard Worker } 70*dd0948b3SAndroid Build Coastguard Worker } 71*dd0948b3SAndroid Build Coastguard Worker }); 72*dd0948b3SAndroid Build Coastguard Worker } 73*dd0948b3SAndroid Build Coastguard Worker getServiceConfig(std::string_view name)74*dd0948b3SAndroid Build Coastguard Workerstd::optional<Service> getServiceConfig(std::string_view name) { 75*dd0948b3SAndroid Build Coastguard Worker lazyLoadConfig(); 76*dd0948b3SAndroid Build Coastguard Worker if (auto entry = serviceConfigs.find(std::string(name)); entry != serviceConfigs.end()) { 77*dd0948b3SAndroid Build Coastguard Worker return entry->second; 78*dd0948b3SAndroid Build Coastguard Worker } 79*dd0948b3SAndroid Build Coastguard Worker return std::nullopt; 80*dd0948b3SAndroid Build Coastguard Worker } 81*dd0948b3SAndroid Build Coastguard Worker 82*dd0948b3SAndroid Build Coastguard Worker } // namespace android::automotive::proxyconfig 83