43 lines
2.1 KiB
Python
43 lines
2.1 KiB
Python
import json, os, stat, subprocess, tempfile, time
|
|
base='/www/backup/txiaw-mysql-fix-20260831'
|
|
directory='/www/server/panel/vhost/nginx'
|
|
site=directory+'/www.txiaw.com.conf'
|
|
nginx='/www/server/nginx/sbin/nginx'
|
|
stamp=time.strftime('%Y%m%d-%H%M%S')
|
|
backup=base+'/before-abuse-connection-close-'+stamp
|
|
os.mkdir(backup,0o700)
|
|
sources=PAYLOAD_FILES
|
|
old={p:open(p,'rb').read() for p in [site]+[directory+'/'+x for x in sources]}
|
|
for p,data in old.items():
|
|
with open(backup+'/'+os.path.basename(p),'wb') as f:f.write(data)
|
|
os.chmod(backup+'/'+os.path.basename(p),0o600)
|
|
def put(p,data):
|
|
st=os.stat(p);fd,tmp=tempfile.mkstemp(prefix='.txiaw-',dir=directory)
|
|
try:
|
|
with os.fdopen(fd,'wb') as f:f.write(data);f.flush();os.fsync(f.fileno())
|
|
os.chmod(tmp,stat.S_IMODE(st.st_mode));os.chown(tmp,st.st_uid,st.st_gid);os.replace(tmp,p)
|
|
finally:
|
|
if os.path.exists(tmp):os.unlink(tmp)
|
|
def run(args):
|
|
return subprocess.check_output(args,stderr=subprocess.STDOUT,timeout=15).decode('utf-8','replace')
|
|
configuration=old[site].decode('utf-8')
|
|
http_include='include '+directory+'/txiaw-home-protection-http.inc;'
|
|
server_include=' include '+directory+'/txiaw-home-protection-server.inc;'
|
|
if http_include not in configuration:configuration=http_include+'\n'+configuration
|
|
if server_include.strip() not in configuration:
|
|
marker=' server_name www.txiaw.com txiaw.com;'
|
|
if configuration.count(marker)!=1:raise RuntimeError('Unexpected server_name configuration; no files changed')
|
|
configuration=configuration.replace(marker,marker+'\n'+server_include,1)
|
|
try:
|
|
for name,data in sources.items():put(directory+'/'+name,data.encode('utf-8'))
|
|
put(site,configuration.encode('utf-8'))
|
|
tested=run([nginx,'-t'])
|
|
run([nginx,'-s','reload'])
|
|
except Exception:
|
|
for p,data in old.items():put(p,data)
|
|
run([nginx,'-t'])
|
|
raise
|
|
r={'status':'confirmed_abuse_closed_without_http_response','request_rate_limits_enabled':False,'backup':backup,'nginx_test_ok':'successful' in tested}
|
|
json.dump(r,open(base+'/abuse-connection-close.json','w'),indent=2)
|
|
print(json.dumps(r),flush=True)
|