Skip to content

Commit 7a8ef96

Browse files
committed
feat: add azure proxy
1 parent 5974758 commit 7a8ef96

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ AZURE_OPENAI_MODEL_MAPPER: gpt-3.5-turbo=gpt-35-turbo
5656

5757
API Key: This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. You can use either `KEY1` or `KEY2`.
5858

59+
### Proxy
60+
61+
**HTTP Proxy**
62+
63+
Env:
64+
65+
````shell
66+
AZURE_OPENAI_HTTP_PROXY=http://127.0.0.1:1087
67+
````
68+
69+
70+
71+
**Socks5 Proxy**
72+
73+
Env:
74+
75+
````shell
76+
AZURE_OPENAI_SOCKS_PROXY=socks5://127.0.0.1:1080
77+
````
78+
79+
5980

6081
### Use Docker
6182

azure/proxy.go

+9
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ func Proxy(c *gin.Context, requestConverter RequestConverter) {
8585
}
8686

8787
proxy := &httputil.ReverseProxy{Director: director}
88+
transport, err := util.NewProxyFromEnv()
89+
if err != nil {
90+
util.SendError(c, errors.Wrap(err, "get proxy error"))
91+
return
92+
}
93+
if transport != nil {
94+
proxy.Transport = transport
95+
}
96+
8897
proxy.ServeHTTP(c.Writer, c.Request)
8998

9099
// issue: https://github.com/Chanzhaoyu/chatgpt-web/issues/831

constant/env.go

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ const (
44
ENV_AZURE_OPENAI_ENDPOINT = "AZURE_OPENAI_ENDPOINT"
55
ENV_AZURE_OPENAI_API_VER = "AZURE_OPENAI_API_VER"
66
ENV_AZURE_OPENAI_MODEL_MAPPER = "AZURE_OPENAI_MODEL_MAPPER"
7+
8+
ENV_AZURE_OPENAI_HTTP_PROXY = "AZURE_OPENAI_HTTP_PROXY"
9+
ENV_AZURE_OPENAI_SOCKS_PROXY = "AZURE_OPENAI_SOCKS_PROXY"
710
)

util/http_proxy.go

+16
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,29 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7+
"github.com/stulzq/azure-openai-proxy/constant"
78
"net"
89
"net/http"
910
"net/url"
11+
"os"
1012

1113
"golang.org/x/net/proxy"
1214
)
1315

16+
func NewProxyFromEnv() (*http.Transport, error) {
17+
socksProxy := os.Getenv(constant.ENV_AZURE_OPENAI_SOCKS_PROXY)
18+
if socksProxy != "" {
19+
return NewSocksProxy(socksProxy)
20+
}
21+
22+
httpProxy := os.Getenv(constant.ENV_AZURE_OPENAI_HTTP_PROXY)
23+
if httpProxy != "" {
24+
return NewHttpProxy(httpProxy)
25+
}
26+
27+
return nil, nil
28+
}
29+
1430
func NewHttpProxy(proxyAddress string) (*http.Transport, error) {
1531
proxyURL, err := url.Parse(proxyAddress)
1632
if err != nil {

0 commit comments

Comments
 (0)