Skip to content

Commit

Permalink
feat:allow ignore SSL Verification for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt committed Jan 26, 2024
1 parent f2b80a5 commit b083a2f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
14 changes: 9 additions & 5 deletions deepl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deepl

import (
"bytes"
"crypto/tls"
"encoding/json"
"github.com/abadojack/whatlanggo"
"github.com/andybalholm/brotli"
Expand Down Expand Up @@ -158,7 +159,7 @@ func Translate(sourceLanguage, targetLanguage, textToTranslate string, options .
request.Header.Set("Connection", "keep-alive")

client := &http.Client{}
if transport := createHttpProxyTransport(clientOpt); transport != nil {
if transport := createProxyTransportWith(clientOpt); transport != nil {
client.Transport = transport
}
resp, err := client.Do(request)
Expand Down Expand Up @@ -193,14 +194,14 @@ func Translate(sourceLanguage, targetLanguage, textToTranslate string, options .
return jsonRpcResponse, nil
}

func createHttpProxyTransport(clientOpt *deepLClientOption) *http.Transport {
func createProxyTransportWith(clientOpt *deepLClientOption) *http.Transport {
var transport *http.Transport
if clientOpt.httpProxy != "" {
httpProxy, _ := url.Parse(clientOpt.httpProxy)
if httpProxy != nil {
return &http.Transport{Proxy: http.ProxyURL(httpProxy)}
transport = &http.Transport{Proxy: http.ProxyURL(httpProxy)}
}
}

if clientOpt.socket5Proxy != "" {
var auth *proxy.Auth
if clientOpt.socket5ProxyUser != "" || clientOpt.socket5proxyPassword != "" {
Expand All @@ -211,8 +212,11 @@ func createHttpProxyTransport(clientOpt *deepLClientOption) *http.Transport {
dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.Dial(network, address)
}
return &http.Transport{DialContext: dialContext}
transport = &http.Transport{DialContext: dialContext}
}
}
if clientOpt.ignoreSSLVerification && transport != nil {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
return nil
}
26 changes: 22 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
package deepl

type deepLClientOption struct {
httpProxy string
socket5Proxy string
socket5ProxyUser string
socket5proxyPassword string
httpProxy string
socket5Proxy string
socket5ProxyUser string
socket5proxyPassword string
ignoreSSLVerification bool
}

type Option func(option *deepLClientOption)

// WithHttpProxy set http proxy.if both httpProxy and sock5 proxy are set,
// http proxy will be over-wrote by sock5 proxy .example http://127.0.0.1:1080
func WithHttpProxy(proxy string) Option {
return WithHttpProxyEx(proxy, false)
}

// WithHttpProxyEx set http proxy.if both httpProxy and sock5 proxy are set,
// http proxy will be over-wrote by sock5 proxy .example http://http://127.0.0.1:1080
// ignoreSSLVerification: ignore SSL verification
func WithHttpProxyEx(proxy string, ignoreSSLVerification bool) Option {
return func(option *deepLClientOption) {
option.httpProxy = proxy
option.ignoreSSLVerification = ignoreSSLVerification
}
}

// WithSocket5Proxy set socket5Proxy.if both httpProxy and sock5 proxy are set,
// http proxy will be over-wrote by sock5 proxy example 127.0.0.1:1080
func WithSocket5Proxy(socket5Proxy string, userName string, password string) Option {
return WithSocket5ProxyEx(socket5Proxy, userName, password, false)
}

// WithSocket5ProxyEx set socket5Proxy.if both httpProxy and sock5 proxy are set,
// http proxy will be over-wrote by sock5 proxy example 127.0.0.1:1080
// ignoreSSLVerification: ignore SSL verification
func WithSocket5ProxyEx(socket5Proxy string, userName string, password string, ignoreSSLVerification bool) Option {
return func(option *deepLClientOption) {
option.socket5Proxy = socket5Proxy
option.socket5ProxyUser = userName
option.socket5proxyPassword = password
option.ignoreSSLVerification = ignoreSSLVerification
}

}

0 comments on commit b083a2f

Please sign in to comment.