12
12
import time
13
13
import json
14
14
import re
15
+ from string import Template
15
16
from BaseHTTPServer import HTTPServer , BaseHTTPRequestHandler
16
17
from SocketServer import ThreadingMixIn
17
18
from cStringIO import StringIO
@@ -44,6 +45,7 @@ class ProxyRequestHandler(BaseHTTPRequestHandler):
44
45
cacert = join_with_script_dir ('ca.crt' )
45
46
certkey = join_with_script_dir ('cert.key' )
46
47
certdir = join_with_script_dir ('certs/' )
48
+ conf_template = Template ("subjectAltName=DNS:${hostname}" )
47
49
timeout = 5
48
50
lock = threading .Lock ()
49
51
@@ -69,13 +71,17 @@ def do_CONNECT(self):
69
71
def connect_intercept (self ):
70
72
hostname = self .path .split (':' )[0 ]
71
73
certpath = "%s/%s.crt" % (self .certdir .rstrip ('/' ), hostname )
74
+ confpath = "%s/%s.cnf" % (self .certdir .rstrip ('/' ), hostname )
72
75
73
76
with self .lock :
74
77
if not os .path .isfile (certpath ):
78
+ with open (confpath , 'w' ) as fp :
79
+ fp .write (self .conf_template .substitute (hostname = hostname ))
75
80
epoch = "%d" % (time .time () * 1000 )
76
81
p1 = Popen (["openssl" , "req" , "-new" , "-key" , self .certkey , "-subj" , "/CN=%s" % hostname ], stdout = PIPE )
77
- p2 = Popen (["openssl" , "x509" , "-req" , "-days" , "3650" , "-CA" , self .cacert , "-CAkey" , self .cakey , "-set_serial" , epoch , "-out" , certpath ], stdin = p1 .stdout , stderr = PIPE )
82
+ p2 = Popen (["openssl" , "x509" , "-req" , "-extfile" , confpath , "- days" , "3650" , "-CA" , self .cacert , "-CAkey" , self .cakey , "-set_serial" , epoch , "-out" , certpath ], stdin = p1 .stdout , stderr = PIPE )
78
83
p2 .communicate ()
84
+ os .unlink (confpath )
79
85
80
86
self .wfile .write ("%s %d %s\r \n " % (self .protocol_version , 200 , 'Connection Established' ))
81
87
self .end_headers ()
0 commit comments