Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--proxy-user flag added #8

Merged
merged 1 commit into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cmd/proxy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"encoding/base64"
"fmt"
"net/url"
"strings"
)

const defaultPort = "1080"
Expand All @@ -22,3 +24,31 @@ func proxyCmd(proxy string) (string, error) {
}
return proxy, nil
}

// proxyUserCmd handles "--proxy-user" related tasks. Returns
// encoded with Base64 string.
func proxyUserCmd(proxyUser string) (string, error) {
err := checkProxyUser(proxyUser)
if err != nil {
return "", err
}
if !proxyNTLM && !proxyNegotiate && !proxyDigest { // Basic auth
encodedProxy := convertBasicAuth(proxyUser)
return encodedProxy, nil
}
return proxyUser, nil
}

// checkProxyUser controls proxyUser whether is in <username:password> format.
func checkProxyUser(proxyUser string) error {
p := strings.Split(proxyUser, ":")
if len(p) != 2 || len(p[0]) == 0 || len(p[1]) == 0 {
return fmt.Errorf("need to specify username and password in <username:password> format")
}
return nil
}

// convertBasicAuth encodes proxy username and password to Base64.
func convertBasicAuth(proxyUser string) string {
return base64.StdEncoding.EncodeToString([]byte(proxyUser))
}
70 changes: 66 additions & 4 deletions cmd/proxy_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package cmd

import (
"log"
"testing"
)

const (
proxyUrl = "proxy://myproxy:1234"
httpProxyUrl = "http://myproxy:1234"
missingProxyUrl = "proxy://proxy"
missingHTTPProxyUrl = "http://myproxy"
proxyUrl = "proxy://myproxy:1234"
httpProxyUrl = "http://myproxy:1234"
missingProxyUrl = "proxy://proxy"
missingHTTPProxyUrl = "http://myproxy"
validProxyUser = "user:pass"
emptyPassProxyUser = "user:"
emptyUserProxyUser = ":pass"
invalidProxyUserLong = "user:pass1:pass2"
)

func TestProxyCmd_ProxyUrl(t *testing.T) {
Expand Down Expand Up @@ -52,3 +57,60 @@ func TestProxyCmd_MissingHTTPProxyUrl(t *testing.T) {
t.Errorf("wrong proxy url. expected url: %s, got: %s", expectedUrl, testProxyUrl)
}
}

func TestCheckValidProxyUser(t *testing.T) {
err := checkProxyUser(validProxyUser)
if err != nil {
t.Errorf(err.Error())
}
}

func TestCheckEmptyUserProxyUser(t *testing.T) {
err := checkProxyUser(emptyUserProxyUser)
if err == nil {
t.Errorf("empyt username.")
}
}

func TestCheckEmptyPassProxyUser(t *testing.T) {
err := checkProxyUser(emptyPassProxyUser)
if err == nil {
t.Errorf("invalid password.")
}
}

func TestCheckEmptyProxyUser(t *testing.T) {
err := checkProxyUser("")
if err == nil {
t.Errorf("empty user")
}
}

func TestCheckLongProxyUser(t *testing.T) {
err := checkProxyUser(invalidProxyUserLong)
if err == nil {
t.Errorf("long proxy user")
}
}

func TestProxyUserCmdBasic(t *testing.T) {
proxyUserCredential, err := proxyUserCmd(validProxyUser)
if err != nil {
t.Errorf(err.Error())
}
if proxyUserCredential == validProxyUser {
t.Errorf("proxy user: <%s> should not be the same with <%s>", validProxyUser, proxyUserCredential)
}
log.Printf("proxy user credential: %s", proxyUserCredential)
}

func TestProxyUserCmdDigest(t *testing.T) {
proxyDigest = true
proxyUserCredential, err := proxyUserCmd(validProxyUser)
if err != nil {
t.Errorf(err.Error())
}
if proxyUserCredential != validProxyUser {
t.Errorf("proxy user: <%s> should be the same with <%s>", validProxyUser, proxyUserCredential)
}
}
41 changes: 39 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,32 @@ import (
)

var (
URL = ""
// URL is the target address.
URL = ""

// proxy is the url in the format of [protocol://]host[:port]. Its flag is
// --proxy [protocol://]host[:port] or -x [protocol://]host[:port].
proxy = ""
c = src.NewClient()

// proxyUser is the username-password pair in <user:password> format. Its flag is
// --proxy-user <user:password>.
proxyUser = ""

// proxyBasic is the flag variable whether indicates command contains --proxy-basic flag.
// Basic is also the default unless anything else is asked for.
proxyBasic = true

// proxyDigest is the flag variable whether indicates command contains --proxy-digest flag.
proxyDigest = false

// proxyNTLM is the flag variable whether indicates command contains --proxy-ntlm flag.
proxyNTLM = false

// proxyNegotiate is the flag variable whether indicates command contains --proxy-negotiate flag.
proxyNegotiate = false

// c is the client.
c = src.NewClient()
)

var rootCmd = &cobra.Command{
Expand All @@ -31,6 +54,11 @@ var rootCmd = &cobra.Command{
func Execute() {
rootCmd.AddCommand(cmdGet)
rootCmd.PersistentFlags().StringVarP(&proxy, "proxy", "x", "", "[protocol://]host[:port] Use this proxy")
rootCmd.PersistentFlags().StringVarP(&proxyUser, "proxy-user", "U", "", "<user:password> Proxy user and password")
rootCmd.PersistentFlags().BoolVarP(&proxyBasic, "proxy-basic", "", true, "Use Basic authentication on the proxy")
rootCmd.PersistentFlags().BoolVarP(&proxyDigest, "proxy-digest", "", false, "Use Digest authentication on the proxy")
rootCmd.PersistentFlags().BoolVarP(&proxyNTLM, "proxy-ntlm", "", false, "Use NTLM authentication on the proxy")
rootCmd.PersistentFlags().BoolVarP(&proxyNegotiate, "proxy-negotiate", "", false, "Use HTTP Negotiate (SPNEGO) authentication on the proxy")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -46,5 +74,14 @@ func checkFlags() error {
}
c.SetProxy(proxy)
}
if proxyUser != "" {
proxyUserCredentials, err := proxyUserCmd(proxyUser)
if err != nil {
return err
}
if !proxyNTLM && !proxyNegotiate && !proxyDigest { // Basic authentication
c.AddHeader("Proxy-Authenticate", fmt.Sprintf("Basic %s", proxyUserCredentials))
}
}
return nil
}