Skip to main content

Overview

Gogram supports proxy connections for scenarios where direct access to Telegram servers is restricted or when you need to route traffic through a proxy server.

Supported Proxy Types

Gogram supports the following proxy protocols:
  • SOCKS5 - Recommended for most use cases
  • SOCKS4 - Legacy SOCKS protocol
  • HTTP/HTTPS - Standard HTTP proxies
  • MTProto - Telegram’s native proxy protocol
    • FakeTLS
    • Obfuscated
    • Classic

Proxy Interface

All proxies implement the Proxy interface:
type Proxy interface {
    GetHost() string
    GetPort() int
    Type() string
    GetUsername() string
    GetPassword() string
    GetSecret() string
}

Creating Proxies

Using ProxyFromURL

The easiest way to create a proxy is using ProxyFromURL:
import "github.com/amarnathcjd/gogram/telegram"

proxy, err := telegram.ProxyFromURL("socks5://localhost:1080")
proxy, err := telegram.ProxyFromURL("socks5://user:pass@proxy.example.com:1080")
proxy, err := telegram.ProxyFromURL("http://proxy.example.com:8080")
proxy, err := telegram.ProxyFromURL("mtproxy://secret@proxy.example.com:443")

client, err := telegram.NewClient(telegram.ClientConfig{
    AppID:   6,
    AppHash: "your_app_hash",
    Proxy:   proxy,
})

Supported URL Formats

socks5://[user:pass@]host:port
socks5://localhost:1080
socks5://user:password@proxy.example.com:1080

Using Proxy Structs

SOCKS5 Proxy

proxy := &telegram.Socks5Proxy{
    BaseProxy: telegram.BaseProxy{
        Host: "localhost",
        Port: 1080,
    },
    Username: "user",
    Password: "pass",
}

client, err := telegram.NewClient(telegram.ClientConfig{
    AppID:   6,
    AppHash: "your_app_hash",
    Proxy:   proxy,
})

SOCKS4 Proxy

proxy := &telegram.Socks4Proxy{
    BaseProxy: telegram.BaseProxy{
        Host: "localhost",
        Port: 1080,
    },
    UserID: "user",
}

client, err := telegram.NewClient(telegram.ClientConfig{
    AppID:   6,
    AppHash: "your_app_hash",
    Proxy:   proxy,
})

HTTP Proxy

proxy := &telegram.HttpProxy{
    BaseProxy: telegram.BaseProxy{
        Host: "proxy.example.com",
        Port: 8080,
    },
    Username: "user",
    Password: "pass",
}

client, err := telegram.NewClient(telegram.ClientConfig{
    AppID:   6,
    AppHash: "your_app_hash",
    Proxy:   proxy,
})

MTProto Proxy

proxy := &telegram.MTProxy{
    BaseProxy: telegram.BaseProxy{
        Host: "proxy.telegram.org",
        Port: 443,
    },
    Secret: "dd1234567890abcdef",
}

client, err := telegram.NewClient(telegram.ClientConfig{
    AppID:   6,
    AppHash: "your_app_hash",
    Proxy:   proxy,
})
MTProto proxies are specifically designed for Telegram and can be more efficient than SOCKS5 or HTTP proxies.

MTProto Proxy Formats

MTProto proxies support multiple URL formats:

proxy, _ := telegram.ProxyFromURL("mtproxy://secret@proxy.telegram.org:443")
proxy, _ := telegram.ProxyFromURL("tg://proxy?server=proxy.telegram.org&port=443&secret=dd123456")
proxy, _ := telegram.ProxyFromURL("dd123456@proxy.telegram.org:443")

Default Ports

If no port is specified, these defaults are used:
Proxy TypeDefault Port
SOCKS4/SOCKS51080
HTTP8080
HTTPS443
MTProto443

Testing Proxy Connection

Verify your proxy is working:
proxy, err := telegram.ProxyFromURL("socks5://localhost:1080")
if err != nil {
    log.Fatal("Invalid proxy URL:", err)
}

client, err := telegram.NewClient(telegram.ClientConfig{
    AppID:   6,
    AppHash: "your_app_hash",
    Proxy:   proxy,
})

if err != nil {
    log.Fatal("Failed to create client:", err)
}

if err := client.Conn(); err != nil {
    log.Fatal("Failed to connect through proxy:", err)
}

log.Println("Successfully connected through proxy!")

Best Practices

Use Reliable Proxies

Choose stable proxy providers with good uptime

Keep Credentials Secure

Store proxy credentials in environment variables

Test Connectivity

Always test proxy connection before production use

Have Fallbacks

Implement fallback logic if proxy fails

Use MTProto When Possible

MTProto proxies are optimized for Telegram

Monitor Performance

Track proxy latency and connection stability

Next Steps