tweek 0.1.0__py3-none-any.whl → 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tweek/__init__.py +2 -2
- tweek/_keygen.py +53 -0
- tweek/audit.py +288 -0
- tweek/cli.py +5398 -2392
- tweek/cli_model.py +380 -0
- tweek/config/families.yaml +609 -0
- tweek/config/manager.py +42 -5
- tweek/config/patterns.yaml +1510 -8
- tweek/config/tiers.yaml +161 -11
- tweek/diagnostics.py +71 -2
- tweek/hooks/break_glass.py +163 -0
- tweek/hooks/feedback.py +223 -0
- tweek/hooks/overrides.py +531 -0
- tweek/hooks/post_tool_use.py +472 -0
- tweek/hooks/pre_tool_use.py +1024 -62
- tweek/integrations/openclaw.py +443 -0
- tweek/integrations/openclaw_server.py +385 -0
- tweek/licensing.py +14 -54
- tweek/logging/bundle.py +2 -2
- tweek/logging/security_log.py +56 -13
- tweek/mcp/approval.py +57 -16
- tweek/mcp/proxy.py +18 -0
- tweek/mcp/screening.py +5 -5
- tweek/mcp/server.py +4 -1
- tweek/memory/__init__.py +24 -0
- tweek/memory/queries.py +223 -0
- tweek/memory/safety.py +140 -0
- tweek/memory/schemas.py +80 -0
- tweek/memory/store.py +989 -0
- tweek/platform/__init__.py +4 -4
- tweek/plugins/__init__.py +40 -24
- tweek/plugins/base.py +1 -1
- tweek/plugins/detectors/__init__.py +3 -3
- tweek/plugins/detectors/{moltbot.py → openclaw.py} +30 -27
- tweek/plugins/git_discovery.py +16 -4
- tweek/plugins/git_registry.py +8 -2
- tweek/plugins/git_security.py +21 -9
- tweek/plugins/screening/__init__.py +10 -1
- tweek/plugins/screening/heuristic_scorer.py +477 -0
- tweek/plugins/screening/llm_reviewer.py +14 -6
- tweek/plugins/screening/local_model_reviewer.py +161 -0
- tweek/proxy/__init__.py +38 -37
- tweek/proxy/addon.py +22 -3
- tweek/proxy/interceptor.py +1 -0
- tweek/proxy/server.py +4 -2
- tweek/sandbox/__init__.py +11 -0
- tweek/sandbox/docker_bridge.py +143 -0
- tweek/sandbox/executor.py +9 -6
- tweek/sandbox/layers.py +97 -0
- tweek/sandbox/linux.py +1 -0
- tweek/sandbox/project.py +548 -0
- tweek/sandbox/registry.py +149 -0
- tweek/security/__init__.py +9 -0
- tweek/security/language.py +250 -0
- tweek/security/llm_reviewer.py +1146 -60
- tweek/security/local_model.py +331 -0
- tweek/security/local_reviewer.py +146 -0
- tweek/security/model_registry.py +371 -0
- tweek/security/rate_limiter.py +11 -6
- tweek/security/secret_scanner.py +70 -4
- tweek/security/session_analyzer.py +26 -2
- tweek/skill_template/SKILL.md +200 -0
- tweek/skill_template/__init__.py +0 -0
- tweek/skill_template/cli-reference.md +331 -0
- tweek/skill_template/overrides-reference.md +184 -0
- tweek/skill_template/scripts/__init__.py +0 -0
- tweek/skill_template/scripts/check_installed.py +170 -0
- tweek/skills/__init__.py +38 -0
- tweek/skills/config.py +150 -0
- tweek/skills/fingerprints.py +198 -0
- tweek/skills/guard.py +293 -0
- tweek/skills/isolation.py +469 -0
- tweek/skills/scanner.py +715 -0
- tweek/vault/__init__.py +0 -1
- tweek/vault/cross_platform.py +12 -1
- tweek/vault/keychain.py +87 -29
- tweek-0.2.1.dist-info/METADATA +281 -0
- tweek-0.2.1.dist-info/RECORD +122 -0
- {tweek-0.1.0.dist-info → tweek-0.2.1.dist-info}/entry_points.txt +8 -1
- {tweek-0.1.0.dist-info → tweek-0.2.1.dist-info}/licenses/LICENSE +80 -0
- tweek-0.2.1.dist-info/top_level.txt +2 -0
- tweek-openclaw-plugin/node_modules/flatted/python/flatted.py +149 -0
- tweek/integrations/moltbot.py +0 -243
- tweek-0.1.0.dist-info/METADATA +0 -335
- tweek-0.1.0.dist-info/RECORD +0 -85
- tweek-0.1.0.dist-info/top_level.txt +0 -1
- {tweek-0.1.0.dist-info → tweek-0.2.1.dist-info}/WHEEL +0 -0
|
@@ -2,24 +2,31 @@
|
|
|
2
2
|
tweek = tweek.cli:main
|
|
3
3
|
|
|
4
4
|
[tweek.compliance]
|
|
5
|
+
gdpr = tweek.plugins.compliance:GDPRCompliancePlugin
|
|
5
6
|
gov = tweek.plugins.compliance:GovCompliancePlugin
|
|
6
7
|
hipaa = tweek.plugins.compliance:HIPAACompliancePlugin
|
|
7
8
|
legal = tweek.plugins.compliance:LegalCompliancePlugin
|
|
8
9
|
pci = tweek.plugins.compliance:PCICompliancePlugin
|
|
10
|
+
soc2 = tweek.plugins.compliance:SOC2CompliancePlugin
|
|
9
11
|
|
|
10
12
|
[tweek.llm_providers]
|
|
11
13
|
anthropic = tweek.plugins.providers:AnthropicProvider
|
|
14
|
+
azure_openai = tweek.plugins.providers:AzureOpenAIProvider
|
|
12
15
|
bedrock = tweek.plugins.providers:BedrockProvider
|
|
13
16
|
google = tweek.plugins.providers:GoogleProvider
|
|
14
17
|
openai = tweek.plugins.providers:OpenAIProvider
|
|
15
18
|
|
|
16
19
|
[tweek.screening]
|
|
20
|
+
heuristic_scorer = tweek.plugins.screening:HeuristicScorerPlugin
|
|
17
21
|
llm_reviewer = tweek.plugins.screening:LLMReviewerPlugin
|
|
22
|
+
local_model_reviewer = tweek.plugins.screening:LocalModelReviewerPlugin
|
|
18
23
|
pattern_matcher = tweek.plugins.screening:PatternMatcherPlugin
|
|
19
24
|
rate_limiter = tweek.plugins.screening:RateLimiterPlugin
|
|
20
25
|
session_analyzer = tweek.plugins.screening:SessionAnalyzerPlugin
|
|
21
26
|
|
|
22
27
|
[tweek.tool_detectors]
|
|
23
28
|
continue = tweek.plugins.detectors:ContinueDetector
|
|
29
|
+
copilot = tweek.plugins.detectors:CopilotDetector
|
|
24
30
|
cursor = tweek.plugins.detectors:CursorDetector
|
|
25
|
-
|
|
31
|
+
openclaw = tweek.plugins.detectors:OpenClawDetector
|
|
32
|
+
windsurf = tweek.plugins.detectors:WindsurfDetector
|
|
@@ -188,3 +188,83 @@
|
|
|
188
188
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
189
|
See the License for the specific language governing permissions and
|
|
190
190
|
limitations under the License.
|
|
191
|
+
|
|
192
|
+
====================================================================
|
|
193
|
+
ADDITIONAL NOTICE — BETA SOFTWARE DISCLAIMER
|
|
194
|
+
====================================================================
|
|
195
|
+
|
|
196
|
+
Tweek is currently beta software. The AI security landscape, including
|
|
197
|
+
the threats, attack techniques, and tools that Tweek is designed to
|
|
198
|
+
defend against, is rapidly evolving. New vulnerabilities, bypass
|
|
199
|
+
methods, and attack vectors emerge frequently, and no security tool
|
|
200
|
+
can guarantee complete protection against all threats at all times.
|
|
201
|
+
|
|
202
|
+
BY USING TWEEK, YOU ACKNOWLEDGE AND AGREE THAT:
|
|
203
|
+
|
|
204
|
+
1. BETA STATUS. Tweek is pre-release software that is still under
|
|
205
|
+
active development. It may contain bugs, errors, or defects that
|
|
206
|
+
could result in incomplete or inaccurate threat detection. Features
|
|
207
|
+
may be added, changed, or removed without prior notice.
|
|
208
|
+
|
|
209
|
+
2. NO GUARANTEE OF SECURITY. Tweek is a defense-in-depth tool
|
|
210
|
+
designed to reduce risk, not eliminate it. Tweek, LLC makes no
|
|
211
|
+
representation or warranty that the Software will detect, prevent,
|
|
212
|
+
or mitigate all security threats, prompt injection attacks,
|
|
213
|
+
credential theft attempts, data exfiltration, or other malicious
|
|
214
|
+
activity targeting AI coding assistants or any other software.
|
|
215
|
+
The inclusion of attack patterns, detection rules, or security
|
|
216
|
+
layers does not constitute a guarantee of protection.
|
|
217
|
+
|
|
218
|
+
3. USE AT YOUR OWN RISK. You assume full responsibility and risk
|
|
219
|
+
for your use of Tweek. You are solely responsible for evaluating
|
|
220
|
+
the suitability of the Software for your use case, safeguarding
|
|
221
|
+
your data, credentials, and systems, and implementing additional
|
|
222
|
+
security measures as you deem appropriate. You should not rely
|
|
223
|
+
solely on Tweek for the security of any system, data, or
|
|
224
|
+
credentials.
|
|
225
|
+
|
|
226
|
+
4. HOLD HARMLESS AND INDEMNIFICATION. To the maximum extent
|
|
227
|
+
permitted by applicable law, you agree to indemnify, defend,
|
|
228
|
+
and hold harmless Tweek, LLC, its owners, officers, employees,
|
|
229
|
+
contributors, and agents from and against any and all claims,
|
|
230
|
+
damages, losses, liabilities, costs, and expenses (including
|
|
231
|
+
reasonable attorneys' fees) arising out of or related to your
|
|
232
|
+
use of the Software, including but not limited to any failure
|
|
233
|
+
of the Software to detect or prevent a security threat, any
|
|
234
|
+
data loss, credential exposure, system compromise, or other
|
|
235
|
+
damages resulting from the use or inability to use the Software.
|
|
236
|
+
|
|
237
|
+
5. LIMITATION OF DAMAGES. In no event shall Tweek, LLC be liable
|
|
238
|
+
for any indirect, incidental, special, consequential, or punitive
|
|
239
|
+
damages, or any loss of profits, data, use, goodwill, or other
|
|
240
|
+
intangible losses, arising out of or in connection with your use
|
|
241
|
+
of the Software, even if Tweek, LLC has been advised of the
|
|
242
|
+
possibility of such damages. To the extent permitted by applicable
|
|
243
|
+
law, the total aggregate liability of Tweek, LLC for all claims
|
|
244
|
+
arising out of or related to the Software shall not exceed the
|
|
245
|
+
amount you paid to Tweek, LLC for the Software in the twelve (12)
|
|
246
|
+
months preceding the claim, or fifty U.S. dollars ($50.00),
|
|
247
|
+
whichever is greater.
|
|
248
|
+
|
|
249
|
+
6. NOT A SUBSTITUTE FOR PROFESSIONAL SECURITY. Tweek is not a
|
|
250
|
+
substitute for professional security auditing, penetration
|
|
251
|
+
testing, or comprehensive security programs. Users operating in
|
|
252
|
+
regulated environments (including but not limited to those subject
|
|
253
|
+
to HIPAA, PCI-DSS, SOC 2, or similar compliance frameworks)
|
|
254
|
+
should consult qualified security professionals and should not
|
|
255
|
+
rely on Tweek alone to satisfy compliance requirements.
|
|
256
|
+
|
|
257
|
+
7. RAPIDLY EVOLVING FIELD. The AI security landscape changes
|
|
258
|
+
rapidly. Attack techniques that did not exist at the time of any
|
|
259
|
+
given release may emerge at any time. Tweek, LLC undertakes no
|
|
260
|
+
obligation to update the Software on any particular schedule or
|
|
261
|
+
to address any specific threat, vulnerability, or attack vector.
|
|
262
|
+
|
|
263
|
+
This additional notice supplements and does not replace the
|
|
264
|
+
warranty disclaimer and limitation of liability provisions in
|
|
265
|
+
Sections 7 and 8 of the Apache License, Version 2.0, above. In
|
|
266
|
+
the event of any conflict between this notice and the Apache
|
|
267
|
+
License, the provision that provides greater protection to
|
|
268
|
+
Tweek, LLC shall control.
|
|
269
|
+
|
|
270
|
+
====================================================================
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# ISC License
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2018-2025, Andrea Giammarchi, @WebReflection
|
|
4
|
+
#
|
|
5
|
+
# Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
# purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
# copyright notice and this permission notice appear in all copies.
|
|
8
|
+
#
|
|
9
|
+
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
14
|
+
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
# PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
|
|
17
|
+
import json as _json
|
|
18
|
+
|
|
19
|
+
class _Known:
|
|
20
|
+
def __init__(self):
|
|
21
|
+
self.key = []
|
|
22
|
+
self.value = []
|
|
23
|
+
|
|
24
|
+
class _String:
|
|
25
|
+
def __init__(self, value):
|
|
26
|
+
self.value = value
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _array_keys(value):
|
|
30
|
+
keys = []
|
|
31
|
+
i = 0
|
|
32
|
+
for _ in value:
|
|
33
|
+
keys.append(i)
|
|
34
|
+
i += 1
|
|
35
|
+
return keys
|
|
36
|
+
|
|
37
|
+
def _object_keys(value):
|
|
38
|
+
keys = []
|
|
39
|
+
for key in value:
|
|
40
|
+
keys.append(key)
|
|
41
|
+
return keys
|
|
42
|
+
|
|
43
|
+
def _is_array(value):
|
|
44
|
+
return isinstance(value, (list, tuple))
|
|
45
|
+
|
|
46
|
+
def _is_object(value):
|
|
47
|
+
return isinstance(value, dict)
|
|
48
|
+
|
|
49
|
+
def _is_string(value):
|
|
50
|
+
return isinstance(value, str)
|
|
51
|
+
|
|
52
|
+
def _index(known, input, value):
|
|
53
|
+
input.append(value)
|
|
54
|
+
index = str(len(input) - 1)
|
|
55
|
+
known.key.append(value)
|
|
56
|
+
known.value.append(index)
|
|
57
|
+
return index
|
|
58
|
+
|
|
59
|
+
def _loop(keys, input, known, output):
|
|
60
|
+
for key in keys:
|
|
61
|
+
value = output[key]
|
|
62
|
+
if isinstance(value, _String):
|
|
63
|
+
_ref(key, input[int(value.value)], input, known, output)
|
|
64
|
+
|
|
65
|
+
return output
|
|
66
|
+
|
|
67
|
+
def _ref(key, value, input, known, output):
|
|
68
|
+
if _is_array(value) and value not in known:
|
|
69
|
+
known.append(value)
|
|
70
|
+
value = _loop(_array_keys(value), input, known, value)
|
|
71
|
+
elif _is_object(value) and value not in known:
|
|
72
|
+
known.append(value)
|
|
73
|
+
value = _loop(_object_keys(value), input, known, value)
|
|
74
|
+
|
|
75
|
+
output[key] = value
|
|
76
|
+
|
|
77
|
+
def _relate(known, input, value):
|
|
78
|
+
if _is_string(value) or _is_array(value) or _is_object(value):
|
|
79
|
+
try:
|
|
80
|
+
return known.value[known.key.index(value)]
|
|
81
|
+
except:
|
|
82
|
+
return _index(known, input, value)
|
|
83
|
+
|
|
84
|
+
return value
|
|
85
|
+
|
|
86
|
+
def _transform(known, input, value):
|
|
87
|
+
if _is_array(value):
|
|
88
|
+
output = []
|
|
89
|
+
for val in value:
|
|
90
|
+
output.append(_relate(known, input, val))
|
|
91
|
+
return output
|
|
92
|
+
|
|
93
|
+
if _is_object(value):
|
|
94
|
+
obj = {}
|
|
95
|
+
for key in value:
|
|
96
|
+
obj[key] = _relate(known, input, value[key])
|
|
97
|
+
return obj
|
|
98
|
+
|
|
99
|
+
return value
|
|
100
|
+
|
|
101
|
+
def _wrap(value):
|
|
102
|
+
if _is_string(value):
|
|
103
|
+
return _String(value)
|
|
104
|
+
|
|
105
|
+
if _is_array(value):
|
|
106
|
+
i = 0
|
|
107
|
+
for val in value:
|
|
108
|
+
value[i] = _wrap(val)
|
|
109
|
+
i += 1
|
|
110
|
+
|
|
111
|
+
elif _is_object(value):
|
|
112
|
+
for key in value:
|
|
113
|
+
value[key] = _wrap(value[key])
|
|
114
|
+
|
|
115
|
+
return value
|
|
116
|
+
|
|
117
|
+
def parse(value, *args, **kwargs):
|
|
118
|
+
json = _json.loads(value, *args, **kwargs)
|
|
119
|
+
wrapped = []
|
|
120
|
+
for value in json:
|
|
121
|
+
wrapped.append(_wrap(value))
|
|
122
|
+
|
|
123
|
+
input = []
|
|
124
|
+
for value in wrapped:
|
|
125
|
+
if isinstance(value, _String):
|
|
126
|
+
input.append(value.value)
|
|
127
|
+
else:
|
|
128
|
+
input.append(value)
|
|
129
|
+
|
|
130
|
+
value = input[0]
|
|
131
|
+
|
|
132
|
+
if _is_array(value):
|
|
133
|
+
return _loop(_array_keys(value), input, [value], value)
|
|
134
|
+
|
|
135
|
+
if _is_object(value):
|
|
136
|
+
return _loop(_object_keys(value), input, [value], value)
|
|
137
|
+
|
|
138
|
+
return value
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def stringify(value, *args, **kwargs):
|
|
142
|
+
known = _Known()
|
|
143
|
+
input = []
|
|
144
|
+
output = []
|
|
145
|
+
i = int(_index(known, input, value))
|
|
146
|
+
while i < len(input):
|
|
147
|
+
output.append(_transform(known, input, input[i]))
|
|
148
|
+
i += 1
|
|
149
|
+
return _json.dumps(output, *args, **kwargs)
|
tweek/integrations/moltbot.py
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Tweek Moltbot Integration - One-command protection setup.
|
|
3
|
-
|
|
4
|
-
Detects Moltbot, configures proxy wrapping, and starts screening
|
|
5
|
-
all tool calls through Tweek's defense pipeline.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
import json
|
|
9
|
-
import subprocess
|
|
10
|
-
from dataclasses import dataclass, field
|
|
11
|
-
from pathlib import Path
|
|
12
|
-
from typing import Optional
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
@dataclass
|
|
16
|
-
class MoltbotSetupResult:
|
|
17
|
-
"""Result of Moltbot protection setup."""
|
|
18
|
-
success: bool = False
|
|
19
|
-
moltbot_detected: bool = False
|
|
20
|
-
moltbot_version: Optional[str] = None
|
|
21
|
-
gateway_port: Optional[int] = None
|
|
22
|
-
gateway_running: bool = False
|
|
23
|
-
proxy_port: Optional[int] = None
|
|
24
|
-
preset: str = "cautious"
|
|
25
|
-
config_path: Optional[str] = None
|
|
26
|
-
error: Optional[str] = None
|
|
27
|
-
warnings: list = field(default_factory=list)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def detect_moltbot_installation() -> dict:
|
|
31
|
-
"""
|
|
32
|
-
Detect Moltbot installation details.
|
|
33
|
-
|
|
34
|
-
Returns dict with:
|
|
35
|
-
installed: bool
|
|
36
|
-
version: str or None
|
|
37
|
-
config_path: Path or None
|
|
38
|
-
gateway_port: int
|
|
39
|
-
process_running: bool
|
|
40
|
-
gateway_active: bool
|
|
41
|
-
"""
|
|
42
|
-
info = {
|
|
43
|
-
"installed": False,
|
|
44
|
-
"version": None,
|
|
45
|
-
"config_path": None,
|
|
46
|
-
"gateway_port": 18789,
|
|
47
|
-
"process_running": False,
|
|
48
|
-
"gateway_active": False,
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
# Check npm global installation
|
|
52
|
-
try:
|
|
53
|
-
proc = subprocess.run(
|
|
54
|
-
["npm", "list", "-g", "moltbot", "--json"],
|
|
55
|
-
capture_output=True,
|
|
56
|
-
text=True,
|
|
57
|
-
timeout=10,
|
|
58
|
-
)
|
|
59
|
-
if proc.returncode == 0:
|
|
60
|
-
data = json.loads(proc.stdout)
|
|
61
|
-
deps = data.get("dependencies", {})
|
|
62
|
-
if "moltbot" in deps:
|
|
63
|
-
info["installed"] = True
|
|
64
|
-
info["version"] = deps["moltbot"].get("version")
|
|
65
|
-
except (subprocess.TimeoutExpired, json.JSONDecodeError, FileNotFoundError):
|
|
66
|
-
pass
|
|
67
|
-
|
|
68
|
-
# Check which/where
|
|
69
|
-
if not info["installed"]:
|
|
70
|
-
try:
|
|
71
|
-
import os
|
|
72
|
-
cmd = ["which", "moltbot"] if os.name != "nt" else ["where", "moltbot"]
|
|
73
|
-
proc = subprocess.run(cmd, capture_output=True, text=True, timeout=5)
|
|
74
|
-
if proc.returncode == 0 and proc.stdout.strip():
|
|
75
|
-
info["installed"] = True
|
|
76
|
-
except (subprocess.TimeoutExpired, FileNotFoundError):
|
|
77
|
-
pass
|
|
78
|
-
|
|
79
|
-
# Check for config file
|
|
80
|
-
config_locations = [
|
|
81
|
-
Path.home() / ".moltbot" / "config.json",
|
|
82
|
-
Path.home() / ".config" / "moltbot" / "config.json",
|
|
83
|
-
]
|
|
84
|
-
for config_path in config_locations:
|
|
85
|
-
if config_path.exists():
|
|
86
|
-
info["installed"] = True
|
|
87
|
-
info["config_path"] = config_path
|
|
88
|
-
try:
|
|
89
|
-
with open(config_path) as f:
|
|
90
|
-
config = json.load(f)
|
|
91
|
-
port = config.get("gateway", {}).get("port")
|
|
92
|
-
if port:
|
|
93
|
-
info["gateway_port"] = port
|
|
94
|
-
except (json.JSONDecodeError, IOError):
|
|
95
|
-
pass
|
|
96
|
-
break
|
|
97
|
-
|
|
98
|
-
# Check for running process
|
|
99
|
-
try:
|
|
100
|
-
proc = subprocess.run(
|
|
101
|
-
["pgrep", "-f", "moltbot"],
|
|
102
|
-
capture_output=True,
|
|
103
|
-
text=True,
|
|
104
|
-
timeout=5,
|
|
105
|
-
)
|
|
106
|
-
if proc.returncode == 0 and proc.stdout.strip():
|
|
107
|
-
info["process_running"] = True
|
|
108
|
-
except (subprocess.TimeoutExpired, FileNotFoundError):
|
|
109
|
-
pass
|
|
110
|
-
|
|
111
|
-
# Check if gateway port is active
|
|
112
|
-
import socket
|
|
113
|
-
try:
|
|
114
|
-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
115
|
-
s.settimeout(1)
|
|
116
|
-
result = s.connect_ex(("127.0.0.1", info["gateway_port"]))
|
|
117
|
-
info["gateway_active"] = result == 0
|
|
118
|
-
except (socket.error, OSError):
|
|
119
|
-
pass
|
|
120
|
-
|
|
121
|
-
return info
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
def setup_moltbot_protection(
|
|
125
|
-
port: Optional[int] = None,
|
|
126
|
-
preset: str = "cautious",
|
|
127
|
-
) -> MoltbotSetupResult:
|
|
128
|
-
"""
|
|
129
|
-
Configure Tweek to protect Moltbot gateway.
|
|
130
|
-
|
|
131
|
-
Args:
|
|
132
|
-
port: Override Moltbot gateway port (default: auto-detect)
|
|
133
|
-
preset: Security preset to apply (paranoid, cautious, trusted)
|
|
134
|
-
|
|
135
|
-
Returns:
|
|
136
|
-
MoltbotSetupResult with setup details
|
|
137
|
-
"""
|
|
138
|
-
result = MoltbotSetupResult(preset=preset)
|
|
139
|
-
|
|
140
|
-
# 1. Detect Moltbot
|
|
141
|
-
moltbot = detect_moltbot_installation()
|
|
142
|
-
result.moltbot_detected = moltbot["installed"]
|
|
143
|
-
result.moltbot_version = moltbot["version"]
|
|
144
|
-
|
|
145
|
-
if not moltbot["installed"]:
|
|
146
|
-
result.error = "Moltbot not detected on this system"
|
|
147
|
-
return result
|
|
148
|
-
|
|
149
|
-
# 2. Resolve gateway port
|
|
150
|
-
if port is not None:
|
|
151
|
-
result.gateway_port = port
|
|
152
|
-
else:
|
|
153
|
-
result.gateway_port = moltbot["gateway_port"]
|
|
154
|
-
|
|
155
|
-
result.gateway_running = moltbot["gateway_active"]
|
|
156
|
-
|
|
157
|
-
# 3. Configure Tweek proxy
|
|
158
|
-
from tweek.proxy import TWEEK_DEFAULT_PORT
|
|
159
|
-
result.proxy_port = TWEEK_DEFAULT_PORT
|
|
160
|
-
|
|
161
|
-
# Check if Tweek port is already in use by something else
|
|
162
|
-
import socket
|
|
163
|
-
try:
|
|
164
|
-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
165
|
-
s.settimeout(1)
|
|
166
|
-
if s.connect_ex(("127.0.0.1", TWEEK_DEFAULT_PORT)) == 0:
|
|
167
|
-
result.warnings.append(
|
|
168
|
-
f"Port {TWEEK_DEFAULT_PORT} is already in use. "
|
|
169
|
-
"Tweek proxy may need a different port."
|
|
170
|
-
)
|
|
171
|
-
except (socket.error, OSError):
|
|
172
|
-
pass
|
|
173
|
-
|
|
174
|
-
# 4. Write config to ~/.tweek/config.yaml
|
|
175
|
-
tweek_dir = Path.home() / ".tweek"
|
|
176
|
-
tweek_dir.mkdir(parents=True, exist_ok=True)
|
|
177
|
-
config_path = tweek_dir / "config.yaml"
|
|
178
|
-
|
|
179
|
-
try:
|
|
180
|
-
import yaml
|
|
181
|
-
except ImportError:
|
|
182
|
-
# Fall back to writing YAML manually if PyYAML not available
|
|
183
|
-
yaml = None
|
|
184
|
-
|
|
185
|
-
tweek_config = {}
|
|
186
|
-
if config_path.exists():
|
|
187
|
-
try:
|
|
188
|
-
if yaml:
|
|
189
|
-
with open(config_path) as f:
|
|
190
|
-
tweek_config = yaml.safe_load(f) or {}
|
|
191
|
-
else:
|
|
192
|
-
tweek_config = {}
|
|
193
|
-
except Exception:
|
|
194
|
-
tweek_config = {}
|
|
195
|
-
|
|
196
|
-
# Set proxy configuration
|
|
197
|
-
tweek_config["proxy"] = tweek_config.get("proxy", {})
|
|
198
|
-
tweek_config["proxy"]["enabled"] = True
|
|
199
|
-
tweek_config["proxy"]["port"] = TWEEK_DEFAULT_PORT
|
|
200
|
-
tweek_config["proxy"]["moltbot"] = {
|
|
201
|
-
"enabled": True,
|
|
202
|
-
"gateway_port": result.gateway_port,
|
|
203
|
-
"wrap_gateway": True,
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
# Set security preset
|
|
207
|
-
tweek_config["security"] = tweek_config.get("security", {})
|
|
208
|
-
tweek_config["security"]["preset"] = preset
|
|
209
|
-
|
|
210
|
-
try:
|
|
211
|
-
if yaml:
|
|
212
|
-
with open(config_path, "w") as f:
|
|
213
|
-
yaml.dump(tweek_config, f, default_flow_style=False)
|
|
214
|
-
else:
|
|
215
|
-
# Manual YAML writing as fallback
|
|
216
|
-
lines = [
|
|
217
|
-
"proxy:",
|
|
218
|
-
f" enabled: true",
|
|
219
|
-
f" port: {TWEEK_DEFAULT_PORT}",
|
|
220
|
-
" moltbot:",
|
|
221
|
-
" enabled: true",
|
|
222
|
-
f" gateway_port: {result.gateway_port}",
|
|
223
|
-
" wrap_gateway: true",
|
|
224
|
-
"security:",
|
|
225
|
-
f" preset: {preset}",
|
|
226
|
-
]
|
|
227
|
-
config_path.write_text("\n".join(lines) + "\n")
|
|
228
|
-
|
|
229
|
-
result.config_path = str(config_path)
|
|
230
|
-
except Exception as e:
|
|
231
|
-
result.error = f"Failed to write config: {e}"
|
|
232
|
-
return result
|
|
233
|
-
|
|
234
|
-
# 5. Apply security preset
|
|
235
|
-
try:
|
|
236
|
-
from tweek.config.manager import ConfigManager
|
|
237
|
-
cfg = ConfigManager()
|
|
238
|
-
cfg.apply_preset(preset)
|
|
239
|
-
except Exception as e:
|
|
240
|
-
result.warnings.append(f"Could not apply preset: {e}")
|
|
241
|
-
|
|
242
|
-
result.success = True
|
|
243
|
-
return result
|