peatio 0.6.3 → 2.4.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,135 +0,0 @@
1
- module Peatio::Ranger
2
- class Connection
3
- def initialize(authenticator, socket, logger)
4
- @authenticator = authenticator
5
- @socket = socket
6
- @logger = logger
7
- end
8
-
9
- def send(method, data)
10
- payload = JSON.dump(method => data)
11
- @logger.debug { payload }
12
- @socket.send payload
13
- end
14
-
15
- def authenticate(jwt)
16
- payload = {}
17
- authorized = false
18
- begin
19
- payload = @authenticator.authenticate!(jwt)
20
- authorized = true
21
- rescue Peatio::Auth::Error => error
22
- @logger.error error.message
23
- end
24
- return [authorized, payload]
25
- end
26
-
27
- def update_streams
28
- @socket.instance_variable_set(:@connection_handler, @client)
29
- end
30
-
31
- def subscribe(streams)
32
- raise "Streams must be an array of strings" unless streams.is_a?(Array)
33
- streams.each do |stream|
34
- next if stream.nil?
35
- @client.streams[stream] = true
36
- end
37
- send :success, message: "subscribed", streams: @client.streams.keys
38
- end
39
-
40
- def unsubscribe(streams)
41
- raise "Streams must be an array of strings" unless streams.is_a?(Array)
42
- streams.each do |stream|
43
- next if stream.nil?
44
- @client.streams.delete(stream)
45
- end
46
- send :success, message: "unsubscribed", streams: @client.streams.keys
47
- end
48
-
49
- def handle(msg)
50
- begin
51
- data = JSON.parse(msg)
52
-
53
- case data["event"]
54
- when "subscribe"
55
- subscribe data["streams"]
56
- when "unsubscribe"
57
- unsubscribe data["streams"]
58
- end
59
-
60
- rescue JSON::ParserError => error
61
- @logger.debug { "#{error}, msg: `#{msg}`" }
62
- end
63
- end
64
-
65
- def handshake(hs)
66
- @client = Peatio::MQ::Events::Client.new(@socket)
67
-
68
- query = URI::decode_www_form(hs.query_string)
69
- subscribe(query.map {|item| item.last if item.first == "stream"})
70
- @logger.info "ranger: WebSocket connection openned"
71
-
72
- if hs.headers_downcased.key?("authorization")
73
- authorized, payload = authenticate(hs.headers["authorization"])
74
-
75
- if !authorized
76
- @logger.info "ranger: #{@client.user} authentication failed"
77
- raise EM::WebSocket::HandshakeError, "Authorization failed"
78
- else
79
- @logger.info [authorized, payload].inspect
80
- @client.user = payload[:uid]
81
- @client.authorized = true
82
- @logger.info "ranger: user #{@client.user} authenticated #{@client.streams}"
83
- end
84
- end
85
- end
86
- end
87
-
88
- def self.run!(jwt_public_key)
89
- host = ENV["RANGER_HOST"] || "0.0.0.0"
90
- port = ENV["RANGER_PORT"] || "8081"
91
-
92
- authenticator = Peatio::Auth::JWTAuthenticator.new(jwt_public_key)
93
-
94
- logger = Peatio::Logger.logger
95
- logger.info "Starting the server on port #{port}"
96
-
97
- EM.run do
98
- Peatio::MQ::Client.new
99
- Peatio::MQ::Client.connect!
100
- Peatio::MQ::Client.create_channel!
101
-
102
- Peatio::MQ::Events.subscribe!
103
-
104
- EM::WebSocket.start(
105
- host: host,
106
- port: port,
107
- secure: false
108
- ) do |socket|
109
- connection = Connection.new(authenticator, socket, logger)
110
-
111
- socket.onopen do |hs|
112
- connection.handshake(hs)
113
- end
114
-
115
- socket.onmessage do |msg|
116
- connection.handle(msg)
117
- end
118
-
119
- socket.onping do |value|
120
- logger.info "Received ping: #{value}"
121
- end
122
-
123
- socket.onclose do
124
- logger.info "ranger: websocket connection closed"
125
- end
126
-
127
- socket.onerror do |e|
128
- logger.error "ranger: WebSocket Error: #{e.message}"
129
- end
130
- end
131
-
132
- yield if block_given?
133
- end
134
- end
135
- end