module ShadowsocksRuby::Util
Various utility functions
Constants
- ATYP_DOMAIN
- ATYP_IPV4
- ATYP_IPV6
Public Instance Methods
bin2hex(bytes)
click to toggle source
Hex encodes a message
@param [String] bytes The bytes to encode
@return [String] Tasty, tasty hexadecimal
# File lib/shadowsocks_ruby/util.rb, line 15 def bin2hex(bytes) bytes.to_s.unpack("H*").first end
hex2bin(hex)
click to toggle source
Hex decodes a message
@param [String] hex hex to decode.
@return [String] crisp and clean bytes
# File lib/shadowsocks_ruby/util.rb, line 24 def hex2bin(hex) [hex.to_s].pack("H*") end
parse_address_bin(bytes)
click to toggle source
Parse address bytes @param [String] bytes The bytes to parse @return [Array<String, Integer>] Return Host, Port
# File lib/shadowsocks_ruby/util.rb, line 31 def parse_address_bin(bytes) 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