proxy.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Proxy - Proxy releated functions
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include<apt-pkg/configuration.h>
  9. #include<apt-pkg/error.h>
  10. #include<apt-pkg/fileutl.h>
  11. #include<apt-pkg/strutl.h>
  12. #include<iostream>
  13. #include <unistd.h>
  14. #include "proxy.h"
  15. /*}}}*/
  16. // AutoDetectProxy - auto detect proxy /*{{{*/
  17. // ---------------------------------------------------------------------
  18. /* */
  19. bool AutoDetectProxy(URI &URL)
  20. {
  21. // we support both http/https debug options
  22. bool Debug = _config->FindB("Debug::Acquire::"+URL.Access,false);
  23. // the user already explicitly set a proxy for this host
  24. if(_config->Find("Acquire::"+URL.Access+"::proxy::"+URL.Host, "") != "")
  25. return true;
  26. // option is "Acquire::http::Proxy-Auto-Detect" but we allow the old
  27. // name without the dash ("-")
  28. std::string AutoDetectProxyCmd = _config->Find("Acquire::"+URL.Access+"::Proxy-Auto-Detect",
  29. _config->Find("Acquire::"+URL.Access+"::ProxyAutoDetect"));
  30. if (AutoDetectProxyCmd.empty())
  31. return true;
  32. if (Debug)
  33. std::clog << "Using auto proxy detect command: " << AutoDetectProxyCmd << std::endl;
  34. std::string const urlstring = URL;
  35. std::vector<const char *> Args;
  36. Args.push_back(AutoDetectProxyCmd.c_str());
  37. Args.push_back(urlstring.c_str());
  38. Args.push_back(nullptr);
  39. FileFd PipeFd;
  40. pid_t Child;
  41. if(Popen(&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
  42. return _error->Error("ProxyAutoDetect command '%s' failed!", AutoDetectProxyCmd.c_str());
  43. char buf[512];
  44. if (PipeFd.ReadLine(buf, sizeof(buf)) == nullptr)
  45. return _error->Error("Failed to read in AutoDetectProxy");
  46. PipeFd.Close();
  47. ExecWait(Child, "ProxyAutoDetect", true);
  48. auto const cleanedbuf = _strstrip(buf);
  49. if (cleanedbuf[0] == '\0')
  50. return _error->Warning("ProxyAutoDetect returned no data");
  51. if (Debug)
  52. std::clog << "auto detect command returned: '" << cleanedbuf << "'" << std::endl;
  53. if (strstr(cleanedbuf, URL.Access.c_str()) == cleanedbuf)
  54. _config->Set("Acquire::"+URL.Access+"::proxy::"+URL.Host, cleanedbuf);
  55. return true;
  56. }
  57. /*}}}*/