From fb56f84dabb46ebd877ad8d76c3537e0cd08f977 Mon Sep 17 00:00:00 2001 From: Adrian Li Date: Fri, 30 Aug 2019 10:24:27 +0800 Subject: [PATCH] Adding proxy support --- doc/kurly.man | 3 +++ main.go | 21 +++++++++++++++------ options.go | 7 +++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/doc/kurly.man b/doc/kurly.man index 864b19f..7066280 100644 --- a/doc/kurly.man +++ b/doc/kurly.man @@ -141,6 +141,9 @@ This option turns on verbose logging in \fBkurly\fP. .IP "-V, --version" Prints the version information of the program. +.IP "-x, --proxy " +Connects using the proxy provided in the URL. + .IP "-X, --request " This option specifies which request method had to be used for the current request. Some common HTTP verbs (methods) used are \fIGET\fP,\fIPOST\fP,\fIPUT\fP,\fIPATCH\fP,\fIDELETE\fP. diff --git a/main.go b/main.go index 8dddf49..5cf5cb6 100644 --- a/main.go +++ b/main.go @@ -25,10 +25,11 @@ import ( ) var ( - client = http.Client{} - Status = log.New(os.Stderr, "*", 0) - Incoming io.Writer - Outgoing io.Writer + client = http.Client{} + transport = http.Transport{} + Status = log.New(os.Stderr, "*", 0) + Incoming io.Writer + Outgoing io.Writer ) const ( @@ -98,11 +99,19 @@ func fetchUrl(target string, opts Options, c *cli.Context) error { client.CheckRedirect = opts.checkRedirect if opts.insecure { - client.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + } + + if opts.proxyUrl != "" { + proxyURL, err := url.Parse(opts.proxyUrl) + if err != nil { + return err } + transport.Proxy = http.ProxyURL(proxyURL) } + client.Transport = &transport + if remote, err = url.Parse(target); err != nil { return fmt.Errorf("Error: %s does not parse correctly as a URL", target) } diff --git a/options.go b/options.go index 7be4c1f..6271570 100644 --- a/options.go +++ b/options.go @@ -56,6 +56,7 @@ type Options struct { head bool insecure bool fdata FormData // fdata is the field for processed form data + proxyUrl string } func (o *Options) getOptions(app *cli.App) { @@ -182,6 +183,11 @@ func (o *Options) getOptions(app *cli.App) { Usage: "Allow insecure server connections when using TLS", Destination: &o.insecure, }, + cli.StringFlag{ + Name: "proxy, x", + Usage: "[protocol://]host[:port] Use this proxy", + Destination: &o.proxyUrl, + }, } } @@ -222,6 +228,7 @@ func (opts *Options) BuildCommonOptions(c *cli.Context) error { opts.dataRaw = c.StringSlice("data-raw") opts.dataURLEncode = c.StringSlice("data-urlencode") opts.form = c.StringSlice("form") + opts.proxyUrl = c.String("proxy") // If verbose set the logs writers if opts.verbose { -- GitLab