-
Notifications
You must be signed in to change notification settings - Fork 13
/
webvpn.py
79 lines (54 loc) · 2.57 KB
/
webvpn.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from argparse import ArgumentParser
from binascii import hexlify, unhexlify
from Crypto.Cipher import AES
def getCiphertext(plaintext, key, cfb_iv, size = 128):
'''From plantext hostname to ciphertext'''
message = plaintext.encode('utf-8')
cfb_cipher_encrypt = AES.new(key, AES.MODE_CFB, cfb_iv, segment_size = size) # Must include segment_size
mid = cfb_cipher_encrypt.encrypt(message)
return hexlify(mid).decode()
def getPlaintext(ciphertext, key, cfb_iv, size = 128):
'''From ciphertext hostname to plaintext'''
message = unhexlify(ciphertext.encode('utf-8'))
cfb_cipher_decrypt = AES.new(key, AES.MODE_CFB, cfb_iv, segment_size = size)
cfb_msg_decrypt = cfb_cipher_decrypt.decrypt(message).decode('utf-8')
return cfb_msg_decrypt
def getVPNUrl(url):
'''From ordinary url to webVPN url'''
parts = url.split('://')
pro = parts[0]
add = parts[1]
hosts = add.split('/')
domain = hosts[0].split(':')[0]
port = '-' + hosts[0].split(':')[1] if ":" in hosts[0] else ''
cph = getCiphertext(domain, key=key_, cfb_iv=iv_)
fold = '/'.join(hosts[1:])
key = hexlify(iv_).decode('utf-8')
return 'https://' + institution + '/' + pro + port + '/' + key + cph + '/' + fold
def getOrdinaryUrl(url):
'''From webVPN url to ordinary url'''
parts = url.split('/')
pro = parts[3]
key_cph = parts[4]
hostname = getPlaintext(key_cph[32:], key=key_, cfb_iv=iv_)
fold = '/'.join(parts[5:])
return pro + "://" + hostname + '/' + fold
if __name__ == '__main__':
parser = ArgumentParser(description="convert between webVPN url and ordinary url")
parser.add_argument("url", help="url to be converted")
codec = parser.add_mutually_exclusive_group(required=True)
codec.add_argument("-e", "--encode", action="store_true", help="convert ordinary url to webVPN url")
codec.add_argument("-d", "--decode", action="store_true", help="convert webVPN url to ordinary url")
parser.add_argument("--key", default="Wxzxvpn2023key@$", help="crypto key")
parser.add_argument("--iv", default="Wxzxvpn2023key@$", help="crypto iv")
parser.add_argument("-i", "--institution", default="webvpn.dlut.edu.cn", help="hostname of institution (only work at encode mode)")
args = parser.parse_args()
url = args.url
key_ = args.key.encode('utf-8')
iv_ = args.iv.encode('utf-8')
if args.encode:
institution = args.institution
print(getVPNUrl(url))
elif args.decode:
institution = url.split('/')[2]
print(getOrdinaryUrl(url))