Browse Source

add support for proxy authentication

Alan Shreve 12 years ago
parent
commit
15e017d285
3 changed files with 17 additions and 2 deletions
  1. 1 1
      src/ngrok/client/cli.go
  2. 15 0
      src/ngrok/conn/conn.go
  3. 1 1
      src/ngrok/version/version.go

+ 1 - 1
src/ngrok/client/cli.go

@@ -104,7 +104,7 @@ func parseArgs() *Options {
 	proxyAddr := flag.String(
 		"proxyAddr",
 		"",
-		"The address of an http proxy to connect through (ex: proxy.example.org:3128)")
+		"The address of an http proxy to connect through (ex: [user:pw@]proxy.example.org:3128)")
 
 	httpAuth := flag.String(
 		"httpauth",

+ 15 - 0
src/ngrok/conn/conn.go

@@ -4,12 +4,14 @@ import (
 	"bufio"
 	"bytes"
 	"crypto/tls"
+	"encoding/base64"
 	"fmt"
 	"io"
 	"math/rand"
 	"net"
 	"net/http"
 	"ngrok/log"
+	"strings"
 )
 
 type Conn interface {
@@ -89,6 +91,15 @@ func Dial(addr, typ string, tlsCfg *tls.Config) (conn *loggedConn, err error) {
 }
 
 func DialHttpProxy(proxyAddr, addr, typ string, tlsCfg *tls.Config) (conn *loggedConn, err error) {
+	var proxyAuth string
+
+	// parse the proxy address for authentication credentials
+	addrParts := strings.Split(proxyAddr, "@")
+	if len(addrParts) == 2 {
+		proxyAddr = addrParts[1]
+		proxyAuth = "Basic " + base64.StdEncoding.EncodeToString([]byte(addrParts[0]))
+	}
+
 	// dial the proxy
 	if conn, err = Dial(proxyAddr, typ, nil); err != nil {
 		return
@@ -99,6 +110,10 @@ func DialHttpProxy(proxyAddr, addr, typ string, tlsCfg *tls.Config) (conn *logge
 	if err != nil {
 		return
 	}
+
+	if proxyAuth != "" {
+		req.Header.Set("Proxy-Authorization", proxyAuth)
+	}
 	req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; ngrok)")
 	req.Write(conn)
 

+ 1 - 1
src/ngrok/version/version.go

@@ -7,7 +7,7 @@ import (
 const (
 	Proto = "2"
 	Major = "1"
-	Minor = "1"
+	Minor = "2"
 )
 
 func MajorMinor() string {