5 #include "cvmfs_config.h"
18 #include "pacparser.h"
33 int retval = vasprintf(&msg, fmt, argp);
43 const bool report_errors)
50 vector<string> components =
SplitString(pac_proxy,
';');
51 for (
unsigned i = 0; i < components.size(); ++i) {
54 for (
unsigned j = 0; j < components[i].length(); ++j) {
55 if ((components[i][j] !=
' ') && (components[i][j] !=
'\t'))
56 next_proxy.push_back(components[i][j]);
60 if (
HasPrefix(next_proxy,
"SOCKS",
false)) {
62 "no support for SOCKS proxy, skipping %s",
63 next_proxy.substr(5).c_str());
67 if ((next_proxy !=
"DIRECT") &&
75 if (
HasPrefix(next_proxy,
"PROXY",
false))
76 next_proxy = next_proxy.substr(5);
78 if (cvmfs_proxy ==
"")
79 cvmfs_proxy = next_proxy;
81 cvmfs_proxy +=
";" + next_proxy;
95 bool retval = pacparser_init();
99 const string pac_string(pac_data, size);
101 retval = pacparser_parse_pac_string(pac_string.c_str());
108 vector<string> host_list;
110 unsigned current_host;
111 download_manager->
GetHostInfo(&host_list, &rtt, ¤t_host);
112 for (
unsigned i = 0; i < host_list.size(); ++i) {
113 size_t hostname_begin = 7;
114 size_t hostname_end = host_list[i].find_first_of(
":/", hostname_begin);
115 size_t hostname_len =
116 (hostname_end == string::npos) ?
117 string::npos : hostname_end-hostname_begin;
118 const string hostname = (hostname_begin > host_list[i].length()) ?
119 "localhost" : host_list[i].substr(hostname_begin, hostname_len);
120 const string url = host_list[i] +
"/.cvmfspublished";
123 char *pac_proxy = pacparser_find_proxy(url.c_str(), hostname.c_str());
124 if (pac_proxy == NULL) {
128 if (*proxies ==
"") {
130 if (*proxies ==
"") {
132 "no valid proxy found (%s returned from pac file)", pac_proxy);
138 if (*proxies != alt_proxies) {
140 "proxy settings for host %s differ from proxy settings for "
141 "other hosts (%s / %s). Not using proxy setting %s.",
142 host_list[i].c_str(), proxies->c_str(), alt_proxies.c_str(),
143 alt_proxies.c_str());
154 char *http_env = getenv(
"http_proxy");
157 "using HTTP proxy server(s) %s from http_proxy environment",
159 return string(http_env);
162 vector<string> pac_paths;
163 char *pac_env = getenv(
"CVMFS_PAC_URLS");
168 for (
unsigned i = 0; i < pac_paths.size(); ++i) {
169 if (pac_paths[i] ==
"auto") {
175 pac_paths[i].c_str());
179 int retval = download_manager->
Fetch(&download_pac);
182 retval =
ParsePac(reinterpret_cast<char*>(pac_memsink.
data()),
188 "failed to parse pac file %s", pac_paths[i].c_str());
192 "using HTTP proxy server(s) %s from pac file %s",
193 proxies.c_str(), pac_paths[i].c_str());
199 pac_paths[i].c_str());
208 const string &cvmfs_proxies,
209 const std::string &path_fallback_cache,
212 if ((cvmfs_proxies ==
"") || (cvmfs_proxies.find(
"auto") == string::npos))
213 return cvmfs_proxies;
216 vector<string> lb_groups =
SplitString(cvmfs_proxies,
';');
217 for (
unsigned i = 0; i < lb_groups.size(); ++i) {
218 if (lb_groups[i] !=
"auto")
221 lb_groups[i] =
AutoProxy(download_manager);
222 if (lb_groups[i].empty())
223 empty_auto =
static_cast<int>(i);
226 if (empty_auto != -1)
227 lb_groups.erase(lb_groups.begin()+
static_cast<unsigned>(empty_auto));
228 string discovered_proxies =
JoinStrings(lb_groups,
";");
230 if (!path_fallback_cache.empty()) {
231 if (empty_auto != -1) {
232 string cached_proxies;
233 int fd = open(path_fallback_cache.c_str(), O_RDONLY);
239 "using cached proxy settings from %s",
240 path_fallback_cache.c_str());
241 return cached_proxies;
249 "failed to write proxy settings into %s",
250 path_fallback_cache.c_str());
255 return discovered_proxies;
262 FILE *log_output = NULL;
268 fprintf(log_output,
"%s\n", msg);
279 string proxy_configuration = argv[2];
280 string host_list = argv[3];
289 return resolved_proxies ==
"";
void SetHostChain(const std::string &host_list)
string JoinStrings(const vector< string > &strings, const string &joint)
int MainResolveProxyDescription(int argc, char **argv)
assert((mem||(size==0))&&"Out Of Memory")
bool SafeWriteToFile(const std::string &content, const std::string &path, int mode)
void SetAltLogFunc(void(*fn)(const LogSource source, const int mask, const char *msg))
vector< string > SplitString(const string &str, char delim)
string AutoProxy(DownloadManager *download_manager)
static void AltCvmfsLogger(const LogSource source, const int mask, const char *msg)
bool HasPrefix(const string &str, const string &prefix, const bool ignore_case)
bool SafeReadToString(int fd, std::string *final_result)
const char * kAutoPacLocation
string ResolveProxyDescription(const string &cvmfs_proxies, const std::string &path_fallback_cache, DownloadManager *download_manager)
Failures Fetch(JobInfo *info)
static bool ParsePac(const char *pac_data, const size_t size, DownloadManager *download_manager, string *proxies)
static int PrintPacError(const char *fmt, va_list argp)
void GetHostInfo(std::vector< std::string > *host_chain, std::vector< int > *rtt, unsigned *current_host)
static string PacProxy2Cvmfs(const string &pac_proxy, const bool report_errors)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)