提交 95ecc606 编写于 作者: yanghye's avatar yanghye

修改内置资源XHR代理

上级 a4a217b4
......@@ -27,7 +27,7 @@ type browserConfig struct {
// LocalResource
// 本地资源加载配置
func (m *browserConfig) LocalResource(config LocalLoadConfig) {
func (m *browserConfig) LocalResource(config *LocalLoadConfig) {
localLoadResourceInit(config)
}
......
......@@ -38,6 +38,7 @@ type LocalLoadResource struct {
// LocalLoadConfig
// 本地&内置资源加载配置
// 然后使用 Build() 函数构建对象
type LocalLoadConfig struct {
Enable bool // 设置是否启用本地资源缓存到内存, 默认false: 未启用
EnableCache bool // 启用缓存,将加载过的资源存储到内存中
......@@ -64,14 +65,22 @@ type source struct {
}
// 初始化本地加载配置对象
func localLoadResourceInit(config LocalLoadConfig) {
if localLoadRes != nil || config.FS == nil {
func localLoadResourceInit(config *LocalLoadConfig) {
if config == nil {
return
}
localLoadRes = &LocalLoadResource{
mimeType: make(map[string]string),
sourceCache: make(map[string]*source),
}
localLoadRes.LocalLoadConfig = *config
}
func (m LocalLoadConfig) Build() *LocalLoadConfig {
if localLoadRes != nil {
return nil
}
var config = &m
// domain 必须设置
if config.Domain == "" {
config.Domain = localDomain
......@@ -80,11 +89,13 @@ func localLoadResourceInit(config LocalLoadConfig) {
if config.Scheme != LocalCSFile && config.Scheme != LocalCSFS {
config.Scheme = LocalCSFS
}
// 默认使用 /index.html
if config.Home == "" {
config.Home = "/index.html"
} else if config.Home[0] != '/' {
config.Home = "/" + config.Home
}
// 默认的资源目录配置
if config.FileRoot == "" {
if config.Scheme == LocalCSFS {
config.FileRoot = "resources"
......@@ -93,19 +104,11 @@ func localLoadResourceInit(config LocalLoadConfig) {
config.FileRoot = wd
}
}
//if config.Proxy != nil {
// if proxy, ok := config.Proxy.(*XHRProxy); ok {
// if proxy.Scheme == LpsTcp {
// proxy.tcpListen()
// }
// }
//}
localLoadRes.LocalLoadConfig = config
}
// 代理配置
if config.Proxy == nil {
func (m LocalLoadConfig) SetEnable(v bool) LocalLoadConfig {
m.Enable = v
return m
}
return config
}
func (m *LocalLoadResource) enable() bool {
......
......@@ -13,6 +13,7 @@ package cef
import (
"bytes"
"crypto/tls"
"embed"
"errors"
. "github.com/energye/energy/v2/consts"
"github.com/energye/energy/v2/logger"
......@@ -35,12 +36,26 @@ type IXHRProxy interface {
// XHRProxy
// 数据请求代理
type XHRProxy struct {
Scheme LocalProxyScheme // http/https/tcp default: http
IP string // default: localhost
Port int // default: 80
SSLCert string // /to/path/cert.crt, https时, 该参数为空跳过检查
SSLKey string // /to/path/key.key, https时, 该参数为空跳过检查
SSLCARoot string // /to/path/ca.crt, https时, 该参数为空跳过检查
Scheme LocalProxyScheme // http/https/tcp default: http
IP string // default: localhost
Port int // default: 80
SSL XHRProxySSL
client *httpClient
}
// XHRProxySSL
// https证书配置,如果其中某一配置为空,则跳过ssl检查, 如果证书配置错误则请求失败
type XHRProxySSL struct {
FS *embed.FS // 证书到内置执行文件时需要设置
RootDir string // 根目录, FS不为空时是内置资源目录名,否则是本地硬盘目录
SSLCert string // RootDir/to/path/cert.crt
SSLKey string // RootDir/to/path/key.key
SSLCARoot string // RootDir/to/path/ca.crt
}
type httpClient struct {
tr *http.Transport
client http.Client
}
// XHRProxyResponse
......@@ -52,6 +67,10 @@ type XHRProxyResponse struct {
Header map[string][]string // 响应头
}
func (m *XHRProxySSL) skipVerify() bool {
return m.RootDir == "" || m.SSLCert == "" || m.SSLKey == "" || m.SSLCARoot == ""
}
func (m *XHRProxy) Send(request *ICefRequest) (*XHRProxyResponse, error) {
if m.Scheme == LpsHttp {
return m.sendHttp(request)
......@@ -125,6 +144,7 @@ func (m *XHRProxy) sendHttp(request *ICefRequest) (*XHRProxyResponse, error) {
cli := &http.Client{
Jar: jar,
}
cli.CloseIdleConnections()
httpResponse, err := cli.Do(httpRequest)
if err != nil {
return nil, err
......@@ -164,6 +184,24 @@ func (m *XHRProxy) sendHttps(request *ICefRequest) (*XHRProxyResponse, error) {
//}
//certPool := x509.NewCertPool()
//certPool.AppendCertsFromPEM(crt)
//if m.client == nil {
// m.client = &httpClient{}
// var config *tls.Config
// if m.SSL.skipVerify() {
//
// } else {
//
// }
//
// m.client.tr = &http.Transport{
// TLSClientConfig: &tls.Config{
// InsecureSkipVerify: true,
// //Certificates: []tls.Certificate{cert},
// //RootCAs: certPool,
// },
// }
//}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
......
......@@ -33,7 +33,7 @@ func main() {
Scheme: consts.LpsHttps,
IP: "energy.yanghy.cn",
},
})
}.Build())
if common.IsLinux() && app.IsUIGtk3() {
cef.BrowserWindow.Config.IconFS = "resources/icon.png"
} else {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册