lib/shadowsocks/local.rb in shadowsocks-0.10 vs lib/shadowsocks/local.rb in shadowsocks-0.11
- old
+ new
@@ -69,22 +69,35 @@
send_data "\x05\x00\x00\x01\x00\x00\x00\x00" + [config.server_port].pack('s>')
@stage = 4
- if config.chnroutes
- @behind_gfw = @ip_detector.behind_gfw?(@remote_addr[3..-1])
- end
+ op = proc {
+ if config.chnroutes
+ @behind_gfw = @ip_detector.behind_gfw?(@remote_addr[3..-1])
+ else
+ false
+ end
+ }
- if config.chnroutes == true and behind_gfw
- @connector = EventMachine.connect @remote_addr[3..-1], @remote_port, DirectConnector, self, crypto
- else
- @connector = EventMachine.connect config.server, config.server_port, ServerConnector, self, crypto
- end
+ callback = proc { |result|
+ begin
+ if !(config.chnroutes and result)
+ raise 'will use remote server'
+ end
+ @connector = EM.connect @remote_addr[3..-1], @remote_port, \
+ DirectConnector, self, crypto
+ rescue Exception
+ @connector = EM.connect config.server, config.server_port, \
+ ServerConnector, self, crypto
+ end
- if data.size > header_length
- cached_pieces.push data[header_length, data.size]
- end
+ if data.size > header_length
+ cached_pieces.push data[header_length, data.size]
+ end
+ }
+
+ EM.defer op, callback
rescue Exception => e
warn e
connection_cleanup
end
end