OneKey End-to-End Encryption (E2EE) Server - A high-performance, secure real-time communication server built with Socket.IO and TypeScript.
- End-to-End Encryption: Secure message transmission between clients
- Real-time Communication: WebSocket-based bidirectional communication using Socket.IO
- Room Management: Dynamic room creation and user management
- Type Safety: Full TypeScript implementation with strict typing
- Scalable Architecture: Modular design with clean separation of concerns
- Cross-Platform Support: Works with web, mobile, and desktop clients
# Install dependencies
yarn install
# Build the project
yarn build# Start the server with hot reload
yarn devThe server will start on port 3868 by default (configurable via PORT environment variable).
# Build the project
yarn build
# Start the production server
yarn startThe server can be configured using environment variables:
| Variable | Default | Description |
|---|---|---|
PORT |
3868 |
Server listening port |
CORS_ORIGINS |
* |
Comma-separated list of allowed CORS origins |
MAX_USERS_PER_ROOM |
2 |
Maximum users allowed per room |
ROOM_TIMEOUT |
3600000 |
Room timeout in milliseconds (1 hour) |
MAX_MESSAGE_SIZE |
10485760 |
Maximum message size in bytes (10MB) |
Example .env file:
PORT=3868
CORS_ORIGINS=http://localhost:3000,https://app.onekey.so
MAX_USERS_PER_ROOM=2
ROOM_TIMEOUT=3600000
MAX_MESSAGE_SIZE=10485760| Event | Description | Payload |
|---|---|---|
create-room |
Create a new room | { roomId?: string, metadata?: object } |
join-room |
Join an existing room | { roomId: string, userId?: string } |
send-encrypted-data |
Send encrypted data to room members | { data: any, targetUserId?: string } |
leave-room |
Leave the current room | { roomId: string } |
get-room-status |
Get room information | { roomId: string } |
get-room-list |
Get list of available rooms | {} |
| Event | Description | Payload |
|---|---|---|
room-created |
Room successfully created | { roomId: string, creatorId: string } |
room-joined |
Successfully joined room | { roomId: string, userId: string, users: string[] } |
user-joined |
Another user joined the room | { userId: string, users: string[] } |
user-left |
User left the room | { userId: string, users: string[] } |
encrypted-data |
Received encrypted data | { data: any, senderId: string } |
room-error |
Error occurred | { code: string, message: string } |
room-status |
Room status information | { roomId: string, users: User[], createdAt: number } |
| Endpoint | Method | Description |
|---|---|---|
/health |
GET | Health check endpoint |
/stats |
GET | Server statistics |
/rooms |
GET | List all active rooms |
/rooms/:roomId |
GET | Get room details |
server.ts: Main server entry point with Express and Socket.IO setuproomManager.ts: Handles room lifecycle and user managemente2eeServerApi.ts: API interface definitionse2eeServerApiProxy.ts: API proxy implementation for remote callsJsBridgeE2EEServer.ts: Server-side bridge implementationJsBridgeE2EEClient.ts: Client-side bridge implementation
@e2eeApiMethod: Marks methods as E2EE API endpoints with automatic validation and error handling
cryptoUtils.ts: Cryptographic operations and key managementbufferUtils.ts: Buffer manipulation and conversion utilitieshexUtils.ts: Hexadecimal encoding/decodingcacheUtils.ts: LRU cache implementation for performancetimerUtils.ts: Timer and timeout managementstringUtils.ts: String manipulation helpers
- Message Size Limits: Prevents DoS attacks by limiting message sizes
- Room Timeouts: Automatic cleanup of inactive rooms
- User Limits: Configurable maximum users per room
- CORS Protection: Configurable CORS origins
- Input Validation: Automatic validation of all API inputs
- Always use HTTPS in production
- Configure CORS origins appropriately
- Implement rate limiting with a reverse proxy
- Monitor room creation patterns for abuse
- Use environment variables for sensitive configuration
packages/transfer-server/
├── src/
│ ├── server.ts # Main server entry
│ ├── roomManager.ts # Room management logic
│ ├── e2eeServerApi.ts # API interfaces
│ ├── e2eeServerApiProxy.ts # API proxy
│ ├── JsBridgeE2EEServer.ts # Server bridge
│ ├── JsBridgeE2EEClient.ts # Client bridge
│ ├── errors.ts # Error definitions
│ ├── types.ts # TypeScript types
│ ├── decorators/
│ │ └── e2eeApiMethod.ts # API decorators
│ └── utils/
│ ├── RemoteApiProxyBase.ts
│ ├── bufferUtils.ts
│ ├── cacheUtils.ts
│ ├── cryptoUtils.ts
│ ├── hexUtils.ts
│ ├── stringUtils.ts
│ └── timerUtils.ts
├── dist/ # Compiled JavaScript
├── package.json
├── tsconfig.json
└── nodemon.json
# Development
yarn dev # Start dev server with hot reload
# Building
yarn build # Compile TypeScript to JavaScript
yarn clean # Remove build artifacts
# Production
yarn start # Start production server# Run tests (when implemented)
yarn test
# Run tests with coverage
yarn test:coverageThe server implements a comprehensive error handling system with specific error codes:
| Error Code | Description |
|---|---|
ROOM_NOT_FOUND |
Requested room does not exist |
ROOM_FULL |
Room has reached maximum capacity |
UNAUTHORIZED |
User not authorized for this operation |
INVALID_DATA |
Invalid data format or content |
TIMEOUT |
Operation timed out |
INTERNAL_ERROR |
Internal server error |
- LRU Cache: Frequently accessed data is cached using LRU strategy
- Memoization: Expensive computations are memoized
- Buffer Pooling: Efficient buffer management for large messages
- Connection Pooling: Optimized Socket.IO connection handling
FROM node:24-alpine
WORKDIR /app
COPY package*.json ./
RUN yarn install --production
COPY . .
RUN yarn build
EXPOSE 3868
CMD ["yarn", "start"]// ecosystem.config.js
module.exports = {
apps: [{
name: 'e2ee-server',
script: './dist/server.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 3868
}
}]
}curl http://localhost:3868/healthcurl http://localhost:3868/stats-
Port Already in Use
# Find process using port 3868 lsof -i :3868 # Kill the process kill -9 <PID>
-
CORS Issues
- Ensure
CORS_ORIGINSenvironment variable is properly configured - Check that client origin matches allowed origins
- Ensure
-
Connection Timeouts
- Verify firewall settings
- Check WebSocket support in reverse proxy configuration
-
Memory Leaks
- Monitor room cleanup with
/statsendpoint - Ensure
ROOM_TIMEOUTis configured appropriately
- Monitor room cleanup with
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is part of the OneKey ecosystem.
For issues and questions, please open an issue on GitHub or contact the OneKey development team.