Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ IDLE_TIMEOUT_SECONDS=300
# Set false to allow TCP connections only.
UDP_ENABLED=true

# ─── Telegram (MTProto) ─────────────────────────────────────────────────────
# Telegram's own protocol, served on its own port because it is not SOCKS5.
# Needs a port of its own: 443 is normally taken by REALITY.
# Add secrets in the dashboard; it generates the first one on first start.
MTPROTO_ENABLED=false
MTPROTO_BIND=0.0.0.0:8443
# Only needed if Telegram's own addresses are unreachable from this host:
# MTPROTO_DCS=2=10.0.0.1:443,4=10.0.0.2:443

# ─── Security ──────────────────────────────────────────────────────────────
SECURITY_HEADERS_ENABLED=true

Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Deploys on any PaaS, including the ones that expose only a single HTTP endpoint.
- **Single-Port Multiplexing** - SOCKS5, TLS, dashboards and the tunnel share one port, so it runs on Render/Koyeb/Heroku/Northflank where raw TCP never reaches the app
- **WebSocket Tunnel** - Carries SOCKS5 over an HTTP upgrade for platforms whose edge terminates TLS; ships with a matching client
- **REALITY** - Xray front door that presents a real website's certificate to an active prober; sessions still land in the SOCKS5 pipeline
- **MTProto** - Telegram's own protocol on its own port, with per-phone secrets managed in the dashboard
- **VLESS Client Management** - Add, disable and revoke REALITY UUIDs from the dashboard; xray is reloaded in place, no restart
- **Client Mode** - Same binary exposes a local SOCKS5 port that relays through the tunnel
- **User Management** - Create, update, delete users with tier-based limits
Expand Down Expand Up @@ -147,6 +148,7 @@ separately on port 443 via xray.
| **HTTP proxy** — `CONNECT` and absolute-form | single port | Browsers and any HTTP client with proxy support |
| **HTTPS** | single port | TLS-wrapped access to any of the above |
| **VLESS + REALITY** | `:443` via xray | Clients on a VPS where the front door must look like a website |
| **MTProto** | `$MTPROTO_BIND` (default `:8443`) | Telegram, which speaks neither SOCKS5 nor HTTP |

Authentication is username/password throughout, taken from `PROXY_USER` and
`PROXY_PASS` for the proxy protocols, and `ADMIN_USER`/`ADMIN_PASS` for the
Expand Down Expand Up @@ -177,6 +179,26 @@ tunnel gets TCP only. And a client that asks to be told an address of `0.0.0.0`
must send to the proxy host it is already connected to, which every mainstream
SOCKS5 client does.

## Telegram (MTProto)

Telegram does not speak SOCKS5 or HTTP, so it needs a front door of its own.
Set `MTPROTO_ENABLED=true`, leave the first secret to be generated, and paste it
into the phone: **Settings → Data and Storage → Proxy**.

Each secret is one phone. The dashboard's **Telegram** panel adds, disables and
revokes them, and a change applies immediately — a revoked phone stops working
at once, with no restart.

This is obfuscated2 (the `dd` secret Telegram apps expect). Two honest limits:

- **The camouflage here is Telegram's, not ours.** The transport looks like
random bytes, which is what every Telegram proxy looks like on the wire. If you
need the port to look like a website to an active prober, that is REALITY's
job — and Telegram does not speak VLESS.
- **Telegram's addresses are compiled in**, which is what every MTProxy does.
If they are unreachable from your network, `MTPROTO_DCS` points them
somewhere else.

## Why the tunnel exists

Render forwards inbound traffic to **one HTTP port** and terminates TLS at its load balancer. Koyeb, Heroku and Northflank behave the same way. A SOCKS5 client opening a raw socket against those hosts never reaches the container, and an in-process reverse proxy cannot help: the edge rejects the bytes before any process sees them.
Expand Down Expand Up @@ -377,6 +399,9 @@ Edit `config.json` or use environment variables (env vars override config file):
| `PROXY_PASS` | changeme | SOCKS5 password, also the HTTP proxy password |
| `AUTH_ENABLED` | true | Enable SOCKS5 and HTTP proxy authentication |
| `UDP_ENABLED` | true | SOCKS5 `UDP ASSOCIATE`. Set false for TCP only |
| `MTPROTO_ENABLED` | false | Telegram front door; generates its first secret on first start |
| `MTPROTO_BIND` | 0.0.0.0:8443 | Telegram port. Avoid 443 when REALITY has it |
| `MTPROTO_DCS` | unset | `dc=address,dc=address`, only when Telegram's own addresses are unreachable |
| `MAX_CONNECTIONS` | 1000 | Max simultaneous connections |
| `ADMIN_USER` | admin | Admin dashboard username |
| `ADMIN_PASS` | changeme | Admin dashboard password |
Expand Down Expand Up @@ -462,6 +487,9 @@ socks5-proxy restore -in socks5-20261017.tar.gz

The archive holds `socks5.db`, `config.json` and, when REALITY is enabled,
`reality-client.json`. **Restoring the same archive keeps every REALITY client
working**, because the keypair travels with it and the managed VLESS UUIDs and
MTProto secrets live in the database; restoring an older or different one
invalidates every client you have imported.
working**, because the keypair travels with it and the managed VLESS UUIDs live
in the database; restoring an older or different one invalidates every client you
have imported.
Expand Down Expand Up @@ -611,6 +639,10 @@ at `/prometheus` with `basic_auth` configured.
| PUT | `/api/users/:id` | Update user |
| DELETE | `/api/users/:id` | Delete user |
| POST | `/api/users/:id/reset` | Reset a user's data usage |
| GET | `/api/mtproto-secrets` | Telegram secrets, with the listener's bind address |
| POST | `/api/mtproto-secrets` | Add a secret; it is generated for you |
| PUT | `/api/mtproto-secrets/:id` | Relabel, or enable/disable, a secret |
| DELETE | `/api/mtproto-secrets/:id` | Revoke a phone, effective immediately |
| GET | `/api/vless-clients` | REALITY client UUIDs, and whether they can be managed |
| POST | `/api/vless-clients` | Add a client; the UUID is generated if omitted |
| PUT | `/api/vless-clients/:id` | Relabel, or enable/disable, a client |
Expand Down
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"max_connections": 1000,
"auth_enabled": true,
"udp_enabled": true,
"mtproto_enabled": false,
"mtproto_bind": "0.0.0.0:8443",
"mtproto_dcs": "",
"username": "changeme",
"password": "changeme",
"whitelist": [],
Expand Down
105 changes: 105 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ func (s *Store) migrate() error {
enabled INTEGER NOT NULL DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`,
// MTProto secrets for the Telegram front door. The stored value is the
// shareable form, dd followed by 32 hex characters, so it can be pasted
// into a phone without editing.
`CREATE TABLE IF NOT EXISTS mtproto_secrets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
secret TEXT UNIQUE NOT NULL,
label TEXT NOT NULL DEFAULT '',
enabled INTEGER NOT NULL DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`,
}

for _, q := range queries {
Expand Down Expand Up @@ -750,6 +760,101 @@ func (s *Store) DeleteVlessClient(id int64) error {
return err
}

// ─── MTProto secrets ────────────────────────────────────────────────────────

type MtprotoSecret struct {
ID int64 `json:"id"`
Secret string `json:"secret"`
Label string `json:"label"`
Enabled bool `json:"enabled"`
CreatedAt string `json:"created_at"`
}

func (s *Store) CreateMtprotoSecret(secret, label string) (*MtprotoSecret, error) {
secret = strings.TrimSpace(strings.ToLower(secret))
if _, err := parseMTProtoSecret(secret); err != nil {
return nil, err
}
s.mu.Lock()
res, err := s.db.Exec("INSERT INTO mtproto_secrets (secret, label) VALUES (?, ?)", secret, label)
s.mu.Unlock()
if err != nil {
return nil, err
}
id, err := res.LastInsertId()
if err != nil {
return nil, err
}
return s.GetMtprotoSecret(id)
}

func (s *Store) GetMtprotoSecret(id int64) (*MtprotoSecret, error) {
s.mu.Lock()
defer s.mu.Unlock()

var sec MtprotoSecret
var enabled int
err := s.db.QueryRow("SELECT id, secret, label, enabled, created_at FROM mtproto_secrets WHERE id=?", id).
Scan(&sec.ID, &sec.Secret, &sec.Label, &enabled, &sec.CreatedAt)
if err != nil {
return nil, err
}
sec.Enabled = enabled != 0
return &sec, nil
}

func (s *Store) ListMtprotoSecrets() ([]MtprotoSecret, error) {
s.mu.Lock()
defer s.mu.Unlock()

rows, err := s.db.Query("SELECT id, secret, label, enabled, created_at FROM mtproto_secrets ORDER BY id")
if err != nil {
return nil, err
}
defer rows.Close()

out := []MtprotoSecret{}
for rows.Next() {
var sec MtprotoSecret
var enabled int
if err := rows.Scan(&sec.ID, &sec.Secret, &sec.Label, &enabled, &sec.CreatedAt); err != nil {
return nil, err
}
sec.Enabled = enabled != 0
out = append(out, sec)
}
return out, rows.Err()
}

func (s *Store) UpdateMtprotoSecret(id int64, label *string, enabled *bool) error {
s.mu.Lock()
defer s.mu.Unlock()

if label != nil {
if _, err := s.db.Exec("UPDATE mtproto_secrets SET label=? WHERE id=?", *label, id); err != nil {
return err
}
}
if enabled != nil {
value := 0
if *enabled {
value = 1
}
if _, err := s.db.Exec("UPDATE mtproto_secrets SET enabled=? WHERE id=?", value, id); err != nil {
return err
}
}
return nil
}

func (s *Store) DeleteMtprotoSecret(id int64) error {
s.mu.Lock()
defer s.mu.Unlock()

_, err := s.db.Exec("DELETE FROM mtproto_secrets WHERE id=?", id)
return err
}

// ─── Connection Tracking ───────────────────────────────────────────────────

func (s *Store) TrackConnectionStart(userID int64, target string) (int64, error) {
Expand Down
14 changes: 11 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
module socks5-enterprise

go 1.24
go 1.24.0

require (
github.com/coder/websocket v1.8.15
github.com/gotd/td v0.140.0
github.com/prometheus/client_golang v1.16.0
modernc.org/sqlite v1.34.5
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-faster/jx v1.2.0 // indirect
github.com/go-faster/xor v1.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gotd/ige v0.2.2 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/sys v0.22.0 // indirect
github.com/segmentio/asm v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.1 // indirect
golang.org/x/sys v0.41.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
modernc.org/libc v1.55.3 // indirect
modernc.org/mathutil v1.6.0 // indirect
Expand Down
Loading
Loading