From feadef044be925389816f25492cc48ad69845b5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BA=A2=E5=B2=A9?= Date: Sat, 26 Aug 2023 20:03:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=86=85=E7=BD=AE=E8=B5=84?= =?UTF-8?q?=E6=BA=90XHR=E4=BB=A3=E7=90=86=E6=B3=A8=E9=87=8A=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cef/browser-config.go | 1 + cef/browser-window.go | 14 +++++++++----- cef/local-load-resource.go | 3 +++ cef/local-load-xhr-proxy.go | 26 +++++++++++++------------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/cef/browser-config.go b/cef/browser-config.go index 7e24f70..0430804 100644 --- a/cef/browser-config.go +++ b/cef/browser-config.go @@ -27,6 +27,7 @@ type browserConfig struct { // LocalResource // 本地资源加载配置 +// 在浏览器进程初始化 func (m *browserConfig) LocalResource(config *LocalLoadConfig) { localLoadResourceInit(config) } diff --git a/cef/browser-window.go b/cef/browser-window.go index 40b6228..cd20d9d 100644 --- a/cef/browser-window.go +++ b/cef/browser-window.go @@ -35,11 +35,15 @@ type windowCurrentProperty struct { // 提供部分窗口属性配置,初始化时生效 // 如需更多属性配置或自定义窗口行为请在`SetBrowserInit`回调函数中使用 type WindowProperty struct { - IsShowModel bool // 是否以模态窗口显示 - WindowInitState types.TWindowState // 窗口 初始状态: 最小化、最大化、全屏, 全屏时隐藏标题栏生效 - WindowType consts.WINDOW_TYPE // 窗口 类型 WINDOW_TYPE default: WT_MAIN_BROWSER - Title string // 窗口 标题 - Url string // 默认打开URL + IsShowModel bool // 是否以模态窗口显示 + WindowInitState types.TWindowState // 窗口 初始状态: 最小化、最大化、全屏, 全屏时隐藏标题栏生效 + WindowType consts.WINDOW_TYPE // 窗口 类型 WINDOW_TYPE default: WT_MAIN_BROWSER + Title string // 窗口 标题 + //Url 默认打开URL, 支持http和LocalLoad(本地资源)加载方式 + // http方式: http://www.example.com, LocalLoad方式: fs://energy/index.html, or file://energy/index.html + // http: 需要web服务支持, LocalLoad: 不需要web服务支持, 如果浏览器调用数据接口需要配置代理 + // LocalLoad: 通过 Config.LocalResource 配置实现 + Url string Icon string // 窗口图标 加载本地图标 local > /app/resources/icon.ico, VF窗口linux使用png IconFS string // 窗口图标 加载emfs内置图标 emfs > resources/icon.ico, VF窗口linux使用png EnableWebkitAppRegion bool // diff --git a/cef/local-load-resource.go b/cef/local-load-resource.go index ca1ea9a..f024a6b 100644 --- a/cef/local-load-resource.go +++ b/cef/local-load-resource.go @@ -77,6 +77,9 @@ func localLoadResourceInit(config *LocalLoadConfig) { localLoadRes.LocalLoadConfig = *config } +// Build +// 构建本地资源加载配置 +// 初始化默认值和默认代理配置 func (m LocalLoadConfig) Build() *LocalLoadConfig { if localLoadRes != nil && !process.Args.IsMain() { return nil diff --git a/cef/local-load-xhr-proxy.go b/cef/local-load-xhr-proxy.go index 1f33cc0..958923c 100644 --- a/cef/local-load-xhr-proxy.go +++ b/cef/local-load-xhr-proxy.go @@ -188,10 +188,11 @@ func (m *XHRProxy) send(scheme string, request *ICefRequest) (*XHRProxyResponse, if err != nil { return nil, err } + // 构造目标地址 targetUrl := new(bytes.Buffer) targetUrl.WriteString(scheme) targetUrl.WriteString(m.IP) - if m.Port > 0 { + if m.Port > 0 { // ip:port targetUrl.WriteString(":") targetUrl.WriteString(strconv.Itoa(m.Port)) } @@ -199,10 +200,10 @@ func (m *XHRProxy) send(scheme string, request *ICefRequest) (*XHRProxyResponse, targetUrl.WriteString(reqUrl.RawQuery) // 读取请求数据 requestData := new(bytes.Buffer) - postData := request.GetPostData() - if postData.IsValid() { - dataCount := int(postData.GetElementCount()) - elements := postData.GetElements() + data := request.GetPostData() + if data.IsValid() { + dataCount := int(data.GetElementCount()) + elements := data.GetElements() for i := 0; i < dataCount; i++ { element := elements.Get(uint32(i)) switch element.GetType() { @@ -220,10 +221,11 @@ func (m *XHRProxy) send(scheme string, request *ICefRequest) (*XHRProxyResponse, } element.Free() } - postData.Free() + data.Free() } - logger.Debug("XHRProxy TargetURL:", targetUrl.String(), "method:", request.Method(), "dataLength:", len(requestData.Bytes())) - httpRequest, err := http.NewRequest(request.Method(), targetUrl.String(), requestData) + tarUrl := targetUrl.String() + logger.Debug("XHRProxy URL:", tarUrl, "method:", request.Method(), "length:", requestData.Len()) + httpRequest, err := http.NewRequest(request.Method(), tarUrl, requestData) if err != nil { return nil, err } @@ -241,11 +243,9 @@ func (m *XHRProxy) send(scheme string, request *ICefRequest) (*XHRProxyResponse, } header.Free() } - - //httpRequest.Header.Add("Host", "energy.yanghy.cn") - //httpRequest.Header.Add("Origin", "https://energy.yanghy.cn") - //httpRequest.Header.Add("Referer", "https://energy.yanghy.cn/") - + //httpRequest.Header.Add("Host", "www.example.com") + //httpRequest.Header.Add("Origin", "https://www.example.com") + //httpRequest.Header.Add("Referer", "https://www.example.com/") httpResponse, err := m.HttpClient.Client.Do(httpRequest) if err != nil { return nil, err -- GitLab