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

add support for http proxy #129

Closed
wants to merge 5 commits into from
Closed
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
68 changes: 68 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"net"
"net/http"
"net/url"
"time"

"github.com/sideshow/apns2/token"
Expand All @@ -37,6 +38,8 @@ var (
// TCPKeepAlive specifies the keep-alive period for an active network
// connection. If zero, keep-alives are not enabled.
TCPKeepAlive = 60 * time.Second
// IdleConnTimeout specifies the max idle time of the connection
IdleConnTimeout = 300 * time.Second
)

// DialTLS is the default dial function for creating TLS connections for
Expand Down Expand Up @@ -93,6 +96,42 @@ func NewClient(certificate tls.Certificate) *Client {
}
}

// NewProxyClient returns a new Client with http proxy enabled
// Since the transport of http1.1 does not support DialTLS with http proxy enabled
// The DialTLS (including TLSDialTimeout and TCPKeepAlive) will be disabled if you use this function
// proxyUrl like http://127.0.0.1:8888
func NewProxyClient(certificate tls.Certificate, proxyUrl string) *Client {
if proxyUrl == "" {
return NewClient(certificate)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{certificate},
}
if len(certificate.Certificate) > 0 {
tlsConfig.BuildNameToCertificate()
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: func(request *http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
},
IdleConnTimeout: IdleConnTimeout,
}
err := http2.ConfigureTransport(transport)
//if configure failed
if err != nil {
return nil
}
return &Client{
HTTPClient: &http.Client{
Transport: transport,
Timeout: HTTPClientTimeout,
},
Certificate: certificate,
Host: DefaultHost,
}
}

// NewTokenClient returns a new Client with an underlying http.Client configured
// with the correct APNs HTTP/2 transport settings. It does not connect to the APNs
// until the first Notification is sent via the Push method.
Expand All @@ -115,6 +154,35 @@ func NewTokenClient(token *token.Token) *Client {
}
}

// NewProxyTokenClient extends NewTokenClient to support http proxy
// Since the transport of http1.1 does not support DialTLS with http proxy enabled
// The DialTLS (including TLSDialTimeout and TCPKeepAlive) will be disabled if you use this function
// proxyUrl like http://127.0.0.1:8888
func NewProxyTokenClient(token *token.Token, proxyUrl string) *Client {
if proxyUrl == "" {
return NewTokenClient(token)
}
transport := &http.Transport{
Proxy: func(request *http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
},
IdleConnTimeout: IdleConnTimeout,
}
err := http2.ConfigureTransport(transport)
//if configure failed
if err != nil {
return nil
}
return &Client{
Token: token,
HTTPClient: &http.Client{
Transport: transport,
Timeout: HTTPClientTimeout,
},
Host: DefaultHost,
}
}

// Development sets the Client to use the APNs development push endpoint.
func (c *Client) Development() *Client {
c.Host = HostDevelopment
Expand Down
10 changes: 10 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func TestTokenDefaultHost(t *testing.T) {
assert.Equal(t, "https://api.development.push.apple.com", client.Host)
}

func TestClientProxyDefaultHost(t *testing.T) {
client := apns.NewProxyClient(mockCert(), "http://127.0.0.1:8888")
assert.Equal(t, "https://api.development.push.apple.com", client.Host)
}

func TestTokenProxyDefaultHost(t *testing.T) {
client := apns.NewProxyTokenClient(mockToken(), "http://127.0.0.1:8888").Development()
assert.Equal(t, "https://api.development.push.apple.com", client.Host)
}

func TestClientDevelopmentHost(t *testing.T) {
client := apns.NewClient(mockCert()).Development()
assert.Equal(t, "https://api.development.push.apple.com", client.Host)
Expand Down