-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathmain.go
480 lines (462 loc) · 16.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package main
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/fatih/color"
"github.com/qtgolang/SunnyNet/SunnyNet"
"github.com/qtgolang/SunnyNet/public"
"wx_channel/pkg/argv"
"wx_channel/pkg/certificate"
"wx_channel/pkg/proxy"
"wx_channel/pkg/util"
)
//go:embed certs/SunnyRoot.cer
var cert_data []byte
//go:embed lib/FileSaver.min.js
var file_saver_js []byte
//go:embed lib/jszip.min.js
var zip_js []byte
//go:embed inject/main.js
var main_js []byte
var Sunny = SunnyNet.NewSunny()
var version = "250215"
var v = "?t=" + version
var port = 2023
// 打印帮助信息
func print_usage() {
fmt.Printf("Usage: wx_video_download [OPTION...]\n")
fmt.Printf("Download WeChat video.\n\n")
fmt.Printf(" --help display this help and exit\n")
fmt.Printf(" -v, --version output version information and exit\n")
fmt.Printf(" -p, --port set proxy server network port\n")
fmt.Printf(" -d, --dev set proxy server network device\n")
os.Exit(0)
}
func main() {
os_env := runtime.GOOS
args := argv.ArgsToMap(os.Args) // 分解参数列表为Map
if _, ok := args["help"]; ok {
print_usage()
} // 存在help则输出帮助信息并退出主程序
if v, ok := args["v"]; ok { // 存在v则输出版本信息并退出主程序
fmt.Printf("v%s %.0s\n", version, v)
os.Exit(0)
}
if v, ok := args["version"]; ok { // 存在version则输出版本信息并退出主程序
fmt.Printf("v%s %.0s\n", version, v)
os.Exit(0)
}
// 设置参数默认值
args["dev"] = argv.ArgsValue(args, "", "d", "dev")
args["port"] = argv.ArgsValue(args, "", "p", "port")
iport, errstr := strconv.Atoi(args["port"])
if errstr != nil {
args["port"] = strconv.Itoa(port) // 用户自定义值解析失败则使用默认端口
} else {
port = iport
}
delete(args, "p") // 删除冗余的参数p
delete(args, "d") // 删除冗余的参数d
signalChan := make(chan os.Signal, 1)
// Notify the signal channel on SIGINT (Ctrl+C) and SIGTERM
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signalChan
fmt.Printf("\n正在关闭服务...%v\n\n", sig)
if os_env == "darwin" {
proxy.DisableProxyInMacOS(proxy.ProxySettings{
Device: args["dev"],
Hostname: "127.0.0.1",
Port: args["port"],
})
}
os.Exit(0)
}()
fmt.Printf("\nv" + version)
fmt.Printf("\n问题反馈 https://github.com/ltaoo/wx_channels_download/issues\n")
existing, err1 := certificate.CheckCertificate("SunnyNet")
if err1 != nil {
fmt.Printf("\nERROR %v\v", err1.Error())
fmt.Printf("按 Ctrl+C 退出...\n")
select {}
}
if !existing {
fmt.Printf("\n\n正在安装证书...\n")
err := certificate.InstallCertificate(cert_data)
time.Sleep(3 * time.Second)
if err != nil {
fmt.Printf("\nERROR %v\n", err.Error())
fmt.Printf("按 Ctrl+C 退出...\n")
select {}
}
}
Sunny.SetPort(port)
Sunny.SetGoCallback(HttpCallback, nil, nil, nil)
err := Sunny.Start().Error
if err != nil {
fmt.Printf("\nERROR %v\n", err.Error())
fmt.Printf("按 Ctrl+C 退出...\n")
select {}
}
proxy_server := fmt.Sprintf("127.0.0.1:%v", port)
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(&url.URL{
Scheme: "http",
Host: proxy_server,
}),
},
}
_, err3 := client.Get("https://sunny.io/")
if err3 == nil {
if os_env == "windows" {
ok := Sunny.StartProcess()
if !ok {
fmt.Printf("\nERROR 启动进程代理失败\n")
fmt.Printf("按 Ctrl+C 退出...\n")
select {}
}
Sunny.ProcessAddName("WeChatAppEx.exe")
}
if os_env == "darwin" {
err := proxy.EnableProxyInMacOS(proxy.ProxySettings{
Device: args["dev"],
Hostname: "127.0.0.1",
Port: args["port"],
})
if err != nil {
fmt.Printf("\nERROR 设置代理失败 %v\n", err.Error())
fmt.Printf("按 Ctrl+C 退出...\n")
select {}
}
}
color.Green(fmt.Sprintf("\n\n服务已正确启动,请打开需要下载的视频号页面进行下载"))
} else {
fmt.Println(fmt.Sprintf("\n\n您还未安装证书,请在浏览器打开 http://%v 并根据说明安装证书\n在安装完成后重新启动此程序即可\n", proxy_server))
}
fmt.Println("\n\n服务正在运行,按 Ctrl+C 退出...")
select {}
}
type ChannelProfile struct {
Title string `json:"title"`
}
type FrontendTip struct {
Msg string `json:"msg"`
}
func HttpCallback(Conn *SunnyNet.HttpConn) {
host := Conn.Request.URL.Hostname()
path := Conn.Request.URL.Path
if Conn.Type == public.HttpSendRequest {
// Conn.Request.Header.Set("Cache-Control", "no-cache")
Conn.Request.Header.Del("Accept-Encoding")
if util.Includes(path, "jszip") {
headers := http.Header{}
headers.Set("Content-Type", "application/javascript")
headers.Set("__debug", "local_file")
Conn.StopRequest(200, zip_js, headers)
return
}
if util.Includes(path, "FileSaver.min") {
headers := http.Header{}
headers.Set("Content-Type", "application/javascript")
headers.Set("__debug", "local_file")
Conn.StopRequest(200, file_saver_js, headers)
return
}
if path == "/__wx_channels_api/profile" {
var data ChannelProfile
body, _ := io.ReadAll(Conn.Request.Body)
_ = Conn.Request.Body.Close()
err := json.Unmarshal(body, &data)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("\n打开了视频\n%s\n", data.Title)
headers := http.Header{}
headers.Set("Content-Type", "application/json")
headers.Set("__debug", "fake_resp")
Conn.StopRequest(200, "{}", headers)
return
}
if path == "/__wx_channels_api/tip" {
var data FrontendTip
body, _ := io.ReadAll(Conn.Request.Body)
_ = Conn.Request.Body.Close()
err := json.Unmarshal(body, &data)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("[FRONTEND]%s\n", data.Msg)
headers := http.Header{}
headers.Set("Content-Type", "application/json")
headers.Set("__debug", "fake_resp")
Conn.StopRequest(200, "{}", headers)
return
}
}
if Conn.Type == public.HttpResponseOK {
content_type := strings.ToLower(Conn.Response.Header.Get("content-type"))
if Conn.Response.Body != nil {
Body, _ := io.ReadAll(Conn.Response.Body)
_ = Conn.Response.Body.Close()
// if content_type == "text/css" {
// Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
// return
// }
// if content_type == "image/svg+xml" {
// Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
// return
// }
// if content_type == "image/jpeg" {
// Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
// return
// }
// if content_type == "image/jpg" {
// Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
// return
// }
// if content_type == "image/png" {
// Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
// return
// }
// if content_type == "image/gif" {
// Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
// return
// }
// if content_type == "video/mp4" {
// Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
// return
// }
// if path == "/web/report-perf" {
// Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
// return
// }
// fmt.Println("HttpCallback", Conn.Type, host, path)
// fmt.Println("Response ContentType is", content_type)
if content_type == "text/html; charset=utf-8" {
// fmt.Println("\n\n检测到页面打开")
// fmt.Println(path)
html := string(Body)
script_reg1 := regexp.MustCompile(`src="([^"]{1,})\.js"`)
html = script_reg1.ReplaceAllString(html, `src="$1.js`+v+`"`)
script_reg2 := regexp.MustCompile(`href="([^"]{1,})\.js"`)
html = script_reg2.ReplaceAllString(html, `href="$1.js`+v+`"`)
Conn.Response.Header.Set("__debug", "append_script")
script2 := ""
// script2 := `<script src="https://debug.funzm.com/target.js"></script>`
// script2 := `<script
// crossorigin="anonymous"
// src="https://pagespy.jikejishu.com/page-spy/index.min.js"
// ></script>
// <script
// crossorigin="anonymous"
// src="https://pagespy.jikejishu.com/plugin/data-harbor/index.min.js"
// ></script>
// <script
// crossorigin="anonymous"
// src="https://pagespy.jikejishu.com/plugin/rrweb/index.min.js"
// ></script>
// <!-- 使用第二步:实例化 PageSpy -->
// <script>
// window.$harbor = new DataHarborPlugin();
// window.$rrweb = new RRWebPlugin();
// [window.$harbor, window.$rrweb].forEach((p) => {
// PageSpy.registerPlugin(p);
// });
// // 实例化的参数都是可选的
// window.$pageSpy = new PageSpy({
// api: "pagespy.jikejishu.com",
// clientOrigin: "https://pagespy.jikejishu.com",
// project: "React 演示",
// autoRender: true,
// title: "PageSpy 🤝 React",
// });
// // 之后即可使用 PageSpy,前往 https://pagespy.jikejishu.com 体验
// </script>`
if host == "channels.weixin.qq.com" && (path == "/web/pages/feed" || path == "/web/pages/home") {
// Conn.Response.Header.Add("wx-channel-video-download", "1")
script := fmt.Sprintf(`<script>%s</script>`, main_js)
html = strings.Replace(html, "<head>", "<head>\n"+script+script2, 1)
fmt.Println("1. 视频详情页 html 注入 js 成功")
Conn.Response.Body = io.NopCloser(bytes.NewBuffer([]byte(html)))
return
}
Conn.Response.Body = io.NopCloser(bytes.NewBuffer([]byte(html)))
return
}
if content_type == "application/javascript" {
content := string(Body)
dep_reg := regexp.MustCompile(`"js/([^"]{1,})\.js"`)
from_reg := regexp.MustCompile(`from {0,1}"([^"]{1,})\.js"`)
lazy_import_reg := regexp.MustCompile(`import\("([^"]{1,})\.js"\)`)
import_reg := regexp.MustCompile(`import {0,1}"([^"]{1,})\.js"`)
content = from_reg.ReplaceAllString(content, `from"$1.js`+v+`"`)
content = dep_reg.ReplaceAllString(content, `"js/$1.js`+v+`"`)
content = lazy_import_reg.ReplaceAllString(content, `import("$1.js`+v+`")`)
content = import_reg.ReplaceAllString(content, `import"$1.js`+v+`"`)
Conn.Response.Header.Set("__debug", "replace_script")
if util.Includes(path, "/t/wx_fed/finder/web/web-finder/res/js/index.publish") {
regexp1 := regexp.MustCompile(`this.sourceBuffer.appendBuffer\(h\),`)
replaceStr1 := `(() => {
if (window.__wx_channels_store__) {
window.__wx_channels_store__.buffers.push(h);
}
})(),this.sourceBuffer.appendBuffer(h),`
if regexp1.MatchString(content) {
fmt.Println("2. 视频播放 js 修改成功")
}
content = regexp1.ReplaceAllString(content, replaceStr1)
regexp2 := regexp.MustCompile(`if\(f.cmd===re.MAIN_THREAD_CMD.AUTO_CUT`)
replaceStr2 := `if(f.cmd==="CUT"){
if (window.__wx_channels_store__) {
console.log("CUT", f, __wx_channels_store__.profile.key);
window.__wx_channels_store__.keys[__wx_channels_store__.profile.key]=f.decryptor_array;
}
}
if(f.cmd===re.MAIN_THREAD_CMD.AUTO_CUT`
content = regexp2.ReplaceAllString(content, replaceStr2)
Conn.Response.Body = io.NopCloser(bytes.NewBuffer([]byte(content)))
return
}
if util.Includes(path, "/t/wx_fed/finder/web/web-finder/res/js/virtual_svg-icons-register") {
regexp1 := regexp.MustCompile(`async finderGetCommentDetail\((\w+)\)\{return(.*?)\}async`)
replaceStr1 := `async finderGetCommentDetail($1) {
var feedResult = await$2;
var data_object = feedResult.data.object;
if (!data_object.objectDesc) {
return feedResult;
}
var media = data_object.objectDesc.media[0];
var profile = media.mediaType !== 4 ? {
type: "picture",
id: data_object.id,
title: data_object.objectDesc.description,
files: data_object.objectDesc.media,
spec: [],
contact: data_object.contact
} : {
type: "media",
duration: media.spec[0].durationMs,
spec: media.spec,
title: data_object.objectDesc.description,
coverUrl: media.coverUrl,
url: media.url+media.urlToken,
size: media.fileSize,
key: media.decodeKey,
id: data_object.id,
nonce_id: data_object.objectNonceId,
nickname: data_object.nickname,
createtime: data_object.createtime,
fileFormat: media.spec.map(o => o.fileFormat),
contact: data_object.contact
};
fetch("/__wx_channels_api/profile", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(profile)
});
if (window.__wx_channels_store__) {
__wx_channels_store__.profile = profile;
window.__wx_channels_store__.profiles.push(profile);
}
return feedResult;
}async`
if regexp1.MatchString(content) {
fmt.Println("3. 视频详情页 js 修改成功")
}
content = regexp1.ReplaceAllString(content, replaceStr1)
regex2 := regexp.MustCompile(`r.default={dialog`)
replaceStr2 := `r.default=window.window.__wx_channels_tip__={dialog`
content = regex2.ReplaceAllString(content, replaceStr2)
regex3 := regexp.MustCompile(`const u=this.storage.getSession`)
replaceStr3 := `return;const u = this.storage.getSession`
content = regex3.ReplaceAllString(content, replaceStr3)
regex4 := regexp.MustCompile(`return this.storage.getSession`)
replaceStr4 := `return null;return this.storage.getSession`
content = regex4.ReplaceAllString(content, replaceStr4)
regex5 := regexp.MustCompile(`this.updateDetail\(o\)`)
replaceStr5 := `(() => {
if (Object.keys(o).length===0){
return;
}
var data_object = o;
var media = data_object.objectDesc.media[0];
var profile = media.mediaType !== 4 ? {
type: "picture",
id: data_object.id,
title: data_object.objectDesc.description,
files: data_object.objectDesc.media,
spec: [],
contact: data_object.contact
} : {
type: "media",
duration: media.spec[0].durationMs,
spec: media.spec,
title: data_object.objectDesc.description,
url: media.url+media.urlToken,
size: media.fileSize,
key: media.decodeKey,
id: data_object.id,
nonce_id: data_object.objectNonceId,
nickname: data_object.nickname,
createtime: data_object.createtime,
fileFormat: media.spec.map(o => o.fileFormat),
contact: data_object.contact
};
if (window.__wx_channels_store__) {
window.__wx_channels_store__.profiles.push(profile);
}
})(),this.updateDetail(o)`
content = regex5.ReplaceAllString(content, replaceStr5)
Conn.Response.Body = io.NopCloser(bytes.NewBuffer([]byte(content)))
return
}
if util.Includes(path, "/t/wx_fed/finder/web/web-finder/res/js/FeedDetail.publish") {
regex := regexp.MustCompile(`,"投诉"\)]`)
replaceStr := `,"投诉"),...(() => {
if (window.__wx_channels_store__ && window.__wx_channels_store__.profile) {
return window.__wx_channels_store__.profile.spec.map((sp) => {
return p("div",{class:"context-item",role:"button",onClick:() => __wx_channels_handle_click_download__(sp)},sp.fileFormat);
});
}
})(),p("div",{class:"context-item",role:"button",onClick:()=>__wx_channels_handle_click_download__()},"原始视频"),p("div",{class:"context-item",role:"button",onClick:__wx_channels_download_cur__},"当前视频"),p("div",{class:"context-item",role:"button",onClick:()=>__wx_channels_handle_download_cover()},"下载封面"),p("div",{class:"context-item",role:"button",onClick:__wx_channels_handle_copy__},"复制链接")]`
content = regex.ReplaceAllString(content, replaceStr)
Conn.Response.Body = io.NopCloser(bytes.NewBuffer([]byte(content)))
return
}
if util.Includes(path, "worker_release") {
regex := regexp.MustCompile(`fmp4Index:p.fmp4Index`)
replaceStr := `decryptor_array:p.decryptor_array,fmp4Index:p.fmp4Index`
content = regex.ReplaceAllString(content, replaceStr)
Conn.Response.Body = io.NopCloser(bytes.NewBuffer([]byte(content)))
return
}
Conn.Response.Body = io.NopCloser(bytes.NewBuffer([]byte(content)))
return
}
Conn.Response.Body = io.NopCloser(bytes.NewBuffer(Body))
}
}
if Conn.Type == public.HttpRequestFail {
//请求错误
// Body := []byte("Hello Sunny Response")
// Conn.Response = &http.Response{
// Body: io.NopCloser(bytes.NewBuffer(Body)),
// }
}
}