-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from xxxsen/xxxsen/feature/support_proxy
支持代理
- Loading branch information
Showing
12 changed files
with
197 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,67 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/cookiejar" | ||
"time" | ||
"net/url" | ||
|
||
"github.com/imroc/req/v3" | ||
) | ||
|
||
var defaultInst IHTTPClient | ||
|
||
func init() { | ||
SetDefault(MustNewClient()) //初始化default, 避免无初始化使用直接炸了 | ||
} | ||
|
||
func SetDefault(c IHTTPClient) { | ||
defaultInst = c | ||
} | ||
|
||
func DefaultClient() IHTTPClient { | ||
return defaultInst | ||
} | ||
|
||
type IHTTPClient interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
type Client struct { | ||
type clientWrap struct { | ||
client *http.Client | ||
} | ||
|
||
func NewClient() IHTTPClient { | ||
func NewClient(opts ...Option) (IHTTPClient, error) { | ||
c := applyOpts(opts...) | ||
// 第三方客户端用着不是很习惯, 考虑到我们需要用到的功能都是在transport里面, | ||
// 所以这里直接把第三方客户端的transport提出来用... | ||
reqClient := req.NewClient() | ||
reqClient.ImpersonateChrome() | ||
reqClient.ImpersonateChrome() //fixme: 部分逻辑看着, 有使用到底层的client, 但是, 貌似不使用这部分东西也能正常绕过cf? | ||
t := reqClient.Transport | ||
jar, _ := cookiejar.New(nil) | ||
client := &http.Client{ | ||
Transport: t, | ||
Jar: jar, | ||
Timeout: 10 * time.Second, | ||
Timeout: c.timeout, | ||
} | ||
if len(c.proxy) > 0 { | ||
proxyUrl, err := url.Parse(c.proxy) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse proxy link failed, err:%w", err) | ||
} | ||
t.Proxy = http.ProxyURL(proxyUrl) // set proxy | ||
} | ||
return &clientWrap{client: client}, nil | ||
} | ||
|
||
func MustNewClient(opts ...Option) IHTTPClient { | ||
c, err := NewClient(opts...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return &Client{client: client} | ||
return c | ||
} | ||
|
||
func (c *Client) Do(req *http.Request) (*http.Response, error) { | ||
func (c *clientWrap) Do(req *http.Request) (*http.Response, error) { | ||
return c.client.Do(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package client | ||
|
||
import "time" | ||
|
||
type config struct { | ||
timeout time.Duration | ||
proxy string | ||
} | ||
|
||
type Option func(c *config) | ||
|
||
func WithTimeout(t time.Duration) Option { | ||
return func(c *config) { | ||
c.timeout = t | ||
} | ||
} | ||
|
||
func WithProxy(link string) Option { | ||
return func(c *config) { | ||
c.proxy = link | ||
} | ||
} | ||
|
||
func applyOpts(opts ...Option) *config { | ||
c := &config{} | ||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
if c.timeout == 0 { | ||
c.timeout = 10 * time.Second | ||
} | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package googletranslator | ||
|
||
type config struct { | ||
proxy string | ||
} | ||
|
||
type Option func(c *config) | ||
|
||
func WithProxyUrl(p string) Option { | ||
return func(c *config) { | ||
c.proxy = p | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package googletranslator | ||
|
||
import ( | ||
"context" | ||
"yamdc/translator" | ||
|
||
gt "github.com/Conight/go-googletrans" | ||
) | ||
|
||
type googleTranslator struct { | ||
t *gt.Translator | ||
} | ||
|
||
func New(opts ...Option) (translator.ITranslator, error) { | ||
c := &config{} | ||
for _, opt := range opts { | ||
opt(c) | ||
} | ||
t := gt.New(gt.Config{ | ||
Proxy: c.proxy, | ||
}) | ||
return &googleTranslator{ | ||
t: t, | ||
}, nil | ||
} | ||
|
||
func (t *googleTranslator) Translate(_ context.Context, wording, src, dst string) (string, error) { | ||
res, err := t.t.Translate(wording, src, dst) | ||
if err != nil { | ||
return "", err | ||
} | ||
return res.Text, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package googletranslator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"yamdc/translator" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTranslate(t *testing.T) { | ||
impl, err := New() | ||
assert.NoError(t, err) | ||
translator.SetTranslator(impl) | ||
assert.NoError(t, err) | ||
res, err := translator.Translate(context.Background(), "hello world", "auto", "zh") | ||
assert.NoError(t, err) | ||
t.Logf("result:%s", res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,23 @@ | ||
package translator | ||
|
||
import ( | ||
translator "github.com/Conight/go-googletrans" | ||
"context" | ||
) | ||
|
||
var defaultTranslator *Translator | ||
|
||
func Init() error { | ||
inst, err := New() | ||
if err != nil { | ||
return err | ||
} | ||
defaultTranslator = inst | ||
return nil | ||
} | ||
|
||
func IsTranslatorEnabled() bool { | ||
return defaultTranslator != nil | ||
type ITranslator interface { | ||
Translate(ctx context.Context, wording string, srclang, dstlang string) (string, error) | ||
} | ||
|
||
type Translator struct { | ||
t *translator.Translator | ||
func SetTranslator(t ITranslator) { | ||
defaultTranslator = t | ||
} | ||
|
||
func New() (*Translator, error) { | ||
t := translator.New() | ||
return &Translator{ | ||
t: t, | ||
}, nil | ||
} | ||
var defaultTranslator ITranslator | ||
|
||
func (t *Translator) Translate(origin, src, dst string) (string, error) { | ||
res, err := t.t.Translate(origin, src, dst) | ||
if err != nil { | ||
return "", err | ||
} | ||
return res.Text, nil | ||
func IsTranslatorEnabled() bool { | ||
return defaultTranslator != nil | ||
} | ||
|
||
func Translate(origin, src, dst string) (string, error) { | ||
return defaultTranslator.Translate(origin, src, dst) | ||
func Translate(ctx context.Context, origin, src, dst string) (string, error) { | ||
return defaultTranslator.Translate(ctx, origin, src, dst) | ||
} |
Oops, something went wrong.