Skip to content

Commit

Permalink
Adding a unixproxy mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
jshirley-stripe committed Aug 25, 2016
1 parent 6d23899 commit 5b9c0dd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
29 changes: 19 additions & 10 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,35 @@ func New(opts map[string]interface{}) *Cli {
endpoint, _ := opts["endpoint"].(string)
url, _ := url.Parse(strings.TrimRight(endpoint, "/"))

transport := &http.Transport{
TLSClientConfig: &tls.Config{},
}

if project, ok := opts["project"].(string); ok {
opts["project"] = strings.ToUpper(project)
}

if insecureSkipVerify, ok := opts["insecure"].(bool); ok {
transport.TLSClientConfig.InsecureSkipVerify = insecureSkipVerify
var ua *http.Client
if unixProxyPath, ok := opts["unixproxy"].(string); ok {
ua = &http.Client{
Jar: cookieJar,
Transport: UnixProxy(unixProxyPath),
}
} else {
transport := &http.Transport{
TLSClientConfig: &tls.Config{},
}
if insecureSkipVerify, ok := opts["insecure"].(bool); ok {
transport.TLSClientConfig.InsecureSkipVerify = insecureSkipVerify
}

ua = &http.Client{
Jar: cookieJar,
Transport: transport,
}
}

cli := &Cli{
endpoint: url,
opts: opts,
cookieFile: filepath.Join(homedir, ".jira.d", "cookies.js"),
ua: &http.Client{
Jar: cookieJar,
Transport: transport,
},
ua: ua,
}

cli.ua.Jar.SetCookies(url, cli.loadCookies())
Expand Down
41 changes: 41 additions & 0 deletions unixproxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package jira

import (
"fmt"
"net"
"net/http"
"os"
"time"
)

type Transport struct {
shadow http.Transport
}

func NewUnixProxyTransport(path string) *Transport {
dial := func(network, addr string) (net.Conn, error) {
return net.Dial("unix", path)
}

shadow := http.Transport{
Dial: dial,
DialTLS: dial,
DisableKeepAlives: true,
ResponseHeaderTimeout: 30 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
}

return &Transport{shadow}
}

func UnixProxy(path string) *Transport {
return NewUnixProxyTransport(os.ExpandEnv(path))
}

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
req2 := *req
url2 := *req.URL
req2.URL = &url2
req2.URL.Opaque = fmt.Sprintf("//%s%s", req.URL.Host, req.URL.EscapedPath())
return t.shadow.RoundTrip(&req2)
}

0 comments on commit 5b9c0dd

Please sign in to comment.