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

Can use contents of file #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 25 additions & 20 deletions lib/apns/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ module APNS
@port = 2195
# openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
@pem = nil # this should be the path of the pem file not the contentes
@pem_contents = nil # Alternative, this is the contents of the pem file instead of the file path
@pass = nil

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

def self.send_notification(device_token, message)
Expand Down Expand Up @@ -64,35 +65,39 @@ def self.feedback
protected

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)

context = OpenSSL::SSL::SSLContext.new
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)
ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
context self.get_context
sock = TCPSocket.new(self.host, self.port)
ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
ssl.connect

return sock, ssl
end

def self.feedback_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)

context = OpenSSL::SSL::SSLContext.new
context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
context = self.get_context

fhost = self.host.gsub('gateway','feedback')
puts fhost

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

return sock, ssl
end

def self.get_context
context = OpenSSL::SSL::SSLContext.new
if self.pem
raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
elsif self.pem_contents
raise "The contents to your pem file are empty!" unless self.pem_contents.empty?
context.cert = OpenSSL::X509::Certificate.new(self.pem_contents)
context.key = OpenSSL::PKey::RSA.new(self.pem_contents, self.pass)
else
raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
end
return context
end

end