tg-ws-proxy-go/internal/telegram/telegram.go

119 lines
2.5 KiB
Go

// Package telegram provides Telegram Desktop integration utilities.
package telegram
import (
"fmt"
"net/url"
"os/exec"
"runtime"
"strings"
)
// ConfigureProxy opens Telegram's proxy configuration URL.
// Returns true if successful, false otherwise.
func ConfigureProxy(host string, port int, username, password string) bool {
// Build tg:// proxy URL with proper encoding
// Format: tg://proxy?server=host&port=port&user=username&pass=password
params := url.Values{}
params.Set("server", host)
params.Set("port", fmt.Sprintf("%d", port))
if username != "" {
params.Set("user", username)
}
if password != "" {
params.Set("pass", password)
}
// Try both formats
urls := []string{
fmt.Sprintf("tg://proxy?%s", params.Encode()),
fmt.Sprintf("tg://socks?%s", params.Encode()),
}
for _, testURL := range urls {
if openURL(testURL) {
return true
}
}
return false
}
// openURL opens a URL in the default browser/application.
func openURL(url string) bool {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
case "linux":
cmd = "xdg-open"
default:
return false
}
args = append(args, url)
err := exec.Command(cmd, args...).Start()
return err == nil
}
// IsTelegramRunning checks if Telegram Desktop is running.
func IsTelegramRunning() bool {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "tasklist"
args = []string{"/FI", "IMAGENAME eq Telegram.exe"}
case "darwin":
cmd = "pgrep"
args = []string{"-x", "Telegram"}
case "linux":
cmd = "pgrep"
args = []string{"-x", "telegram-desktop"}
default:
return false
}
output, err := exec.Command(cmd, args...).Output()
if err != nil {
return false
}
return len(strings.TrimSpace(string(output))) > 0
}
// GetTelegramPath returns the path to Telegram Desktop executable.
func GetTelegramPath() string {
switch runtime.GOOS {
case "windows":
// Common installation paths
paths := []string{
"%APPDATA%\\Telegram Desktop\\Telegram.exe",
"%LOCALAPPDATA%\\Programs\\Telegram Desktop\\Telegram.exe",
"%PROGRAMFILES%\\Telegram Desktop\\Telegram.exe",
}
for _, path := range paths {
cmd := exec.Command("cmd", "/c", "echo", path)
output, err := cmd.Output()
if err == nil {
return strings.TrimSpace(string(output))
}
}
return ""
case "darwin":
return "/Applications/Telegram.app"
case "linux":
return "telegram-desktop"
default:
return ""
}
}