Skip to content

Commit

Permalink
code:Allow Ignore SSL Verification in proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt committed Jan 26, 2024
1 parent 947e08a commit 81d3370
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
43 changes: 15 additions & 28 deletions internal/communicate/communicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package communicate
import (
"bytes"
"context"
"crypto/tls"
"encoding/binary"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -44,23 +45,14 @@ func init() {
}

type Communicate struct {
text string
voice string
pitch string
rate string
volume string
voiceLanguageRegion string

httpProxy string
socket5Proxy string
socket5ProxyUser string
socket5ProxyPass string
text string

audioDataIndex int
prevIdx int
shiftTime int
finalUtterance map[int]int
op chan map[string]interface{}
opt *communicateOption.CommunicateOption
}

type textEntry struct {
Expand Down Expand Up @@ -112,16 +104,8 @@ func NewCommunicate(text string, opt *communicateOption.CommunicateOption) (*Com
return nil, err
}
return &Communicate{
text: text,
pitch: opt.Pitch,
voice: opt.Voice,
voiceLanguageRegion: opt.VoiceLangRegion,
rate: opt.Rate,
volume: opt.Volume,
httpProxy: opt.HttpProxy,
socket5Proxy: opt.Socket5Proxy,
socket5ProxyUser: opt.Socket5ProxyUser,
socket5ProxyPass: opt.Socket5ProxyPass,
text: text,
opt: opt,
}, nil
}

Expand Down Expand Up @@ -173,7 +157,7 @@ func makeHeaders() http.Header {
func (c *Communicate) stream() (<-chan map[string]interface{}, error) {
texts := splitTextByByteLength(
escape(removeIncompatibleCharacters(c.text)),
calculateMaxMessageSize(c.pitch, c.voice, c.rate, c.volume),
calculateMaxMessageSize(c.opt.Pitch, c.opt.Voice, c.opt.Rate, c.opt.Volume),
)
c.audioDataIndex = len(texts)

Expand Down Expand Up @@ -236,7 +220,7 @@ func (c *Communicate) sendSSML(conn *websocket.Conn, currentTime string, text []
ssmlHeadersAppendExtraData(
generateConnectID(),
currentTime,
makeSsml(string(text), c.pitch, c.voice, c.rate, c.volume),
makeSsml(string(text), c.opt.Pitch, c.opt.Voice, c.opt.Rate, c.opt.Volume),
),
))
}
Expand Down Expand Up @@ -386,18 +370,21 @@ func sumWithMap(idx int, m map[int]int) int {
}

func setupWebSocketProxy(dialer *websocket.Dialer, c *Communicate) {
if c.httpProxy != "" {
proxyUrl, _ := url.Parse(c.httpProxy)
if c.opt.HttpProxy != "" {
proxyUrl, _ := url.Parse(c.opt.HttpProxy)
dialer.Proxy = http.ProxyURL(proxyUrl)
}
if c.socket5Proxy != "" {
auth := &proxy.Auth{User: c.socket5ProxyUser, Password: c.socket5ProxyPass}
socket5ProxyDialer, _ := proxy.SOCKS5("tcp", c.socket5Proxy, auth, proxy.Direct)
if c.opt.Socket5Proxy != "" {
auth := &proxy.Auth{User: c.opt.Socket5ProxyUser, Password: c.opt.Socket5ProxyPass}
socket5ProxyDialer, _ := proxy.SOCKS5("tcp", c.opt.Socket5Proxy, auth, proxy.Direct)
dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
return socket5ProxyDialer.Dial(network, address)
}
dialer.NetDialContext = dialContext
}
if c.opt.IgnoreSSL {
dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
}

func getMetaHubFrom(data []byte) (*metaHub, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/communicateOption/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type CommunicateOption struct {
Socket5Proxy string
Socket5ProxyUser string
Socket5ProxyPass string
IgnoreSSL bool
}

func (c *CommunicateOption) CheckAndApplyDefaultOption() {
Expand Down
31 changes: 22 additions & 9 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package edgetts
import "github.com/lib-x/edgetts/internal/communicateOption"

type option struct {
Voice string
VoiceLangRegion string
Pitch string
Rate string
Volume string
HttpProxy string
Socket5Proxy string
Socket5ProxyUser string
Socket5ProxyPass string
Voice string
VoiceLangRegion string
Pitch string
Rate string
Volume string
HttpProxy string
Socket5Proxy string
Socket5ProxyUser string
Socket5ProxyPass string
IgnoreSSLVerification bool
}

func (o *option) toInternalOption() *communicateOption.CommunicateOption {
Expand All @@ -25,6 +26,7 @@ func (o *option) toInternalOption() *communicateOption.CommunicateOption {
Socket5Proxy: o.Socket5Proxy,
Socket5ProxyUser: o.Socket5ProxyUser,
Socket5ProxyPass: o.Socket5ProxyPass,
IgnoreSSL: o.IgnoreSSLVerification,
}
}

Expand Down Expand Up @@ -65,15 +67,26 @@ func WithVolume(volume string) Option {
}

func WithHttpProxy(proxy string) Option {
return WithHttpProxyEx(proxy, false)
}

func WithHttpProxyEx(proxy string, ignoreSSLVerification bool) Option {
return func(option *option) {
option.HttpProxy = proxy
option.IgnoreSSLVerification = ignoreSSLVerification
}
}

func WithSocket5Proxy(proxy, userName, password string) Option {
return WithSocket5ProxyEx(proxy, userName, password, false)
}

func WithSocket5ProxyEx(proxy, userName, password string, ignoreSSLVerification bool) Option {
return func(option *option) {
option.Socket5Proxy = proxy
option.Socket5ProxyUser = userName
option.Socket5ProxyPass = password
option.IgnoreSSLVerification = ignoreSSLVerification
}

}

0 comments on commit 81d3370

Please sign in to comment.