Skip to content

Commit

Permalink
更新UI修复BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
gcslaoli committed Dec 17, 2023
1 parent 0952359 commit b39951e
Show file tree
Hide file tree
Showing 26 changed files with 633 additions and 164 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
group.GET("/g/:gizmoId/c/:convId", GC)
group.GET(("/gpts/mine"), Mine)

// s.BindHandler("/_next/data/*any", Next)
s.BindHandler("/_next/data/*any", ProxyNext)

group.GET("/login", Login)
group.POST("/login", LoginPost)
Expand Down
63 changes: 37 additions & 26 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (

func Index(r *ghttp.Request) {

if r.Session.MustGet("userToken").IsEmpty() {
ctx := r.GetCtx()
if r.Session.MustGet("offical-session").IsEmpty() {
r.Response.RedirectTo("/login")
// r.Response.Writer.Write([]byte("Hello XyHelper"))
return
Expand Down Expand Up @@ -55,19 +56,22 @@ func Index(r *ghttp.Request) {
}`

propsJson := gjson.New(props)
propsJson.Set("query.model", model)
propsJson.Set("buildId", config.BuildId)
if model != "" {
propsJson.Set("query.model", model)
}
propsJson.Set("buildId", config.CacheBuildId)
propsJson.Set("assetPrefix", config.AssetPrefix)

r.Response.WriteTpl("chat-"+config.BuildDate+".html", g.Map{
r.Response.WriteTpl(config.CacheBuildId+"/chat.html", g.Map{
"props": propsJson,
"arkoseUrl": config.ArkoseUrl,
"assetPrefix": config.AssetPrefix,
"envScript": config.GetEnvScript(ctx),
})
}
func C(r *ghttp.Request) {

if r.Session.MustGet("userToken").IsEmpty() {
ctx := r.GetCtx()
if r.Session.MustGet("offical-session").IsEmpty() {
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -111,20 +115,21 @@ func C(r *ghttp.Request) {

propsJson := gjson.New(props)
propsJson.Set("query.default.1", convId)
propsJson.Set("buildId", config.BuildId)
propsJson.Set("buildId", config.CacheBuildId)
propsJson.Set("assetPrefix", config.AssetPrefix)

r.Response.WriteTpl("chat-"+config.BuildDate+".html", g.Map{
r.Response.WriteTpl(config.CacheBuildId+"/chat.html", g.Map{
"props": propsJson,
"arkoseUrl": config.ArkoseUrl,
"assetPrefix": config.AssetPrefix,
"envScript": config.GetEnvScript(ctx),
})
}

// Discovery 发现
func Discovery(r *ghttp.Request) {

if r.Session.MustGet("userToken").IsEmpty() {
if r.Session.MustGet("offical-session").IsEmpty() {
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -163,19 +168,20 @@ func Discovery(r *ghttp.Request) {
}
`
propsJson := gjson.New(props)
propsJson.Set("buildId", config.BuildId)
propsJson.Set("buildId", config.CacheBuildId)

r.Response.WriteTpl("discovery-"+config.BuildDate+".html", g.Map{
r.Response.WriteTpl(config.CacheBuildId+"/discovery.html", g.Map{
"arkoseUrl": config.ArkoseUrl,
"props": propsJson,
"assetPrefix": config.AssetPrefix,
"envScript": config.GetEnvScript(r.GetCtx()),
})
}

// Editor 编辑器
func Editor(r *ghttp.Request) {

if r.Session.MustGet("userToken").IsEmpty() {
if r.Session.MustGet("offical-session").IsEmpty() {
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -215,7 +221,7 @@ func Editor(r *ghttp.Request) {
}
`
propsJson := gjson.New(props)
propsJson.Set("buildId", config.BuildId)
propsJson.Set("buildId", config.CacheBuildId)
propsJson.Set("assetPrefix", config.AssetPrefix)

// if slug != "" {
Expand All @@ -224,17 +230,18 @@ func Editor(r *ghttp.Request) {
// }
// propsJson.Dump()

r.Response.WriteTpl("editor-"+config.BuildDate+".html", g.Map{
r.Response.WriteTpl(config.CacheBuildId+"/editor.html", g.Map{
"arkoseUrl": config.ArkoseUrl,
"props": propsJson,
"assetPrefix": config.AssetPrefix,
"envScript": config.GetEnvScript(r.GetCtx()),
})
}

// Slug 编辑器
func Slug(r *ghttp.Request) {

if r.Session.MustGet("userToken").IsEmpty() {
if r.Session.MustGet("offical-session").IsEmpty() {
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -277,20 +284,21 @@ func Slug(r *ghttp.Request) {
propsJson := gjson.New(props)

propsJson.Set("query.slug", slug)
propsJson.Set("buildId", config.BuildId)
propsJson.Set("buildId", config.CacheBuildId)
propsJson.Set("assetPrefix", config.AssetPrefix)

r.Response.WriteTpl("slug-"+config.BuildDate+".html", g.Map{
r.Response.WriteTpl(config.CacheBuildId+"/slug.html", g.Map{
"arkoseUrl": config.ArkoseUrl,
"props": propsJson,
"assetPrefix": config.AssetPrefix,
"envScript": config.GetEnvScript(r.GetCtx()),
})
}

// G 游戏
func G(r *ghttp.Request) {

if r.Session.MustGet("userToken").IsEmpty() {
if r.Session.MustGet("offical-session").IsEmpty() {
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -333,20 +341,21 @@ func G(r *ghttp.Request) {
`
propsJson := gjson.New(props)
propsJson.Set("query.gizmoId", gizmoId)
propsJson.Set("buildId", config.BuildId)
propsJson.Set("buildId", config.CacheBuildId)
propsJson.Set("assetPrefix", config.AssetPrefix)

r.Response.WriteTpl("g-"+config.BuildDate+".html", g.Map{
r.Response.WriteTpl(config.CacheBuildId+"/g.html", g.Map{
"arkoseUrl": config.ArkoseUrl,
"props": propsJson,
"assetPrefix": config.AssetPrefix,
"envScript": config.GetEnvScript(r.GetCtx()),
})
}

// GC 游戏会话
func GC(r *ghttp.Request) {

if r.Session.MustGet("userToken").IsEmpty() {
if r.Session.MustGet("offical-session").IsEmpty() {
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -393,18 +402,19 @@ func GC(r *ghttp.Request) {
propsJson := gjson.New(props)
propsJson.Set("query.gizmoId", gizmoId)
propsJson.Set("query.convId", convId)
propsJson.Set("buildId", config.BuildId)
propsJson.Set("buildId", config.CacheBuildId)

r.Response.WriteTpl("gc-"+config.BuildDate+".html", g.Map{
r.Response.WriteTpl(config.CacheBuildId+"/gc.html", g.Map{
"arkoseUrl": config.ArkoseUrl,
"props": propsJson,
"assetPrefix": config.AssetPrefix,
"envScript": config.GetEnvScript(r.GetCtx()),
})
}

// Mine 我的
func Mine(r *ghttp.Request) {
if r.Session.MustGet("userToken").IsEmpty() {
if r.Session.MustGet("offical-session").IsEmpty() {
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -446,13 +456,14 @@ func Mine(r *ghttp.Request) {
"scriptLoader": []
}`
propsJson := gjson.New(props)
propsJson.Set("buildId", config.BuildId)
propsJson.Set("buildId", config.CacheBuildId)
propsJson.Set("assetPrefix", config.AssetPrefix)

r.Response.WriteTpl("mine-"+config.BuildDate+".html", g.Map{
r.Response.WriteTpl(config.CacheBuildId+"/mine.html", g.Map{
"arkoseUrl": config.ArkoseUrl,
"props": propsJson,
"assetPrefix": config.AssetPrefix,
"envScript": config.GetEnvScript(r.GetCtx()),
})

}
8 changes: 7 additions & 1 deletion api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Login(r *ghttp.Request) {
func LoginPost(r *ghttp.Request) {
ctx := r.GetCtx()
// 如果用户名为空,就是token登录
g.Log().Debug(ctx, "1232", r.Get("username").String() == "")
// g.Log().Debug(ctx, "1232", r.Get("username").String() == "")
if r.Get("username").String() == "" {
// token登录
userToken := r.Get("password").String()
Expand All @@ -45,6 +45,8 @@ func LoginPost(r *ghttp.Request) {
})
return
}
officialSession := record["officialSession"].String()
r.Session.Set("offical-session", officialSession)
r.Session.Set("userToken", userToken)
r.Response.RedirectTo("/")
return
Expand Down Expand Up @@ -94,6 +96,8 @@ func LoginPost(r *ghttp.Request) {
})
return
}
officialSession := user["officialSession"].String()
r.Session.Set("offical-session", officialSession)
r.Session.Set("userToken", user["userToken"].String())
r.Response.RedirectTo("/")

Expand Down Expand Up @@ -126,6 +130,8 @@ func LoginToken(r *ghttp.Request) {
})
return
}
officialSession := record["officialSession"].String()
r.Session.Set("offical-session", officialSession)
r.Session.Set("userToken", r.Get("access_token").String())
r.Response.RedirectTo("/")
}
78 changes: 36 additions & 42 deletions api/next.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,54 @@
package api

import (
backendapi "chatgpt-mirror-server/backend-api"
"bytes"
"chatgpt-mirror-server/config"
"chatgpt-mirror-server/utility"
"io"
"net/http"
"time"
"net/http/httputil"
"net/url"

"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)

func Next(r *ghttp.Request) {
func ProxyNext(r *ghttp.Request) {
ctx := r.Context()
path := r.RequestURI
userToken := r.Session.MustGet("userToken").String()
if userToken == "" {
r.Response.WriteStatus(401)
return
officalSession := gjson.New(r.Session.MustGet("offical-session"))
refreshCookie := officalSession.Get("refreshToken").String()
u, _ := url.Parse(config.CHATPROXY(ctx))
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
writer.WriteHeader(http.StatusBadGateway)
}
officialAccessToken := backendapi.AccessTokenCache.MustGet(ctx, userToken).String()
if officialAccessToken == "" {
record, _, err := ChatgptSessionService.GetSessionByUserToken(ctx, userToken)
req := r.Request.Clone(ctx)
// 替换path 中的 cacheBuildId 为 buildId
req.URL.Path = gstr.Replace(req.URL.Path, config.CacheBuildId, config.BuildId, 1)
req.Header.Set("Cookie", "__Secure-next-auth.session-token="+refreshCookie)
proxy.ModifyResponse = func(response *http.Response) error {
response.Header.Del("Set-Cookie")
// 读取响应体
body, err := io.ReadAll(response.Body)
if err != nil {
g.Log().Error(ctx, err)
r.Response.WriteStatus(http.StatusUnauthorized)
return
return err
}
if record.IsEmpty() {
g.Log().Error(ctx, "session is empty")
r.Response.WriteStatus(http.StatusUnauthorized)
return
}
officialSession := record["officialSession"].String()
if officialSession == "" {
r.Response.WriteStatus(http.StatusUnauthorized)
return
}
officialAccessToken = utility.AccessTokenFormSession(officialSession)
backendapi.AccessTokenCache.Set(ctx, userToken, officialAccessToken, time.Minute)
}
refreshCookie := gjson.New(officialAccessToken).Get("refreshToken").String()
res, err := g.Client().SetCookie("refreshToken", refreshCookie).Get(ctx, config.CHATPROXY(ctx)+path)
if err != nil {
r.Response.WriteStatus(http.StatusUnauthorized)
return
}
res.RawDump()
resStr := res.ReadAllString()
if res.StatusCode != http.StatusOK {
r.Response.Status = res.StatusCode
r.Response.Write(resStr)
// 修改响应体
bodyJson := gjson.New(body)
bodyJson.Set("pageProps.user.email", "[email protected]")
bodyJson.Set("pageProps.user.name", "admin")
bodyJson.Set("pageProps.user.image", "/avatars.png")
bodyJson.Set("pageProps.user.picture", "/avatars.png")
bodyJson.Set("pageProps.user.id", "user-xadmin")

// 写入响应体
response.Body = io.NopCloser(bytes.NewReader(gconv.Bytes(bodyJson)))
// 重写响应头大小
response.ContentLength = int64(len(body))

return
return nil
}
r.Response.Write(resStr)
proxy.ServeHTTP(r.Response.Writer.RawWriter(), req)

}
5 changes: 4 additions & 1 deletion api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func Session(r *ghttp.Request) {
record, expireTime, err := ChatgptSessionService.GetSessionByUserToken(ctx, userToken.String())
if err != nil {
g.Log().Error(ctx, err)
r.Response.WriteStatus(http.StatusUnauthorized)
r.Session.RemoveAll()
r.Response.WriteJson(g.Map{})
return
}
if record.IsEmpty() {
Expand All @@ -39,12 +40,14 @@ func Session(r *ghttp.Request) {
sessionJson := gjson.New(sessionVar)
if sessionJson.Get("accessToken").String() == "" {
g.Log().Error(ctx, "get session error", sessionJson)
r.Session.RemoveAll()
r.Response.WriteStatus(http.StatusUnauthorized)
return
}
cool.DBM(model.NewChatgptSession()).Where("email=?", record["email"].String()).Update(g.Map{
"officialSession": sessionJson.String(),
})
r.Session.Set("offical-session", sessionJson.String())
backendapi.AccessTokenCache.Set(ctx, userToken.String(), sessionJson.Get("accessToken").String(), 10*24*time.Hour)
sessionJson.Set("accessToken", userToken.String())
sessionJson.Set("user.email", "[email protected]")
Expand Down
Loading

0 comments on commit b39951e

Please sign in to comment.