scripting improv
This commit is contained in:
@@ -1,42 +1,29 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Minimal NFQUEUE example script.
|
"""Minimal NFQUEUE example: accept every packet from a queue."""
|
||||||
|
|
||||||
Expected argv:
|
|
||||||
argv[1] = queue number
|
|
||||||
Any extra args are optional.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from netfilterqueue import NetfilterQueue
|
from netfilterqueue import NetfilterQueue
|
||||||
except Exception as exc: # pragma: no cover
|
except Exception as exc:
|
||||||
print(f"Failed to import netfilterqueue: {exc}", file=sys.stderr)
|
print(f"Failed to import netfilterqueue: {exc}", file=sys.stderr)
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
nfq = NetfilterQueue()
|
||||||
logger = logging.getLogger("hello-nfqueue")
|
|
||||||
|
|
||||||
_running = True
|
|
||||||
|
|
||||||
|
|
||||||
def _stop(_sig: int, _frame: Optional[object]) -> None:
|
|
||||||
global _running
|
|
||||||
_running = False
|
|
||||||
|
|
||||||
|
|
||||||
def _handle_packet(packet) -> None:
|
def _handle_packet(packet) -> None:
|
||||||
# This demo accepts all packets and logs basic metadata.
|
|
||||||
logger.info("packet id=%s len=%s", packet.get_id(), len(packet.get_payload()))
|
|
||||||
packet.accept()
|
packet.accept()
|
||||||
|
|
||||||
|
|
||||||
|
def _stop(_sig, _frame) -> None:
|
||||||
|
raise SystemExit(0)
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) != 2:
|
||||||
print("Usage: hello_nfqueue.py <qnum> [extra args...]", file=sys.stderr)
|
print("Usage: hello_nfqueue.py <qnum>", file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -48,19 +35,13 @@ def main() -> int:
|
|||||||
signal.signal(signal.SIGINT, _stop)
|
signal.signal(signal.SIGINT, _stop)
|
||||||
signal.signal(signal.SIGTERM, _stop)
|
signal.signal(signal.SIGTERM, _stop)
|
||||||
|
|
||||||
nfq = NetfilterQueue()
|
|
||||||
logger.info("Binding to NFQUEUE %d", qnum)
|
|
||||||
nfq.bind(qnum, _handle_packet)
|
nfq.bind(qnum, _handle_packet)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while _running:
|
nfq.run()
|
||||||
nfq.run(block=True)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
logger.info("Unbinding NFQUEUE %d", qnum)
|
|
||||||
nfq.unbind()
|
nfq.unbind()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -376,6 +376,7 @@ Type=simple
|
|||||||
ExecStart={exec_start}
|
ExecStart={exec_start}
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=2
|
RestartSec=2
|
||||||
|
TimeoutStopSec=10
|
||||||
StandardOutput=syslog
|
StandardOutput=syslog
|
||||||
StandardError=syslog
|
StandardError=syslog
|
||||||
|
|
||||||
|
|||||||
@@ -166,8 +166,7 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
const val = ta.value ?? '';
|
const val = ta.value ?? '';
|
||||||
ta.setSelectionRange(val.length, val.length);
|
ta.setSelectionRange(val.length, val.length);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {}
|
||||||
}
|
|
||||||
}, 80);
|
}, 80);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -489,7 +488,10 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
return <Badge color={isActive ? '#52c41a' : '#ff4d4f'} text={isActive ? 'Active' : 'Inactive'} />;
|
return <Badge color={isActive ? '#52c41a' : '#ff4d4f'} text={isActive ? 'Active' : 'Inactive'} />;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const isScriptActive = useCallback((record: ScriptWithStatus) => record.mappings.some((mapping) => mapping.active), []);
|
const isScriptActive = useCallback(
|
||||||
|
(record: ScriptWithStatus) => record.mappings.some((mapping) => mapping.active),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
const saveAddRequirements = useCallback(async () => {
|
const saveAddRequirements = useCallback(async () => {
|
||||||
if (!addReqTarget) return;
|
if (!addReqTarget) return;
|
||||||
@@ -539,7 +541,6 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
render: (_: unknown, record: ScriptWithStatus) => (
|
render: (_: unknown, record: ScriptWithStatus) => (
|
||||||
<Space>
|
<Space>
|
||||||
<code>{record.name}</code>
|
<code>{record.name}</code>
|
||||||
{record.is_protected_example ? <Badge color="#1677ff" text="Protected" /> : null}
|
|
||||||
</Space>
|
</Space>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
@@ -741,7 +742,6 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
expandable={expandable}
|
expandable={expandable}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
||||||
<Modal
|
<Modal
|
||||||
open={uploadModalVisible}
|
open={uploadModalVisible}
|
||||||
title="Upload or create script"
|
title="Upload or create script"
|
||||||
@@ -817,7 +817,6 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
</Form>
|
</Form>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
|
|
||||||
<Modal
|
<Modal
|
||||||
open={editorModalVisible}
|
open={editorModalVisible}
|
||||||
title={currentEditingName ? `Editing — ${currentEditingName}` : 'Editor'}
|
title={currentEditingName ? `Editing — ${currentEditingName}` : 'Editor'}
|
||||||
@@ -875,7 +874,6 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
<PythonEditor value={editorValue} onChange={setEditorValue} height={520} />
|
<PythonEditor value={editorValue} onChange={setEditorValue} height={520} />
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
|
|
||||||
<Modal
|
<Modal
|
||||||
open={reqModalVisible}
|
open={reqModalVisible}
|
||||||
title={reqEditingName ? `requirements.txt — ${reqEditingName}` : 'requirements.txt'}
|
title={reqEditingName ? `requirements.txt — ${reqEditingName}` : 'requirements.txt'}
|
||||||
@@ -914,7 +912,6 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
<PythonEditor value={reqEditorValue} onChange={setReqEditorValue} height={420} />
|
<PythonEditor value={reqEditorValue} onChange={setReqEditorValue} height={420} />
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
|
|
||||||
<Modal
|
<Modal
|
||||||
open={addReqModalVisible}
|
open={addReqModalVisible}
|
||||||
title={addReqTarget ? `Add requirements — ${addReqTarget}` : 'Add requirements'}
|
title={addReqTarget ? `Add requirements — ${addReqTarget}` : 'Add requirements'}
|
||||||
@@ -968,7 +965,6 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
|
|
||||||
<Modal
|
<Modal
|
||||||
open={pipModalVisible}
|
open={pipModalVisible}
|
||||||
title="pip install output"
|
title="pip install output"
|
||||||
@@ -987,7 +983,6 @@ export default function ScriptsManager({ onOpenInEditor }: { onOpenInEditor?: (c
|
|||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
|
|
||||||
<Modal
|
<Modal
|
||||||
open={enableModalVisible}
|
open={enableModalVisible}
|
||||||
title={`Enable ${enableTarget ?? ''}`}
|
title={`Enable ${enableTarget ?? ''}`}
|
||||||
|
|||||||
Reference in New Issue
Block a user