Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: chromeos/network/onc/onc_utils.cc

Issue 1228543002: Translate ONC ProxySettings <-> Shill ProxyConfig (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add SchemeToString Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/network/onc/onc_utils.h ('k') | chromeos/network/onc/onc_utils_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/network/onc/onc_utils.h" 5 #include "chromeos/network/onc/onc_utils.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chromeos/network/network_event_log.h" 14 #include "chromeos/network/network_event_log.h"
15 #include "chromeos/network/onc/onc_mapper.h" 15 #include "chromeos/network/onc/onc_mapper.h"
16 #include "chromeos/network/onc/onc_signature.h" 16 #include "chromeos/network/onc/onc_signature.h"
17 #include "chromeos/network/onc/onc_utils.h" 17 #include "chromeos/network/onc/onc_utils.h"
18 #include "chromeos/network/onc/onc_validator.h" 18 #include "chromeos/network/onc/onc_validator.h"
19 #include "components/device_event_log/device_event_log.h" 19 #include "components/device_event_log/device_event_log.h"
20 #include "components/proxy_config/proxy_config_dictionary.h"
20 #include "crypto/encryptor.h" 21 #include "crypto/encryptor.h"
21 #include "crypto/hmac.h" 22 #include "crypto/hmac.h"
22 #include "crypto/symmetric_key.h" 23 #include "crypto/symmetric_key.h"
24 #include "net/base/host_port_pair.h"
23 #include "net/cert/pem_tokenizer.h" 25 #include "net/cert/pem_tokenizer.h"
24 #include "net/cert/x509_certificate.h" 26 #include "net/cert/x509_certificate.h"
27 #include "net/proxy/proxy_bypass_rules.h"
28 #include "net/proxy/proxy_config.h"
29 #include "net/proxy/proxy_server.h"
30 #include "url/url_constants.h"
25 31
26 using namespace ::onc; 32 using namespace ::onc;
27 33
28 namespace chromeos { 34 namespace chromeos {
29 namespace onc { 35 namespace onc {
30 36
31 namespace { 37 namespace {
32 38
33 const char kUnableToDecrypt[] = "Unable to decrypt encrypted ONC"; 39 const char kUnableToDecrypt[] = "Unable to decrypt encrypted ONC";
34 const char kUnableToDecode[] = "Unable to decode encrypted ONC"; 40 const char kUnableToDecode[] = "Unable to decode encrypted ONC";
(...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 property_basename = property_key; 737 property_basename = property_key;
732 recommended_property_key = ::onc::kRecommended; 738 recommended_property_key = ::onc::kRecommended;
733 } 739 }
734 740
735 const base::ListValue* recommended_keys = nullptr; 741 const base::ListValue* recommended_keys = nullptr;
736 return (onc->GetList(recommended_property_key, &recommended_keys) && 742 return (onc->GetList(recommended_property_key, &recommended_keys) &&
737 recommended_keys->Find(base::StringValue(property_basename)) != 743 recommended_keys->Find(base::StringValue(property_basename)) !=
738 recommended_keys->end()); 744 recommended_keys->end());
739 } 745 }
740 746
747 namespace {
748
749 const char kDirectScheme[] = "direct";
750 const char kQuicScheme[] = "quic";
751 const char kSocksScheme[] = "socks";
752 const char kSocks4Scheme[] = "socks4";
753 const char kSocks5Scheme[] = "socks5";
754
755 net::ProxyServer ConvertOncProxyLocationToHostPort(
756 net::ProxyServer::Scheme default_proxy_scheme,
757 const base::DictionaryValue& onc_proxy_location) {
758 std::string host;
759 onc_proxy_location.GetStringWithoutPathExpansion(::onc::proxy::kHost, &host);
760 // Parse |host| according to the format [<scheme>"://"]<server>[":"<port>].
761 net::ProxyServer proxy_server =
762 net::ProxyServer::FromURI(host, default_proxy_scheme);
763 int port = 0;
764 onc_proxy_location.GetIntegerWithoutPathExpansion(::onc::proxy::kPort, &port);
765
766 // Replace the port parsed from |host| by the provided |port|.
767 return net::ProxyServer(
768 proxy_server.scheme(),
769 net::HostPortPair(proxy_server.host_port_pair().host(),
770 static_cast<uint16>(port)));
771 }
772
773 void AppendProxyServerForScheme(const base::DictionaryValue& onc_manual,
774 const std::string& onc_scheme,
775 std::string* spec) {
776 const base::DictionaryValue* onc_proxy_location = nullptr;
777 if (!onc_manual.GetDictionaryWithoutPathExpansion(onc_scheme,
778 &onc_proxy_location)) {
779 return;
780 }
781
782 net::ProxyServer::Scheme default_proxy_scheme = net::ProxyServer::SCHEME_HTTP;
783 std::string url_scheme;
784 if (onc_scheme == ::onc::proxy::kFtp) {
785 url_scheme = url::kFtpScheme;
786 } else if (onc_scheme == ::onc::proxy::kHttp) {
787 url_scheme = url::kHttpScheme;
788 } else if (onc_scheme == ::onc::proxy::kHttps) {
789 url_scheme = url::kHttpsScheme;
790 } else if (onc_scheme == ::onc::proxy::kSocks) {
791 default_proxy_scheme = net::ProxyServer::SCHEME_SOCKS4;
792 url_scheme = kSocksScheme;
793 } else {
794 NOTREACHED();
795 }
796
797 net::ProxyServer proxy_server = ConvertOncProxyLocationToHostPort(
798 default_proxy_scheme, *onc_proxy_location);
799
800 ProxyConfigDictionary::EncodeAndAppendProxyServer(url_scheme, proxy_server,
801 spec);
802 }
803
804 net::ProxyBypassRules ConvertOncExcludeDomainsToBypassRules(
805 const base::ListValue& onc_exclude_domains) {
806 net::ProxyBypassRules rules;
807 for (base::ListValue::const_iterator it = onc_exclude_domains.begin();
808 it != onc_exclude_domains.end(); ++it) {
809 std::string rule;
810 (*it)->GetAsString(&rule);
811 rules.AddRuleFromString(rule);
812 }
813 return rules;
814 }
815
816 std::string SchemeToString(net::ProxyServer::Scheme scheme) {
817 switch (scheme) {
818 case net::ProxyServer::SCHEME_DIRECT:
819 return kDirectScheme;
820 case net::ProxyServer::SCHEME_HTTP:
821 return url::kHttpScheme;
822 case net::ProxyServer::SCHEME_SOCKS4:
823 return kSocks4Scheme;
824 case net::ProxyServer::SCHEME_SOCKS5:
825 return kSocks5Scheme;
826 case net::ProxyServer::SCHEME_HTTPS:
827 return url::kHttpsScheme;
828 case net::ProxyServer::SCHEME_QUIC:
829 return kQuicScheme;
830 case net::ProxyServer::SCHEME_INVALID:
831 break;
832 }
833 NOTREACHED();
834 return "";
835 }
836
837 void SetProxyForScheme(const net::ProxyConfig::ProxyRules& proxy_rules,
838 const std::string& scheme,
839 const std::string& onc_scheme,
840 base::DictionaryValue* dict) {
841 const net::ProxyList* proxy_list = nullptr;
842 if (proxy_rules.type == net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY) {
843 proxy_list = &proxy_rules.single_proxies;
844 } else if (proxy_rules.type ==
845 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME) {
846 proxy_list = proxy_rules.MapUrlSchemeToProxyList(scheme);
847 }
848 if (!proxy_list || proxy_list->IsEmpty())
849 return;
850 const net::ProxyServer& server = proxy_list->Get();
851 scoped_ptr<base::DictionaryValue> url_dict(new base::DictionaryValue);
852 std::string host = server.host_port_pair().host();
853
854 // For all proxy types except SOCKS, the default scheme of the proxy host is
855 // HTTP.
856 net::ProxyServer::Scheme default_scheme =
857 (onc_scheme == ::onc::proxy::kSocks) ? net::ProxyServer::SCHEME_SOCKS4
858 : net::ProxyServer::SCHEME_HTTP;
859 // Only prefix the host with a non-default scheme.
860 if (server.scheme() != default_scheme)
pneubeck (no reviews) 2015/07/10 07:20:42 as explained in the other comment, it should also
stevenjb 2015/07/10 16:16:59 I am going to leave it as is for now because it wo
861 host = SchemeToString(server.scheme()) + "://" + host;
862 url_dict->SetStringWithoutPathExpansion(::onc::proxy::kHost, host);
863 url_dict->SetIntegerWithoutPathExpansion(::onc::proxy::kPort,
864 server.host_port_pair().port());
865 dict->SetWithoutPathExpansion(onc_scheme, url_dict.release());
866 }
867
868 } // namespace
869
870 scoped_ptr<base::DictionaryValue> ConvertOncProxySettingsToProxyConfig(
871 const base::DictionaryValue& onc_proxy_settings) {
872 std::string type;
873 onc_proxy_settings.GetStringWithoutPathExpansion(::onc::proxy::kType, &type);
874 scoped_ptr<base::DictionaryValue> proxy_dict;
875
876 if (type == ::onc::proxy::kDirect) {
877 proxy_dict.reset(ProxyConfigDictionary::CreateDirect());
878 } else if (type == ::onc::proxy::kWPAD) {
879 proxy_dict.reset(ProxyConfigDictionary::CreateAutoDetect());
880 } else if (type == ::onc::proxy::kPAC) {
881 std::string pac_url;
882 onc_proxy_settings.GetStringWithoutPathExpansion(::onc::proxy::kPAC,
883 &pac_url);
884 GURL url(pac_url);
885 DCHECK(url.is_valid()) << "Invalid URL in ProxySettings.PAC";
886 proxy_dict.reset(ProxyConfigDictionary::CreatePacScript(url.spec(), false));
887 } else if (type == ::onc::proxy::kManual) {
888 const base::DictionaryValue* manual_dict = nullptr;
889 onc_proxy_settings.GetDictionaryWithoutPathExpansion(::onc::proxy::kManual,
890 &manual_dict);
891 std::string manual_spec;
892 AppendProxyServerForScheme(*manual_dict, ::onc::proxy::kFtp, &manual_spec);
893 AppendProxyServerForScheme(*manual_dict, ::onc::proxy::kHttp, &manual_spec);
894 AppendProxyServerForScheme(*manual_dict, ::onc::proxy::kSocks,
895 &manual_spec);
896 AppendProxyServerForScheme(*manual_dict, ::onc::proxy::kHttps,
897 &manual_spec);
898
899 const base::ListValue* exclude_domains = nullptr;
900 net::ProxyBypassRules bypass_rules;
901 if (onc_proxy_settings.GetListWithoutPathExpansion(
902 ::onc::proxy::kExcludeDomains, &exclude_domains)) {
903 bypass_rules.AssignFrom(
904 ConvertOncExcludeDomainsToBypassRules(*exclude_domains));
905 }
906 proxy_dict.reset(ProxyConfigDictionary::CreateFixedServers(
907 manual_spec, bypass_rules.ToString()));
908 } else {
909 NOTREACHED();
910 }
911 return proxy_dict.Pass();
912 }
913
914 scoped_ptr<base::DictionaryValue> ConvertProxyConfigToOncProxySettings(
915 const base::DictionaryValue& proxy_config_value) {
916 // Create a ProxyConfigDictionary from the DictionaryValue.
917 scoped_ptr<ProxyConfigDictionary> proxy_config(
918 new ProxyConfigDictionary(&proxy_config_value));
919
920 // Create the result DictionaryValue and populate it.
921 scoped_ptr<base::DictionaryValue> proxy_settings(new base::DictionaryValue);
922 ProxyPrefs::ProxyMode mode;
923 if (!proxy_config->GetMode(&mode))
924 return nullptr;
925 switch (mode) {
926 case ProxyPrefs::MODE_DIRECT: {
927 proxy_settings->SetStringWithoutPathExpansion(::onc::proxy::kType,
928 ::onc::proxy::kDirect);
929 break;
930 }
931 case ProxyPrefs::MODE_AUTO_DETECT: {
932 proxy_settings->SetStringWithoutPathExpansion(::onc::proxy::kType,
933 ::onc::proxy::kWPAD);
934 break;
935 }
936 case ProxyPrefs::MODE_PAC_SCRIPT: {
937 proxy_settings->SetStringWithoutPathExpansion(::onc::proxy::kType,
938 ::onc::proxy::kPAC);
939 std::string pac_url;
940 proxy_config->GetPacUrl(&pac_url);
941 proxy_settings->SetStringWithoutPathExpansion(::onc::proxy::kPAC,
942 pac_url);
943 break;
944 }
945 case ProxyPrefs::MODE_FIXED_SERVERS: {
946 proxy_settings->SetString(::onc::proxy::kType, ::onc::proxy::kManual);
947 scoped_ptr<base::DictionaryValue> manual(new base::DictionaryValue);
948 std::string proxy_rules_string;
949 if (proxy_config->GetProxyServer(&proxy_rules_string)) {
950 net::ProxyConfig::ProxyRules proxy_rules;
951 proxy_rules.ParseFromString(proxy_rules_string);
952 SetProxyForScheme(proxy_rules, url::kFtpScheme, ::onc::proxy::kFtp,
953 manual.get());
954 SetProxyForScheme(proxy_rules, url::kHttpScheme, ::onc::proxy::kHttp,
955 manual.get());
956 SetProxyForScheme(proxy_rules, url::kHttpsScheme, ::onc::proxy::kHttps,
957 manual.get());
958 SetProxyForScheme(proxy_rules, kSocksScheme, ::onc::proxy::kSocks,
959 manual.get());
960 }
961 proxy_settings->SetWithoutPathExpansion(::onc::proxy::kManual,
962 manual.release());
963
964 // Convert the 'bypass_list' string into dictionary entries.
965 std::string bypass_rules_string;
966 if (proxy_config->GetBypassList(&bypass_rules_string)) {
967 net::ProxyBypassRules bypass_rules;
968 bypass_rules.ParseFromString(bypass_rules_string);
969 scoped_ptr<base::ListValue> exclude_domains(new base::ListValue);
970 for (const net::ProxyBypassRules::Rule* rule : bypass_rules.rules())
971 exclude_domains->AppendString(rule->ToString());
972 if (!exclude_domains->empty()) {
973 proxy_settings->SetWithoutPathExpansion(::onc::proxy::kExcludeDomains,
974 exclude_domains.release());
975 }
976 }
977 break;
978 }
979 default: {
980 LOG(ERROR) << "Unexpected proxy mode in Shill config: " << mode;
981 return nullptr;
982 }
983 }
984 return proxy_settings.Pass();
985 }
986
741 } // namespace onc 987 } // namespace onc
742 } // namespace chromeos 988 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/onc/onc_utils.h ('k') | chromeos/network/onc/onc_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698