Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Added support for HTTP CONNECT proxying
Browse files Browse the repository at this point in the history
  • Loading branch information
abrgr committed Oct 9, 2015
1 parent 5b6590e commit fb92e41
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/apns/core.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module APNS
require "net/http"
require 'socket'
require 'openssl'
require 'json'
require 'uri'

@host = 'gateway.sandbox.push.apple.com'
@port = 2195
Expand All @@ -10,7 +12,7 @@ module APNS
@pass = nil

class << self
attr_accessor :host, :pem, :port, :pass
attr_accessor :host, :pem, :port, :pass, :proxy
end

def self.send_notification(device_token, message)
Expand Down Expand Up @@ -63,6 +65,25 @@ def self.feedback

protected

def self.open_conn_with_proxy(host, port)
return TCPSocket.new(self.host, self.port) unless self.proxy.present?

proxy_uri = URI.parse(self.proxy)
sock = TCPSocket.new(proxy_uri.host, proxy_uri.port)
sock << "CONNECT #{self.host}:#{self.port} HTTP/1.1\r\n"
sock << "Host: #{self.host}:#{self.port}\r\n"
sock << "Proxy-Authorization: Basic #{["#{proxy_uri.user}:#{proxy_uri.password}"].pack("m").chomp}\r\n" if proxy_uri.user
sock << "\r\n"

buffer = Net::BufferedIO.new(sock)
response = Net::HTTPResponse.read_new(buffer)
if not response.is_a? Net::HTTPOK
raise SocketError.new("Proxy refused connection [#{response.code}]")
end

return sock
end

def self.open_connection
raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
Expand All @@ -71,7 +92,7 @@ def self.open_connection
context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)

sock = TCPSocket.new(self.host, self.port)
sock = self.open_conn_with_proxy(self.host, self.port)
ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
ssl.connect

Expand All @@ -89,7 +110,7 @@ def self.feedback_connection
fhost = self.host.gsub('gateway','feedback')
puts fhost

sock = TCPSocket.new(fhost, 2196)
sock = self.open_conn_with_proxy(fhost, 2196)
ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
ssl.connect

Expand Down

0 comments on commit fb92e41

Please sign in to comment.