<commit_before>=begin
                  Arachni
  Copyright (c) 2010-2011 Tasos "Zapotek" Laskos <<EMAIL>>

  This is free software; you can copy and distribute and modify
  this program under the term of the GPL v2.0 License
  (See LICENSE file for details)

=end

require 'webrick'
require 'webrick/https'
require 'xmlrpc/server'
require 'openssl'

module Arachni

require Options.instance.dir['lib'] + 'rpc/xml/server/output'
require Options.instance.dir['lib'] + 'rpc/xml/server/framework'
require Options.instance.dir['lib'] + 'rpc/xml/server/options'

module RPC
module XML
module Server

#
# XMLRPC Server class
#
# Provides an XML-RPC server to assist with general integration and UI development.
#
# Only instantiated by the Dispatcher to provide support for multiple
# and concurent XMLRPC clients/scans.
#
# @author: Tasos "Zapotek" Laskos
#                                      <<EMAIL>>
#                                      <<EMAIL>>
# @version: 0.1.2
#
class Instance

    # the output interface for XML-RPC
    include Arachni::UI::Output
    include Arachni::Module::Utilities

    alias :shutdown! :shutdown

    #
    # Initializes the XML-RPC interface, the HTTP(S) server and the framework.
    #
    # @param    [Options]    opts
    #
    def initialize( opts )

        @opts = opts

        prep_framework
        banner

        if @opts.help
            print_help
            exit 0
        end

        if @opts.debug
            debug!
        end


        if @opts.reroute_to_logfile
            reroute_to_file( @opts.dir['root'] +
                "logs/XMLRPC-Server - #{Process.pid}:#{@opts.rpc_port} - #{Time.now.asctime}.log" )
        else
            reroute_to_file( false )
        end

        pkey = ::OpenSSL::PKey::RSA.new( File.read( opts.ssl_pkey ) )         if opts.ssl_pkey
        cert = ::OpenSSL::X509::Certificate.new( File.read( opts.ssl_cert ) ) if opts.ssl_cert

        if opts.ssl_pkey || opts.ssl_pkey
            verification = OpenSSL::SSL::VERIFY_PEER |
                ::OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
        else
            verification = ::OpenSSL::SSL::VERIFY_NONE
        end

        print_status( 'Initing HTTP Server...' )
        @server = ::WEBrick::HTTPServer.new(
            :Port            => opts.rpc_port || 1337,
            :SSLEnable       => opts.ssl      || false,
            :SSLVerifyClient => verification,
            :SSLCertName     => [ [ "CN", ::WEBrick::Utils::getservername ] ],
            :SSLCertificate  => cert,
            :SSLPrivateKey   => pkey,
            :SSLCACertificateFile => opts.ssl_ca
        )

        set_handlers

        # trap interupts and exit cleanly when required
        trap( 'HUP' ) { @server.shutdown }
        trap( 'INT' ) { @server.shutdown }

    end

    #
    # Resets the framework leaving it lemon fresh for the next scan.
    #
    # If you reuse without reseting, Arachni will eat your kitten!<br/>
    # (And I don't mean sexually...)
    #
    def reset

        print_status( 'Resetting...' )

        exception_jail {
            @framework.modules.clear
            Arachni.reset
            Arachni::Options.instance.reset
            prep_framework
            set_handlers
            output
        }

        print_status( 'Done.' )

        return true
    end

    #
    # Flushes the output buffer and returns all pending system messages.
    #
    # All messages are classified based on their type.
    #
    # @return   [Array<Hash>]
    #
    def output
        flush_buffer( )
    end

    #
    # Makes the HTTP(S) server go bye-bye...Lights out!
    #
    def shutdown
        print_status( 'Shutting down...' )
        @server.shutdown
        print_status( 'Done.' )
        return true
    end

    #
    # Starts the HTTP(S) server and the XML-RPC service.
    #
    def run( )

        begin
            print_status( 'Starting the server...' )
            # start the show!<commit_msg>moved alias for shutdown after the initial decleration<commit_after>=begin
                  Arachni
  Copyright (c) 2010-2011 <NAME>" Laskos <<EMAIL>>

  This is free software; you can copy and distribute and modify
  this program under the term of the GPL v2.0 License
  (See LICENSE file for details)

=end

require 'webrick'
require 'webrick/https'
require 'xmlrpc/server'
require 'openssl'

module Arachni

require Options.instance.dir['lib'] + 'rpc/xml/server/output'
require Options.instance.dir['lib'] + 'rpc/xml/server/framework'
require Options.instance.dir['lib'] + 'rpc/xml/server/options'

module RPC
module XML
module Server

#
# XMLRPC Server class
#
# Provides an XML-RPC server to assist with general integration and UI development.
#
# Only instantiated by the Dispatcher to provide support for multiple
# and concurent XMLRPC clients/scans.
#
# @author: Tasos "Zapotek" Laskos
#                                      <<EMAIL>>
#                                      <<EMAIL>>
# @version: 0.1.2
#
class Instance

    # the output interface for XML-RPC
    include Arachni::UI::Output
    include Arachni::Module::Utilities

    #
    # Initializes the XML-RPC interface, the HTTP(S) server and the framework.
    #
    # @param    [Options]    opts
    #
    def initialize( opts )

        @opts = opts

        prep_framework
        banner

        if @opts.help
            print_help
            exit 0
        end

        if @opts.debug
            debug!
        end


        if @opts.reroute_to_logfile
            reroute_to_file( @opts.dir['root'] +
                "logs/XMLRPC-Server - #{Process.pid}:#{@opts.rpc_port} - #{Time.now.asctime}.log" )
        else
            reroute_to_file( false )
        end

        pkey = ::OpenSSL::PKey::RSA.new( File.read( opts.ssl_pkey ) )         if opts.ssl_pkey
        cert = ::OpenSSL::X509::Certificate.new( File.read( opts.ssl_cert ) ) if opts.ssl_cert

        if opts.ssl_pkey || opts.ssl_pkey
            verification = OpenSSL::SSL::VERIFY_PEER |
                ::OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
        else
            verification = ::OpenSSL::SSL::VERIFY_NONE
        end

        print_status( 'Initing HTTP Server...' )
        @server = ::WEBrick::HTTPServer.new(
            :Port            => opts.rpc_port || 1337,
            :SSLEnable       => opts.ssl      || false,
            :SSLVerifyClient => verification,
            :SSLCertName     => [ [ "CN", ::WEBrick::Utils::getservername ] ],
            :SSLCertificate  => cert,
            :SSLPrivateKey   => pkey,
            :SSLCACertificateFile => opts.ssl_ca
        )

        set_handlers

        # trap interupts and exit cleanly when required
        trap( 'HUP' ) { @server.shutdown }
        trap( 'INT' ) { @server.shutdown }

    end

    #
    # Resets the framework leaving it lemon fresh for the next scan.
    #
    # If you reuse without reseting, Arachni will eat your kitten!<br/>
    # (And I don't mean sexually...)
    #
    def reset

        print_status( 'Resetting...' )

        exception_jail {
            @framework.modules.clear
            Arachni.reset
            Arachni::Options.instance.reset
            prep_framework
            set_handlers
            output
        }

        print_status( 'Done.' )

        return true
    end

    #
    # Flushes the output buffer and returns all pending system messages.
    #
    # All messages are classified based on their type.
    #
    # @return   [Array<Hash>]
    #
    def output
        flush_buffer( )
    end

    #
    # Makes the HTTP(S) server go bye-bye...Lights out!
    #
    def shutdown
        print_status( 'Shutting down...' )
        @server.shutdown
        print_status( 'Done.' )
        return true
    end
    alias :shutdown! :shutdown

    #
    # Starts the HTTP(S) server and the XML-RPC service.
    #
    def run( )

        begin
            print_status( 'Starting the server...' )
            # start the show!
{'fraction_non_alphanumeric': 0.10017596782302665, 'fraction_numerical': 0.005279034690799397, 'mean_word_length': 1.8787988422575976, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1660378', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2346, 'n_words': 635}