Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/net/dns/resolver/socks.rb
Views: 11784
# -*- coding: binary -*-1require 'socket'2require 'ipaddr'34class RawSocket # :nodoc:56@@id_arr = []78def initialize(src_addr,dest_addr)910# Define socket11begin12@socket = Socket.new PF_INET, SOCK_RAW, IPPROTO_RAW13rescue SystemCallError => e14raise SystemCallError, "You must be root to use raw sockets! #{e}"15end1617@socket.setsockopt IPPROTO_IP, IP_HDRINCL, 11819# Checks addresses20@src_addr = check_addr src_addr21@dest_addr = check_addr dest_addr2223# Source and destination port are zero24@src_port = 025@dest_port = 02627# Set correct protocol version in the header28@version = @dest_addr.ipv4? ? "0100" : "0110"2930# Total length: must be overridden by subclasses31@tot_lenght = 203233# Protocol: must be overridden by subclasses34@protocol = 1 # ICMP by default3536# Generate a new id37# @id = genID38@id = 12343940# Generate peer sockaddr41@to = Socket.pack_sockaddr_in @dest_port, @dest_addr.to_s42end4344def send(payload = '')45packet = make_ip_header([[ @version+'0101', 'B8' ], # version, hlen46[ 0, 'C' ], # tos47[ @tot_lenght + payload.size, 'n' ], # total len48[ @id, 'n' ], # id49[ 0, 'n' ], # flags, offset50[ 64, 'C' ], # ttl51[ @protocol, 'C' ], # protocol52[ 0, 'n' ], # checksum53[ @src_addr.to_i, 'N' ], # source54[ @dest_addr.to_i, 'N' ], # destination55])56packet << make_transport_header(payload.size)57packet << [payload].pack("a*")58@socket.send(packet,0,@to)59end6061private6263def check_addr addr64case addr65when String66IPAddr.new addr67when IPAddr68addr69else70raise ArgumentError, "Wrong address format: #{addr}"71end72end7374def check_port port75if (1..65535).include? port and port.kind_of? Integer76port77else78raise ArgumentError, "Port #{port} not valid"79end80end8182def genID83while (@@id_arr.include?(q = rand(65535)))84end85@@id_arr.push(q)86q87end8889def ipchecksum(data)90checksum = data.unpack("n*").inject(0) { |s, x| s + x }91((checksum >> 16) + (checksum & 0xffff)) ^ 0xffff92end9394def make_ip_header(parts)95template = ''96data = []97parts.each do |part|98data += part[0..-2]99template << part[-1]100end101data_str = data.pack(template)102checksum = ipchecksum(data_str)103data[-3] = checksum104data.pack(template)105end106107def make_transport_header108""109end110111end112113class UdpRawSocket < RawSocket # :nodoc:114115def initialize(src_addr,src_port,dest_addr,dest_port)116117super(src_addr,dest_addr)118119# Check ports120@src_port = check_port src_port121@dest_port = check_port dest_port122123# Total length: must be overridden by subclasses124@tot_lenght = 20 + 8 # 8 bytes => UDP Header125126# Protocol: must be overridden by subclasses127@protocol = 17 # UDP protocol128129@to = Socket.pack_sockaddr_in @dest_port, @dest_addr.to_s130end131132private133134def make_udp_header(parts)135template = ''136data = []137parts.each do |part|138data += part[0..-2]139template << part[-1]140end141data.pack(template)142end143144def make_transport_header(pay_size)145make_udp_header([146[ @src_port, 'n'], # source port147[ @dest_port, 'n' ], # destination port148[ 8 + pay_size, 'n' ], # len149[ 0, 'n' ] # checksum (mandatory)150])151end152153end154155156