Exemple #1
0
 def make(self, host, port, https=False, bufsize=16384, timeout=10):
     sock = socket.create_connection((host, port), timeout=timeout)
     set_recv_buf(sock, bufsize)
     if https:
         context = ssl._create_default_https_context()
         sock = context.wrap_socket(sock, server_hostname=host)
     return Connection(sock, not https)
Exemple #2
0
 def __init__(self,
              host,
              port=None,
              key_file=None,
              cert_file=None,
              timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
              source_address=None,
              *,
              context=None,
              check_hostname=None):
     super(HTTPSConnection, self).__init__(host, port, timeout,
                                           source_address)
     if (key_file is not None or cert_file is not None
             or check_hostname is not None):
         import warnings
         warnings.warn(
             'key_file, cert_file and check_hostname are deprecated, use a custom context instead.',
             DeprecationWarning, 2)
     self.key_file = key_file
     self.cert_file = cert_file
     if context is None:
         context = ssl._create_default_https_context()
     will_verify = context.verify_mode != ssl.CERT_NONE
     if check_hostname is None:
         check_hostname = context.check_hostname
     if check_hostname and not will_verify:
         raise ValueError(
             'check_hostname needs a SSL context with either CERT_OPTIONAL or CERT_REQUIRED'
         )
     if key_file or cert_file:
         context.load_cert_chain(cert_file, key_file)
     self._context = context
     self._check_hostname = check_hostname
Exemple #3
0
    def do_GET(self):
        print('[*] GET', self.headers['Host'] + "/" + self.path)
        hs = {}
        for h in self.headers:
            #if str(h) != 'Accept-Encoding':
            hs[h] = self.headers[h]
        print("\t\tcookie: ", self.headers['Cookie'])
        print("\n")

        for i in range(10):
            try:
                ctx = ssl._create_default_https_context()
                ctx.check_hostname = False
                conn = http.client.HTTPSConnection(self.headers['Host'], context=ctx, check_hostname=False)
                conn.request("GET", self.path, headers=hs)  # TODO check if headers are working
                r1 = conn.getresponse()
                data1 = r1.read()

                self.send_response(r1.status)
                for h in r1.headers:
                    self.send_header(h, r1.headers[h])
                self.end_headers()
                self.wfile.write(data1)
                return
            except Exception as e:
                logger.error("while do_GET" + str(e))
    def __init__(self,
                 moodle_domain: str,
                 moodle_path: str = '/',
                 token: str = '',
                 skip_cert_verify=False):
        """
        Opens a connection to the Moodle system
        """
        if skip_cert_verify:
            context = ssl._create_unverified_context()
        else:
            context = ssl._create_default_https_context()
        self.connection = HTTPSConnection(moodle_domain, context=context)

        self.token = token
        self.moodle_domain = moodle_domain
        self.moodle_path = moodle_path

        RequestHelper.stdHeader = {
            'User-Agent': ('Mozilla/5.0 (X11; Linux x86_64)' +
                           ' AppleWebKit/537.36 (KHTML, like Gecko)' +
                           ' Chrome/78.0.3904.108 Safari/537.36'),
            'Content-Type':
            'application/x-www-form-urlencoded'
        }
Exemple #5
0
def get_client(url, verify_ssl):
    context = ssl._create_default_https_context()
    if verify_ssl == 'false':
        context = ssl._create_unverified_context()

    client = ServerProxy(url, verbose=0, context=context)
    return client
Exemple #6
0
 def __init__(self,
              host,
              port=None,
              service_id=None,
              check_hostname=None,
              context=None,
              **kwargs):
     self._service_id = service_id
     if self._service_id:
         check_hostname = False
         # dont fial for older verions of python
         # without ssl._create_default_https_context
         # that also don't check by default
         try:
             context = ssl._create_default_https_context()
             context.check_hostname = False
             context.verify_mode = ssl.CERT_NONE
         except:
             pass
     http.client.HTTPSConnection.__init__(self,
                                          host,
                                          port,
                                          check_hostname=check_hostname,
                                          context=context,
                                          **kwargs)
Exemple #7
0
def connection_test():
    context = ssl._create_default_https_context()

    verify_ssl = request.headers.get('Secured')
    if verify_ssl == 'false':
        context = ssl._create_unverified_context()

    creds = request.get_json() or {}
    port = "443"
    if creds['port'] and creds['port'] != 0:
        port = creds['port']
    # check the host and port first before login
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(TIMEOUT)
        s.connect((creds['host'], int(port)))
    except Exception as e:
        return jsonify(error=str(e)), 404

    # login
    try:
        suma_url = "https://" + creds['host'] + ":" + str(port) + "/rpc/api"
        suma_username = creds['username']
        suma_password = creds['password']
        client = ServerProxy(suma_url, verbose=0, context=context)
        key = client.auth.login(suma_username, suma_password)
        return jsonify(key)
    except Exception as e:
        if 'SSL:' in str(e) or 'doesn\'t match' in str(e):
            return jsonify(error=str(e)), 403
        else:
            return jsonify(error=str(e)), 401
Exemple #8
0
def make_http_request(url, data=None, files=None, certificate=None):
    # type: (str, Optional[Envelope], Optional[Attachments], str) -> Tuple[int, bytes]
    if data is None:
        data = {}

    if files is None:
        files = {}

    schema, netloc, url, params, query, fragments = urlparse(url)
    logger.debug("Sending POST request to {0}".format(url))
    body, boundary = encode_multipart_formdata(data, files)

    if certificate:
        ssl_context = ssl.create_default_context(cafile=certificate)
    else:
        ssl_context = ssl._create_default_https_context()

    connection = HTTPSConnection(netloc, context=ssl_context)
    connection.connect()

    connection.putrequest("POST", url)
    connection.putheader("Content-Type",
                         "multipart/form-data; boundary={0}".format(boundary))
    connection.putheader("Content-Length", str(len(body)))
    connection.endheaders()

    connection.send(body)

    r = connection.getresponse()

    return r.status, r.read()
 def __init__(self,
              host,
              port=None,
              key_file=None,
              cert_file=None,
              timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
              source_address=None,
              *,
              context=None,
              check_hostname=None):
     super(HTTPSConnection, self).__init__(host, port, timeout,
                                           source_address)
     self.key_file = key_file
     self.cert_file = cert_file
     if context is None:
         context = ssl._create_default_https_context()
     will_verify = context.verify_mode != ssl.CERT_NONE
     if check_hostname is None:
         check_hostname = context.check_hostname
     if check_hostname and not will_verify:
         raise ValueError("check_hostname needs a SSL context with "
                          "either CERT_OPTIONAL or CERT_REQUIRED")
     if key_file or cert_file:
         context.load_cert_chain(cert_file, key_file)
     self._context = context
     self._check_hostname = check_hostname
def get_video_tag(video_href, rank_spider, df):
    sleep_time = (random.random() + 1) * random.randint(1, 4) + random.randint(
        1, 2)
    sleep(sleep_time)
    Req = urllib.request.Request(video_href, headers=rank_spider.get_head())
    context = ssl._create_default_https_context()
    res = urllib.request.urlopen(Req, context=context)

    res_Str = res.read()
    print(res.getcode())

    buff = BytesIO(res_Str)
    f = gzip.GzipFile(fileobj=buff)
    try:
        res_Str = f.read()
    except:
        pass
    bsoup = BeautifulSoup(res_Str.decode('utf-8'), 'html.parser')

    tag = bsoup.find_all(re.compile('^li'), attrs={"class": "tag"})
    tags = []
    for tag_i in tag:
        tags.append(tag_i.a.contents)

    df['标签'] = [tags]
    # comments = bsoup.find_all(re.compile('^div'), attrs={"class": "comment-list"})
    # print(comments)
    # for comment_i in comments:
    #     print(comment_i.div.contents[1])
    #     print(comment_i.div.contents[1].div.a.contents)
    return df
    def get_url2html(self, urls, ToPath, flag=None):
        if os.path.exists(ToPath):
            pass
        else:
            os.mkdir(ToPath)
        if flag == 1:  # 早
            ToPath = ToPath + 'morning/'
            if os.path.exists(ToPath):
                pass
            else:
                os.mkdir(ToPath)
        else:
            ToPath = ToPath + 'night/'
            if os.path.exists(ToPath):
                pass
            else:
                os.mkdir(ToPath)

        for rank_i in urls.items():
            sleep(2)
            print('抓取{}排行榜'.format(rank_i[0]))
            Res = urllib.request.Request(rank_i[1], headers=self.get_head())
            context = ssl._create_default_https_context()
            res = urllib.request.urlopen(Res, context=context)
            # print('INFO: \n',res.info())
            print('STATUS: \n', res.getcode())
            res_Str = res.read()
            # print('CONTENTS: {}\n'.format(type(res_Str.decode('utf-8'))),res_Str.decode('utf-8'))

            with open(ToPath + rank_i[0] + '.html', 'wb') as f:
                f.write(res_Str)
            f.close()
def multipleCtxReinitializationsWithBadSettingBeforeFinalGoodDefault():
    import ssl
    ctx = ssl._create_unverified_context()
    ctx.verify_mode = ssl.CERT_NONE  # Noncompliant {{Enable server certificate validation on this SSL/TLS connection.}}
    #                     ^^^^^^^^^

    ctx = ssl._create_default_https_context(
    )  # Compliant (S4830) - bydefault = ctx.verify_mode = ssl.CERT_REQUIRED
Exemple #13
0
    def getConnection(self, host, timeout=300):
        self.context = ssl._create_default_https_context(cafile=self.cacert)
        # self.context.set_servername_callback(cb_sni)
        self.context.load_cert_chain(self.cert, self.key)
        self.context.verify_mode = ssl.CERT_REQUIRED

        self.con = MyHTTPSConnection(host, context=self.context, handler=self)
        return self.con
Exemple #14
0
	def connect(self):
		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		if self.ssl:
			context = ssl._create_default_https_context()
			context.check_hostname = self.host
			self.sock = context.wrap_socket(self.sock, server_hostname=self.host)
		self.sock.settimeout(3)
		self.sock.connect((self.host, self.port))
Exemple #15
0
def email(website):

    message = "Check this out this is the link:" + website
    port = 465
    context = ssl._create_default_https_context()
    server = smtplib.SMTP_SSL("smtp.gmail.com", port, context=context)
    server.login(sender_email, password)

    server.sendmail(sender_email, receiver_email, message)
Exemple #16
0
class PredixConfig(Config):
    PROTOCOL = 'https'
    CONTEXT = ssl._create_default_https_context()

    if os.getenv('VCAP_SERVICES'):
        UAA_URL = json.loads(
            os.getenv('VCAP_SERVICES'))['predix-uaa'][0]['credentials']['uri']
        CLIENT_ID, CLIENT_SECRET = base64.b64decode(
            bytes(os.getenv('base64ClientCredential'),
                  'utf-8')).decode().split(':')
Exemple #17
0
 def __init__(self, host, port=None, key_file=None, cert_file=None,
              strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
              source_address=None, context=None):
     HTTPConnection.__init__(self, host, port, strict, timeout,
                             source_address)
     self.key_file = key_file
     self.cert_file = cert_file
     if context is None:
         context = ssl._create_default_https_context()
     if key_file or cert_file:
         context.load_cert_chain(cert_file, key_file)
     self._context = context
Exemple #18
0
 def __init__(self, params='', url=''):
     self.ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
     if url != '':
         self.base_url = url
     else:
         self.base_url = 'default-url1'
     self.context = ssl._create_default_https_context()
     if params != '':
         self.url = '{}?{}'.format(self.base_url, urlencode(params))
     else:
         self.url = self.base_url
     self.response = Request(self.url, headers={'User-agent': self.ua})
Exemple #19
0
 def __init__(self, host, port=None, key_file=None, cert_file=None,
              strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
              source_address=None, context=None):
     HTTPConnection.__init__(self, host, port, strict, timeout,
                             source_address)
     self.key_file = key_file
     self.cert_file = cert_file
     if context is None:
         context = ssl._create_default_https_context()
     if key_file or cert_file:
         context.load_cert_chain(cert_file, key_file)
     self._context = context
    def __init__(self, host, port=None, service_id=None, check_hostname=None, context=None, **kwargs):
        self._service_id = service_id
        if self._service_id:
            context = ssl._create_default_https_context()
            context.check_hostname = False
            context.verify_mode = ssl.CERT_NONE
            context.load_cert_chain(settings.ssl_cert_path, settings.ssl_key_path)
            context.load_default_certs()
        http.client.HTTPSConnection.__init__(self, host, port,
            check_hostname=check_hostname, context=context, **kwargs)

        if not is_local(host):
            self._create_connection = create_tor_connection
Exemple #21
0
def collect_ssl(info_add):
    import os
    try:
        import ssl
    except ImportError:
        return
    try:
        import _ssl
    except ImportError:
        _ssl = None

    def format_attr(attr, value):
        if attr.startswith('OP_'):
            return '%#8x' % value
        else:
            return value

    attributes = (
        'OPENSSL_VERSION',
        'OPENSSL_VERSION_INFO',
        'HAS_SNI',
        'OP_ALL',
        'OP_NO_TLSv1_1',
    )
    copy_attributes(info_add, ssl, 'ssl.%s', attributes, formatter=format_attr)

    for name, ctx in (
        ('SSLContext', ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)),
        ('default_https_context', ssl._create_default_https_context()),
        ('stdlib_context', ssl._create_stdlib_context()),
    ):
        attributes = (
            'minimum_version',
            'maximum_version',
            'protocol',
            'options',
            'verify_mode',
        )
        copy_attributes(info_add, ctx, f'ssl.{name}.%s', attributes)

    env_names = ["OPENSSL_CONF", "SSLKEYLOGFILE"]
    if _ssl is not None and hasattr(_ssl, 'get_default_verify_paths'):
        parts = _ssl.get_default_verify_paths()
        env_names.extend((parts[0], parts[2]))

    for name in env_names:
        try:
            value = os.environ[name]
        except KeyError:
            continue
        info_add('ssl.environ[%s]' % name, value)
Exemple #22
0
def error_test(url):
    headers = {
        'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
    }
    ssl_contex = ssl._create_default_https_context()
    req = request.Request(url=url, headers=headers)
    try:
        response = request.urlopen(req, context=ssl_contex, timeout=90)
        print(response.status)
    except error.HTTPError as err:
        print(err.code, err.reason)
    except error.URLError as err:
        print(err.reason)
Exemple #23
0
def download_https(url, host='youtube.com'):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    context = ssl._create_default_https_context()
    context.check_hostname = host
    sock = context.wrap_socket(sock, server_hostname=host)
    try:
        sock.connect((host, 443))
    except Exception as e:
        print_debug('{} ({}{})'.format(e, host, url), 1)
        return None
    sock.settimeout(1)
    sock.write(b"GET " + url + b' HTTP/1.1\r\nHost: www.' + host.encode() +
               b'\r\nAccept-Language: en\r\n\r\n')
    return recv(sock, 'https://' + host + url.decode())
 def __init__(self, host, port=None, service_id=None, check_hostname=None, context=None, **kwargs):
     self._service_id = service_id
     if self._service_id:
         check_hostname = False
         # dont fial for older verions of python
         # without ssl._create_default_https_context
         # that also don't check by default
         try:
             context = ssl._create_default_https_context()
             context.check_hostname = False
             context.verify_mode = ssl.CERT_NONE
         except:
             pass
     http.client.HTTPSConnection.__init__(self, host, port,
             check_hostname=check_hostname, context=context, **kwargs)
Exemple #25
0
        def __init__(self,
                     host,
                     port=None,
                     key_file=None,
                     cert_file=None,
                     timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                     source_address=None,
                     *,
                     context=None,
                     check_hostname=None,
                     sock_keepalive=False,
                     tcp_keepalive=60,
                     tcp_keepintvl=60,
                     tcp_keepcnt=3):
            """
            :param host:            target host (fqdn or ip-address
            :param port:            target port
            :param timeout:         blocking calls time-out
            :param source_address:  source address tuple (ip. port) to be used
            :param sock_keepalive:  enable TCP keepalive, if True
            :param tcp_keepalive:   idle time used when SO_KEEPALIVE is enable
            :param tcp_keepintvl:   interval between keepalives
            :param tcp_keepcnt:     number of keepalives before close
            """
            # added to standard method:
            self.logger = logging.getLogger(__name__ + '.HTTPConnection')
            self.sock_keepalive = sock_keepalive
            self.tcp_keepalive = tcp_keepalive
            self.tcp_keepintvl = tcp_keepintvl
            self.tcp_keepcnt = tcp_keepcnt
            # end of addition

            super(HTTPSConnection, self).__init__(host, port, timeout,
                                                  source_address)
            self.key_file = key_file
            self.cert_file = cert_file
            if context is None:
                context = ssl._create_default_https_context()
            will_verify = context.verify_mode != ssl.CERT_NONE
            if check_hostname is None:
                check_hostname = context.check_hostname
            if check_hostname and not will_verify:
                raise ValueError("check_hostname needs a SSL context with "
                                 "either CERT_OPTIONAL or CERT_REQUIRED")
            if key_file or cert_file:
                context.load_cert_chain(cert_file, key_file)
            self._context = context
            self._check_hostname = check_hostname
Exemple #26
0
def _create_https_context(check_hostname, cafile, context):
    if context is None:
        context = ssl._create_default_https_context()
        # Enable PHA for TLS 1.3 connections if available
        if getattr(context, "post_handshake_auth", None) is not None:
            context.post_handshake_auth = True
    if cafile is not None:
        context.load_verify_locations(cafile)
    will_verify = context.verify_mode != ssl.CERT_NONE
    if check_hostname is None:
        check_hostname = context.check_hostname
    if check_hostname and not will_verify:
        raise ValueError("check_hostname needs a SSL context with "
                         "either CERT_OPTIONAL or CERT_REQUIRED")
    if check_hostname is not None:
        context.check_hostname = check_hostname
    return context
Exemple #27
0
    def __init__(self, host, port=None, key_file=None, cert_file=None,
                 key_password=None, strict=None,
                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                 source_address=None, verify_server=False, ca_certs=None):

        # The httplib.HTTPSConnection init arguments have changed over different Python versions:
        # Py 2.6: httplib.HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout]]]]])
        # Py 2.7: httplib.HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout[, source_address[, context]]]]]]])
        # Py 3.4: http.client.HTTPSConnection(host, port=None, key_file=None, cert_file=None, [timeout, ]source_address=None, *, context=None, check_hostname=None)

        if sys.version_info.major == 2 and sys.version_info.minor == 6:
            six.moves.http_client.HTTPSConnection.__init__(
                self, host, port, key_file, cert_file, strict, timeout)

            self.context = None
        elif ((sys.version_info.major == 2 and sys.version_info.minor == 7)
                or (sys.version_info.major == 3 and sys.version_info.minor == 4)):

            self.context = ssl._create_default_https_context(
                ssl.Purpose.CLIENT_AUTH, cafile=ca_certs)

            if cert_file or key_file:
                self.context.load_cert_chain(
                    cert_file, key_file, password=key_password)

            if sys.version_info.major == 2 and sys.version_info.minor == 7:
                six.moves.http_client.HTTPSConnection.__init__(
                    self, host, port, strict=strict, timeout=timeout,
                    source_address=source_address, context=self.context)

            elif sys.version_info.major == 3 and sys.version_info.minor == 4:
                super(VerifiableHTTPSConnection, self).__init__(
                    host, port, timeout=timeout, source_address=source_address,
                    context=self.context)
        else:
            raise RuntimeError("Unsupported Python version: '{0}'".format(sys.version))

        self.cert_file = cert_file
        self.key_file = key_file

        if verify_server:
            self.cert_reqs = ssl.CERT_REQUIRED
        else:
            self.cert_reqs = ssl.CERT_NONE
        self.ca_certs = ca_certs
Exemple #28
0
    def https(self, url, headers):
        '''
		Se connecte au port 443 (https)
		'''
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        context = ssl._create_default_https_context()
        context.check_hostname = self.host
        sock = context.wrap_socket(sock, server_hostname=self.host)
        try:
            sock.connect((self.host, 443))
        except Exception as e:
            self.Error(str(e))
            return None
        sock.settimeout(self.inter)
        # Envoie la requête
        sock.write(headers)
        # Recoie la requête
        return self.recv(sock, url)
Exemple #29
0
 def __init__(self, host, port=None, key_file=None, cert_file=None,
              timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
              source_address=None, *, context=None,
              check_hostname=None):
     super().__init__(host, port, timeout, source_address)
     self.key_file = key_file
     self.cert_file = cert_file
     if context is None:
         context = ssl._create_default_https_context()
     will_verify = context.verify_mode != ssl.CERT_NONE
     if check_hostname is None:
         check_hostname = context.check_hostname
     if check_hostname and not will_verify:
         raise ValueError("check_hostname needs a SSL context with "
                          "either CERT_OPTIONAL or CERT_REQUIRED")
     if key_file or cert_file:
         context.load_cert_chain(cert_file, key_file)
     self._context = context
     self._check_hostname = check_hostname
Exemple #30
0
        def __init__(self, host, port=None, key_file=None, cert_file=None,
                     timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                     source_address=None, *, context=None,
                     check_hostname=None, sock_keepalive=False,
                     tcp_keepalive=60, tcp_keepintvl=60, tcp_keepcnt=3):
            """
            :param host:            target host (fqdn or ip-address
            :param port:            target port
            :param timeout:         blocking calls time-out
            :param source_address:  source address tuple (ip. port) to be used
            :param sock_keepalive:  enable TCP keepalive, if True
            :param tcp_keepalive:   idle time used when SO_KEEPALIVE is enable
            :param tcp_keepintvl:   interval between keepalives
            :param tcp_keepcnt:     number of keepalives before close
            """
            # added to standard method:
            self.logger = logging.getLogger(__name__ + '.HTTPConnection')
            self.sock_keepalive = sock_keepalive
            self.tcp_keepalive = tcp_keepalive
            self.tcp_keepintvl = tcp_keepintvl
            self.tcp_keepcnt = tcp_keepcnt
            # end of addition

            super(HTTPSConnection, self).__init__(host, port, timeout,
                                                  source_address)
            self.key_file = key_file
            self.cert_file = cert_file
            if context is None:
                context = ssl._create_default_https_context()
            will_verify = context.verify_mode != ssl.CERT_NONE
            if check_hostname is None:
                check_hostname = context.check_hostname
            if check_hostname and not will_verify:
                raise ValueError("check_hostname needs a SSL context with "
                                 "either CERT_OPTIONAL or CERT_REQUIRED")
            if key_file or cert_file:
                context.load_cert_chain(cert_file, key_file)
            self._context = context
            self._check_hostname = check_hostname
Exemple #31
0
    def make_a_connection(self, req):
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect((req.host, 443))
        self.browser.sendall(OK_REPLY.encode())
        self.browser = ssl.wrap_socket(self.browser,
                                       keyfile='server.key',
                                       certfile='server.crt',
                                       server_side=True,
                                       do_handshake_on_connect=False)
        try:
            self.browser.do_handshake()
        except Exception:
            pass

        self.browser.setblocking(0)
        client.setblocking(0)
        self.browser.settimeout(3)
        client.settimeout(3)

        context = ssl._create_default_https_context()
        client = context.wrap_socket(client, server_hostname=req.host)

        return client
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.group_id = None

        self.ssl_context = ssl._create_default_https_context() if self.api.verify else ssl._create_unverified_context()

        self.instance_url = '{}/api/v4'.format(
            self._url_match.group('base_url'))

        # fetch project_id via api, thanks to lewicki-pk
        # https://github.com/oasiswork/redmine-gitlab-migrator/pull/2
        # but also take int account, that there might be the same project in different namespaces
        path_with_namespace = (
            '{namespace}/{project_name}'.format(
                **self._url_match.groupdict()))
        projectId = -1
        groupId = None

        project_info = self.api.get('{}/projects/{}'.format(self.instance_url,urllib.parse.quote_plus(path_with_namespace)))

        projectId = project_info.get('id')
        if project_info.get('namespace').get('kind') == 'group':
            groupId = project_info.get('namespace').get('id')

        self.project_id = projectId
        if projectId == -1 :
            raise ValueError('Could not get project_id for path_with_namespace: {}'.format(path_with_namespace))
        if groupId:
            self.group_id = groupId

        self.api_url = (
            '{base_url}api/v4/projects/'.format(
                **self._url_match.groupdict())) + str(projectId)
        
        self.group_api_url = (
            '{base_url}api/v4/groups/'.format(
                **self._url_match.groupdict())) + str(groupId)
    def __init__(self,
                 host,
                 port=None,
                 service_id=None,
                 check_hostname=None,
                 context=None,
                 **kwargs):
        self._service_id = service_id
        if self._service_id:
            context = ssl._create_default_https_context()
            context.check_hostname = False
            context.verify_mode = ssl.CERT_NONE
            context.load_cert_chain(settings.ssl_cert_path,
                                    settings.ssl_key_path)
            context.load_default_certs()
        http.client.HTTPSConnection.__init__(self,
                                             host,
                                             port,
                                             check_hostname=check_hostname,
                                             context=context,
                                             **kwargs)

        if not is_local(host):
            self._create_connection = create_tor_connection
Exemple #34
0
 def __init__(self, hostname, apikey, name, timeout, verbose, package):
     # Need to set type to correct value for use in API call
     if package == "wildfire2": self.type = "wildfire"
     elif package == "app": self.type = "content"
     elif package == "appthreat": self.type = "content"
     elif package == "antivirus": self.type = "anti-virus"
     else: self.type = package
     self.hostname = hostname
     self.username = None
     self.password = None
     self.name = name
     self.timeout = timeout
     self.apikey = apikey
     self.app_version = None
     self.threat_version = None
     self.av_version = None
     self.wf_version = None
     self.package = package
     self.path = self.PACKAGE[package]
     self.verbose = verbose
     self.cert_verify = False
     self.panxapi = pan.xapi.PanXapi(hostname=hostname, api_key=apikey, timeout=timeout)
     if not self.cert_verify: self.context = ssl._create_unverified_context()
     else: self.context = ssl._create_default_https_context()
Exemple #35
0
def main():
    arg_parser()
    if not args.debug:
        logging.root.setLevel(logging.INFO)
    else:
        logging.root.setLevel(logging.DEBUG)

    if args.timeout:
        socket.setdefaulttimeout(args.timeout)

    if args.insecure:
        ssl._create_default_https_context = ssl._create_unverified_context
        args.certs = ssl.CERT_NONE
    else:
        certs = args.append_certs or []
        try:
            import certifi
        except ImportError:
            pass
        else:
            certs.append(certifi.where())
        if certs:
            context = ssl._create_default_https_context()
            for cert in certs:
                if os.path.isfile(cert):
                    context.load_verify_locations(cert)
                elif os.path.isdir(cert):
                    context.load_verify_locations(capath=cert)
            https_handler = HTTPSHandler(context=context)
            http.add_default_handler(https_handler)
            args.certs = certs
        else:
            args.certs = None

    proxies = None
    if args.proxy == 'system':
        proxies = getproxies()
        args.proxy = proxies.get('http') or proxies.get('https', 'none')
    args.proxy = args.proxy.lower()
    if not args.proxy.startswith(('http', 'socks', 'none')):
        args.proxy = 'http://' + args.proxy

    if args.proxy == 'none':
        proxies = {}
    elif args.proxy.startswith(('http', 'socks')):
        if args.proxy.startswith(('https', 'socks')):
            try:
                import extproxy
            except ImportError:
                logger.error('Please install ExtProxy to use proxy: ' + args.proxy)
                raise
        proxies = {
            'http': args.proxy,
            'https': args.proxy
        }
    proxy_handler = ProxyHandler(proxies)

    http.add_default_handler(proxy_handler)

    if args.no_http_cache:
        http.CACHED.set(0)

    #mkdir and cd to output dir
    if not args.output_dir == '.':
        try:
            if not os.path.exists(args.output_dir):
                os.makedirs(args.output_dir)
        except:
            logger.warning('No permission or Not found ' + args.output_dir)
            logger.warning('use current folder')
            args.output_dir = '.'
    if os.path.exists(args.output_dir):
        os.chdir(args.output_dir)

    exit = 0
    try:
        for url in args.video_urls:
            http.reset_headers()
            http.uninstall_cookie()
            try:
                m, u = url_to_module(url)
                if args.playlist:
                    parser = m.parser_list
                    m.start = args.start
                else:
                    parser = m.parser
                info = parser(u)
                if isinstance(info, (GeneratorType, list)):
                    for i in info:
                        handle_videoinfo(i)
                else:
                    handle_videoinfo(info)
            except AssertionError as e:
                logger.critical(str(e))
                exit = 1
            except (RuntimeError, NotImplementedError, SyntaxError) as e:
                logger.error(repr(e))
                exit = 1
    except KeyboardInterrupt:
        logger.info('Interrupted by Ctrl-C')
    except Exception as e:
        errmsg = str(e)
        logger.debug(errmsg, exc_info=True)
        if 'local issuer' in errmsg:
            logger.warning('Please install or update Certifi, and try again:\n'
                           'pip3 install certifi --upgrade')
        exit = 255
    sys.exit(exit)
import socket, ssl, pprint
import time

crtf = "cert.pem"
keyf = "cert.key"
cacrtf = "cert.pem"

if 1:
    socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # ssl_socket = ssl.wrap_socket(socket, cert_reqs=ssl.CERT_REQUIRED, ca_certs=None)
    ssl_socket = ssl.wrap_socket(socket, cert_reqs=ssl.CERT_NONE, ca_certs=None)
    ssl_socket.connect(('127.0.0.1', 4430))
    print(repr(ssl_socket.getpeername()))
    print(ssl_socket.cipher())
    print(pprint.pformat(ssl_socket.getpeercert()))
    ssl_socket.write("Time: %s\r\n" % time.time())
    data = ssl_socket.read()
    print(data)
    ssl_socket.close()
else:
    context = ssl._create_default_https_context()
    sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock = context.wrap_socket(sc, server_hostname='www.baidu.com')
Exemple #37
0
    def __init__(self,
                 courses: [Course],
                 moodle_service: MoodleService,
                 storage_path: str,
                 skip_cert_verify: bool = False):
        """
        Initiates the DownloadService with all files that
        need to be downloaded. A URLTarget is created for each file.
        @param courses: A list of courses that contains all modified files.
        @param moodle_service: A reference to the moodle_service, currently
                               only to get to the state_recorder and the token.
        @param storage_path: The location where the files will be saved.
        """

        # How much threads should be created
        DownloadService.thread_count = 5
        # How often should the downloader try to download
        # a file again if an error occurs.
        DownloadService.url_tries = 3

        self.courses = courses
        self.state_recorder = moodle_service.recorder
        self.token = moodle_service.config_helper.get_token()
        self.storage_path = storage_path

        # The wait queue for all URL targets to be downloaded.
        self.queue = Queue(0)
        # A list of the created threads
        self.threads = []
        # A lock to stabilize thread insecure resources.
        # writing in DB
        self.lock = threading.Lock()
        # reading file system
        self.lock2 = threading.Lock()

        # report is used to collect successful and failed downloads
        self.report = {'success': [], 'failure': []}
        # thread_report is used to get live reports from the threads
        self.thread_report = [{
            'total': 0,
            'percentage': 0
        } for i in range(self.thread_count)]
        # Collects the total size of the files that needs to be downloaded.
        self.total_to_download = 0

        # delete files, that should be deleted
        self.state_recorder.batch_delete_files(self.courses)

        if skip_cert_verify:
            self.ssl_context = ssl._create_unverified_context()
        else:
            self.ssl_context = ssl._create_default_https_context()

        # Prepopulate queue with any files that were given
        for course in self.courses:
            for file in course.files:
                if (file.deleted is False):
                    self.total_to_download += file.content_filesize

                    save_destination = self.genPath(self.storage_path, course,
                                                    file)

                    self.queue.put(
                        URLTarget(file, course, save_destination, self.token,
                                  self.thread_report, self.lock2,
                                  self.ssl_context))
Exemple #38
0
def _create_https_context(verify=True):
    context = ssl._create_default_https_context()
    if verify is False:
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE
    return context