proxy.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // AutoDetectProxy - auto detect proxy /*{{{*/
  16. // ---------------------------------------------------------------------
  17. /* */
  18. bool AutoDetectProxy(URI &URL)
  19. {
  20. // we support both http/https debug options
  21. bool Debug = _config->FindB("Debug::Acquire::"+URL.Access,false);
  22. // option is "Acquire::http::Proxy-Auto-Detect" but we allow the old
  23. // name without the dash ("-")
  24. std::string AutoDetectProxyCmd = _config->Find("Acquire::"+URL.Access+"::Proxy-Auto-Detect",
  25. _config->Find("Acquire::"+URL.Access+"::ProxyAutoDetect"));
  26. if (AutoDetectProxyCmd.empty())
  27. return true;
  28. if (Debug)
  29. std::clog << "Using auto proxy detect command: " << AutoDetectProxyCmd << std::endl;
  30. int Pipes[2] = {-1,-1};
  31. if (pipe(Pipes) != 0)
  32. return _error->Errno("pipe", "Failed to create Pipe");
  33. pid_t Process = ExecFork();
  34. if (Process == 0)
  35. {
  36. close(Pipes[0]);
  37. dup2(Pipes[1],STDOUT_FILENO);
  38. SetCloseExec(STDOUT_FILENO,false);
  39. std::string foo = URL;
  40. const char *Args[4];
  41. Args[0] = AutoDetectProxyCmd.c_str();
  42. Args[1] = foo.c_str();
  43. Args[2] = 0;
  44. execv(Args[0],(char **)Args);
  45. std::cerr << "Failed to exec method " << Args[0] << std::endl;
  46. _exit(100);
  47. }
  48. char buf[512];
  49. int InFd = Pipes[0];
  50. close(Pipes[1]);
  51. int res = read(InFd, buf, sizeof(buf)-1);
  52. ExecWait(Process, "ProxyAutoDetect", true);
  53. if (res < 0)
  54. return _error->Errno("read", "Failed to read");
  55. if (res == 0)
  56. return _error->Warning("ProxyAutoDetect returned no data");
  57. // add trailing \0
  58. buf[res] = 0;
  59. if (Debug)
  60. std::clog << "auto detect command returned: '" << buf << "'" << std::endl;
  61. if (strstr(buf, URL.Access.c_str()) == buf)
  62. _config->Set("Acquire::"+URL.Access+"::proxy::"+URL.Host, _strstrip(buf));
  63. return true;
  64. }
  65. /*}}}*/