def _test_connectivity(self, param):

        action_result = ActionResult(dict(param))
        self.add_action_result(action_result)

        params = dict()
        params['url'] = "https://www.screenshotmachine.com"
        self.save_progress("Checking to see if Screenshotmachine.com is online...")

        params['key'] = self._api_key

        # Check if we have a Secret Phrase
        if self._api_phrase is None:
            params['hash'] = ""
        else:
            params['hash'] = str(hashlib.md5((params['url'] + self._api_phrase).encode('utf-8')).hexdigest())

        params['cacheLimit'] = '0'
        ret_val, resp_data = self._make_rest_call('', action_result, params, method='post', stream=True)

        if phantom.is_fail(ret_val):
            action_result.append_to_message('Test connectivity failed')
            return action_result.get_status()

        self.save_progress("Test Connectivity Passed")
        return action_result.set_status(ret_val, "Test Connectivity Passed")
Beispiel #2
0
    def _expand_url(self, param):

        # Get the config
        config = self.get_config()

        # Add an action result to the App Run
        action_result = ActionResult(dict(param))
        self.add_action_result(action_result)

        # Get url and test for service
        shortUrl = param.get(URLEXPANDER_URL)

        # Search for goo.gl short link
        if shortUrl.find('goo.gl', 0, 20) > 0:
            # Get the server
            server = config.get(URLEXPANDER_JSON_GOOGL_SERVER)
            apikey = config.get(URLEXPANDER_JSON_GOOGL_APIKEY)

            if (not server):
                self.save_progress("Goo.gl server not set")
                return self.get_status()

            self.save_progress("Querying goo.gl server")

            # Progress
            self.save_progress(phantom.APP_PROG_CONNECTING_TO_ELLIPSES, server)

            try:
                response = requests.get(
                    'https://{}/urlshortener/v1/url?'.format(server),
                    params={
                        'key': apikey,
                        'shortUrl': shortUrl
                    },
                    verify=False)
                if response.status_code == 200:
                    action_result.set_status(phantom.APP_SUCCESS)

            except Exception as e:
                action_result.set_status(phantom.APP_ERROR,
                                         urlexpander_ERR_SERVER_CONNECTION, e)
                action_result.append_to_message(
                    urlexpander_ERR_CONNECTIVITY_TEST)
                return action_result.get_status()

            data = response.json()
            longUrl = data['longUrl']
            results = {"longUrl": longUrl}
            self.debug_print(results)
            action_result.add_data(results)

        # Search for bit.ly short link
        elif shortUrl.find('bit.ly', 0, 20) > 0:
            # Get the server
            server = config.get(URLEXPANDER_JSON_BITLY_SERVER)
            apikey = config.get(URLEXPANDER_JSON_BITLY_APIKEY)

            if (not server):
                self.save_progress("Bit.ly server not set")
                return self.get_status()

            self.save_progress("Querying Bit.ly server")

            # Progress
            self.save_progress(phantom.APP_PROG_CONNECTING_TO_ELLIPSES, server)

            try:
                url = "https://{}/v3/expand".format(server)
                params = {
                    "access_token": "{}".format(apikey),
                    "shortUrl": shortUrl
                }
                headers = {'cache-control': "no-cache"}
                response = requests.post(url, headers=headers, params=params)
                action_result.set_status(phantom.APP_SUCCESS)

            except Exception as e:
                action_result.set_status(phantom.APP_ERROR,
                                         URLEXPANDER_ERR_SERVER_CONNECTION, e)
                action_result.append_to_message(
                    URLEXPANDER_ERR_CONNECTIVITY_TEST)
                return action_result.get_status()

            data = response.json()
            bitly_dict = data['data']['expand'][0]
            longUrl = bitly_dict['long_url']
            results = {"longUrl": longUrl}
            self.debug_print(results)
            action_result.add_data(results)

        return action_result.get_status()