Spaces:
Sleeping
Sleeping
Igor commited on
Feat: Add FastAPI Microservice and Dockerfile
Browse files- Dockerfile +14 -0
- README.md +32 -0
- requirements.txt +2 -0
- server.py +163 -0
- stealth.py +2 -2
Dockerfile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
# Expose port 8000
|
| 11 |
+
EXPOSE 8000
|
| 12 |
+
|
| 13 |
+
# Run the server
|
| 14 |
+
CMD ["python", "server.py"]
|
README.md
CHANGED
|
@@ -91,6 +91,38 @@ When entering a new community, analyze the top 1000 most common words in that fo
|
|
| 91 |
|
| 92 |
*Note: If you use Stealth Mode, ensure your new words do not collide with the template filler words in `stealth.py`.*
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
## Security Note
|
| 95 |
* **Encryption:** Uses `cryptography` library (AES-GCM, X25519, HKDF). Mathematically secure.
|
| 96 |
* **Steganography:** The hiding mechanism relies on a 256-word dictionary. While "Stealth Mode" mimics grammar, sophisticated statistical analysis *could* potentially detect the anomaly (unusual word frequency). Use with awareness.
|
|
|
|
| 91 |
|
| 92 |
*Note: If you use Stealth Mode, ensure your new words do not collide with the template filler words in `stealth.py`.*
|
| 93 |
|
| 94 |
+
## Docker / HTTP API
|
| 95 |
+
|
| 96 |
+
For bots and agents, you can run `shitposter` as a local microservice.
|
| 97 |
+
|
| 98 |
+
### 1. Build & Run
|
| 99 |
+
```bash
|
| 100 |
+
docker build -t shitposter .
|
| 101 |
+
docker run -p 8000:8000 shitposter
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
### 2. API Endpoints
|
| 105 |
+
The service runs at `http://localhost:8000`. Documentation (Swagger UI) is available at `/docs`.
|
| 106 |
+
|
| 107 |
+
* **`GET /keygen`**: Returns `{ "private_key_b64": "...", "public_signal": "..." }`
|
| 108 |
+
* **`POST /encrypt`**:
|
| 109 |
+
```json
|
| 110 |
+
{
|
| 111 |
+
"message": "Hello World",
|
| 112 |
+
"sender_private_key_b64": "...",
|
| 113 |
+
"recipient_public_signal": "...",
|
| 114 |
+
"stealth": true
|
| 115 |
+
}
|
| 116 |
+
```
|
| 117 |
+
* **`POST /decrypt`**:
|
| 118 |
+
```json
|
| 119 |
+
{
|
| 120 |
+
"ciphertext_shitpost": "...",
|
| 121 |
+
"recipient_private_key_b64": "...",
|
| 122 |
+
"sender_public_signal": "..."
|
| 123 |
+
}
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
## Security Note
|
| 127 |
* **Encryption:** Uses `cryptography` library (AES-GCM, X25519, HKDF). Mathematically secure.
|
| 128 |
* **Steganography:** The hiding mechanism relies on a 256-word dictionary. While "Stealth Mode" mimics grammar, sophisticated statistical analysis *could* potentially detect the anomaly (unusual word frequency). Use with awareness.
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
cryptography
|
| 2 |
click
|
|
|
|
|
|
|
|
|
| 1 |
cryptography
|
| 2 |
click
|
| 3 |
+
fastapi
|
| 4 |
+
uvicorn
|
server.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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itposterCrypto
|
| 8 |
+
from steg import encode_bytes, decode_string, WORDLIST
|
| 9 |
+
from stealth import generate_stealth_text
|
| 10 |
+
|
| 11 |
+
app = FastAPI(
|
| 12 |
+
title="Shitposter Cipher API",
|
| 13 |
+
description="Microservice for AI Agent Secure Communication",
|
| 14 |
+
version="1.0.0"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
CRYPTO = ShitposterCrypto()
|
| 18 |
+
|
| 19 |
+
class KeyGenResponse(BaseModel):
|
| 20 |
+
private_key_b64: str
|
| 21 |
+
public_signal: str
|
| 22 |
+
|
| 23 |
+
class EncryptRequest(BaseModel):
|
| 24 |
+
message: str
|
| 25 |
+
sender_private_key_b64: str
|
| 26 |
+
recipient_public_signal: str
|
| 27 |
+
stealth: bool = True
|
| 28 |
+
|
| 29 |
+
class DecryptRequest(BaseModel):
|
| 30 |
+
ciphertext_shitpost: str
|
| 31 |
+
recipient_private_key_b64: str
|
| 32 |
+
sender_public_signal: str
|
| 33 |
+
|
| 34 |
+
class ScanRequest(BaseModel):
|
| 35 |
+
text: str
|
| 36 |
+
try_shared_key_b64: Optional[str] = None
|
| 37 |
+
|
| 38 |
+
@app.get("/keygen", response_model=KeyGenResponse)
|
| 39 |
+
def generate_identity():
|
| 40 |
+
"""Generates a new Identity (Private Key + Public Signal)."""
|
| 41 |
+
priv, pub_bytes = CRYPTO.generate_keypair()
|
| 42 |
+
priv_bytes = CRYPTO.get_private_bytes(priv)
|
| 43 |
+
|
| 44 |
+
return {
|
| 45 |
+
"private_key_b64": base64.b64encode(priv_bytes).decode('utf-8'),
|
| 46 |
+
"public_signal": encode_bytes(pub_bytes)
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
@app.post("/encrypt")
|
| 50 |
+
def encrypt_message(req: EncryptRequest):
|
| 51 |
+
"""Encrypts a message using Ephemeral ECDH (Sender Priv + Recipient Pub)."""
|
| 52 |
+
try:
|
| 53 |
+
# Decode inputs
|
| 54 |
+
priv_bytes = base64.b64decode(req.sender_private_key_b64)
|
| 55 |
+
sender_priv = CRYPTO.load_private_key(priv_bytes)
|
| 56 |
+
|
| 57 |
+
recipient_pub_bytes = decode_string(req.recipient_public_signal)
|
| 58 |
+
|
| 59 |
+
if len(recipient_pub_bytes) != 32:
|
| 60 |
+
raise HTTPException(status_code=400, detail="Invalid Public Signal (must be 32 bytes)")
|
| 61 |
+
|
| 62 |
+
# Derive Secret
|
| 63 |
+
shared_key = CRYPTO.derive_shared_secret(sender_priv, recipient_pub_bytes)
|
| 64 |
+
|
| 65 |
+
# Encrypt
|
| 66 |
+
encrypted_payload = CRYPTO.encrypt_message(shared_key, req.message)
|
| 67 |
+
|
| 68 |
+
# Steganography
|
| 69 |
+
raw_words = [WORDLIST[b] for b in encrypted_payload]
|
| 70 |
+
|
| 71 |
+
if req.stealth:
|
| 72 |
+
output = generate_stealth_text(raw_words)
|
| 73 |
+
else:
|
| 74 |
+
output = " ".join(raw_words)
|
| 75 |
+
|
| 76 |
+
return {"shitpost": output}
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 80 |
+
|
| 81 |
+
@app.post("/decrypt")
|
| 82 |
+
def decrypt_message(req: DecryptRequest):
|
| 83 |
+
"""Decrypts a shitpost using Ephemeral ECDH (Recipient Priv + Sender Pub)."""
|
| 84 |
+
try:
|
| 85 |
+
# Decode inputs
|
| 86 |
+
priv_bytes = base64.b64decode(req.recipient_private_key_b64)
|
| 87 |
+
recipient_priv = CRYPTO.load_private_key(priv_bytes)
|
| 88 |
+
|
| 89 |
+
sender_pub_bytes = decode_string(req.sender_public_signal)
|
| 90 |
+
|
| 91 |
+
# Derive Secret
|
| 92 |
+
shared_key = CRYPTO.derive_shared_secret(recipient_priv, sender_pub_bytes)
|
| 93 |
+
|
| 94 |
+
# Parse Shitpost
|
| 95 |
+
# Note: decode_string handles the stealth/punctuation stripping now
|
| 96 |
+
payload = decode_string(req.ciphertext_shitpost)
|
| 97 |
+
|
| 98 |
+
# Decrypt
|
| 99 |
+
plaintext = CRYPTO.decrypt_message(shared_key, payload)
|
| 100 |
+
|
| 101 |
+
return {"message": plaintext}
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
raise HTTPException(status_code=400, detail=f"Decryption failed: {str(e)}")
|
| 105 |
+
|
| 106 |
+
@app.post("/scan")
|
| 107 |
+
def scan_text(req: ScanRequest):
|
| 108 |
+
"""Scans text for public keys or potential messages."""
|
| 109 |
+
import re
|
| 110 |
+
cleaned = re.sub(r'[^a-zA-Z\s]', ' ', req.text)
|
| 111 |
+
words = cleaned.lower().split()
|
| 112 |
+
word_set = set(WORDLIST)
|
| 113 |
+
|
| 114 |
+
candidates = []
|
| 115 |
+
current_sequence = []
|
| 116 |
+
|
| 117 |
+
# Simple extraction of valid word runs
|
| 118 |
+
for w in words:
|
| 119 |
+
if w in word_set:
|
| 120 |
+
current_sequence.append(w)
|
| 121 |
+
else:
|
| 122 |
+
if current_sequence:
|
| 123 |
+
candidates.append(current_sequence)
|
| 124 |
+
current_sequence = []
|
| 125 |
+
if current_sequence:
|
| 126 |
+
candidates.append(current_sequence)
|
| 127 |
+
|
| 128 |
+
results = []
|
| 129 |
+
|
| 130 |
+
shared_key = None
|
| 131 |
+
if req.try_shared_key_b64:
|
| 132 |
+
shared_key = base64.b64decode(req.try_shared_key_b64)
|
| 133 |
+
|
| 134 |
+
for seq in candidates:
|
| 135 |
+
if len(seq) < 12: continue
|
| 136 |
+
|
| 137 |
+
seq_str = " ".join(seq)
|
| 138 |
+
item = {
|
| 139 |
+
"type": "unknown",
|
| 140 |
+
"length": len(seq),
|
| 141 |
+
"signal": seq_str
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
if len(seq) == 32:
|
| 145 |
+
item["type"] = "public_key"
|
| 146 |
+
elif len(seq) > 16:
|
| 147 |
+
item["type"] = "message"
|
| 148 |
+
# Try decrypt if key provided
|
| 149 |
+
if shared_key:
|
| 150 |
+
try:
|
| 151 |
+
payload = decode_string(seq_str)
|
| 152 |
+
pt = CRYPTO.decrypt_message(shared_key, payload)
|
| 153 |
+
item["decrypted"] = pt
|
| 154 |
+
except:
|
| 155 |
+
pass
|
| 156 |
+
|
| 157 |
+
results.append(item)
|
| 158 |
+
|
| 159 |
+
return {"found": results}
|
| 160 |
+
|
| 161 |
+
if __name__ == "__main__":
|
| 162 |
+
import uvicorn
|
| 163 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
stealth.py
CHANGED
|
@@ -9,7 +9,7 @@ TEMPLATES = [
|
|
| 9 |
"Honestly, I think the {} is causing the lag.",
|
| 10 |
"Did you check if the {} configures the {} correctly?",
|
| 11 |
"The documentation for {} says it depends on {} but that seems wrong.",
|
| 12 |
-
"My
|
| 13 |
"Why does the {} always break when I touch the {}?",
|
| 14 |
"It is basically a {} issue, not an equipment problem.",
|
| 15 |
"I rewrote the {} to improve the {} usage.",
|
|
@@ -20,7 +20,7 @@ TEMPLATES = [
|
|
| 20 |
"I suspect the {} is conflicting with the {}.",
|
| 21 |
"Is there a reason the {} is not compatible with {}?",
|
| 22 |
"The {} implementation in this project is terrible.",
|
| 23 |
-
"Make sure to
|
| 24 |
]
|
| 25 |
|
| 26 |
def generate_stealth_text(payload_words):
|
|
|
|
| 9 |
"Honestly, I think the {} is causing the lag.",
|
| 10 |
"Did you check if the {} configures the {} correctly?",
|
| 11 |
"The documentation for {} says it depends on {} but that seems wrong.",
|
| 12 |
+
"My output is showing a weird fault in the {} tier.",
|
| 13 |
"Why does the {} always break when I touch the {}?",
|
| 14 |
"It is basically a {} issue, not an equipment problem.",
|
| 15 |
"I rewrote the {} to improve the {} usage.",
|
|
|
|
| 20 |
"I suspect the {} is conflicting with the {}.",
|
| 21 |
"Is there a reason the {} is not compatible with {}?",
|
| 22 |
"The {} implementation in this project is terrible.",
|
| 23 |
+
"Make sure to enable the {} toggle before running the {}.",
|
| 24 |
]
|
| 25 |
|
| 26 |
def generate_stealth_text(payload_words):
|