diff --git a/cli.go b/cli.go index 88f3ef27..fedaeed6 100644 --- a/cli.go +++ b/cli.go @@ -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()) diff --git a/unixproxy.go b/unixproxy.go new file mode 100644 index 00000000..1873b8d7 --- /dev/null +++ b/unixproxy.go @@ -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) +}