Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support socks5 proxy #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
TELEGRAM_ID=
TELEGRAM_TOKEN=
# Set it if you need surf the Internet through a proxy; DO NOT prefix with `socks5://` or `socks5h://`
# SOCKS5_PROXY=ip:port
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/launchdarkly/eventsource v1.7.1
github.com/playwright-community/playwright-go v0.2000.1
github.com/spf13/viper v1.14.0
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b h1:tvrvnPFcdzp294diPnrdZZZ8XUt2Tyj7svb7X52iDuU=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down
30 changes: 27 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

Expand All @@ -16,6 +18,7 @@ import (
"github.com/m1guelpf/chatgpt-telegram/src/markdown"
"github.com/m1guelpf/chatgpt-telegram/src/ratelimit"
"github.com/m1guelpf/chatgpt-telegram/src/session"
"golang.org/x/net/proxy"
)

type Conversation struct {
Expand Down Expand Up @@ -49,9 +52,30 @@ func main() {
log.Fatalf("Couldn't load .env file: %v", err)
}

bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
log.Fatalf("Couldn't start Telegram bot: %v", err)
var bot *tgbotapi.BotAPI
socks5Proxy := strings.TrimSpace(os.Getenv("SOCKS5_PROXY"))
if socks5Proxy != "" {
socks5, err := proxy.SOCKS5("tcp", socks5Proxy, nil, proxy.Direct)
if err != nil {
log.Fatalf("Couldn't set socks5 proxy: %v", err)
}
client := &http.Client{
Transport: &http.Transport{Dial: socks5.Dial},
}
bot, err = tgbotapi.NewBotAPIWithClient(
os.Getenv("TELEGRAM_TOKEN"),
tgbotapi.APIEndpoint,
client,
)
if err != nil {
log.Fatalf("Couldn't start Telegram bot with socks5 proxy: %v", err)
}
} else {
var err error
bot, err = tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
log.Fatalf("Couldn't start Telegram bot: %v", err)
}
}

c := make(chan os.Signal, 2)
Expand Down
13 changes: 12 additions & 1 deletion src/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"log"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -73,7 +75,16 @@ func GetSession() (string, error) {
}

func launchBrowser(pw *playwright.Playwright, url string, headless bool) (playwright.BrowserContext, playwright.Page, error) {
browser, err := pw.Chromium.LaunchPersistentContext("/tmp/chatgpt", playwright.BrowserTypeLaunchPersistentContextOptions{Headless: playwright.Bool(headless)})
opts := playwright.BrowserTypeLaunchPersistentContextOptions{
Headless: playwright.Bool(headless),
}
socks5Proxy := strings.TrimSpace(os.Getenv("SOCKS5_PROXY"))
if socks5Proxy != "" {
opts.Proxy = &playwright.BrowserTypeLaunchPersistentContextOptionsProxy{
Server: playwright.String("socks5://" + socks5Proxy),
}
}
browser, err := pw.Chromium.LaunchPersistentContext("/tmp/chatgpt", opts)
if err != nil {
return nil, nil, errors.New(fmt.Sprintf("Couldn't launch headless browser: %v", err))
}
Expand Down