DTech &

D-TECH Engineering Project
Back to Gateway

1. System Overview

DTech is an innovative Android application built with Flutter that transforms a mobile device into an active web server. It allows users to host and serve a static website (HTML, CSS, JS, Images, etc.) directly from their phone's local storage.

What makes DTech uniquely powerful is its dual-mode accessibility:

1. Local Wi-Fi Mode: Devices on the same Wi-Fi network can access the hosted website directly via the phone's local IP address.

2. Global Internet Tunneling Mode: Users anywhere in the world can access the phone's local web server through a public subdomain without the need for complex port forwarding, proxy servers, or centralized relay points (like Ngrok).

The Core Problem Solved:

Mobile networks often use Carrier-Grade NAT (CGNAT) and strict firewalls, making it impossible to expose a local port on a mobile phone to the public internet using traditional methods. Cloudflare tunnels often face Anycast data center isolation issues. DTech bypasses these limitations by establishing a direct Peer-to-Peer (P2P) WebRTC connection between the visitor's web browser and the Flutter application on the phone. This allows HTTP traffic to be tunneled seamlessly over WebRTC data channels, turning the browser itself into a proxy.

---

2. Architecture & How It Works

The architecture of DTech is divided into two major components: the Local Web Server (on the phone) and the WebRTC Tunnel (the bridge between the global internet and the phone).

A. The Local Web Server

When a user selects a folder and starts the server via the Flutter UI, the app utilizes the Dart shelf and shelf_static packages to bind an HTTP server to port 8080. This server listens for incoming HTTP requests (GET, POST, etc.) and serves the static files from the selected directory. This handles the core file-hosting functionality.

B. The WebRTC Tunnel (The Magic)

When global tunneling is enabled, the app establishes an internet gateway. This involves several coordinated moving parts:

1. Signaling via Firebase Realtime Database:

Because the visitor's browser and the Android phone do not initially know how to connect to each other across the internet, they use a Firebase Realtime Database as a "Signaling Server". They exchange Session Description Protocol (SDP) "Offers" and "Answers", as well as ICE Candidates (which map out potential network paths) through this central database. Once they find a valid path (often using public STUN servers provided by Google), the direct P2P connection is established, and Firebase steps out of the way.

2. The Cloudflare Worker App Shell (worker.js):

When a visitor navigates to the user's public subdomain (managed by Cloudflare), a Cloudflare Worker intercepts the request. Instead of serving the website directly (which it can't, because the files are on the phone), the Worker serves an "App Shell". This is a basic HTML file containing JavaScript.

3. The Service Worker Interceptor (sw.js):

The Service Worker runs in the background of the visitor's browser. Its job is to intercept *every single HTTP request* (images, CSS, API calls) made by the <iframe> (the actual website being viewed).

4. Data Serialization & Transport:

The App Shell receives the intercepted request. If there is a request body (e.g., a form upload), it base64 encodes it. It formats the request into a string (e.g., requestId|method|path|headersJSON|bodyStr) and sends it across the internet directly to the phone via the WebRTC RTCDataChannel.

5. The Flutter Proxy (WebRTCTunnelService):

Inside the Flutter app, the WebRTCTunnelService listens to the WebRTC Data Channel.

6. Reassembly and Browser Delivery:

The App Shell in the browser receives the chunks, stitches the binary file back together, and passes it to the Service Worker. The Service Worker then completes the intercepted request, handing the final file over to the browser's rendering engine. To the end-user, it feels exactly like browsing a normal website, entirely unaware that the assets are being streamed P2P from a mobile device.

---

3. Detailed File-by-File Analysis

The Global Gateway

#### worker.js (Cloudflare Worker)

This is the single entry point for external traffic. It serves two distinct roles based on the requested URL:

The Flutter Core (lib/core/)

#### server_manager.dart

This is the central "brain" of the application state. It extends ChangeNotifier so the UI can react to state changes.

#### webrtc_tunnel_service.dart

The most complex and vital component of the app's backend.

#### network_utils.dart

A utility class dedicated to discovering the phone's Local Area Network (LAN) IP address. It iterates through the device's network interfaces (looking for wlan, en, or eth), ignoring loopback addresses, to find the IPv4 address so the UI can display instructions for local Wi-Fi access.

#### visitor_tracker.dart

A simple state management class (ChangeNotifier) that maintains a counter of unique visitors (currently basic, incrementing when / or index.html is requested) and keeps a rolling list of the last 50 HTTP requests (e.g., GET /style.css) to be displayed in the Dashboard UI logs.

#### preferences_service.dart

A wrapper around the shared_preferences package. It provides asynchronous getter and setter methods to persist user configurations across app restarts, such as the selected website folder path, the chosen subdomain, and the auto-start/tunnel toggles.

The Application UI

#### lib/main.dart

The entry point of the Flutter application.

#### lib/features/dashboard/dashboard_screen.dart

The primary and only screen of the application.

Visit Live Platform