Skip to content

Commit

Permalink
[Add] 🎉 Microsoft Login
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-zklcdc committed Mar 10, 2024
1 parent 95238b4 commit 0ec691e
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 34 deletions.
168 changes: 168 additions & 0 deletions api/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package api

import (
"adams549659584/go-proxy-bingai/common/helper"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

msauth "github.com/Harry-zklcdc/ms-auth"
)

type loginReqStruct struct {
Account string `json:"account"`
Password string `json:"password,omitempty"`
Type string `json:"type"`
VerifyCode string `json:"verify_code,omitempty"`
Cookies string `json:"cookies,omitempty"`
Context any `json:"context,omitempty"`
}

type loginRespStruct struct {
Cookies string `json:"cookies"`
Code string `json:"code,omitempty"`
Context any `json:"context,omitempty"`
}

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

switch r.Method {
case "POST":
loginPostHandler(w, r)
case "PUT":
loginPutHandler(w, r)
default:
helper.CommonResult(w, http.StatusMethodNotAllowed, "Method Not Allowed", nil)
}
}

func loginPostHandler(w http.ResponseWriter, r *http.Request) {
resq, err := io.ReadAll(r.Body)
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, "Server Error", err)
return
}

var req loginReqStruct
var respdata loginRespStruct
err = json.Unmarshal(resq, &req)
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, "Server Error", err)
return
}

if req.Account == "" || req.Type == "" {
helper.CommonResult(w, http.StatusBadRequest, "Account or Type Empty", nil)
return
}

switch req.Type {
case msauth.TYPE_PASSWD:
if req.Password == "" {
helper.CommonResult(w, http.StatusBadRequest, "Password Empty for Type of password login", nil)
return
}

auth := msauth.NewAuth(req.Account, req.Password, msauth.TYPE_PASSWD)
cookies, err := auth.Auth()
if err != nil {
helper.CommonResult(w, http.StatusUnauthorized, "login fail", err)
return
}
respdata.Cookies = cookies
helper.CommonResult(w, http.StatusOK, "login success", respdata)
case msauth.TYPE_DEVICE:
auth := msauth.NewAuth(req.Account, "", msauth.TYPE_DEVICE)
cookies, err := auth.Auth()
if err != nil {
if !strings.HasPrefix(err.Error(), "device login need handler to continue") {
helper.CommonResult(w, http.StatusUnauthorized, "login fail", err)
return
}
}
fmt.Println(err.Error())
respdata.Cookies = cookies
respdata.Code = strings.Split(err.Error(), "code: ")[1]
respdata.Context = auth
helper.CommonResult(w, http.StatusCreated, "login need handler to continue", respdata)
case msauth.TYPE_EMAIL:
auth := msauth.NewAuth(req.Account, "", msauth.TYPE_EMAIL)
cookies, err := auth.Auth()
if err != nil {
if err.Error() != "email login need code to continue" {
helper.CommonResult(w, http.StatusUnauthorized, "login fail", err)
return
}
}
respdata.Cookies = cookies
respdata.Context = auth
helper.CommonResult(w, http.StatusCreated, "login need code to continue", respdata)
}
}

func loginPutHandler(w http.ResponseWriter, r *http.Request) {
resq, err := io.ReadAll(r.Body)
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, "Server Error", err)
return
}

var req loginReqStruct
var respdata loginRespStruct
err = json.Unmarshal(resq, &req)
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, "Server Error", err)
return
}

if req.Account == "" || req.Type == "" {
helper.CommonResult(w, http.StatusBadRequest, "Account or Type Empty", nil)
return
}

switch req.Type {
case msauth.TYPE_DEVICE:
d, err := json.Marshal(req.Context)
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, "Server Error", err)
}
auth := msauth.NewAuth(req.Account, "", msauth.TYPE_DEVICE)
auth.SetContext(d)
auth.SetCookie(req.Cookies)
cookies, err := auth.AuthDevice()
if err != nil {
helper.CommonResult(w, http.StatusUnauthorized, "login fail", err)
return
}
respdata.Cookies = cookies
helper.CommonResult(w, http.StatusOK, "login success", respdata)
case msauth.TYPE_EMAIL:
if req.VerifyCode == "" {
helper.CommonResult(w, http.StatusBadRequest, "VerifyCode Empty for Type of email login", nil)
return
}
d, err := json.Marshal(req.Context)
if err != nil {
helper.CommonResult(w, http.StatusInternalServerError, "Server Error", err)
}
auth := msauth.NewAuth(req.Account, "", msauth.TYPE_EMAIL)
auth.SetContext(d)
auth.SetCookie(req.Cookies)
cookies, err := auth.AuthEmail(req.VerifyCode)
if err != nil {
helper.CommonResult(w, http.StatusUnauthorized, "login fail", err)
return
}
respdata.Cookies = cookies
helper.CommonResult(w, http.StatusOK, "login success", respdata)
default:
helper.CommonResult(w, http.StatusBadRequest, "Type Not Support", nil)
return
}
}
22 changes: 12 additions & 10 deletions cloudflare/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,26 +455,28 @@ export default {
return Response.json({ code: 200, message: 'success', data: { isSysCK: false, isAuth: true, info: CUSTOM_OPTIONS.INFO } })
}
let targetUrl;
if (currentUrl.pathname.includes('/sydney')) {
if (currentUrl.pathname.startsWith('/sydney')) {
targetUrl = new URL(SYDNEY_ORIGIN + currentUrl.pathname + currentUrl.search);
} else if (currentUrl.pathname.includes('/edgesvc')) {
} else if (currentUrl.pathname.startsWith('/edgesvc')) {
targetUrl = new URL(EDGE_ORIGIN + currentUrl.pathname + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer/')) {
} else if (currentUrl.pathname.startsWith('/designer/')) {
targetUrl = new URL(DESIGNER_ORIGIN + currentUrl.pathname.replaceAll('/designer/', '/') + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer-cdn/')) {
} else if (currentUrl.pathname.startsWith('/designer-cdn/')) {
targetUrl = new URL(DESIGNER_CDN_ORIGIN + currentUrl.pathname.replaceAll('/designer-cdn/', '/') + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer-app/')) {
} else if (currentUrl.pathname.startsWith('/designer-app/')) {
targetUrl = new URL(DESIGNER_APP_ORIGIN + currentUrl.pathname.replaceAll('/designer-app/', '/') + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer-app-edog/')) {
} else if (currentUrl.pathname.startsWith('/designer-app-edog/')) {
targetUrl = new URL(DESIGNER_APP_EDOG_ORIGIN + currentUrl.pathname.replaceAll('/designer-app-edog/', '/') + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer-document/')) {
} else if (currentUrl.pathname.startsWith('/designer-document/')) {
targetUrl = new URL(DESIGNER_DOCUMENT_ORIGIN + currentUrl.pathname.replaceAll('/designer-document/', '/') + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer-userassets/')) {
} else if (currentUrl.pathname.startsWith('/designer-userassets/')) {
targetUrl = new URL(DESIGNER_USERASSETS_ORIGIN + currentUrl.pathname.replaceAll('/designer-userassets/', '/') + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer-mediasuggestion/')) {
} else if (currentUrl.pathname.startsWith('/designer-mediasuggestion/')) {
targetUrl = new URL(DESIGNER_MEDIASUGGESTION_ORIGIN + currentUrl.pathname.replaceAll('/designer-mediasuggestion/', '/') + currentUrl.search);
} else if (currentUrl.pathname.includes('/designer-rtc/')) {
} else if (currentUrl.pathname.startsWith('/designer-rtc/')) {
targetUrl = new URL(DESIGNER_RTC_ORIGIN + currentUrl.pathname.replaceAll('/designer-rtc/', '/') + currentUrl.search);
} else if (currentUrl.pathname.startsWith('/api/ms/login')) {
targetUrl = new URL(CUSTOM_OPTIONS.BYPASS_SERVER + currentUrl.pathname + currentUrl.search);
} else {
targetUrl = new URL(BING_ORIGIN + currentUrl.pathname + currentUrl.search);
}
Expand Down
Loading

0 comments on commit 0ec691e

Please sign in to comment.