Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhuapiaoyuan committed Dec 17, 2023
2 parents 8a0642e + ab10eab commit 108ca14
Show file tree
Hide file tree
Showing 24 changed files with 585 additions and 129 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.GET("/share/:shareId", Share)
Expand Down
71 changes: 45 additions & 26 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

func Index(r *ghttp.Request) {

if r.Session.MustGet("userToken").IsEmpty() {
ctx := r.GetCtx()
if r.Session.MustGet("offical-session").IsEmpty() {
r.Session.RemoveAll()
r.Response.RedirectTo("/login")
// r.Response.Writer.Write([]byte("Hello XyHelper"))
return
Expand Down Expand Up @@ -55,19 +57,23 @@ 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.Session.RemoveAll()
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -111,20 +117,22 @@ 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.Session.RemoveAll()
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -163,19 +171,21 @@ 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.Session.RemoveAll()
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -215,7 +225,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 +234,19 @@ 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.Session.RemoveAll()
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -277,20 +289,22 @@ 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.Session.RemoveAll()
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -333,20 +347,22 @@ 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.Session.RemoveAll()
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -393,18 +409,20 @@ 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.Session.RemoveAll()
r.Response.RedirectTo("/login")
return
}
Expand Down Expand Up @@ -446,13 +464,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 @@ -56,7 +56,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 @@ -76,6 +76,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 @@ -148,6 +150,8 @@ func LoginPost(r *ghttp.Request) {
}
}

officialSession := user["officialSession"].String()
r.Session.Set("offical-session", officialSession)
r.Session.Set("userToken", user["userToken"].String())
r.Response.RedirectTo("/")

Expand Down Expand Up @@ -180,6 +184,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("/")
}
15 changes: 10 additions & 5 deletions 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 Down Expand Up @@ -49,17 +50,21 @@ func Session(r *ghttp.Request) {

r.Response.WriteJsonExit(sessionJson)
} else {

sessionJson := gjson.New(record["officialSession"].String())
getSessionUrl := config.CHATPROXY(ctx) + "/auth/token"
getSessionUrl := config.CHATPROXY(ctx) + "/getsession"
refreshCookie := sessionJson.Get("refreshCookie").String()

expires := sessionJson.Get("expires").Time()

// 判断是否过期
if expires.Before(time.Now()) {
g.Log().Info(ctx, "session 过期,重新获取")
sessionVar := g.Client().SetHeader("authkey", config.AUTHKEY(ctx)).PostVar(ctx, getSessionUrl, g.Map{
"username": record["email"].String(),
"password": record["password"].String(),
"authkey": config.AUTHKEY(ctx),
"username": record["email"].String(),
"password": record["password"].String(),
"authkey": config.AUTHKEY(ctx),
"refreshCookie": refreshCookie,
})
sessionJson = gjson.New(sessionVar)
if sessionJson.Get("accessToken").String() == "" {
Expand Down
57 changes: 0 additions & 57 deletions arkose/proxyarkose.go

This file was deleted.

1 change: 1 addition & 0 deletions backend-api/backend-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func ProxyAll(r *ghttp.Request) {

ctx := r.GetCtx()
// 获取header中的token Authorization: Bearer xxx 去掉Bearer

userToken := r.Header.Get("Authorization")[7:]
isStream := strings.Contains(r.Header.Get("accept"), "text/event-stream")

Expand Down
6 changes: 2 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ modules:
enable: 1

# 接入网关地址
#CHATPROXY: "https://demo.xyhelper.cn"
#CHATPROXY: "https://gateway.chatgpt.ggss.club"
#CHATPROXY: "http://192.168.0.138:5000"
CHATPROXY: "http://127.0.0.1:7999"
CHATPROXY: "https://demo.xyhelper.cn"
# CHATPROXY: "http://172.17.0.1:7009"
# 接入网关的authkey
AUTHKEY: "xyhelper"
ONLYTOKEN: false
Expand Down
Loading

0 comments on commit 108ca14

Please sign in to comment.