File: getcert.py

package info (click to toggle)
trojan 1.16.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 472 kB
  • sloc: cpp: 2,815; sh: 102; python: 28; makefile: 11
file content (50 lines) | stat: -rwxr-xr-x 1,825 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3

# This file is part of the trojan project.
# Trojan is an unidentifiable mechanism that helps you bypass GFW.
# Copyright (C) 2017-2020  The Trojan Authors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import socket
import ssl
import sys

def input_with_default(prompt, default):
    print('{} [{}]: '.format(prompt, default), file=sys.stderr, end='')
    line = input()
    return line if line else default

def main(argc, argv):
    if argc == 1:
        hostname = input_with_default('Enter hostname', 'example.com')
        port = int(input_with_default('Enter port number', '443'))
    elif argc == 2:
        hostname = argv[1]
        port = 443
    elif argc == 3:
        hostname = argv[1]
        port = int(argv[2])
    else:
        print('usage: {} [hostname] [port]'.format(argv[0]), file=sys.stderr)
        exit(1)
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    with socket.create_connection((hostname, port)) as sock:
        with ctx.wrap_socket(sock, server_hostname=hostname) as ssock:
            print(ssl.DER_cert_to_PEM_cert(ssock.getpeercert(True)), end='')

if __name__ == '__main__':
    main(len(sys.argv), sys.argv)