Module: ShadowsocksRuby::Util
- Defined in:
- lib/shadowsocks_ruby/util.rb
Overview
Various utility functions
Constant Summary collapse
- ATYP_IPV4 =
1
- ATYP_DOMAIN =
3
- ATYP_IPV6 =
4
Class Method Summary collapse
-
.bin2hex(bytes) ⇒ String
Hex encodes a message.
-
.hex2bin(hex) ⇒ String
Hex decodes a message.
-
.parse_address_bin(bytes) ⇒ Array<String, Integer>
Parse address bytes.
Class Method Details
.bin2hex(bytes) ⇒ String
Hex encodes a message
15 16 17 |
# File 'lib/shadowsocks_ruby/util.rb', line 15 def bin2hex(bytes) bytes.to_s.unpack("H*").first end |
.hex2bin(hex) ⇒ String
Hex decodes a message
24 25 26 |
# File 'lib/shadowsocks_ruby/util.rb', line 24 def hex2bin(hex) [hex.to_s].pack("H*") end |
.parse_address_bin(bytes) ⇒ Array<String, Integer>
Parse address bytes
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/shadowsocks_ruby/util.rb', line 31 def parse_address_bin(bytes) bytes = bytes.dup address_type = bytes.slice!(0, 1).unpack("C").first case address_type when ATYP_IPV4 host = IPAddr.ntop bytes.slice!(0, 4) port = bytes.slice!(0, 2).unpack('n').first [host, port] when ATYP_IPV6 host = IPAddr.ntop bytes.slice!(0, 16) port = bytes.slice!(0, 2).unpack('n').first [host, port] when ATYP_DOMAIN domain_len = bytes.slice!(0, 1).unpack("C").first host = bytes.slice!(0, domain_len) port = bytes.slice!(0, 2).unpack('n').first [host, port] else raise PharseError, "unknown address_type: #{address_type}" end end |