jack 1 tahun lalu
melakukan
ea7576902e
4 mengubah file dengan 131 tambahan dan 0 penghapusan
  1. 62 0
      .gitignore
  2. 11 0
      Dockerfile
  3. 10 0
      docker-compose.yaml
  4. 48 0
      main.py

+ 62 - 0
.gitignore

@@ -0,0 +1,62 @@
+.DS_Store
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+env/
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+*.egg-info/
+.installed.cfg
+*.egg
+.idea/*
+xml_files/
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*,cover
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+

+ 11 - 0
Dockerfile

@@ -0,0 +1,11 @@
+FROM python:3.8
+
+WORKDIR /app
+
+COPY main.py /app
+
+RUN pip install --no-cache-dir fastapi uvicorn python-multipart
+
+EXPOSE 29981
+
+CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "29981"]

+ 10 - 0
docker-compose.yaml

@@ -0,0 +1,10 @@
+version: '3.8'
+
+services:
+  send_gotify:
+    build: .
+    image: checkchan_middleware
+    container_name: checkchan_middleware
+    ports:
+      - "29981:29981"
+    restart: always

+ 48 - 0
main.py

@@ -0,0 +1,48 @@
+from fastapi import FastAPI, HTTPException
+import subprocess
+
+app = FastAPI()
+
+# Gotify 服务器的 URL
+GOTIFY_URL = "https://gotify.erhe.top"
+
+@app.get("/webhook")
+async def receive_checkchan_webhook(key: str, title: str, message: str):
+    if not key:
+        raise HTTPException(status_code=400, detail="Missing required parameter 'key'")
+    if key != 'A_SgWPLrgFAXm0d':
+        raise HTTPException(status_code=401, detail="Invalid API key")
+    
+    try:
+        # 构建命令行参数
+        command = [
+            'curl',
+            '-k',  # 允许不安全的 HTTPS 请求
+            f'{GOTIFY_URL}/message?token={key}', 
+            '-F', f'title={title}',
+            '-F', f'message={message}',
+            '-F', 'priority=5'
+        ]
+
+        # 执行命令
+        result = subprocess.run(command, check=True, text=True, capture_output=True)
+        print('命令执行成功,输出:')
+        print(result.stdout)
+        
+        if result.returncode == 0:
+            return {"status": "success", "message": "Message sent to Gotify"}
+        else:
+            raise HTTPException(
+                status_code=500, detail="Failed to send message to Gotify")
+    except subprocess.CalledProcessError as e:
+        print('命令执行失败:')
+        print(e.stderr)
+        raise HTTPException(
+            status_code=500, detail="Failed to send message to Gotify")
+    except Exception as e:
+        raise HTTPException(status_code=500, detail=str(e))
+
+# 运行 Uvicorn 服务器
+if __name__ == "__main__":
+    import uvicorn
+    uvicorn.run(app, host="0.0.0.0", port=29981)