Spaces:
Sleeping
Sleeping
Update main.go
Browse files
main.go
CHANGED
@@ -1,121 +1,165 @@
|
|
1 |
package main
|
2 |
|
3 |
import (
|
4 |
-
|
5 |
-
|
6 |
-
"strings"
|
7 |
"os"
|
|
|
8 |
)
|
9 |
|
10 |
var strValues string
|
11 |
var strUvalues string
|
12 |
-
var password
|
13 |
|
14 |
-
func init(){
|
|
|
15 |
if password == "" {
|
16 |
-
|
17 |
}
|
18 |
-
|
19 |
|
20 |
func filterCookieValues(cookieValues string, keepKeys []string) string {
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
}
|
34 |
|
35 |
func contains(keys []string, key string) bool {
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
}
|
43 |
|
44 |
func handleSET(w http.ResponseWriter, req *http.Request) {
|
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 |
func handleGET(w http.ResponseWriter, req *http.Request) {
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
result := `{"result": {"cookies": "` + strValues + `"}}`
|
77 |
-
|
78 |
-
|
79 |
}
|
80 |
|
81 |
func handleCLS(w http.ResponseWriter, req *http.Request) {
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
}
|
95 |
|
96 |
func handleHisU(w http.ResponseWriter, req *http.Request) {
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
}
|
103 |
-
|
104 |
-
|
105 |
}
|
106 |
|
107 |
func handleRoot(w http.ResponseWriter, req *http.Request) {
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
}
|
110 |
|
111 |
func main() {
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
121 |
}
|
|
|
1 |
package main
|
2 |
|
3 |
import (
|
4 |
+
"fmt"
|
5 |
+
"net/http"
|
|
|
6 |
"os"
|
7 |
+
"strings"
|
8 |
)
|
9 |
|
10 |
var strValues string
|
11 |
var strUvalues string
|
12 |
+
var password string
|
13 |
|
14 |
+
func init() {
|
15 |
+
password = os.Getenv("PASSWORD")
|
16 |
if password == "" {
|
17 |
+
password = "123456"
|
18 |
}
|
19 |
+
}
|
20 |
|
21 |
func filterCookieValues(cookieValues string, keepKeys []string) string {
|
22 |
+
newCookieValues := ""
|
23 |
+
pairs := strings.Split(cookieValues, ";")
|
24 |
+
for _, pair := range pairs {
|
25 |
+
parts := strings.Split(pair, "=")
|
26 |
+
key := strings.TrimSpace(parts[0])
|
27 |
+
value := strings.Join(parts[1:], "=")
|
28 |
+
if contains(keepKeys, key) {
|
29 |
+
newCookieValues += key + "=" + value + "; "
|
30 |
+
}
|
31 |
+
}
|
32 |
+
newCookieValues = newCookieValues[:len(newCookieValues)-2]
|
33 |
+
return newCookieValues
|
34 |
}
|
35 |
|
36 |
func contains(keys []string, key string) bool {
|
37 |
+
for _, k := range keys {
|
38 |
+
if k == key {
|
39 |
+
return true
|
40 |
+
}
|
41 |
+
}
|
42 |
+
return false
|
43 |
}
|
44 |
|
45 |
func handleSET(w http.ResponseWriter, req *http.Request) {
|
46 |
+
pwd := req.URL.Query().Get("pwd")
|
47 |
+
if pwd == "" || pwd!= password {
|
48 |
+
w.WriteHeader(http.StatusUnauthorized)
|
49 |
+
w.Write([]byte("Invalid password"))
|
50 |
+
return
|
51 |
+
}
|
52 |
+
keepKeys := []string{"_U", "MUID", "KievRPSSecAuth", "cct", "buid", "ak_bmsc", "bm_sv", "_RwBf", "SRCHHPGUSR", "WLS"}
|
53 |
+
keepKeysU := []string{"_U", "WLS"}
|
54 |
+
cookieValues := req.Header.Get("Cookie-Values")
|
55 |
+
setValue := filterCookieValues(cookieValues, keepKeys)
|
56 |
+
getUValue := filterCookieValues(cookieValues, keepKeysU)
|
57 |
+
|
58 |
+
if setValue!= "" {
|
59 |
+
strValues = setValue
|
60 |
+
if getUValue!= "" &&!strings.Contains(strUvalues, getUValue) {
|
61 |
+
strUvalues += ";" + getUValue
|
62 |
+
}
|
63 |
+
w.Write([]byte("Set value successfully"))
|
64 |
+
} else {
|
65 |
+
w.WriteHeader(http.StatusBadRequest)
|
66 |
+
w.Write([]byte("No Cookie-Values in header"))
|
67 |
+
}
|
68 |
}
|
69 |
|
70 |
func handleGET(w http.ResponseWriter, req *http.Request) {
|
71 |
+
pwd := req.URL.Query().Get("pwd")
|
72 |
+
if pwd == "" || pwd!= password {
|
73 |
+
w.WriteHeader(http.StatusUnauthorized)
|
74 |
+
w.Write([]byte("Invalid password"))
|
75 |
+
return
|
76 |
+
}
|
77 |
result := `{"result": {"cookies": "` + strValues + `"}}`
|
78 |
+
w.Header().Set("Content-Type", "application/json")
|
79 |
+
w.Write([]byte(fmt.Sprintf("%v", result)))
|
80 |
}
|
81 |
|
82 |
func handleCLS(w http.ResponseWriter, req *http.Request) {
|
83 |
+
pwd := req.URL.Query().Get("pwd")
|
84 |
+
if pwd == "" || pwd!= password {
|
85 |
+
w.WriteHeader(http.StatusUnauthorized)
|
86 |
+
w.Write([]byte("Invalid password"))
|
87 |
+
return
|
88 |
+
}
|
89 |
+
replacedStr := strings.ReplaceAll(strUvalues, ";", "<br>")
|
90 |
+
|
91 |
+
strValues = ""
|
92 |
+
strUvalues = ""
|
93 |
+
|
94 |
+
w.Write([]byte("Clear value successfully\n" + replacedStr))
|
95 |
}
|
96 |
|
97 |
func handleHisU(w http.ResponseWriter, req *http.Request) {
|
98 |
+
pwd := req.URL.Query().Get("pwd")
|
99 |
+
if pwd == "" || pwd!= password {
|
100 |
+
w.WriteHeader(http.StatusUnauthorized)
|
101 |
+
w.Write([]byte("Invalid password"))
|
102 |
+
return
|
103 |
+
}
|
104 |
+
replacedStr := strings.ReplaceAll(strUvalues, ";", "<br>")
|
105 |
+
w.Write([]byte("Ukey History:\n" + replacedStr))
|
106 |
}
|
107 |
|
108 |
func handleRoot(w http.ResponseWriter, req *http.Request) {
|
109 |
+
w.Write([]byte("Please visit /SET, /GET, or /CLS with?pwd=xxxxxx"))
|
110 |
+
}
|
111 |
+
|
112 |
+
// 新增处理 /q 的函数
|
113 |
+
func handleQ(w http.ResponseWriter, req *http.Request) {
|
114 |
+
url := req.URL.Query().Get("url")
|
115 |
+
if url == "" {
|
116 |
+
w.WriteHeader(http.StatusBadRequest)
|
117 |
+
w.Write([]byte("URL is required"))
|
118 |
+
return
|
119 |
+
}
|
120 |
+
keepKeys := []string{"_U", "MUID", "KievRPSSecAuth", "cct", "buid", "ak_bmsc", "bm_sv", "_RwBf", "SRCHHPGUSR", "WLS"}
|
121 |
+
client := &http.Client{}
|
122 |
+
req, err := http.NewRequest("GET", url, nil)
|
123 |
+
if err!= nil {
|
124 |
+
w.WriteHeader(http.StatusInternalServerError)
|
125 |
+
w.Write([]byte(err.Error()))
|
126 |
+
return
|
127 |
+
}
|
128 |
+
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0")
|
129 |
+
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
|
130 |
+
resp, err := client.Do(req)
|
131 |
+
if err!= nil {
|
132 |
+
w.WriteHeader(http.StatusInternalServerError)
|
133 |
+
w.Write([]byte(err.Error()))
|
134 |
+
return
|
135 |
+
}
|
136 |
+
defer resp.Body.Close()
|
137 |
+
setCookieArray := resp.Header["Set-Cookie"]
|
138 |
+
cookies := ""
|
139 |
+
if setCookieArray!= nil {
|
140 |
+
// 只保留键值对,并替换 & 为 _
|
141 |
+
cookies = setCookieArray[0]
|
142 |
+
pairs := strings.Split(cookies, ";")
|
143 |
+
keyValuePair := pairs[0]
|
144 |
+
keyValuePair = strings.ReplaceAll(keyValuePair, "&", "_")
|
145 |
+
cookies = keyValuePair
|
146 |
+
}
|
147 |
+
setValue := filterCookieValues(cookies, keepKeys)
|
148 |
+
result := `{"result": {"cookies": "` + setValue + `"}}`
|
149 |
+
w.Header().Set("Content-Type", "application/json")
|
150 |
+
w.Write([]byte(fmt.Sprintf("%v", result)))
|
151 |
}
|
152 |
|
153 |
func main() {
|
154 |
+
port := ":7860"
|
155 |
+
http.HandleFunc("/SET", handleSET)
|
156 |
+
http.HandleFunc("/GET", handleGET)
|
157 |
+
http.HandleFunc("/CLS", handleCLS)
|
158 |
+
http.HandleFunc("/HisU", handleHisU)
|
159 |
+
http.HandleFunc("/", handleRoot)
|
160 |
+
// 新增处理 /q 的路由
|
161 |
+
http.HandleFunc("/q", handleQ)
|
162 |
+
|
163 |
+
fmt.Printf("Server is running on port %s\n", port)
|
164 |
+
http.ListenAndServe(port, nil)
|
165 |
}
|