Skip to content

Commit 4169649

Browse files
committed
Add proxy support and instance-specific configurations
Enhanced the application with proxy capabilities, allowing instance-specific and global proxy configurations. Updated browser manager and API to incorporate proxy settings, introduced a new proxy server package, and refined configuration management to support these changes.
1 parent e8c0ea2 commit 4169649

File tree

8 files changed

+420
-7
lines changed

8 files changed

+420
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ docs/
1010
chromium/
1111
cert/ca.crt
1212
cert/ca.key
13-
*.log
13+
*.log
14+
.junie/

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,18 @@ browser:
8888
- "--lang=en-US"
8989
- "--accept-lang=en-US"
9090
user-data-dir: "/anyAIProxyAPI/user-data-dir"
91+
proxy-url: "http://user:[email protected]:8080/" # proxy url for browser, if instance-alone is false, this proxy setting will be ignored
9192
api-port: "2048"
9293
headless: false
94+
instance-alone: true # if true, each instance will have its own browser instance
9395
logfile: "any-ai-proxy.log"
9496
tokens: # Global tokens for API validation (optional)
9597
- "global-token-1"
9698
- "global-token-2"
9799
instance:
98100
- name: "gemini-aistudio"
99101
adapter: "gemini-aistudio"
100-
proxy-url: ""
102+
proxy-url: "socks5://user:[email protected]:1080/" # proxy url for each instance browser, if instance-alone is true, this proxy setting will be used
101103
url: "https://aistudio.google.com/prompts/new_chat"
102104
sniff-url:
103105
- "https://alkalimakersuite-pa.clients6.google.com/$rpc/google.internal.alkali.applications.makersuite.v1.MakerSuiteService/GenerateContent"
@@ -113,7 +115,7 @@ instance:
113115
- "gemini-token-4"
114116
- name: "chatgpt"
115117
adapter: "chatgpt"
116-
proxy-url: ""
118+
proxy-url: "" # proxy url for each instance browser, if this setting is empty, the browser will be directly connected to the internet
117119
url: "https://chatgpt.com/"
118120
sniff-url:
119121
- "https://chatgpt.com/backend-api/conversation"
@@ -146,12 +148,15 @@ instance:
146148
- `fingerprint-chromium-path`: Path to Fingerprint Chromium browser
147149
- `args`: Browser launch arguments
148150
- `user-data-dir`: User data directory
151+
- `proxy-url`: Proxy URL for browser, if `instance-alone` is false, this proxy setting will be ignored
149152
- `api-port`: Port for the API server
150153
- `headless`: Run browser in headless mode
154+
- `instance-alone`: Run each instance will have its own browser instance
151155
- `tokens`: Global tokens for API validation (optional)
152156
- `instance`: Array of AI service instances to manage. Each instance has its own configuration
153157
- `name`: Instance name
154158
- `adapter`: Adapter name (corresponds to different AI services)
159+
- `proxy-url`: Proxy URL for each instance browser, if `instance-alone` is false, this proxy setting will be ignored
155160
- `url`: AI service URL
156161
- `sniff-url`: URL patterns for intercepting responses
157162
- `auth`: Authentication configuration
@@ -304,7 +309,9 @@ For detailed information about the runner system, see [runner.md](runner.md).
304309
│ ├── browser/ # Browser management
305310
│ │ └── chrome/ # ChromeDP manager
306311
│ ├── config/ # Configuration handling
312+
│ ├── html/ # HTML content
307313
│ ├── method/ # Automation methods
314+
│ ├── proxy/ # Proxy server
308315
│ ├── runner/ # Workflow execution engine
309316
│ └── utils/ # Utility functions
310317
├── runner/ # Workflow configurations

internal/api/handlers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/http"
1414
"os"
1515
"path/filepath"
16+
"strconv"
1617
"strings"
1718
"sync"
1819
"time"
@@ -108,6 +109,26 @@ func (h *APIHandlers) TakeScreenshot(c *gin.Context) {
108109
}
109110
}
110111

112+
func (h *APIHandlers) ProxyPac(c *gin.Context) {
113+
c.Status(http.StatusOK)
114+
c.Header("Content-Type", "application/x-ns-proxy-autoconfig")
115+
if h.appConfig.InstanceAlone {
116+
index, ok := c.GetQuery("index")
117+
if ok {
118+
i, err := strconv.ParseUint(index, 10, 32)
119+
if err != nil {
120+
_, _ = c.Writer.Write([]byte(`function FindProxyForURL(url, host) {return "PROXY 127.0.0.1:3120";}`))
121+
return
122+
}
123+
124+
port := 3120 + i
125+
_, _ = c.Writer.Write([]byte(fmt.Sprintf(`function FindProxyForURL(url, host) {return "PROXY 127.0.0.1:%d";}`, port)))
126+
}
127+
} else {
128+
_, _ = c.Writer.Write([]byte(`function FindProxyForURL(url, host) {return "PROXY 127.0.0.1:3120";}`))
129+
}
130+
}
131+
111132
// ChatCompletions handles the /v1/chat/completions endpoint
112133
func (h *APIHandlers) ChatCompletions(c *gin.Context) {
113134
rawJson, err := c.GetRawData()

internal/api/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (s *Server) setupRoutes() {
106106
})
107107

108108
s.engine.GET("/screenshot", s.handlers.TakeScreenshot)
109+
s.engine.GET("/proxy.pac", s.handlers.ProxyPac)
109110
}
110111

111112
// Start starts the API server

internal/browser/chrome/manager.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Manager struct {
2424

2525
// NewManager creates a new Chromedp Manager instance.
2626
// It initializes the allocator context but does not launch the browser yet.
27-
func NewManager(appConfig *config.AppConfig) (*Manager, error) {
27+
func NewManager(appConfig *config.AppConfig, indies ...int) (*Manager, error) {
2828
if appConfig == nil {
2929
return nil, fmt.Errorf("appConfig cannot be nil")
3030
}
@@ -41,9 +41,8 @@ func NewManager(appConfig *config.AppConfig) (*Manager, error) {
4141
chromedp.NoFirstRun,
4242
chromedp.NoDefaultBrowserCheck,
4343
chromedp.Flag("fingerprint", "1000"),
44-
// chromedp.Flag("ignore-certificate-errors", true),
45-
// chromedp.Flag("proxy-pac-url", "http://127.0.0.1:2048/proxy.pac"),
4644

45+
// chromedp.Flag("ignore-certificate-errors", true),
4746
// chromedp.Flag("disable-background-timer-throttling", true),
4847
// chromedp.Flag("disable-backgrounding-occluded-windows", true),
4948
// chromedp.Flag("disable-renderer-backgrounding", true),
@@ -56,6 +55,16 @@ func NewManager(appConfig *config.AppConfig) (*Manager, error) {
5655
// chromedp.Flag("remote-debugging-port", "9222"), // 如果需要远程调试
5756
}
5857

58+
if len(indies) > 0 {
59+
if appConfig.Instance[indies[0]].ProxyURL != "" {
60+
opts = append(opts, chromedp.Flag("proxy-pac-url", fmt.Sprintf("http://127.0.0.1:2048/proxy.pac?index=%d", indies[0])))
61+
}
62+
} else {
63+
if appConfig.Browser.ProxyURL != "" {
64+
opts = append(opts, chromedp.Flag("proxy-pac-url", "http://127.0.0.1:2048/proxy.pac"))
65+
}
66+
}
67+
5968
if execPath != "" {
6069
opts = append(opts, chromedp.ExecPath(execPath))
6170
}

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type AppConfigBrowser struct {
2222
FingerprintChromiumPath string `yaml:"fingerprint-chromium-path"`
2323
Args []string `yaml:"args"`
2424
UserDataDir string `yaml:"user-data-dir,omitempty"`
25+
ProxyURL string `yaml:"proxy-url"`
2526
}
2627
type AppConfigRunner struct {
2728
Init string `yaml:"init"`

0 commit comments

Comments
 (0)