Skip to content

Commit

Permalink
Add check_hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
URenko committed Sep 23, 2018
1 parent 44557f1 commit cdcf40d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## 一键使用
### Windows
[点此下载](https://github.com/URenko/Accesser/releases/download/v0.4.7/Windows_x64.zip),解压后运行`start.bat`即可,首次运行可能会申请管理员权限
[点此进入下载页](https://github.com/URenko/Accesser/releases/latest),下载Windows_x64.zip,解压后运行`start.bat`即可,首次运行可能会申请管理员权限

## 依赖
- Python3.7 (其他版本未测试)
Expand All @@ -19,7 +19,10 @@
- 增加支持的域名:
`domains.txt`中添加新行再加入域名,重新启动程序

## 当前自动导入证书支持
- [x] Windows
- [ ] Mac OS
- [ ] Linux
## 当前支持
| |Windows|Mac OS|Linux|
|-------------------|-------|------|-----|
|基础支持 ||||
|自动导入证书至系统 || | |
|自动导入证书至Firefox| | | |
|自动更新HOSTS || | |
43 changes: 25 additions & 18 deletions accesser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def handle(self):
context = ssl.create_default_context()
context.check_hostname = False
self.remote_sock = context.wrap_socket(self.remote_sock)
cert = self.remote_sock.getpeercert()
if check_hostname:
ssl.match_hostname(cert, self.host)
self.remote_sock.sendall(self.raw_request)
self.forward(self.request, self.remote_sock)

Expand All @@ -140,27 +143,31 @@ def handle(self):

config = configparser.ConfigParser()
config.read('setting.ini')
check_hostname = int(config['setting']['check_hostname'])
now_dn_st_mtime = os.stat('domains.txt').st_mtime
domainsupdate = False
if float(config['internal']['dn_st_mtime']) < now_dn_st_mtime:
domainsupdate = True
if not win32elevate.areAdminRightsElevated():
win32elevate.elevateAdminRun(' '.join(sys.argv), reattachConsole=False)
sys.exit(0)
logging.info('Updating HOSTS...')
with open(r"C:\Windows\System32\drivers\etc\hosts") as f:
host_content = f.read()
with open('domains.txt') as f:
for domain in f:
if not re.search(r'(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(?P<oth>\s+'+domain.strip()+')', host_content):
host_content += '\n127.0.0.1\t'+domain.strip()
host_content = re.sub(r'(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(?P<oth>\s+'+domain.strip()+')',r'127.0.0.1\g<oth>',host_content)
with open(r"C:\Windows\System32\drivers\etc\hosts", 'w') as f:
f.write(host_content)
config['internal']['dn_st_mtime'] = str(now_dn_st_mtime)
with open('setting.ini', 'w') as f:
config.write(f)
logging.info('Updating fin')
if sys.platform.startswith('win'):
domainsupdate = True
if not win32elevate.areAdminRightsElevated():
win32elevate.elevateAdminRun(' '.join(sys.argv), reattachConsole=False)
sys.exit(0)
logging.info('Updating HOSTS...')
with open(r"C:\Windows\System32\drivers\etc\hosts") as f:
host_content = f.read()
with open('domains.txt') as f:
for domain in f:
if not re.search(r'(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(?P<oth>\s+'+domain.strip()+')', host_content):
host_content += '\n127.0.0.1\t'+domain.strip()
host_content = re.sub(r'(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(?P<oth>\s+'+domain.strip()+')',r'127.0.0.1\g<oth>',host_content)
with open(r"C:\Windows\System32\drivers\etc\hosts", 'w') as f:
f.write(host_content)
config['internal']['dn_st_mtime'] = str(now_dn_st_mtime)
with open('setting.ini', 'w') as f:
config.write(f)
logging.info('Updating fin')
else:
logging.warning('other platform support is under development, please update HOSTS manually and then use -r to update server cert.')

if not os.path.exists('CERT'):
os.mkdir('CERT')
Expand Down
3 changes: 2 additions & 1 deletion setting.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[setting]
check_hostname = 1
[internal]
dn_st_mtime = 0

0 comments on commit cdcf40d

Please sign in to comment.