diff -cr stunnel-3.22/client.c pstunnel-3.22/client.c
*** stunnel-3.22/client.c	Sun Dec 23 20:41:32 2001
--- pstunnel-3.22/client.c	Wed Jul 10 11:42:29 2002
***************
*** 79,84 ****
--- 79,85 ----
  static int make_sockets(int [2]);
  #endif
  static int connect_remote(CLI *c);
+ int connect_to_finaldest(int);
  static int waitforsocket(int, int);
  static void reset(int, char *);

***************
*** 835,841 ****
          log(LOG_DEBUG, "%s connecting %s:%d", options.servname,
              inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
          leave_critical_section(CRIT_NTOA);
!         if(!connect(s, (struct sockaddr *) &addr, sizeof(addr)))
              return s; /* success */
      }
      sockerror("remote connect");
--- 836,843 ----
          log(LOG_DEBUG, "%s connecting %s:%d", options.servname,
              inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
          leave_critical_section(CRIT_NTOA);
!         if(!connect(s, (struct sockaddr *) &addr, sizeof(addr))
! 	   && !connect_to_finaldest(s))
              return s; /* success */
      }
      sockerror("remote connect");
***************
*** 938,943 ****
--- 940,989 ----
      l.l_linger=0;
      if(setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *)&l, sizeof(l)))
          log_error(LOG_DEBUG, get_last_socket_error(), txt);
+ }
+
+ int connect_to_finaldest(int s) {
+     char buff[STRLEN];
+     int len, code;
+
+     if (!(options.option & OPT_WEBPROXY))
+         return 0;
+
+ #ifdef HAVE_SNPRINTF
+     len=snprintf(buff, STRLEN,
+ #else
+     len=sprintf(buff,
+ #endif
+         "CONNECT %s HTTP/1.0\r\n\r\n", options.finaldest);
+     len=writesocket(s, buff, len);
+     if(len<0) {
+         sockerror("writesocket (finaldest)");
+         closesocket(s);
+         return -1;
+     }
+     log(LOG_DEBUG, "me ---> proxy: %s", buff);
+
+     len=readsocket(s, buff, STRLEN-1);
+     if(len<0) {
+         sockerror("readsocket (finaldest)");
+         closesocket(s);
+         return -1;
+     }
+     buff[len]='\0';
+     log(LOG_DEBUG, "proxy ---> me: %s", buff);
+
+     code = 0;
+     if(sscanf(buff, "HTTP/%*s %d %*s", &code) != 1) {
+         log(LOG_ERR, "error: %s", buff);
+         return -1;
+     }
+
+     if(code != 200) {
+         log(LOG_WARNING, "return code not 200: %s", buff);
+         return -1;
+     }
+
+     return 0;
  }

  /* End of client.c */
diff -cr stunnel-3.22/options.c pstunnel-3.22/options.c
*** stunnel-3.22/options.c	Sun Dec 23 21:08:51 2001
--- pstunnel-3.22/options.c	Wed Jul 10 11:39:36 2002
***************
*** 82,91 ****
      options.rand_file=NULL;
      options.rand_write=1;
      options.random_bytes=RANDOM_BYTES;
      options.output_file=NULL;
      options.local_ip=NULL;
      opterr=0;
!     while ((c = getopt(argc, argv, 
"A:a:cp:v:d:fTl:L:r:s:g:t:u:n:N:hC:D:O:E:R:WB:VP:S:o:I:")) != EOF)
          switch (c) {
              case 'A':
                      safecopy(options.cert_file,optarg);
--- 82,92 ----
      options.rand_file=NULL;
      options.rand_write=1;
      options.random_bytes=RANDOM_BYTES;
+     options.finaldest=NULL;
      options.output_file=NULL;
      options.local_ip=NULL;
      opterr=0;
!     while ((c = getopt(argc, argv, 
"A:a:cp:v:d:fTl:L:r:s:g:t:u:n:N:hC:D:O:E:R:WB:VP:S:Z:o:I:")) != EOF)
          switch (c) {
              case 'A':
                      safecopy(options.cert_file,optarg);
***************
*** 240,247 ****
                      options.output_file=optarg;
                  break;
              case 'I':
!                     host2num(&options.local_ip, optarg);
!                 break;
              case '?':
                  log(LOG_ERR, "Illegal option: '%c'", optopt);
                  print_info();
--- 241,258 ----
                      options.output_file=optarg;
                  break;
              case 'I':
! 	            host2num(&options.local_ip, optarg);
! 		break;
! 	    case 'Z':
!                    if(!(options.option & OPT_CLIENT)) {
!                      log(LOG_ERR, "webproxy -Z option runs only in client 
mode");
!                      fprintf(stderr, "webproxy -Z option runs only in 
client mode");
!                      print_help();
!                  }
!                  options.option |= OPT_WEBPROXY;
!                  options.finaldest = optarg;
!                  break;
!
              case '?':
                  log(LOG_ERR, "Illegal option: '%c'", optopt);
                  print_info();
***************
*** 350,355 ****
--- 361,368 ----
          "\n\t-d [host:]port -r [host:]port"
  #endif

+ 	"\n\t[-Z host:port ] "
+
          /* Argument notes */

          "\n\n  -h\t\tprint this help screen"
***************
*** 357,362 ****
--- 370,377 ----
          "\n"
          "\n  -d [host:]port   daemon mode (host defaults to INADDR_ANY)"
          "\n  -r [host:]port   connect to remote service (host defaults to 
INADDR_LOOPBACK)"
+         "\n  -Z host:port     the remote SSL host, if in client mode and 
using a webproxy"
+         "\n                   eg. -c -d localhost:9999 -r proxy:3128 -Z 
remotehost:443"
  #ifndef USE_WIN32
          "\n  -l program\texecute local inetd-type program"
          "\n  -L program\topen local pty and execute program"
diff -cr stunnel-3.22/prototypes.h pstunnel-3.22/prototypes.h
*** stunnel-3.22/prototypes.h	Sun Nov 11 20:16:01 2001
--- pstunnel-3.22/prototypes.h	Wed Jul 10 11:39:36 2002
***************
*** 79,84 ****
--- 79,85 ----
  #define OPT_REMOTE      0x20
  #define OPT_TRANSPARENT 0x40
  #define OPT_PTY         0x80
+ #define OPT_WEBPROXY    0x100

  typedef struct {
      char pem[STRLEN];                        /* pem (priv key/cert) 
filename */
***************
*** 109,114 ****
--- 110,116 ----
      int random_bytes;                       /* how many random bytes to 
read */
      char *pid_dir;
      int cert_defaults;
+     char *finaldest;
      char *output_file;
      u32 *local_ip;
  } server_options;
diff -cr stunnel-3.22/stunnel.c pstunnel-3.22/stunnel.c
*** stunnel-3.22/stunnel.c	Thu Dec 20 08:53:54 2001
--- pstunnel-3.22/stunnel.c	Wed Jul 10 11:42:02 2002
***************
*** 49,54 ****
--- 49,55 ----

      /* Prototypes */
  static void daemon_loop();
+
  #ifndef USE_WIN32
  static void daemonize();
  static void create_pid();



