Guest User

Untitled

a guest
May 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. openssl pkcs12 -in cred.p12 -out cert.pem -nodes -clcerts
  2.  
  3. from struct import pack
  4. from OpenSSL import SSL
  5. from twisted.internet import reactor
  6. from twisted.internet.protocol import ClientFactory, Protocol
  7. from twisted.internet.ssl import ClientContextFactory
  8. import binascii
  9. import struct
  10.  
  11. APNS_SERVER_HOSTNAME = "gateway.sandbox.push.apple.com"
  12. APNS_SERVER_PORT = 2195
  13. APNS_SSL_CERTIFICATE_FILE = "<your ssl certificate.pem>"
  14. APNS_SSL_PRIVATE_KEY_FILE = "<your ssl private key.pem>"
  15. DEVICE_TOKEN = "<hexlified device token>"
  16. MESSAGE = '{"aps":{"alert":"twisted test"}}'
  17.  
  18. class APNSClientContextFactory(ClientContextFactory):
  19. def __init__(self):
  20. self.ctx = SSL.Context(SSL.SSLv3_METHOD)
  21. self.ctx.use_certificate_file(APNS_SSL_CERTIFICATE_FILE)
  22. self.ctx.use_privatekey_file(APNS_SSL_PRIVATE_KEY_FILE)
  23.  
  24. def getContext(self):
  25. return self.ctx
  26.  
  27. class APNSProtocol(Protocol):
  28.  
  29. def connectionMade(self):
  30. print "connection made"
  31. self.sendMessage(binascii.unhexlify(DEVICE_TOKEN), MESSAGE)
  32. self.transport.loseConnection()
  33.  
  34. def sendMessage(self, deviceToken, payload):
  35. # notification messages are binary messages in network order
  36. # using the following format:
  37. # <1 byte command> <2 bytes length><token> <2 bytes length><payload>
  38. fmt = "!cH32sH%ds" % len(payload)
  39. command = 'x00'
  40. msg = struct.pack(fmt, command, 32, deviceToken,
  41. len(payload), payload)
  42. print "%s: %s" %(binascii.hexlify(deviceToken), binascii.hexlify(msg))
  43. self.transport.write(msg)
  44.  
  45. class APNSClientFactory(ClientFactory):
  46. def buildProtocol(self, addr):
  47. print "Connected to APNS Server %s:%u" % (addr.host, addr.port)
  48. return APNSProtocol()
  49.  
  50. def clientConnectionLost(self, connector, reason):
  51. print "Lost connection. Reason: %s" % reason
  52.  
  53. def clientConnectionFailed(self, connector, reason):
  54. print "Connection failed. Reason: %s" % reason
  55.  
  56. if __name__ == '__main__':
  57. reactor.connectSSL(APNS_SERVER_HOSTNAME,
  58. APNS_SERVER_PORT,
  59. APNSClientFactory(),
  60. APNSClientContextFactory())
  61. reactor.run()
  62.  
  63. from struct import pack
  64. from OpenSSL import SSL
  65. from twisted.internet import reactor
  66. from twisted.internet.protocol import ClientFactory, Protocol
  67. from twisted.internet.ssl import ClientContextFactory
  68. import binascii
  69. import struct
  70.  
  71. APNS_SERVER_HOSTNAME = "gateway.sandbox.push.apple.com"
  72. APNS_SERVER_PORT = 2195
  73. APNS_SSL_CERTIFICATE_FILE = "<your ssl certificate.pem>"
  74. APNS_SSL_PRIVATE_KEY_FILE = "<your ssl private key.pem>"
  75. DEVICE_TOKEN = "<hexlified device token>"
  76. MESSAGE = '{"aps":{"alert":"twisted test"}}'
  77.  
  78. class APNSClientContextFactory(ClientContextFactory):
  79. def __init__(self):
  80. self.ctx = SSL.Context(SSL.SSLv3_METHOD)
  81. self.ctx.use_certificate_file(APNS_SSL_CERTIFICATE_FILE)
  82. self.ctx.use_privatekey_file(APNS_SSL_PRIVATE_KEY_FILE)
  83.  
  84. def getContext(self):
  85. return self.ctx
  86.  
  87. class APNSProtocol(Protocol):
  88.  
  89. def connectionMade(self):
  90. print "connection made"
  91. self.sendMessage(binascii.unhexlify(DEVICE_TOKEN), MESSAGE)
  92. self.transport.loseConnection()
  93.  
  94. def sendMessage(self, deviceToken, payload):
  95. # notification messages are binary messages in network order
  96. # using the following format:
  97. # <1 byte command> <2 bytes length><token> <2 bytes length><payload>
  98. fmt = "!cH32sH%ds" % len(payload)
  99. command = 'x00'
  100. msg = struct.pack(fmt, command, 32, deviceToken,
  101. len(payload), payload)
  102. print "%s: %s" %(binascii.hexlify(deviceToken), binascii.hexlify(msg))
  103. self.transport.write(msg)
  104.  
  105. class APNSClientFactory(ClientFactory):
  106. def buildProtocol(self, addr):
  107. print "Connected to APNS Server %s:%u" % (addr.host, addr.port)
  108. return APNSProtocol()
  109.  
  110. def clientConnectionLost(self, connector, reason):
  111. print "Lost connection. Reason: %s" % reason
  112.  
  113. def clientConnectionFailed(self, connector, reason):
  114. print "Connection failed. Reason: %s" % reason
  115.  
  116. if __name__ == '__main__':
  117. reactor.connectSSL(APNS_SERVER_HOSTNAME,
  118. APNS_SERVER_PORT,
  119. APNSClientFactory(),
  120. APNSClientContextFactory())
  121. reactor.run()
  122.  
  123. from struct import pack
  124. from OpenSSL import SSL
  125. from twisted.internet import reactor
  126. from twisted.internet.protocol import ClientFactory, Protocol
  127. from twisted.internet.ssl import ClientContextFactory
  128. import binascii
  129. import struct
  130.  
  131. APNS_SERVER_HOSTNAME = "gateway.sandbox.push.apple.com"
  132. APNS_SERVER_PORT = 2195
  133. APNS_SSL_CERTIFICATE_FILE = "<your ssl certificate.pem>"
  134. APNS_SSL_PRIVATE_KEY_FILE = "<your ssl private key.pem>"
  135. DEVICE_TOKEN = "<hexlified device token>"
  136. MESSAGE = '{"aps":{"alert":"twisted test"}}'
  137.  
  138. class APNSClientContextFactory(ClientContextFactory):
  139. def __init__(self):
  140. self.ctx = SSL.Context(SSL.SSLv3_METHOD)
  141. self.ctx.use_certificate_file(APNS_SSL_CERTIFICATE_FILE)
  142. self.ctx.use_privatekey_file(APNS_SSL_PRIVATE_KEY_FILE)
  143.  
  144. def getContext(self):
  145. return self.ctx
  146.  
  147. class APNSProtocol(Protocol):
  148.  
  149. def connectionMade(self):
  150. print "connection made"
  151. self.sendMessage(binascii.unhexlify(DEVICE_TOKEN), MESSAGE)
  152. self.transport.loseConnection()
  153.  
  154. def sendMessage(self, deviceToken, payload):
  155. # notification messages are binary messages in network order
  156. # using the following format:
  157. # <1 byte command> <2 bytes length><token> <2 bytes length><payload>
  158. fmt = "!cH32sH%ds" % len(payload)
  159. command = 'x00'
  160. msg = struct.pack(fmt, command, 32, deviceToken,
  161. len(payload), payload)
  162. print "%s: %s" %(binascii.hexlify(deviceToken), binascii.hexlify(msg))
  163. self.transport.write(msg)
  164.  
  165. class APNSClientFactory(ClientFactory):
  166. def buildProtocol(self, addr):
  167. print "Connected to APNS Server %s:%u" % (addr.host, addr.port)
  168. return APNSProtocol()
  169.  
  170. def clientConnectionLost(self, connector, reason):
  171. print "Lost connection. Reason: %s" % reason
  172.  
  173. def clientConnectionFailed(self, connector, reason):
  174. print "Connection failed. Reason: %s" % reason
  175.  
  176. if __name__ == '__main__':
  177. reactor.connectSSL(APNS_SERVER_HOSTNAME,
  178. APNS_SERVER_PORT,
  179. APNSClientFactory(),
  180. APNSClientContextFactory())
  181. reactor.run()
  182.  
  183. from struct import pack
  184. from OpenSSL import SSL
  185. from twisted.internet import reactor
  186. from twisted.internet.protocol import ClientFactory, Protocol
  187. from twisted.internet.ssl import ClientContextFactory
  188.  
  189. APNS_SERVER_HOSTNAME = "<insert the push hostname from your iPhone developer portal>"
  190. APNS_SERVER_PORT = 2195
  191. APNS_SSL_CERTIFICATE_FILE = "<your ssl certificate.pem>"
  192. APNS_SSL_PRIVATE_KEY_FILE = "<your ssl private key.pem>"
  193.  
  194. class APNSClientContextFactory(ClientContextFactory):
  195. def __init__(self):
  196. self.ctx = SSL.Context(SSL.SSLv3_METHOD)
  197. self.ctx.use_certificate_file(APNS_SSL_CERTIFICATE_FILE)
  198. self.ctx.use_privatekey_file(APNS_SSL_PRIVATE_KEY_FILE)
  199.  
  200. def getContext(self):
  201. return self.ctx
  202.  
  203. class APNSProtocol(Protocol):
  204. def sendMessage(self, deviceToken, payload):
  205. # notification messages are binary messages in network order
  206. # using the following format:
  207. # <1 byte command> <2 bytes length><token> <2 bytes length><payload>
  208. fmt = "!cH32cH%dc" % len(payload)
  209. command = 0
  210. msg = struct.pack(fmt, command, deviceToken,
  211. len(payload), payload)
  212. self.transport.write(msg)
  213.  
  214. class APNSClientFactory(ClientFactory):
  215. def buildProtocol(self, addr):
  216. print "Connected to APNS Server %s:%u" % (addr.host, addr.port)
  217. return APNSProtocol()
  218.  
  219. def clientConnectionLost(self, connector, reason):
  220. print "Lost connection. Reason: %s" % reason
  221.  
  222. def clientConnectionFailed(self, connector, reason):
  223. print "Connection failed. Reason: %s" % reason
  224.  
  225. if __name__ == '__main__':
  226. reactor.connectSSL(APNS_SERVER_HOSTNAME,
  227. APNS_SERVER_PORT,
  228. APNSClientFactory(),
  229. APNSClientContextFactory())
  230. reactor.run()
Add Comment
Please, Sign In to add comment