Skip to content

【WIP】CLI バージョンを作成#251

Closed
kakira9618 wants to merge 15 commits into
masterfrom
feat/cli-ver
Closed

【WIP】CLI バージョンを作成#251
kakira9618 wants to merge 15 commits into
masterfrom
feat/cli-ver

Conversation

@kakira9618

@kakira9618 kakira9618 commented Jun 9, 2026

Copy link
Copy Markdown
Member

概要

PacketProxy の CLI 版を作成。
boot

機能

  • PacketProxy の CLI Server を作成
    • GUI ではなくCUI で Server が起動します。指定したポートで REST API をホストし、curlなどの別クライアントから情報を取得・操作することが可能です。
  • PacketProxy の CLI Client を作成
    • エンコーダーモジュールを用いて、標準入力のデータを encode / decode して標準出力に出力することが出来ます。
  • 必要なロジックテストを追加

使用方法

詳しくは docs/cli.md を参照してください。

CLI Server / CLI Client 共通

  • ビルド
$ ./gradlew installDist

CLI Server

  • 起動 (8888番ポート)
$ BIN=build/install/PacketProxy/bin/PacketProxy
$ $BIN server --api-port 8888 --api-key mytoken

デフォルトでは、GUI 版で最後に使った DB / 設定で起動します。

  • ステータス確認
$ curl -s -H "Authorization: Bearer mytoken" http://localhost:8888/api/status | jq .
{
  "status": "READY",
  "startedAt": 1781002189458,
  "readyAt": 1781002201145,
  "uptimeSec": 106
}
  • パケット一覧取得(先着n件)
$ curl -s -H "Authorization: Bearer mytoken" http://localhost:8888/api/packets\?limit\=2 | jq .
{
  "data": [
    {
      "id": 1,
      "direction": "CLIENT",
      "listenPort": 0,
      "clientIp": "127.0.0.1",
      "serverIp": "216.239.38.223",
      "serverPort": 443,
      "serverName": "play.googleapis.com",
      "encoderName": "HTTP",
      "modified": false,
      "resend": false,
      "date": 1772765941307
    },
    {
      "id": 2,
      "direction": "CLIENT",
      "listenPort": 0,
      "clientIp": "127.0.0.1",
      "serverIp": "104.16.5.34",
      "serverPort": 443,
      "serverName": "registry.npmjs.org",
      "encoderName": "HTTP",
      "modified": false,
      "resend": false,
      "date": 1772765941352
    }
  ],
  "offset": 0,
  "limit": 2
}

他のAPIは docs/cli.md を参照してください。

  • ステータス確認 (GET /api/status)
  • パケット履歴 (GET /api/packets)
  • パケット詳細 (GET /api/packets/{id})
  • 再送 (POST /api/packets/{id}/resend)
  • バルク送信 (POST /api/packets/{id}/bulk-send)
  • VulCheck チェッカー取得 (GET /api/vulcheckers)
  • VulCheck 実行 (POST /api/vulcheckers/{name}/run)
  • 設定取得 (GET /api/config)
  • 設定変更 (PUT /api/config)
  • DB バックアップ (GET /api/db)

CLI Client

  • 標準入力の内容をエンコーダーモジュールを利用してdecodeして標準出力に出力する
$ echo 'hello' | $BIN decode --encoder "Sample UpperCase" --text >decoded.txt
$ cat decoded.txt
HELLO
  • 標準入力の内容をエンコーダーモジュールを利用してencodeして標準出力に出力する
$ echo 'HELLO' | $BIN decode --encoder "Sample UpperCase" --text >encoded.txt
$ cat encoded.txt
hello

kakira9618 and others added 15 commits June 9, 2026 18:42
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…NT modes

Replace the boolean isGulp flag with a LogMode enum so CLI subcommands
can route log output appropriately: SERVER_STDERR sends to stderr for CI
visibility, SILENT suppresses all output to keep encode/decode stdout clean.
AppInitializer gains setLogMode() so callers override before initCore().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…fety

The CLI runs encode/decode from a plain main thread without AWT/EDT
serialization. Without synchronized, concurrent test runs can race on
the lazy singleton initialization.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…x dump

Previously raw bytes were cast to String, producing mojibake when the
pending buffer contained binary protocol data. Now the log line shows
[binary N bytes] followed by the first 64 bytes in hex, matching the
format used by other tools in the project.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…hutdown

The server subcommand needs to close all active listen ports on
SIGINT/SIGTERM. stopAll() iterates the internal map under its own
lock so existing per-port close() errors do not abort the loop.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
….main()

CliRoot registers server/encode/decode/encoders as subcommands.
PacketProxy.main() short-circuits to picocli when the first arg matches
a known CLI subcommand, leaving the legacy --gulp path untouched.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Codec is a stateless object that wraps EncoderManager, reads stdin (or
--in file), runs the chosen direction through the encoder, and writes to
stdout (or --out file). EncodeCommand/DecodeCommand delegate to Codec.
EncodersCommand lists all registered encoder names.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…tion

Full Packet rows include large blob columns (decoded_data etc.) that are
expensive to serialise over the REST API. querySummaryRange selects only
the metadata columns needed for listing; querySummaryRangeFiltered adds
a raw WHERE clause for server-side filtering.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ManagementApiServer embeds NanoHTTPD and dispatches to five handlers:
  PacketApiHandler   GET /api/packets        — paginated packet listing
  ResendApiHandler   POST /api/resend        — resend a captured packet
  VulCheckApiHandler POST /api/vulcheck      — run a vuln-check script
  ConfigApiHandler   GET/POST /api/config    — export/import JSON config
  /api/status returns STARTING → RUNNING → STOPPING lifecycle state

Bearer-token auth is optional; --api-key activates it.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…nfig mode

ServerCommand blocks in the foreground until SIGINT/SIGTERM.
Key behaviours:
  --config omitted  → reuse existing ~/.packetproxy/db/resources.sqlite3
  --config FILE     → import JSON and apply to the DB
  --api-port PORT   → start ManagementApiServer before initComponents()
                      so /api/status returns STARTING during boot
  --no-log          → LogMode.SILENT

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
EncoderCharacterizationTest pins encode/decode round-trip behaviour for
EncodeSample and EncodeSampleUpperCase so CLI regressions are caught.
EncoderManagerCharacterizationTest verifies headless singleton access.
CodecTest drives the full Codec.run() path via file I/O, covering text
mode, binary passthrough, and unknown-encoder error reporting.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Build and export are moved before the new subcommand dispatch so the
binary is always up to date. server/encode/decode/encoders are forwarded
directly to the binary; all other args fall through to the legacy gulp path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
cli-plan.md tracks the design decisions and item-by-item progress for the
CLI feature work. cli.md documents usage examples for the new subcommands.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kakira9618 kakira9618 closed this Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant