# tik-choco-lab/webrtc-p2p-tunnel architecture

> Architecture diagram of the GitHub repository tik-choco-lab/webrtc-p2p-tunnel, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tik-choco-lab/webrtc-p2p-tunnel
- Repository: https://github.com/tik-choco-lab/webrtc-p2p-tunnel

## Overview

##### Project type and purpose
- This is a Go-based command-line application (“webrtc-p2p-tunnel”) that establishes a peer-to-peer tunnel over WebRTC DataChannels. It listens on a local TCP port, uses a signaling service to exchange SDP/ICE with a remote peer, and forwards TCP-level traffic bidirectionally through the WebRTC channel.

##### File structure
- cmd/tunnel/main.go: The CLI entrypoint that parses flags, sets up logging, and orchestrates the tunnel.
- internal/logger/logger.go: A thin wrapper around Go’s logging primitives, used by all modules.
- internal/signal: Implements a signaling client for exchanging SDP/ICE messages with a remote peer (likely over WebSocket or HTTP).
- internal/rtc: Manages WebRTC PeerConnection and DataChannel (“chat.go”, “peer.go”, “tunnel_message.go”).
- internal/tcp/listener.go: Listens on a local TCP port, accepts inbound connections, and hands off I/O to the DataChannel.

##### README for additional insights
- Usage is simply “go run ./cmd/tunnel” or “go build ./cmd/tunnel.” No built-in signaling server provided, so the CLI must connect to a remote signaling endpoint (configurable via flags).

##### Be very detailed
- Eight components (CLI, logger, signal, rtc-peer, rtc-channel, tcp-listener, external signaling server, remote TCP endpoint).
- Initialization sequence: CLI → logger → signal.connect() → rtc.setup() → datachannel.open → tcp.listen().

## Diagram (Mermaid)

```mermaid
flowchart TB
    SignalingServer["External Signaling Server"]:::external

    subgraph "CLI Layer"
        CLI["CLI/Tunnel Process"]:::local
    end

    CLI -->|initializes| Logger
    CLI -->|connects| SignalClient
    CLI -->|sets_up| RTCPeer
    CLI -->|starts| TCPListener

    Logger["Logger Module"]:::local
    SignalClient["Signaling Client"]:::local
    SignalMessage["Signal Message"]:::local
    RTCPeer["WebRTC PeerConnection"]:::local
    ChatChannel["DataChannel (chat)"]:::local
    TunnelMsg["Tunnel Message"]:::local
    TCPListener["TCP Listener"]:::local
    RemoteTCP["Remote TCP Endpoint"]:::external

    SignalClient -->|uses| SignalMessage
    SignalClient -->|exchange_offer_answer_and_ICE| SignalingServer
    SignalingServer -->|forward_messages| SignalClient

    RTCPeer -->|opens| ChatChannel
    RTCPeer -->|handles_tunnel_msgs| TunnelMsg

    TCPListener -.->|sends_bytes| ChatChannel
    ChatChannel -.->|receives_bytes| TCPListener

    ChatChannel -.->|tunneled_data| RemoteTCP
    RemoteTCP -.->|return_data| ChatChannel

    click CLI "https://github.com/tik-choco-lab/webrtc-p2p-tunnel/blob/main/cmd/tunnel/main.go"
    click Logger "https://github.com/tik-choco-lab/webrtc-p2p-tunnel/blob/main/internal/logger/logger.go"
    click SignalClient "https://github.com/tik-choco-lab/webrtc-p2p-tunnel/blob/main/internal/signal/client.go"
    click SignalMessage "https://github.com/tik-choco-lab/webrtc-p2p-tunnel/blob/main/internal/signal/message.go"
    click RTCPeer "https://github.com/tik-choco-lab/webrtc-p2p-tunnel/blob/main/internal/rtc/peer.go"
    click ChatChannel "https://github.com/tik-choco-lab/webrtc-p2p-tunnel/blob/main/internal/rtc/chat.go"
    click TunnelMsg "https://github.com/tik-choco-lab/webrtc-p2p-tunnel/blob/main/internal/rtc/tunnel_message.go"
    click TCPListener "https://github.com/tik-choco-lab/webrtc-p2p-tunnel/blob/main/internal/tcp/listener.go"

    classDef local fill:#cde8ff,stroke:#036,stroke-width:1px
    classDef external fill:#d4f8d4,stroke:#2d7,stroke-width:1px
    classDef network fill:#ffddb3,stroke:#f80,stroke-width:1px
```
