Skip to content

Commit a53197d

Browse files
committed
fix: 指定userAgent,防止被天地图服务拒绝访问(解决天地图瓦片浏览问题)
TODO 下载天地图瓦片报错 Input buffer contains unsupported image format
1 parent ededd67 commit a53197d

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

packages/main/src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ const createWindow = async () => {
6464
: new URL('../renderer/dist/index.html', 'file://' + __dirname).toString();
6565

6666

67-
await mainWindow.loadURL(pageUrl);
67+
await mainWindow.loadURL(pageUrl, {
68+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
69+
});
6870

6971
ipcHandle(mainWindow);
7072
};

packages/main/src/ipHandle.js

+22-24
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
// 伪造IP
22

33
//浏览器库
4-
const userAgents = [
5-
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
6-
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)',
7-
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
8-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20',
9-
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6',
10-
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER',
11-
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0) ,Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
12-
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
13-
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)',
14-
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
15-
'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre',
16-
'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52',
17-
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
18-
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)',
19-
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6',
20-
'Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6',
21-
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)',
22-
'Opera/9.25 (Windows NT 5.1; U; en), Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
23-
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
24-
];
4+
// const userAgents = [
5+
// 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
6+
// 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)',
7+
// 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
8+
// 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20',
9+
// 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6',
10+
// 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER',
11+
// 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0) ,Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
12+
// 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
13+
// 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)',
14+
// 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
15+
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre',
16+
// 'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52',
17+
// 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
18+
// 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)',
19+
// 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6',
20+
// 'Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6',
21+
// 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)',
22+
// 'Opera/9.25 (Windows NT 5.1; U; en), Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
23+
// 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
24+
// ];
2525
//构造请求头-浏览器
2626
function randomHead() {
27-
return userAgents[
28-
Math.floor(Math.random() * (0 - userAgents.length) + userAgents.length)
29-
];
27+
return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
3028
}
3129
//构造请求头-ip
3230
function returnIp() {

packages/main/src/ipcMain.js

+16-29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fse = require('fs-extra');
55
const fs = require('fs');
66
const sharp = require('sharp');
77
const request = require('superagent');
8+
const path = require('path');
89
import { requestHandle } from './ipHandle';
910

1011
ipcMain.handle('show-dialog', async () => {
@@ -19,28 +20,11 @@ ipcMain.on('ensure-dir', (event, args) => {
1920

2021
// 下载事件
2122
export function ipcHandle(win) {
22-
// electron downloadURL方法
23-
// let currentConfig = null;
24-
// ipcMain.on('save-image', (event, item) => {
25-
// currentConfig = item;
26-
// win.webContents.downloadURL(currentConfig.url);
27-
// });
28-
// win.webContents.session.on('will-download', (event, item) => {
29-
// const savePath = currentConfig.savePath;
30-
// // 设置下载目录,阻止系统dialog的出现
31-
// item.setSavePath(savePath);
32-
// // 下载任务完成
33-
// item.once('done', (e, state) => {
34-
// win.webContents.send('imageDownloadDone', {
35-
// state,
36-
// });
37-
// });
38-
// });
39-
4023

4124
// superagent & sharp 下载图片
4225
ipcMain.on('save-image', (event, args) => {
4326
// sharp(base64Data).composite 反过来试试
27+
const savePath = path.normalize(args.savePath);
4428
const sharpStream = sharp({
4529
failOnError: false,
4630
});
@@ -51,30 +35,31 @@ export function ipcHandle(win) {
5135
promises.push(
5236
sharpStream
5337
.composite([{ input: dataBuffer, gravity: 'centre', blend: 'dest-in' }])
54-
.toFile(args.savePath),
38+
.toFile(savePath),
5539
);
5640
} else {
5741
promises.push(
5842
sharpStream
5943
// .ensureAlpha()
60-
.toFile(args.savePath),
44+
.toFile(savePath),
6145
);
6246
}
6347

6448
// got.stream(args.url).pipe(sharpStream);
49+
// TODO 下载天地图瓦片报错 Input buffer contains unsupported image format
6550
requestHandle(request.get(args.url)).pipe(sharpStream);
6651
Promise.all(promises)
6752
.then(() => {
6853
win.webContents.send('imageDownloadDone', {
6954
state: 'completed',
7055
});
7156
})
72-
.catch(() => {
73-
// console.error('Error processing files, let\'s clean it up', err);
57+
.catch((err) => {
58+
console.error('错误', err);
7459
try {
75-
fs.unlinkSync(args.savePath);
76-
} catch (e) {
77-
console.error(e);
60+
fs.unlinkSync(savePath);
61+
} catch {
62+
// do nothing
7863
}
7964
win.webContents.send('imageDownloadDone', {
8065
state: 'error',
@@ -85,6 +70,7 @@ export function ipcHandle(win) {
8570
// superagent & sharp 下载、合并图片
8671
ipcMain.on('save-image-merge', (event, args) => {
8772
try {
73+
const savePath = path.normalize(args.savePath);
8874
let imgBack;
8975
const imgBuffer = [];
9076
args.layers.forEach(async (item, index) => {
@@ -116,17 +102,18 @@ export function ipcHandle(win) {
116102
}));
117103
}
118104
opration
119-
.toFile(args.savePath)
105+
.toFile(savePath)
120106
.then(() => {
121107
win.webContents.send('imageDownloadDone', {
122108
state: 'completed',
123109
});
124110
})
125-
.catch(() => {
111+
.catch((err) => {
112+
console.error('错误', err);
126113
try {
127-
fs.unlinkSync(args.savePath);
114+
fs.unlinkSync(savePath);
128115
} catch (e) {
129-
console.error(e);
116+
// do nothing
130117
}
131118
});
132119
}

0 commit comments

Comments
 (0)