File size: 8,002 Bytes
d669ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package api

import (
	"adams549659584/go-proxy-bingai/common"
	"adams549659584/go-proxy-bingai/common/helper"
	"net/http"
)

const respChallengeHtml = `
<html>

<head>
  <script type="text/javascript">
    (() => {
      const verify = async () => {
        const IG = window.parent._G.IG;
        const convId = window.parent.CIB.manager.conversation.id;
        const rid = window.parent.CIB.manager.conversation.messages[0].requestId;
        const iframeid = new URL(window.location.href).searchParams.get('iframeid');
        const TA = window.parent._G.AT;
        const T = await window.parent.aesEncrypt(TA, IG);
        const params = new URLSearchParams();
        params.append("IG", IG);
        params.append("iframeid", iframeid);
        params.append("convId", convId);
        params.append("rid", rid);
        params.append("T", T);
        const res = await fetch('/challenge/verify?' + params, { credentials: 'include' });
        if (!res.ok) {
          if (res.status === 451) {
            throw decodeURI(window.parent.base58Decode(window.parent._G.TIP)) + '. ' + decodeURI(window.parent.base58Decode(window.parent._G.TIPC));
            window.parent.window.$dialog.warning({
							title: decodeURI(window.parent.base58Decode(window.parent._G.TIP)),
							content: decodeURI(window.parent.base58Decode(window.parent._G.TIPC)),
							maskClosable: false,
							closable: false,
							closeOnEsc: false,
						});
          } else {
            throw await res.text();
          }
        }
        const rjson = await res.text();
        if (rjson.indexOf("cct=") < 0) {
          throw "Bypass 服务似乎没有正常工作!请检查BYPASS_SERVER环境变量是否正确配置。";
        }
      }
      window.addEventListener("load", async () => {
        const successDIV = document.getElementById("success");
        const verifyingDIV = document.getElementById("verifying");
        const failDIV = document.getElementById("fail");
        const errorText = document.getElementById("error-text");
        await new Promise((t) => setTimeout(t, 1000));
        try {
          await verify();
        } catch (error) {
          console.error("verify error", error);
          verifyingDIV.style.display = "none";
          failDIV.style.display = "flex";
          errorText.innerText = error;
          errorText.style.display = "block";
          setTimeout(() => location.reload(), 2500);
          return;
        }
        verifyingDIV.style.display = "none";
        successDIV.style.display = "flex";
        setTimeout(() => window.parent.postMessage("verificationComplete", "*"), 2000);
      });
    })();
  </script>
  <style>
    .verifyContainer {
      display: flex;
      align-items: center;
      gap: 12px;
      width: 300px;
      height: 63px;
      background-color: #fafafa;
      border: 1px solid #e0e0e0;
      padding: 0 10px;
    }

    #success-icon {
      display: flex;
      margin-right: 8px;
      border-radius: 50%;
      box-shadow: inset 0 0 0 #038127;
      width: 30px;
      height: 30px;
      animation: scale-up-center 0.6s cubic-bezier(0.55, 0.085, 0.68, 0.53) both;
      stroke-width: 6px;
      stroke: #f8f8f8;
      stroke-miterlimit: 10;
    }

    #spinner-icon {
      display: flex;
      margin-right: 8px;
      width: 30px;
      height: 30px;
      animation: rotate 5s linear infinite;
    }

    @keyframes rotate {
      100% {
        transform: rotate(360deg);
      }
    }

    @keyframes scale-up-center {
      0% {
        transform: scale(0.01);
      }

      100% {
        transform: scale(1);
      }
    }

    .circle {
      stroke-width: 3px;
      stroke-linecap: round;
      stroke: #000000;
      stroke-dasharray: 0, 100, 0;
      stroke-dashoffset: 200;
      stroke-miterlimit: 1;
      stroke-linejoin: round;
    }

    .success-circle {
      stroke-dashoffset: 0;
      stroke-width: 2;
      stroke-miterlimit: 10;
      stroke: #1d1f20;
      fill: #1d1f20;
    }

    .cb-container {
      display: flex;
      align-items: center;
      margin-left: 11px;
    }

    #fail-icon {
      display: flex;
      margin-right: 8px;
      border-radius: 50%;
      width: 30px;
      height: 30px;
      stroke-width: 6px;
      stroke: #f8f8f8;
      stroke-miterlimit: 10;
      box-shadow: inset 0 0 0 #1d1f20;
      animation: fillfail-offlabel 0.4s ease-in-out 0.4s forwards, scale 0.3s ease-in-out 0.9s both;
    }

    .failure-circle {
      stroke-dasharray: 166;
      stroke-dashoffset: 166;
      stroke-width: 2;
      stroke-miterlimit: 10;
      stroke: #1d1f20;
      fill: none;
      animation: stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards;
    }

    @keyframes stroke {
      100% {
        stroke-dashoffset: 0;
      }
    }

    @keyframes fillfail-offlabel {
      100% {
        box-shadow: inset 0 0 0 30px #1d1f20;
      }
    }
  </style>
</head>

<body>
  <p id="error-text" style="font-size: 10px;margin: 0;border: 0;color: red;display: none;"></p>
  <div id="verifyContainer" class="verifyContainer">
    <div id="success" class="cb-container" style="display: none;align-items: center; visibility: visible;" role="alert">
      <svg id="success-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52" aria-hidden="true">
        <circle class="success-circle" cx="26" cy="26" r="25"></circle>
        <path class="p1" d="m13,26l9.37,9l17.63,-18"></path>
      </svg>
      <span id="success-text">成功!</span>
    </div>
    <div id="verifying" class="cb-container" style="display: flex; visibility: visible;align-items: center;">
      <svg id="spinner-icon" viewBox="0 0 30 30" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
        class="">
        <line x1="15" x2="15" y1="1.5" y2="5.5" class="circle"></line>
        <line x1="24.5459" x2="24.5459" y1="5.45405" y2="10.45405" transform="rotate(45 24.5459 5.45405)"
          class="circle"></line>
        <line x1="28.5" x2="28.5" y1="15" y2="20" transform="rotate(90 28.5 15)" class="circle"></line>
        <line x1="24.5459" x2="24.5459" y1="24.546" y2="29.546" transform="rotate(135 24.5459 24.546)" class="circle">
        </line>
        <line x1="15" x2="15" y1="28.5" y2="33.5" transform="rotate(180 15 28.5)" class="circle"></line>
        <line x1="5.4541" x2="5.4541" y1="24.5459" y2="29.5459" transform="rotate(-135 5.4541 24.5459)" class="circle">
        </line>
        <line x1="1.5" x2="1.5" y1="15" y2="20" transform="rotate(-90 1.5 15)" class="circle"></line>
        <line x1="5.45408" x2="5.45408" y1="5.45404" y2="10.45404" transform="rotate(-45 5.45408 5.45404)"
          class="circle"></line>
      </svg>
      <div id="verifying-msg">
        <span id="verifying-text">人机验证中...</span>
      </div>
    </div>
    <div id="fail" class="cb-container" style="display: none; visibility: visible;" role="alert">
      <svg id="fail-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52" aria-hidden="true">
        <circle class="failure-circle" cx="26" cy="26" r="25" fill="none"></circle>
        <path class="failure-cross" fill="none" d="M14.1 27.2 l24.124.2"></path>
      </svg>
      <div id="failure-msg">
        <span id="fail-text">失败!</span>
        <br><span id="fr-helper"><a href="https://github.com/Harry-zklcdc/go-bingai-pass"
            id="fr-helper-link">遇到问题?</a></span>
      </div>
    </div>
  </div>
</body>

</html>
`

func ChallengeHandler(w http.ResponseWriter, r *http.Request) {
	if !helper.CheckAuth(r) {
		helper.UnauthorizedResult(w)
		return
	}

	if r.Method != "GET" {
		helper.CommonResult(w, http.StatusMethodNotAllowed, "Method Not Allowed", nil)
		return
	}

	if r.URL.Query().Get("h") != "" {
		tmpReq := r.URL.Query()
		tmpReq.Del("h")
		r.URL.RawQuery = tmpReq.Encode()

		common.NewSingleHostReverseProxy(common.BING_URL).ServeHTTP(w, r)
		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.Write([]byte(respChallengeHtml))
}