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/telephony/modem.rb
Views: 11766
module Telephony12class Modem3attr_accessor :serialport, :sp, :sock4attr_accessor :baud, :data_bits, :parity, :stop_bits5attr_accessor :flowcontrol6attr_accessor :display7attr_reader :commandstate89NONE = SerialPort::NONE10HARD = SerialPort::HARD11SOFT = SerialPort::SOFT12SPACE = SerialPort::SPACE13MARK = SerialPort::MARK14EVEN = SerialPort::EVEN15ODD = SerialPort::ODD1617def initialize(serialport = '/dev/ttyS0')18@serialport = serialport || '/dev/ttyS0'19@sp = nil20@baud = 240021@data_bits = 822@parity = NONE23@stop_bits = 124@flowcontrol = NONE25@commandstate = true26@display = true2728# Connect to and init modem29begin30#print("Opening Serial Port #{@serialport} (#{@baud} #{@data_bits}#{@parity}#{@stop_bits})\r\n")31@sp = SerialPort.create(serialport)32@sp.modem_params = {'baud' => @baud, 'data_bits' => @data_bits, 'parity' => @parity, 'stop_bits' => @stop_bits}33@sp.read_timeout = -134@sp.rts = 135@sp.dtr = 1 if sp.respond_to?:dtr= # unsupported in windows ):36@sock = @sp37@sock.extend(Rex::IO::Stream)38@sock.extend(Rex::IO::StreamAbstraction::Ext)39rescue ::Interrupt40raise $!41rescue ::Exception => e42print("Error opening serial port #{@serialport} : #{e.class} #{e} #{e.backtrace}\r\n")43return true44end45end4647# This provides pass-through method support for the SerialPort object48def method_missing(meth, *args); self.sp.send(meth, *args); end4950def put_command(command, timeout)51switchback = false52if ! @commandstate53commandstate54switchback = true55end5657begin58self.flush # TODO: This doesn't work in exploits for some reason but it does in aux modules59@sp.puts command + "\r\n"60echo = get_response(timeout) # read back the echoed command (not really a response)6162rescue ::Interrupt63raise $!64rescue ::Exception => e65print("Error sending command to modem: #{e.class} #{e} #{e.backtrace}\r\n")66return67end6869result = get_response(timeout)7071datastate if switchback == true7273return result74end7576def get_response(timeout)77time = Time.now7879@sp.read_timeout = -180result = ''81while Time.now <= time + timeout82# get a char from the modem83begin84c = @sp.getc85if c86c = c.chr87result += c88if c == "\n"89result = result.chomp90break if result.length > 091end92end93rescue ::Interrupt94raise $!95rescue ::Exception => e96print("Error reading from modem: #{e.class} #{e} #{e.backtrace}\r\n")97return98end99end100101if result.length > 0102print "[m] #{result}\r\n" if @display103else104result = 'TIMEOUT'105end106107return result108end109110def commandstate111if ! @commandstate112@sp.break 10 # tenths of a second113@sp.puts '+++'114@sp.break 10 # tenths of a second115result = get_response(3)116if result != 'OK'117print( "Error switching to command state: FAILED\r\n" )118return false119else120@commandstate = true121end122end123124return true125end126127def datastate128if @commandstate129result = put_command('ATO0', 3)130if result =~ /CONNECT/i131@commandstate = false132else133print( "Error switching to data state: FAILED\r\n" )134return false135end136end137138return true139end140141def hangup142flush143if @commandstate == true144#print( "Hanging up... (commandstate ATH0)\r\n" )145result = put_command('ATH0', 3)146return true if result == 'OK' or result == 'NO CARRIER'147else148if @sp.respond_to?:dtr= # unsupported in windows ):149#print( "Hanging up... (DTR = 0)\r\n" )150@sp.dtr = 0151sleep 0.75152@sp.dtr = 1153result = get_response(3)154@commandstate = true if result == 'NO CARRIER'155return true156else157#print( "Hanging up... (datastate ATH0)\r\n" )158commandstate159result = put_command('ATH0', 3)160return true if result == 'OK'161end162end163164return false165end166167# flush any stale data in the read buffer168def flush169@sp.read_timeout = -1170while @sp.getc; end171end172173# TODO: convert all calls to Modem.params to Modem.modem_params and remove this def174def params=(params)175@sp.modem_params = params176end177178end179180end181182183