jokyone commited on
Commit
8787e42
·
verified ·
1 Parent(s): 4c1612a

Update main.go

Browse files
Files changed (1) hide show
  1. main.go +129 -85
main.go CHANGED
@@ -1,121 +1,165 @@
1
  package main
2
 
3
  import (
4
- "fmt"
5
- "net/http"
6
- "strings"
7
  "os"
 
8
  )
9
 
10
  var strValues string
11
  var strUvalues string
12
- var password = os.Getenv("PASSWORD")
13
 
14
- func init(){
 
15
  if password == "" {
16
- password = "123456"
17
  }
18
- }
19
 
20
  func filterCookieValues(cookieValues string, keepKeys []string) string {
21
- newCookieValues := ""
22
- pairs := strings.Split(cookieValues, ";")
23
- for _, pair := range pairs {
24
- parts := strings.Split(pair, "=")
25
- key := strings.TrimSpace(parts[0])
26
- value := strings.Join(parts[1:], "=")
27
- if contains(keepKeys, key) {
28
- newCookieValues += key + "=" + value + "; "
29
- }
30
- }
31
- newCookieValues = newCookieValues[:len(newCookieValues)-2]
32
- return newCookieValues
33
  }
34
 
35
  func contains(keys []string, key string) bool {
36
- for _, k := range keys {
37
- if k == key {
38
- return true
39
- }
40
- }
41
- return false
42
  }
43
 
44
  func handleSET(w http.ResponseWriter, req *http.Request) {
45
- pwd := req.URL.Query().Get("pwd")
46
- if pwd == "" || pwd != password {
47
- w.WriteHeader(http.StatusUnauthorized)
48
- w.Write([]byte("Invalid password"))
49
- return
50
- }
51
- keepKeys := []string{"_U", "MUID", "KievRPSSecAuth", "cct", "_RwBf", "SRCHHPGUSR", "WLS"}
52
- keepKeysU := []string{"_U", "WLS"}
53
- cookieValues := req.Header.Get("Cookie-Values")
54
- setValue := filterCookieValues(cookieValues, keepKeys)
55
- getUValue := filterCookieValues(cookieValues, keepKeysU)
56
-
57
- if setValue != "" {
58
- strValues = setValue
59
- if getUValue != "" && !strings.Contains(strUvalues, getUValue) {
60
- strUvalues += ";" + getUValue
61
- }
62
- w.Write([]byte("Set value successfully"))
63
- } else {
64
- w.WriteHeader(http.StatusBadRequest)
65
- w.Write([]byte("No Cookie-Values in header"))
66
- }
67
  }
68
 
69
  func handleGET(w http.ResponseWriter, req *http.Request) {
70
- pwd := req.URL.Query().Get("pwd")
71
- if pwd == "" || pwd != password {
72
- w.WriteHeader(http.StatusUnauthorized)
73
- w.Write([]byte("Invalid password"))
74
- return
75
- }
76
  result := `{"result": {"cookies": "` + strValues + `"}}`
77
- w.Header().Set("Content-Type", "application/json")
78
- w.Write([]byte(fmt.Sprintf("%v", result)))
79
  }
80
 
81
  func handleCLS(w http.ResponseWriter, req *http.Request) {
82
- pwd := req.URL.Query().Get("pwd")
83
- if pwd == "" || pwd != password {
84
- w.WriteHeader(http.StatusUnauthorized)
85
- w.Write([]byte("Invalid password"))
86
- return
87
- }
88
- replacedStr := strings.ReplaceAll(strUvalues, ";", "<br>")
89
-
90
- strValues = ""
91
- strUvalues = ""
92
-
93
- w.Write([]byte("Clear value successfully\n" + replacedStr))
94
  }
95
 
96
  func handleHisU(w http.ResponseWriter, req *http.Request) {
97
- pwd := req.URL.Query().Get("pwd")
98
- if pwd == "" || pwd != password {
99
- w.WriteHeader(http.StatusUnauthorized)
100
- w.Write([]byte("Invalid password"))
101
- return
102
- }
103
- replacedStr := strings.ReplaceAll(strUvalues, ";", "<br>")
104
- w.Write([]byte("Ukey History:\n" + replacedStr))
105
  }
106
 
107
  func handleRoot(w http.ResponseWriter, req *http.Request) {
108
- w.Write([]byte("Please visit /SET, /GET, or /CLS with ?pwd=xxxxxx"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  }
110
 
111
  func main() {
112
- port := ":7860"
113
- http.HandleFunc("/SET", handleSET)
114
- http.HandleFunc("/GET", handleGET)
115
- http.HandleFunc("/CLS", handleCLS)
116
- http.HandleFunc("/HisU", handleHisU)
117
- http.HandleFunc("/", handleRoot)
118
-
119
- fmt.Printf("Server is running on port %s\n", port)
120
- http.ListenAndServe(port, nil)
 
 
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
  }