Browse Source

Cleaner UpstreamProxyConfig, added package README

Eugene Fryntov 11 years ago
parent
commit
7b54b54805
1 changed files with 11 additions and 49 deletions
  1. 11 49
      psiphon/upstreamproxy/upstreamProxy.go

+ 11 - 49
psiphon/upstreamproxy/upstreamProxy.go

@@ -1,76 +1,38 @@
 package upstreamproxy
 
 import (
-	"errors"
-	"fmt"
 	"golang.org/x/net/proxy"
 	"net"
 	"net/url"
+        "fmt"
 )
 
 type DialFunc func(string, string) (net.Conn, error)
 
-type proxyType int
-
-const (
-	HTTP proxyType = iota
-	SOCKS4A
-	SOCKS5
-)
-
 type UpstreamProxyConfig struct {
 	ForwardDialFunc DialFunc
-	ProxyAddress    string
-	ProxyType       proxyType
-	Username        string
-	Password        string
+	ProxyURIString  string
 }
 
-// UpstreamProxyConfig proxy.Dialer interface
+// UpstreamProxyConfig implements proxy.Dialer interface
 // so we can pass it to proxy.FromURL
 func (u *UpstreamProxyConfig) Dial(network, addr string) (net.Conn, error) {
 	return u.ForwardDialFunc(network, addr)
 }
 
-func NewProxyDialer(config *UpstreamProxyConfig) DialFunc {
-	proxyURI, err := makeProxyUri(config)
+func NewProxyDialFunc(config *UpstreamProxyConfig) DialFunc {
+	proxyURI, err := url.Parse(config.ProxyURIString)
 	if err != nil {
 		return func(network, addr string) (net.Conn, error) {
-			return nil, err
+                    return nil,  fmt.Errorf("Upstream proxy URI parsing error: %v", err)
 		}
 	}
-	dialer, err := proxy.FromURL(proxyURI, config)
-	return dialer.Dial
-}
-
-func proxySchemeFromType(ptype proxyType) (string, error) {
-	proxySchemeDict := map[proxyType]string{
-		HTTP:    "http",
-		SOCKS4A: "socks4a",
-		SOCKS5:  "socks5",
-	}
-	if val, ok := proxySchemeDict[ptype]; ok {
-		return val, nil
-	}
-	return "", errors.New("Unsupported proxy type")
-}
 
-func makeProxyUri(config *UpstreamProxyConfig) (*url.URL, error) {
-	scheme, err := proxySchemeFromType(config.ProxyType)
+	dialer, err := proxy.FromURL(proxyURI, config)
 	if err != nil {
-		return nil, err
-	}
-	var uriUserInfo string
-	if config.Username != "" {
-		uriUserInfo = config.Username
-	}
-	if config.Password != "" {
-		uriUserInfo = fmt.Sprint(uriUserInfo, ":", config.Password)
-	}
-	if uriUserInfo != "" {
-		uriUserInfo = fmt.Sprint(uriUserInfo, "@")
+		return func(network, addr string) (net.Conn, error) {
+			return nil, err
+		}
 	}
-	proxyUriStr := fmt.Sprint(scheme, "://", uriUserInfo, config.ProxyAddress)
-
-	return url.Parse(proxyUriStr)
+	return dialer.Dial
 }