proxy.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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) == false)
  42. return _error->Error("ProxyAutoDetect command '%s' failed!", AutoDetectProxyCmd.c_str());
  43. char buf[512];
  44. bool const goodread = PipeFd.ReadLine(buf, sizeof(buf)) != nullptr;
  45. PipeFd.Close();
  46. if (ExecWait(Child, "ProxyAutoDetect") == false)
  47. return false;
  48. // no output means the detector has no idea which proxy to use
  49. // and apt will use the generic proxy settings
  50. if (goodread == false)
  51. return true;
  52. auto const cleanedbuf = _strstrip(buf);
  53. // We warn about this as the implementor probably meant to use DIRECT instead
  54. if (cleanedbuf[0] == '\0')
  55. {
  56. _error->Warning("ProxyAutoDetect command returned an empty line");
  57. return true;
  58. }
  59. if (Debug)
  60. std::clog << "auto detect command returned: '" << cleanedbuf << "'" << std::endl;
  61. if (strstr(cleanedbuf, URL.Access.c_str()) == cleanedbuf || strcmp(cleanedbuf, "DIRECT") == 0)
  62. _config->Set("Acquire::"+URL.Access+"::proxy::"+URL.Host, cleanedbuf);
  63. return true;
  64. }
  65. /*}}}*/