.gitignore 수정, 텔레그램 봇 명령어 수정, 지수 조회 오류 수정
This commit is contained in:
@@ -273,43 +273,68 @@ class TelegramBotServer:
|
||||
|
||||
async def exec_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""/exec: 원격 명령어 실행"""
|
||||
if not context.args:
|
||||
# ultimate_handler에서는 context.args가 비어있으므로 직접 파싱
|
||||
text = update.message.text.strip()
|
||||
parts = text.split(maxsplit=1) # "/exec" 와 나머지 명령어로 분리
|
||||
|
||||
if len(parts) < 2:
|
||||
await update.message.reply_text("❌ 사용법: /exec <command>")
|
||||
return
|
||||
|
||||
command = " ".join(context.args)
|
||||
command = parts[1] # "/exec" 이후의 모든 텍스트
|
||||
await update.message.reply_text(f"⚙️ 실행 중: `{command}`", parse_mode="Markdown")
|
||||
|
||||
try:
|
||||
# 보안: 위험한 명령어 차단
|
||||
dangerous_keywords = ['rm', 'del', 'format', 'shutdown', 'reboot']
|
||||
dangerous_keywords = ['rm', 'del', 'format', 'shutdown', 'reboot', 'ipconfig']
|
||||
if any(keyword in command.lower() for keyword in dangerous_keywords):
|
||||
await update.message.reply_text("⛔ 위험한 명령어는 실행할 수 없습니다.")
|
||||
return
|
||||
|
||||
# Windows에서는 PowerShell을 명시적으로 사용
|
||||
import platform
|
||||
if platform.system() == 'Windows':
|
||||
exec_command = ['powershell', '-Command', command]
|
||||
else:
|
||||
exec_command = command
|
||||
|
||||
# 명령어 실행 (타임아웃 30초)
|
||||
result = subprocess.run(
|
||||
command,
|
||||
shell=True,
|
||||
exec_command,
|
||||
shell=False if platform.system() == 'Windows' else True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding='utf-8',
|
||||
errors='replace', # 인코딩 오류 무시
|
||||
timeout=30,
|
||||
cwd=os.getcwd()
|
||||
)
|
||||
|
||||
output = result.stdout if result.stdout else result.stderr
|
||||
if not output:
|
||||
output = "명령어 실행 완료 (출력 없음)"
|
||||
# stdout와 stderr 모두 확인
|
||||
output = result.stdout.strip() if result.stdout else ""
|
||||
error_output = result.stderr.strip() if result.stderr else ""
|
||||
|
||||
if output and error_output:
|
||||
combined = f"[STDOUT]\n{output}\n\n[STDERR]\n{error_output}"
|
||||
elif output:
|
||||
combined = output
|
||||
elif error_output:
|
||||
combined = f"[ERROR]\n{error_output}"
|
||||
else:
|
||||
combined = "명령어 실행 완료 (출력 없음)"
|
||||
|
||||
# 출력이 너무 길면 잘라내기
|
||||
if len(output) > 3000:
|
||||
output = output[:3000] + "\n... (출력이 너무 깁니다)"
|
||||
if len(combined) > 3000:
|
||||
combined = combined[:3000] + "\n... (출력이 너무 깁니다)"
|
||||
|
||||
await update.message.reply_text(f"```\n{output}\n```", parse_mode="Markdown")
|
||||
await update.message.reply_text(f"```\n{combined}\n```", parse_mode="Markdown")
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
await update.message.reply_text("⏱️ 명령어 실행 시간 초과 (30초)")
|
||||
except Exception as e:
|
||||
print(f"❌ [Telegram /exec] Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
await update.message.reply_text(f"❌ 실행 오류: {str(e)}")
|
||||
|
||||
def run(self):
|
||||
|
||||
Reference in New Issue
Block a user