Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
61f55674
Commit
61f55674
authored
Jan 10, 2026
by
yangjianbo
Browse files
Merge branch 'main' of
https://github.com/mt21625457/aicodex2api
parents
eeb1282f
7d1fe818
Changes
103
Hide whitespace changes
Inline
Side-by-side
backend/internal/handler/admin/account_handler.go
View file @
61f55674
...
...
@@ -116,6 +116,7 @@ type BulkUpdateAccountsRequest struct {
Concurrency
*
int
`json:"concurrency"`
Priority
*
int
`json:"priority"`
Status
string
`json:"status" binding:"omitempty,oneof=active inactive error"`
Schedulable
*
bool
`json:"schedulable"`
GroupIDs
*
[]
int64
`json:"group_ids"`
Credentials
map
[
string
]
any
`json:"credentials"`
Extra
map
[
string
]
any
`json:"extra"`
...
...
@@ -136,6 +137,11 @@ func (h *AccountHandler) List(c *gin.Context) {
accountType
:=
c
.
Query
(
"type"
)
status
:=
c
.
Query
(
"status"
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
accounts
,
total
,
err
:=
h
.
adminService
.
ListAccounts
(
c
.
Request
.
Context
(),
page
,
pageSize
,
platform
,
accountType
,
status
,
search
)
if
err
!=
nil
{
...
...
@@ -655,6 +661,7 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
req
.
Concurrency
!=
nil
||
req
.
Priority
!=
nil
||
req
.
Status
!=
""
||
req
.
Schedulable
!=
nil
||
req
.
GroupIDs
!=
nil
||
len
(
req
.
Credentials
)
>
0
||
len
(
req
.
Extra
)
>
0
...
...
@@ -671,6 +678,7 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
Concurrency
:
req
.
Concurrency
,
Priority
:
req
.
Priority
,
Status
:
req
.
Status
,
Schedulable
:
req
.
Schedulable
,
GroupIDs
:
req
.
GroupIDs
,
Credentials
:
req
.
Credentials
,
Extra
:
req
.
Extra
,
...
...
backend/internal/handler/admin/group_handler.go
View file @
61f55674
...
...
@@ -2,6 +2,7 @@ package admin
import
(
"strconv"
"strings"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
...
...
@@ -67,6 +68,12 @@ func (h *GroupHandler) List(c *gin.Context) {
page
,
pageSize
:=
response
.
ParsePagination
(
c
)
platform
:=
c
.
Query
(
"platform"
)
status
:=
c
.
Query
(
"status"
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
isExclusiveStr
:=
c
.
Query
(
"is_exclusive"
)
var
isExclusive
*
bool
...
...
@@ -75,7 +82,7 @@ func (h *GroupHandler) List(c *gin.Context) {
isExclusive
=
&
val
}
groups
,
total
,
err
:=
h
.
adminService
.
ListGroups
(
c
.
Request
.
Context
(),
page
,
pageSize
,
platform
,
status
,
isExclusive
)
groups
,
total
,
err
:=
h
.
adminService
.
ListGroups
(
c
.
Request
.
Context
(),
page
,
pageSize
,
platform
,
status
,
search
,
isExclusive
)
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
err
)
return
...
...
backend/internal/handler/admin/proxy_handler.go
View file @
61f55674
...
...
@@ -51,6 +51,11 @@ func (h *ProxyHandler) List(c *gin.Context) {
protocol
:=
c
.
Query
(
"protocol"
)
status
:=
c
.
Query
(
"status"
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
proxies
,
total
,
err
:=
h
.
adminService
.
ListProxiesWithAccountCount
(
c
.
Request
.
Context
(),
page
,
pageSize
,
protocol
,
status
,
search
)
if
err
!=
nil
{
...
...
backend/internal/handler/admin/redeem_handler.go
View file @
61f55674
...
...
@@ -5,6 +5,7 @@ import (
"encoding/csv"
"fmt"
"strconv"
"strings"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
...
...
@@ -41,6 +42,11 @@ func (h *RedeemHandler) List(c *gin.Context) {
codeType
:=
c
.
Query
(
"type"
)
status
:=
c
.
Query
(
"status"
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
codes
,
total
,
err
:=
h
.
adminService
.
ListRedeemCodes
(
c
.
Request
.
Context
(),
page
,
pageSize
,
codeType
,
status
,
search
)
if
err
!=
nil
{
...
...
backend/internal/handler/admin/setting_handler.go
View file @
61f55674
...
...
@@ -2,8 +2,10 @@ package admin
import
(
"log"
"strings"
"time"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
...
...
@@ -38,33 +40,37 @@ func (h *SettingHandler) GetSettings(c *gin.Context) {
}
response
.
Success
(
c
,
dto
.
SystemSettings
{
RegistrationEnabled
:
settings
.
RegistrationEnabled
,
EmailVerifyEnabled
:
settings
.
EmailVerifyEnabled
,
SMTPHost
:
settings
.
SMTPHost
,
SMTPPort
:
settings
.
SMTPPort
,
SMTPUsername
:
settings
.
SMTPUsername
,
SMTPPasswordConfigured
:
settings
.
SMTPPasswordConfigured
,
SMTPFrom
:
settings
.
SMTPFrom
,
SMTPFromName
:
settings
.
SMTPFromName
,
SMTPUseTLS
:
settings
.
SMTPUseTLS
,
TurnstileEnabled
:
settings
.
TurnstileEnabled
,
TurnstileSiteKey
:
settings
.
TurnstileSiteKey
,
TurnstileSecretKeyConfigured
:
settings
.
TurnstileSecretKeyConfigured
,
SiteName
:
settings
.
SiteName
,
SiteLogo
:
settings
.
SiteLogo
,
SiteSubtitle
:
settings
.
SiteSubtitle
,
APIBaseURL
:
settings
.
APIBaseURL
,
ContactInfo
:
settings
.
ContactInfo
,
DocURL
:
settings
.
DocURL
,
DefaultConcurrency
:
settings
.
DefaultConcurrency
,
DefaultBalance
:
settings
.
DefaultBalance
,
EnableModelFallback
:
settings
.
EnableModelFallback
,
FallbackModelAnthropic
:
settings
.
FallbackModelAnthropic
,
FallbackModelOpenAI
:
settings
.
FallbackModelOpenAI
,
FallbackModelGemini
:
settings
.
FallbackModelGemini
,
FallbackModelAntigravity
:
settings
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
settings
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
settings
.
IdentityPatchPrompt
,
RegistrationEnabled
:
settings
.
RegistrationEnabled
,
EmailVerifyEnabled
:
settings
.
EmailVerifyEnabled
,
SMTPHost
:
settings
.
SMTPHost
,
SMTPPort
:
settings
.
SMTPPort
,
SMTPUsername
:
settings
.
SMTPUsername
,
SMTPPasswordConfigured
:
settings
.
SMTPPasswordConfigured
,
SMTPFrom
:
settings
.
SMTPFrom
,
SMTPFromName
:
settings
.
SMTPFromName
,
SMTPUseTLS
:
settings
.
SMTPUseTLS
,
TurnstileEnabled
:
settings
.
TurnstileEnabled
,
TurnstileSiteKey
:
settings
.
TurnstileSiteKey
,
TurnstileSecretKeyConfigured
:
settings
.
TurnstileSecretKeyConfigured
,
LinuxDoConnectEnabled
:
settings
.
LinuxDoConnectEnabled
,
LinuxDoConnectClientID
:
settings
.
LinuxDoConnectClientID
,
LinuxDoConnectClientSecretConfigured
:
settings
.
LinuxDoConnectClientSecretConfigured
,
LinuxDoConnectRedirectURL
:
settings
.
LinuxDoConnectRedirectURL
,
SiteName
:
settings
.
SiteName
,
SiteLogo
:
settings
.
SiteLogo
,
SiteSubtitle
:
settings
.
SiteSubtitle
,
APIBaseURL
:
settings
.
APIBaseURL
,
ContactInfo
:
settings
.
ContactInfo
,
DocURL
:
settings
.
DocURL
,
DefaultConcurrency
:
settings
.
DefaultConcurrency
,
DefaultBalance
:
settings
.
DefaultBalance
,
EnableModelFallback
:
settings
.
EnableModelFallback
,
FallbackModelAnthropic
:
settings
.
FallbackModelAnthropic
,
FallbackModelOpenAI
:
settings
.
FallbackModelOpenAI
,
FallbackModelGemini
:
settings
.
FallbackModelGemini
,
FallbackModelAntigravity
:
settings
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
settings
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
settings
.
IdentityPatchPrompt
,
})
}
...
...
@@ -88,6 +94,12 @@ type UpdateSettingsRequest struct {
TurnstileSiteKey
string
`json:"turnstile_site_key"`
TurnstileSecretKey
string
`json:"turnstile_secret_key"`
// LinuxDo Connect OAuth 登录(终端用户 SSO)
LinuxDoConnectEnabled
bool
`json:"linuxdo_connect_enabled"`
LinuxDoConnectClientID
string
`json:"linuxdo_connect_client_id"`
LinuxDoConnectClientSecret
string
`json:"linuxdo_connect_client_secret"`
LinuxDoConnectRedirectURL
string
`json:"linuxdo_connect_redirect_url"`
// OEM设置
SiteName
string
`json:"site_name"`
SiteLogo
string
`json:"site_logo"`
...
...
@@ -165,34 +177,67 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
}
}
// LinuxDo Connect 参数验证
if
req
.
LinuxDoConnectEnabled
{
req
.
LinuxDoConnectClientID
=
strings
.
TrimSpace
(
req
.
LinuxDoConnectClientID
)
req
.
LinuxDoConnectClientSecret
=
strings
.
TrimSpace
(
req
.
LinuxDoConnectClientSecret
)
req
.
LinuxDoConnectRedirectURL
=
strings
.
TrimSpace
(
req
.
LinuxDoConnectRedirectURL
)
if
req
.
LinuxDoConnectClientID
==
""
{
response
.
BadRequest
(
c
,
"LinuxDo Client ID is required when enabled"
)
return
}
if
req
.
LinuxDoConnectRedirectURL
==
""
{
response
.
BadRequest
(
c
,
"LinuxDo Redirect URL is required when enabled"
)
return
}
if
err
:=
config
.
ValidateAbsoluteHTTPURL
(
req
.
LinuxDoConnectRedirectURL
);
err
!=
nil
{
response
.
BadRequest
(
c
,
"LinuxDo Redirect URL must be an absolute http(s) URL"
)
return
}
// 如果未提供 client_secret,则保留现有值(如有)。
if
req
.
LinuxDoConnectClientSecret
==
""
{
if
previousSettings
.
LinuxDoConnectClientSecret
==
""
{
response
.
BadRequest
(
c
,
"LinuxDo Client Secret is required when enabled"
)
return
}
req
.
LinuxDoConnectClientSecret
=
previousSettings
.
LinuxDoConnectClientSecret
}
}
settings
:=
&
service
.
SystemSettings
{
RegistrationEnabled
:
req
.
RegistrationEnabled
,
EmailVerifyEnabled
:
req
.
EmailVerifyEnabled
,
SMTPHost
:
req
.
SMTPHost
,
SMTPPort
:
req
.
SMTPPort
,
SMTPUsername
:
req
.
SMTPUsername
,
SMTPPassword
:
req
.
SMTPPassword
,
SMTPFrom
:
req
.
SMTPFrom
,
SMTPFromName
:
req
.
SMTPFromName
,
SMTPUseTLS
:
req
.
SMTPUseTLS
,
TurnstileEnabled
:
req
.
TurnstileEnabled
,
TurnstileSiteKey
:
req
.
TurnstileSiteKey
,
TurnstileSecretKey
:
req
.
TurnstileSecretKey
,
SiteName
:
req
.
SiteName
,
SiteLogo
:
req
.
SiteLogo
,
SiteSubtitle
:
req
.
SiteSubtitle
,
APIBaseURL
:
req
.
APIBaseURL
,
ContactInfo
:
req
.
ContactInfo
,
DocURL
:
req
.
DocURL
,
DefaultConcurrency
:
req
.
DefaultConcurrency
,
DefaultBalance
:
req
.
DefaultBalance
,
EnableModelFallback
:
req
.
EnableModelFallback
,
FallbackModelAnthropic
:
req
.
FallbackModelAnthropic
,
FallbackModelOpenAI
:
req
.
FallbackModelOpenAI
,
FallbackModelGemini
:
req
.
FallbackModelGemini
,
FallbackModelAntigravity
:
req
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
req
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
req
.
IdentityPatchPrompt
,
RegistrationEnabled
:
req
.
RegistrationEnabled
,
EmailVerifyEnabled
:
req
.
EmailVerifyEnabled
,
SMTPHost
:
req
.
SMTPHost
,
SMTPPort
:
req
.
SMTPPort
,
SMTPUsername
:
req
.
SMTPUsername
,
SMTPPassword
:
req
.
SMTPPassword
,
SMTPFrom
:
req
.
SMTPFrom
,
SMTPFromName
:
req
.
SMTPFromName
,
SMTPUseTLS
:
req
.
SMTPUseTLS
,
TurnstileEnabled
:
req
.
TurnstileEnabled
,
TurnstileSiteKey
:
req
.
TurnstileSiteKey
,
TurnstileSecretKey
:
req
.
TurnstileSecretKey
,
LinuxDoConnectEnabled
:
req
.
LinuxDoConnectEnabled
,
LinuxDoConnectClientID
:
req
.
LinuxDoConnectClientID
,
LinuxDoConnectClientSecret
:
req
.
LinuxDoConnectClientSecret
,
LinuxDoConnectRedirectURL
:
req
.
LinuxDoConnectRedirectURL
,
SiteName
:
req
.
SiteName
,
SiteLogo
:
req
.
SiteLogo
,
SiteSubtitle
:
req
.
SiteSubtitle
,
APIBaseURL
:
req
.
APIBaseURL
,
ContactInfo
:
req
.
ContactInfo
,
DocURL
:
req
.
DocURL
,
DefaultConcurrency
:
req
.
DefaultConcurrency
,
DefaultBalance
:
req
.
DefaultBalance
,
EnableModelFallback
:
req
.
EnableModelFallback
,
FallbackModelAnthropic
:
req
.
FallbackModelAnthropic
,
FallbackModelOpenAI
:
req
.
FallbackModelOpenAI
,
FallbackModelGemini
:
req
.
FallbackModelGemini
,
FallbackModelAntigravity
:
req
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
req
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
req
.
IdentityPatchPrompt
,
}
if
err
:=
h
.
settingService
.
UpdateSettings
(
c
.
Request
.
Context
(),
settings
);
err
!=
nil
{
...
...
@@ -210,33 +255,37 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
}
response
.
Success
(
c
,
dto
.
SystemSettings
{
RegistrationEnabled
:
updatedSettings
.
RegistrationEnabled
,
EmailVerifyEnabled
:
updatedSettings
.
EmailVerifyEnabled
,
SMTPHost
:
updatedSettings
.
SMTPHost
,
SMTPPort
:
updatedSettings
.
SMTPPort
,
SMTPUsername
:
updatedSettings
.
SMTPUsername
,
SMTPPasswordConfigured
:
updatedSettings
.
SMTPPasswordConfigured
,
SMTPFrom
:
updatedSettings
.
SMTPFrom
,
SMTPFromName
:
updatedSettings
.
SMTPFromName
,
SMTPUseTLS
:
updatedSettings
.
SMTPUseTLS
,
TurnstileEnabled
:
updatedSettings
.
TurnstileEnabled
,
TurnstileSiteKey
:
updatedSettings
.
TurnstileSiteKey
,
TurnstileSecretKeyConfigured
:
updatedSettings
.
TurnstileSecretKeyConfigured
,
SiteName
:
updatedSettings
.
SiteName
,
SiteLogo
:
updatedSettings
.
SiteLogo
,
SiteSubtitle
:
updatedSettings
.
SiteSubtitle
,
APIBaseURL
:
updatedSettings
.
APIBaseURL
,
ContactInfo
:
updatedSettings
.
ContactInfo
,
DocURL
:
updatedSettings
.
DocURL
,
DefaultConcurrency
:
updatedSettings
.
DefaultConcurrency
,
DefaultBalance
:
updatedSettings
.
DefaultBalance
,
EnableModelFallback
:
updatedSettings
.
EnableModelFallback
,
FallbackModelAnthropic
:
updatedSettings
.
FallbackModelAnthropic
,
FallbackModelOpenAI
:
updatedSettings
.
FallbackModelOpenAI
,
FallbackModelGemini
:
updatedSettings
.
FallbackModelGemini
,
FallbackModelAntigravity
:
updatedSettings
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
updatedSettings
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
updatedSettings
.
IdentityPatchPrompt
,
RegistrationEnabled
:
updatedSettings
.
RegistrationEnabled
,
EmailVerifyEnabled
:
updatedSettings
.
EmailVerifyEnabled
,
SMTPHost
:
updatedSettings
.
SMTPHost
,
SMTPPort
:
updatedSettings
.
SMTPPort
,
SMTPUsername
:
updatedSettings
.
SMTPUsername
,
SMTPPasswordConfigured
:
updatedSettings
.
SMTPPasswordConfigured
,
SMTPFrom
:
updatedSettings
.
SMTPFrom
,
SMTPFromName
:
updatedSettings
.
SMTPFromName
,
SMTPUseTLS
:
updatedSettings
.
SMTPUseTLS
,
TurnstileEnabled
:
updatedSettings
.
TurnstileEnabled
,
TurnstileSiteKey
:
updatedSettings
.
TurnstileSiteKey
,
TurnstileSecretKeyConfigured
:
updatedSettings
.
TurnstileSecretKeyConfigured
,
LinuxDoConnectEnabled
:
updatedSettings
.
LinuxDoConnectEnabled
,
LinuxDoConnectClientID
:
updatedSettings
.
LinuxDoConnectClientID
,
LinuxDoConnectClientSecretConfigured
:
updatedSettings
.
LinuxDoConnectClientSecretConfigured
,
LinuxDoConnectRedirectURL
:
updatedSettings
.
LinuxDoConnectRedirectURL
,
SiteName
:
updatedSettings
.
SiteName
,
SiteLogo
:
updatedSettings
.
SiteLogo
,
SiteSubtitle
:
updatedSettings
.
SiteSubtitle
,
APIBaseURL
:
updatedSettings
.
APIBaseURL
,
ContactInfo
:
updatedSettings
.
ContactInfo
,
DocURL
:
updatedSettings
.
DocURL
,
DefaultConcurrency
:
updatedSettings
.
DefaultConcurrency
,
DefaultBalance
:
updatedSettings
.
DefaultBalance
,
EnableModelFallback
:
updatedSettings
.
EnableModelFallback
,
FallbackModelAnthropic
:
updatedSettings
.
FallbackModelAnthropic
,
FallbackModelOpenAI
:
updatedSettings
.
FallbackModelOpenAI
,
FallbackModelGemini
:
updatedSettings
.
FallbackModelGemini
,
FallbackModelAntigravity
:
updatedSettings
.
FallbackModelAntigravity
,
EnableIdentityPatch
:
updatedSettings
.
EnableIdentityPatch
,
IdentityPatchPrompt
:
updatedSettings
.
IdentityPatchPrompt
,
})
}
...
...
@@ -298,6 +347,18 @@ func diffSettings(before *service.SystemSettings, after *service.SystemSettings,
if
req
.
TurnstileSecretKey
!=
""
{
changed
=
append
(
changed
,
"turnstile_secret_key"
)
}
if
before
.
LinuxDoConnectEnabled
!=
after
.
LinuxDoConnectEnabled
{
changed
=
append
(
changed
,
"linuxdo_connect_enabled"
)
}
if
before
.
LinuxDoConnectClientID
!=
after
.
LinuxDoConnectClientID
{
changed
=
append
(
changed
,
"linuxdo_connect_client_id"
)
}
if
req
.
LinuxDoConnectClientSecret
!=
""
{
changed
=
append
(
changed
,
"linuxdo_connect_client_secret"
)
}
if
before
.
LinuxDoConnectRedirectURL
!=
after
.
LinuxDoConnectRedirectURL
{
changed
=
append
(
changed
,
"linuxdo_connect_redirect_url"
)
}
if
before
.
SiteName
!=
after
.
SiteName
{
changed
=
append
(
changed
,
"site_name"
)
}
...
...
@@ -337,6 +398,12 @@ func diffSettings(before *service.SystemSettings, after *service.SystemSettings,
if
before
.
FallbackModelAntigravity
!=
after
.
FallbackModelAntigravity
{
changed
=
append
(
changed
,
"fallback_model_antigravity"
)
}
if
before
.
EnableIdentityPatch
!=
after
.
EnableIdentityPatch
{
changed
=
append
(
changed
,
"enable_identity_patch"
)
}
if
before
.
IdentityPatchPrompt
!=
after
.
IdentityPatchPrompt
{
changed
=
append
(
changed
,
"identity_patch_prompt"
)
}
return
changed
}
...
...
backend/internal/handler/admin/user_handler.go
View file @
61f55674
...
...
@@ -2,6 +2,7 @@ package admin
import
(
"strconv"
"strings"
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
...
...
@@ -63,10 +64,17 @@ type UpdateBalanceRequest struct {
func
(
h
*
UserHandler
)
List
(
c
*
gin
.
Context
)
{
page
,
pageSize
:=
response
.
ParsePagination
(
c
)
search
:=
c
.
Query
(
"search"
)
// 标准化和验证 search 参数
search
=
strings
.
TrimSpace
(
search
)
if
len
(
search
)
>
100
{
search
=
search
[
:
100
]
}
filters
:=
service
.
UserListFilters
{
Status
:
c
.
Query
(
"status"
),
Role
:
c
.
Query
(
"role"
),
Search
:
c
.
Query
(
"
search
"
)
,
Search
:
search
,
Attributes
:
parseAttributeFilters
(
c
),
}
...
...
backend/internal/handler/api_key_handler.go
View file @
61f55674
...
...
@@ -27,16 +27,20 @@ func NewAPIKeyHandler(apiKeyService *service.APIKeyService) *APIKeyHandler {
// CreateAPIKeyRequest represents the create API key request payload
type
CreateAPIKeyRequest
struct
{
Name
string
`json:"name" binding:"required"`
GroupID
*
int64
`json:"group_id"`
// nullable
CustomKey
*
string
`json:"custom_key"`
// 可选的自定义key
Name
string
`json:"name" binding:"required"`
GroupID
*
int64
`json:"group_id"`
// nullable
CustomKey
*
string
`json:"custom_key"`
// 可选的自定义key
IPWhitelist
[]
string
`json:"ip_whitelist"`
// IP 白名单
IPBlacklist
[]
string
`json:"ip_blacklist"`
// IP 黑名单
}
// UpdateAPIKeyRequest represents the update API key request payload
type
UpdateAPIKeyRequest
struct
{
Name
string
`json:"name"`
GroupID
*
int64
`json:"group_id"`
Status
string
`json:"status" binding:"omitempty,oneof=active inactive"`
Name
string
`json:"name"`
GroupID
*
int64
`json:"group_id"`
Status
string
`json:"status" binding:"omitempty,oneof=active inactive"`
IPWhitelist
[]
string
`json:"ip_whitelist"`
// IP 白名单
IPBlacklist
[]
string
`json:"ip_blacklist"`
// IP 黑名单
}
// List handles listing user's API keys with pagination
...
...
@@ -110,9 +114,11 @@ func (h *APIKeyHandler) Create(c *gin.Context) {
}
svcReq
:=
service
.
CreateAPIKeyRequest
{
Name
:
req
.
Name
,
GroupID
:
req
.
GroupID
,
CustomKey
:
req
.
CustomKey
,
Name
:
req
.
Name
,
GroupID
:
req
.
GroupID
,
CustomKey
:
req
.
CustomKey
,
IPWhitelist
:
req
.
IPWhitelist
,
IPBlacklist
:
req
.
IPBlacklist
,
}
key
,
err
:=
h
.
apiKeyService
.
Create
(
c
.
Request
.
Context
(),
subject
.
UserID
,
svcReq
)
if
err
!=
nil
{
...
...
@@ -144,7 +150,10 @@ func (h *APIKeyHandler) Update(c *gin.Context) {
return
}
svcReq
:=
service
.
UpdateAPIKeyRequest
{}
svcReq
:=
service
.
UpdateAPIKeyRequest
{
IPWhitelist
:
req
.
IPWhitelist
,
IPBlacklist
:
req
.
IPBlacklist
,
}
if
req
.
Name
!=
""
{
svcReq
.
Name
=
&
req
.
Name
}
...
...
backend/internal/handler/auth_handler.go
View file @
61f55674
...
...
@@ -15,14 +15,16 @@ type AuthHandler struct {
cfg
*
config
.
Config
authService
*
service
.
AuthService
userService
*
service
.
UserService
settingSvc
*
service
.
SettingService
}
// NewAuthHandler creates a new AuthHandler
func
NewAuthHandler
(
cfg
*
config
.
Config
,
authService
*
service
.
AuthService
,
userService
*
service
.
UserService
)
*
AuthHandler
{
func
NewAuthHandler
(
cfg
*
config
.
Config
,
authService
*
service
.
AuthService
,
userService
*
service
.
UserService
,
settingService
*
service
.
SettingService
)
*
AuthHandler
{
return
&
AuthHandler
{
cfg
:
cfg
,
authService
:
authService
,
userService
:
userService
,
settingSvc
:
settingService
,
}
}
...
...
backend/internal/handler/auth_linuxdo_oauth.go
0 → 100644
View file @
61f55674
package
handler
import
(
"context"
"encoding/base64"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/Wei-Shaw/sub2api/internal/config"
infraerrors
"github.com/Wei-Shaw/sub2api/internal/pkg/errors"
"github.com/Wei-Shaw/sub2api/internal/pkg/oauth"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
"github.com/imroc/req/v3"
"github.com/tidwall/gjson"
)
const
(
linuxDoOAuthCookiePath
=
"/api/v1/auth/oauth/linuxdo"
linuxDoOAuthStateCookieName
=
"linuxdo_oauth_state"
linuxDoOAuthVerifierCookie
=
"linuxdo_oauth_verifier"
linuxDoOAuthRedirectCookie
=
"linuxdo_oauth_redirect"
linuxDoOAuthCookieMaxAgeSec
=
10
*
60
// 10 minutes
linuxDoOAuthDefaultRedirectTo
=
"/dashboard"
linuxDoOAuthDefaultFrontendCB
=
"/auth/linuxdo/callback"
linuxDoOAuthMaxRedirectLen
=
2048
linuxDoOAuthMaxFragmentValueLen
=
512
linuxDoOAuthMaxSubjectLen
=
64
-
len
(
"linuxdo-"
)
)
type
linuxDoTokenResponse
struct
{
AccessToken
string
`json:"access_token"`
TokenType
string
`json:"token_type"`
ExpiresIn
int64
`json:"expires_in"`
RefreshToken
string
`json:"refresh_token,omitempty"`
Scope
string
`json:"scope,omitempty"`
}
type
linuxDoTokenExchangeError
struct
{
StatusCode
int
ProviderError
string
ProviderDescription
string
Body
string
}
func
(
e
*
linuxDoTokenExchangeError
)
Error
()
string
{
if
e
==
nil
{
return
""
}
parts
:=
[]
string
{
fmt
.
Sprintf
(
"token exchange status=%d"
,
e
.
StatusCode
)}
if
strings
.
TrimSpace
(
e
.
ProviderError
)
!=
""
{
parts
=
append
(
parts
,
"error="
+
strings
.
TrimSpace
(
e
.
ProviderError
))
}
if
strings
.
TrimSpace
(
e
.
ProviderDescription
)
!=
""
{
parts
=
append
(
parts
,
"error_description="
+
strings
.
TrimSpace
(
e
.
ProviderDescription
))
}
return
strings
.
Join
(
parts
,
" "
)
}
// LinuxDoOAuthStart 启动 LinuxDo Connect OAuth 登录流程。
// GET /api/v1/auth/oauth/linuxdo/start?redirect=/dashboard
func
(
h
*
AuthHandler
)
LinuxDoOAuthStart
(
c
*
gin
.
Context
)
{
cfg
,
err
:=
h
.
getLinuxDoOAuthConfig
(
c
.
Request
.
Context
())
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
err
)
return
}
state
,
err
:=
oauth
.
GenerateState
()
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
infraerrors
.
InternalServer
(
"OAUTH_STATE_GEN_FAILED"
,
"failed to generate oauth state"
)
.
WithCause
(
err
))
return
}
redirectTo
:=
sanitizeFrontendRedirectPath
(
c
.
Query
(
"redirect"
))
if
redirectTo
==
""
{
redirectTo
=
linuxDoOAuthDefaultRedirectTo
}
secureCookie
:=
isRequestHTTPS
(
c
)
setCookie
(
c
,
linuxDoOAuthStateCookieName
,
encodeCookieValue
(
state
),
linuxDoOAuthCookieMaxAgeSec
,
secureCookie
)
setCookie
(
c
,
linuxDoOAuthRedirectCookie
,
encodeCookieValue
(
redirectTo
),
linuxDoOAuthCookieMaxAgeSec
,
secureCookie
)
codeChallenge
:=
""
if
cfg
.
UsePKCE
{
verifier
,
err
:=
oauth
.
GenerateCodeVerifier
()
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
infraerrors
.
InternalServer
(
"OAUTH_PKCE_GEN_FAILED"
,
"failed to generate pkce verifier"
)
.
WithCause
(
err
))
return
}
codeChallenge
=
oauth
.
GenerateCodeChallenge
(
verifier
)
setCookie
(
c
,
linuxDoOAuthVerifierCookie
,
encodeCookieValue
(
verifier
),
linuxDoOAuthCookieMaxAgeSec
,
secureCookie
)
}
redirectURI
:=
strings
.
TrimSpace
(
cfg
.
RedirectURL
)
if
redirectURI
==
""
{
response
.
ErrorFrom
(
c
,
infraerrors
.
InternalServer
(
"OAUTH_CONFIG_INVALID"
,
"oauth redirect url not configured"
))
return
}
authURL
,
err
:=
buildLinuxDoAuthorizeURL
(
cfg
,
state
,
codeChallenge
,
redirectURI
)
if
err
!=
nil
{
response
.
ErrorFrom
(
c
,
infraerrors
.
InternalServer
(
"OAUTH_BUILD_URL_FAILED"
,
"failed to build oauth authorization url"
)
.
WithCause
(
err
))
return
}
c
.
Redirect
(
http
.
StatusFound
,
authURL
)
}
// LinuxDoOAuthCallback 处理 OAuth 回调:创建/登录用户,然后重定向到前端。
// GET /api/v1/auth/oauth/linuxdo/callback?code=...&state=...
func
(
h
*
AuthHandler
)
LinuxDoOAuthCallback
(
c
*
gin
.
Context
)
{
cfg
,
cfgErr
:=
h
.
getLinuxDoOAuthConfig
(
c
.
Request
.
Context
())
if
cfgErr
!=
nil
{
response
.
ErrorFrom
(
c
,
cfgErr
)
return
}
frontendCallback
:=
strings
.
TrimSpace
(
cfg
.
FrontendRedirectURL
)
if
frontendCallback
==
""
{
frontendCallback
=
linuxDoOAuthDefaultFrontendCB
}
if
providerErr
:=
strings
.
TrimSpace
(
c
.
Query
(
"error"
));
providerErr
!=
""
{
redirectOAuthError
(
c
,
frontendCallback
,
"provider_error"
,
providerErr
,
c
.
Query
(
"error_description"
))
return
}
code
:=
strings
.
TrimSpace
(
c
.
Query
(
"code"
))
state
:=
strings
.
TrimSpace
(
c
.
Query
(
"state"
))
if
code
==
""
||
state
==
""
{
redirectOAuthError
(
c
,
frontendCallback
,
"missing_params"
,
"missing code/state"
,
""
)
return
}
secureCookie
:=
isRequestHTTPS
(
c
)
defer
func
()
{
clearCookie
(
c
,
linuxDoOAuthStateCookieName
,
secureCookie
)
clearCookie
(
c
,
linuxDoOAuthVerifierCookie
,
secureCookie
)
clearCookie
(
c
,
linuxDoOAuthRedirectCookie
,
secureCookie
)
}()
expectedState
,
err
:=
readCookieDecoded
(
c
,
linuxDoOAuthStateCookieName
)
if
err
!=
nil
||
expectedState
==
""
||
state
!=
expectedState
{
redirectOAuthError
(
c
,
frontendCallback
,
"invalid_state"
,
"invalid oauth state"
,
""
)
return
}
redirectTo
,
_
:=
readCookieDecoded
(
c
,
linuxDoOAuthRedirectCookie
)
redirectTo
=
sanitizeFrontendRedirectPath
(
redirectTo
)
if
redirectTo
==
""
{
redirectTo
=
linuxDoOAuthDefaultRedirectTo
}
codeVerifier
:=
""
if
cfg
.
UsePKCE
{
codeVerifier
,
_
=
readCookieDecoded
(
c
,
linuxDoOAuthVerifierCookie
)
if
codeVerifier
==
""
{
redirectOAuthError
(
c
,
frontendCallback
,
"missing_verifier"
,
"missing pkce verifier"
,
""
)
return
}
}
redirectURI
:=
strings
.
TrimSpace
(
cfg
.
RedirectURL
)
if
redirectURI
==
""
{
redirectOAuthError
(
c
,
frontendCallback
,
"config_error"
,
"oauth redirect url not configured"
,
""
)
return
}
tokenResp
,
err
:=
linuxDoExchangeCode
(
c
.
Request
.
Context
(),
cfg
,
code
,
redirectURI
,
codeVerifier
)
if
err
!=
nil
{
description
:=
""
var
exchangeErr
*
linuxDoTokenExchangeError
if
errors
.
As
(
err
,
&
exchangeErr
)
&&
exchangeErr
!=
nil
{
log
.
Printf
(
"[LinuxDo OAuth] token exchange failed: status=%d provider_error=%q provider_description=%q body=%s"
,
exchangeErr
.
StatusCode
,
exchangeErr
.
ProviderError
,
exchangeErr
.
ProviderDescription
,
truncateLogValue
(
exchangeErr
.
Body
,
2048
),
)
description
=
exchangeErr
.
Error
()
}
else
{
log
.
Printf
(
"[LinuxDo OAuth] token exchange failed: %v"
,
err
)
description
=
err
.
Error
()
}
redirectOAuthError
(
c
,
frontendCallback
,
"token_exchange_failed"
,
"failed to exchange oauth code"
,
singleLine
(
description
))
return
}
email
,
username
,
subject
,
err
:=
linuxDoFetchUserInfo
(
c
.
Request
.
Context
(),
cfg
,
tokenResp
)
if
err
!=
nil
{
log
.
Printf
(
"[LinuxDo OAuth] userinfo fetch failed: %v"
,
err
)
redirectOAuthError
(
c
,
frontendCallback
,
"userinfo_failed"
,
"failed to fetch user info"
,
""
)
return
}
// 安全考虑:不要把第三方返回的 email 直接映射到本地账号(可能与本地邮箱用户冲突导致账号被接管)。
// 统一使用基于 subject 的稳定合成邮箱来做账号绑定。
if
subject
!=
""
{
email
=
linuxDoSyntheticEmail
(
subject
)
}
jwtToken
,
_
,
err
:=
h
.
authService
.
LoginOrRegisterOAuth
(
c
.
Request
.
Context
(),
email
,
username
)
if
err
!=
nil
{
// 避免把内部细节泄露给客户端;给前端保留结构化原因与提示信息即可。
redirectOAuthError
(
c
,
frontendCallback
,
"login_failed"
,
infraerrors
.
Reason
(
err
),
infraerrors
.
Message
(
err
))
return
}
fragment
:=
url
.
Values
{}
fragment
.
Set
(
"access_token"
,
jwtToken
)
fragment
.
Set
(
"token_type"
,
"Bearer"
)
fragment
.
Set
(
"redirect"
,
redirectTo
)
redirectWithFragment
(
c
,
frontendCallback
,
fragment
)
}
func
(
h
*
AuthHandler
)
getLinuxDoOAuthConfig
(
ctx
context
.
Context
)
(
config
.
LinuxDoConnectConfig
,
error
)
{
if
h
!=
nil
&&
h
.
settingSvc
!=
nil
{
return
h
.
settingSvc
.
GetLinuxDoConnectOAuthConfig
(
ctx
)
}
if
h
==
nil
||
h
.
cfg
==
nil
{
return
config
.
LinuxDoConnectConfig
{},
infraerrors
.
ServiceUnavailable
(
"CONFIG_NOT_READY"
,
"config not loaded"
)
}
if
!
h
.
cfg
.
LinuxDo
.
Enabled
{
return
config
.
LinuxDoConnectConfig
{},
infraerrors
.
NotFound
(
"OAUTH_DISABLED"
,
"oauth login is disabled"
)
}
return
h
.
cfg
.
LinuxDo
,
nil
}
func
linuxDoExchangeCode
(
ctx
context
.
Context
,
cfg
config
.
LinuxDoConnectConfig
,
code
string
,
redirectURI
string
,
codeVerifier
string
,
)
(
*
linuxDoTokenResponse
,
error
)
{
client
:=
req
.
C
()
.
SetTimeout
(
30
*
time
.
Second
)
form
:=
url
.
Values
{}
form
.
Set
(
"grant_type"
,
"authorization_code"
)
form
.
Set
(
"client_id"
,
cfg
.
ClientID
)
form
.
Set
(
"code"
,
code
)
form
.
Set
(
"redirect_uri"
,
redirectURI
)
if
cfg
.
UsePKCE
{
form
.
Set
(
"code_verifier"
,
codeVerifier
)
}
r
:=
client
.
R
()
.
SetContext
(
ctx
)
.
SetHeader
(
"Accept"
,
"application/json"
)
switch
strings
.
ToLower
(
strings
.
TrimSpace
(
cfg
.
TokenAuthMethod
))
{
case
""
,
"client_secret_post"
:
form
.
Set
(
"client_secret"
,
cfg
.
ClientSecret
)
case
"client_secret_basic"
:
r
.
SetBasicAuth
(
cfg
.
ClientID
,
cfg
.
ClientSecret
)
case
"none"
:
default
:
return
nil
,
fmt
.
Errorf
(
"unsupported token_auth_method: %s"
,
cfg
.
TokenAuthMethod
)
}
resp
,
err
:=
r
.
SetFormDataFromValues
(
form
)
.
Post
(
cfg
.
TokenURL
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"request token: %w"
,
err
)
}
body
:=
strings
.
TrimSpace
(
resp
.
String
())
if
!
resp
.
IsSuccessState
()
{
providerErr
,
providerDesc
:=
parseOAuthProviderError
(
body
)
return
nil
,
&
linuxDoTokenExchangeError
{
StatusCode
:
resp
.
StatusCode
,
ProviderError
:
providerErr
,
ProviderDescription
:
providerDesc
,
Body
:
body
,
}
}
tokenResp
,
ok
:=
parseLinuxDoTokenResponse
(
body
)
if
!
ok
||
strings
.
TrimSpace
(
tokenResp
.
AccessToken
)
==
""
{
return
nil
,
&
linuxDoTokenExchangeError
{
StatusCode
:
resp
.
StatusCode
,
Body
:
body
,
}
}
if
strings
.
TrimSpace
(
tokenResp
.
TokenType
)
==
""
{
tokenResp
.
TokenType
=
"Bearer"
}
return
tokenResp
,
nil
}
func
linuxDoFetchUserInfo
(
ctx
context
.
Context
,
cfg
config
.
LinuxDoConnectConfig
,
token
*
linuxDoTokenResponse
,
)
(
email
string
,
username
string
,
subject
string
,
err
error
)
{
client
:=
req
.
C
()
.
SetTimeout
(
30
*
time
.
Second
)
authorization
,
err
:=
buildBearerAuthorization
(
token
.
TokenType
,
token
.
AccessToken
)
if
err
!=
nil
{
return
""
,
""
,
""
,
fmt
.
Errorf
(
"invalid token for userinfo request: %w"
,
err
)
}
resp
,
err
:=
client
.
R
()
.
SetContext
(
ctx
)
.
SetHeader
(
"Accept"
,
"application/json"
)
.
SetHeader
(
"Authorization"
,
authorization
)
.
Get
(
cfg
.
UserInfoURL
)
if
err
!=
nil
{
return
""
,
""
,
""
,
fmt
.
Errorf
(
"request userinfo: %w"
,
err
)
}
if
!
resp
.
IsSuccessState
()
{
return
""
,
""
,
""
,
fmt
.
Errorf
(
"userinfo status=%d"
,
resp
.
StatusCode
)
}
return
linuxDoParseUserInfo
(
resp
.
String
(),
cfg
)
}
func
linuxDoParseUserInfo
(
body
string
,
cfg
config
.
LinuxDoConnectConfig
)
(
email
string
,
username
string
,
subject
string
,
err
error
)
{
email
=
firstNonEmpty
(
getGJSON
(
body
,
cfg
.
UserInfoEmailPath
),
getGJSON
(
body
,
"email"
),
getGJSON
(
body
,
"user.email"
),
getGJSON
(
body
,
"data.email"
),
getGJSON
(
body
,
"attributes.email"
),
)
username
=
firstNonEmpty
(
getGJSON
(
body
,
cfg
.
UserInfoUsernamePath
),
getGJSON
(
body
,
"username"
),
getGJSON
(
body
,
"preferred_username"
),
getGJSON
(
body
,
"name"
),
getGJSON
(
body
,
"user.username"
),
getGJSON
(
body
,
"user.name"
),
)
subject
=
firstNonEmpty
(
getGJSON
(
body
,
cfg
.
UserInfoIDPath
),
getGJSON
(
body
,
"sub"
),
getGJSON
(
body
,
"id"
),
getGJSON
(
body
,
"user_id"
),
getGJSON
(
body
,
"uid"
),
getGJSON
(
body
,
"user.id"
),
)
subject
=
strings
.
TrimSpace
(
subject
)
if
subject
==
""
{
return
""
,
""
,
""
,
errors
.
New
(
"userinfo missing id field"
)
}
if
!
isSafeLinuxDoSubject
(
subject
)
{
return
""
,
""
,
""
,
errors
.
New
(
"userinfo returned invalid id field"
)
}
email
=
strings
.
TrimSpace
(
email
)
if
email
==
""
{
// LinuxDo Connect 的 userinfo 可能不提供 email。为兼容现有用户模型(email 必填且唯一),使用稳定的合成邮箱。
email
=
linuxDoSyntheticEmail
(
subject
)
}
username
=
strings
.
TrimSpace
(
username
)
if
username
==
""
{
username
=
"linuxdo_"
+
subject
}
return
email
,
username
,
subject
,
nil
}
func
buildLinuxDoAuthorizeURL
(
cfg
config
.
LinuxDoConnectConfig
,
state
string
,
codeChallenge
string
,
redirectURI
string
)
(
string
,
error
)
{
u
,
err
:=
url
.
Parse
(
cfg
.
AuthorizeURL
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"parse authorize_url: %w"
,
err
)
}
q
:=
u
.
Query
()
q
.
Set
(
"response_type"
,
"code"
)
q
.
Set
(
"client_id"
,
cfg
.
ClientID
)
q
.
Set
(
"redirect_uri"
,
redirectURI
)
if
strings
.
TrimSpace
(
cfg
.
Scopes
)
!=
""
{
q
.
Set
(
"scope"
,
cfg
.
Scopes
)
}
q
.
Set
(
"state"
,
state
)
if
cfg
.
UsePKCE
{
q
.
Set
(
"code_challenge"
,
codeChallenge
)
q
.
Set
(
"code_challenge_method"
,
"S256"
)
}
u
.
RawQuery
=
q
.
Encode
()
return
u
.
String
(),
nil
}
func
redirectOAuthError
(
c
*
gin
.
Context
,
frontendCallback
string
,
code
string
,
message
string
,
description
string
)
{
fragment
:=
url
.
Values
{}
fragment
.
Set
(
"error"
,
truncateFragmentValue
(
code
))
if
strings
.
TrimSpace
(
message
)
!=
""
{
fragment
.
Set
(
"error_message"
,
truncateFragmentValue
(
message
))
}
if
strings
.
TrimSpace
(
description
)
!=
""
{
fragment
.
Set
(
"error_description"
,
truncateFragmentValue
(
description
))
}
redirectWithFragment
(
c
,
frontendCallback
,
fragment
)
}
func
redirectWithFragment
(
c
*
gin
.
Context
,
frontendCallback
string
,
fragment
url
.
Values
)
{
u
,
err
:=
url
.
Parse
(
frontendCallback
)
if
err
!=
nil
{
// 兜底:尽力跳转到默认页面,避免卡死在回调页。
c
.
Redirect
(
http
.
StatusFound
,
linuxDoOAuthDefaultRedirectTo
)
return
}
if
u
.
Scheme
!=
""
&&
!
strings
.
EqualFold
(
u
.
Scheme
,
"http"
)
&&
!
strings
.
EqualFold
(
u
.
Scheme
,
"https"
)
{
c
.
Redirect
(
http
.
StatusFound
,
linuxDoOAuthDefaultRedirectTo
)
return
}
u
.
Fragment
=
fragment
.
Encode
()
c
.
Header
(
"Cache-Control"
,
"no-store"
)
c
.
Header
(
"Pragma"
,
"no-cache"
)
c
.
Redirect
(
http
.
StatusFound
,
u
.
String
())
}
func
firstNonEmpty
(
values
...
string
)
string
{
for
_
,
v
:=
range
values
{
v
=
strings
.
TrimSpace
(
v
)
if
v
!=
""
{
return
v
}
}
return
""
}
func
parseOAuthProviderError
(
body
string
)
(
providerErr
string
,
providerDesc
string
)
{
body
=
strings
.
TrimSpace
(
body
)
if
body
==
""
{
return
""
,
""
}
providerErr
=
firstNonEmpty
(
getGJSON
(
body
,
"error"
),
getGJSON
(
body
,
"code"
),
getGJSON
(
body
,
"error.code"
),
)
providerDesc
=
firstNonEmpty
(
getGJSON
(
body
,
"error_description"
),
getGJSON
(
body
,
"error.message"
),
getGJSON
(
body
,
"message"
),
getGJSON
(
body
,
"detail"
),
)
if
providerErr
!=
""
||
providerDesc
!=
""
{
return
providerErr
,
providerDesc
}
values
,
err
:=
url
.
ParseQuery
(
body
)
if
err
!=
nil
{
return
""
,
""
}
providerErr
=
firstNonEmpty
(
values
.
Get
(
"error"
),
values
.
Get
(
"code"
))
providerDesc
=
firstNonEmpty
(
values
.
Get
(
"error_description"
),
values
.
Get
(
"error_message"
),
values
.
Get
(
"message"
))
return
providerErr
,
providerDesc
}
func
parseLinuxDoTokenResponse
(
body
string
)
(
*
linuxDoTokenResponse
,
bool
)
{
body
=
strings
.
TrimSpace
(
body
)
if
body
==
""
{
return
nil
,
false
}
accessToken
:=
strings
.
TrimSpace
(
getGJSON
(
body
,
"access_token"
))
if
accessToken
!=
""
{
tokenType
:=
strings
.
TrimSpace
(
getGJSON
(
body
,
"token_type"
))
refreshToken
:=
strings
.
TrimSpace
(
getGJSON
(
body
,
"refresh_token"
))
scope
:=
strings
.
TrimSpace
(
getGJSON
(
body
,
"scope"
))
expiresIn
:=
gjson
.
Get
(
body
,
"expires_in"
)
.
Int
()
return
&
linuxDoTokenResponse
{
AccessToken
:
accessToken
,
TokenType
:
tokenType
,
ExpiresIn
:
expiresIn
,
RefreshToken
:
refreshToken
,
Scope
:
scope
,
},
true
}
values
,
err
:=
url
.
ParseQuery
(
body
)
if
err
!=
nil
{
return
nil
,
false
}
accessToken
=
strings
.
TrimSpace
(
values
.
Get
(
"access_token"
))
if
accessToken
==
""
{
return
nil
,
false
}
expiresIn
:=
int64
(
0
)
if
raw
:=
strings
.
TrimSpace
(
values
.
Get
(
"expires_in"
));
raw
!=
""
{
if
v
,
err
:=
strconv
.
ParseInt
(
raw
,
10
,
64
);
err
==
nil
{
expiresIn
=
v
}
}
return
&
linuxDoTokenResponse
{
AccessToken
:
accessToken
,
TokenType
:
strings
.
TrimSpace
(
values
.
Get
(
"token_type"
)),
ExpiresIn
:
expiresIn
,
RefreshToken
:
strings
.
TrimSpace
(
values
.
Get
(
"refresh_token"
)),
Scope
:
strings
.
TrimSpace
(
values
.
Get
(
"scope"
)),
},
true
}
func
getGJSON
(
body
string
,
path
string
)
string
{
path
=
strings
.
TrimSpace
(
path
)
if
path
==
""
{
return
""
}
res
:=
gjson
.
Get
(
body
,
path
)
if
!
res
.
Exists
()
{
return
""
}
return
res
.
String
()
}
func
truncateLogValue
(
value
string
,
maxLen
int
)
string
{
value
=
strings
.
TrimSpace
(
value
)
if
value
==
""
||
maxLen
<=
0
{
return
""
}
if
len
(
value
)
<=
maxLen
{
return
value
}
value
=
value
[
:
maxLen
]
for
!
utf8
.
ValidString
(
value
)
{
value
=
value
[
:
len
(
value
)
-
1
]
}
return
value
}
func
singleLine
(
value
string
)
string
{
value
=
strings
.
TrimSpace
(
value
)
if
value
==
""
{
return
""
}
return
strings
.
Join
(
strings
.
Fields
(
value
),
" "
)
}
func
sanitizeFrontendRedirectPath
(
path
string
)
string
{
path
=
strings
.
TrimSpace
(
path
)
if
path
==
""
{
return
""
}
if
len
(
path
)
>
linuxDoOAuthMaxRedirectLen
{
return
""
}
// 只允许同源相对路径(避免开放重定向)。
if
!
strings
.
HasPrefix
(
path
,
"/"
)
{
return
""
}
if
strings
.
HasPrefix
(
path
,
"//"
)
{
return
""
}
if
strings
.
Contains
(
path
,
"://"
)
{
return
""
}
if
strings
.
ContainsAny
(
path
,
"
\r\n
"
)
{
return
""
}
return
path
}
func
isRequestHTTPS
(
c
*
gin
.
Context
)
bool
{
if
c
.
Request
.
TLS
!=
nil
{
return
true
}
proto
:=
strings
.
ToLower
(
strings
.
TrimSpace
(
c
.
GetHeader
(
"X-Forwarded-Proto"
)))
return
proto
==
"https"
}
func
encodeCookieValue
(
value
string
)
string
{
return
base64
.
RawURLEncoding
.
EncodeToString
([]
byte
(
value
))
}
func
decodeCookieValue
(
value
string
)
(
string
,
error
)
{
raw
,
err
:=
base64
.
RawURLEncoding
.
DecodeString
(
value
)
if
err
!=
nil
{
return
""
,
err
}
return
string
(
raw
),
nil
}
func
readCookieDecoded
(
c
*
gin
.
Context
,
name
string
)
(
string
,
error
)
{
ck
,
err
:=
c
.
Request
.
Cookie
(
name
)
if
err
!=
nil
{
return
""
,
err
}
return
decodeCookieValue
(
ck
.
Value
)
}
func
setCookie
(
c
*
gin
.
Context
,
name
string
,
value
string
,
maxAgeSec
int
,
secure
bool
)
{
http
.
SetCookie
(
c
.
Writer
,
&
http
.
Cookie
{
Name
:
name
,
Value
:
value
,
Path
:
linuxDoOAuthCookiePath
,
MaxAge
:
maxAgeSec
,
HttpOnly
:
true
,
Secure
:
secure
,
SameSite
:
http
.
SameSiteLaxMode
,
})
}
func
clearCookie
(
c
*
gin
.
Context
,
name
string
,
secure
bool
)
{
http
.
SetCookie
(
c
.
Writer
,
&
http
.
Cookie
{
Name
:
name
,
Value
:
""
,
Path
:
linuxDoOAuthCookiePath
,
MaxAge
:
-
1
,
HttpOnly
:
true
,
Secure
:
secure
,
SameSite
:
http
.
SameSiteLaxMode
,
})
}
func
truncateFragmentValue
(
value
string
)
string
{
value
=
strings
.
TrimSpace
(
value
)
if
value
==
""
{
return
""
}
if
len
(
value
)
>
linuxDoOAuthMaxFragmentValueLen
{
value
=
value
[
:
linuxDoOAuthMaxFragmentValueLen
]
for
!
utf8
.
ValidString
(
value
)
{
value
=
value
[
:
len
(
value
)
-
1
]
}
}
return
value
}
func
buildBearerAuthorization
(
tokenType
,
accessToken
string
)
(
string
,
error
)
{
tokenType
=
strings
.
TrimSpace
(
tokenType
)
if
tokenType
==
""
{
tokenType
=
"Bearer"
}
if
!
strings
.
EqualFold
(
tokenType
,
"Bearer"
)
{
return
""
,
fmt
.
Errorf
(
"unsupported token_type: %s"
,
tokenType
)
}
accessToken
=
strings
.
TrimSpace
(
accessToken
)
if
accessToken
==
""
{
return
""
,
errors
.
New
(
"missing access_token"
)
}
if
strings
.
ContainsAny
(
accessToken
,
"
\t\r\n
"
)
{
return
""
,
errors
.
New
(
"access_token contains whitespace"
)
}
return
"Bearer "
+
accessToken
,
nil
}
func
isSafeLinuxDoSubject
(
subject
string
)
bool
{
subject
=
strings
.
TrimSpace
(
subject
)
if
subject
==
""
||
len
(
subject
)
>
linuxDoOAuthMaxSubjectLen
{
return
false
}
for
_
,
r
:=
range
subject
{
switch
{
case
r
>=
'0'
&&
r
<=
'9'
:
case
r
>=
'a'
&&
r
<=
'z'
:
case
r
>=
'A'
&&
r
<=
'Z'
:
case
r
==
'_'
||
r
==
'-'
:
default
:
return
false
}
}
return
true
}
func
linuxDoSyntheticEmail
(
subject
string
)
string
{
subject
=
strings
.
TrimSpace
(
subject
)
if
subject
==
""
{
return
""
}
return
"linuxdo-"
+
subject
+
service
.
LinuxDoConnectSyntheticEmailDomain
}
backend/internal/handler/auth_linuxdo_oauth_test.go
0 → 100644
View file @
61f55674
package
handler
import
(
"strings"
"testing"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/stretchr/testify/require"
)
func
TestSanitizeFrontendRedirectPath
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
"/dashboard"
,
sanitizeFrontendRedirectPath
(
"/dashboard"
))
require
.
Equal
(
t
,
"/dashboard"
,
sanitizeFrontendRedirectPath
(
" /dashboard "
))
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
"dashboard"
))
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
"//evil.com"
))
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
"https://evil.com"
))
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
"/
\n
foo"
))
long
:=
"/"
+
strings
.
Repeat
(
"a"
,
linuxDoOAuthMaxRedirectLen
)
require
.
Equal
(
t
,
""
,
sanitizeFrontendRedirectPath
(
long
))
}
func
TestBuildBearerAuthorization
(
t
*
testing
.
T
)
{
auth
,
err
:=
buildBearerAuthorization
(
""
,
"token123"
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"Bearer token123"
,
auth
)
auth
,
err
=
buildBearerAuthorization
(
"bearer"
,
"token123"
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"Bearer token123"
,
auth
)
_
,
err
=
buildBearerAuthorization
(
"MAC"
,
"token123"
)
require
.
Error
(
t
,
err
)
_
,
err
=
buildBearerAuthorization
(
"Bearer"
,
"token 123"
)
require
.
Error
(
t
,
err
)
}
func
TestLinuxDoParseUserInfoParsesIDAndUsername
(
t
*
testing
.
T
)
{
cfg
:=
config
.
LinuxDoConnectConfig
{
UserInfoURL
:
"https://connect.linux.do/api/user"
,
}
email
,
username
,
subject
,
err
:=
linuxDoParseUserInfo
(
`{"id":123,"username":"alice"}`
,
cfg
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"123"
,
subject
)
require
.
Equal
(
t
,
"alice"
,
username
)
require
.
Equal
(
t
,
"linuxdo-123@linuxdo-connect.invalid"
,
email
)
}
func
TestLinuxDoParseUserInfoDefaultsUsername
(
t
*
testing
.
T
)
{
cfg
:=
config
.
LinuxDoConnectConfig
{
UserInfoURL
:
"https://connect.linux.do/api/user"
,
}
email
,
username
,
subject
,
err
:=
linuxDoParseUserInfo
(
`{"id":"123"}`
,
cfg
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
"123"
,
subject
)
require
.
Equal
(
t
,
"linuxdo_123"
,
username
)
require
.
Equal
(
t
,
"linuxdo-123@linuxdo-connect.invalid"
,
email
)
}
func
TestLinuxDoParseUserInfoRejectsUnsafeSubject
(
t
*
testing
.
T
)
{
cfg
:=
config
.
LinuxDoConnectConfig
{
UserInfoURL
:
"https://connect.linux.do/api/user"
,
}
_
,
_
,
_
,
err
:=
linuxDoParseUserInfo
(
`{"id":"123@456"}`
,
cfg
)
require
.
Error
(
t
,
err
)
tooLong
:=
strings
.
Repeat
(
"a"
,
linuxDoOAuthMaxSubjectLen
+
1
)
_
,
_
,
_
,
err
=
linuxDoParseUserInfo
(
`{"id":"`
+
tooLong
+
`"}`
,
cfg
)
require
.
Error
(
t
,
err
)
}
func
TestParseOAuthProviderErrorJSON
(
t
*
testing
.
T
)
{
code
,
desc
:=
parseOAuthProviderError
(
`{"error":"invalid_client","error_description":"bad secret"}`
)
require
.
Equal
(
t
,
"invalid_client"
,
code
)
require
.
Equal
(
t
,
"bad secret"
,
desc
)
}
func
TestParseOAuthProviderErrorForm
(
t
*
testing
.
T
)
{
code
,
desc
:=
parseOAuthProviderError
(
"error=invalid_request&error_description=Missing+code_verifier"
)
require
.
Equal
(
t
,
"invalid_request"
,
code
)
require
.
Equal
(
t
,
"Missing code_verifier"
,
desc
)
}
func
TestParseLinuxDoTokenResponseJSON
(
t
*
testing
.
T
)
{
token
,
ok
:=
parseLinuxDoTokenResponse
(
`{"access_token":"t1","token_type":"Bearer","expires_in":3600,"scope":"user"}`
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
"t1"
,
token
.
AccessToken
)
require
.
Equal
(
t
,
"Bearer"
,
token
.
TokenType
)
require
.
Equal
(
t
,
int64
(
3600
),
token
.
ExpiresIn
)
require
.
Equal
(
t
,
"user"
,
token
.
Scope
)
}
func
TestParseLinuxDoTokenResponseForm
(
t
*
testing
.
T
)
{
token
,
ok
:=
parseLinuxDoTokenResponse
(
"access_token=t2&token_type=bearer&expires_in=60"
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
"t2"
,
token
.
AccessToken
)
require
.
Equal
(
t
,
"bearer"
,
token
.
TokenType
)
require
.
Equal
(
t
,
int64
(
60
),
token
.
ExpiresIn
)
}
func
TestSingleLineStripsWhitespace
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
"hello world"
,
singleLine
(
"hello
\r\n
world"
))
require
.
Equal
(
t
,
""
,
singleLine
(
"
\n\t\r
"
))
}
backend/internal/handler/dto/mappers.go
View file @
61f55674
...
...
@@ -53,16 +53,18 @@ func APIKeyFromService(k *service.APIKey) *APIKey {
return
nil
}
return
&
APIKey
{
ID
:
k
.
ID
,
UserID
:
k
.
UserID
,
Key
:
k
.
Key
,
Name
:
k
.
Name
,
GroupID
:
k
.
GroupID
,
Status
:
k
.
Status
,
CreatedAt
:
k
.
CreatedAt
,
UpdatedAt
:
k
.
UpdatedAt
,
User
:
UserFromServiceShallow
(
k
.
User
),
Group
:
GroupFromServiceShallow
(
k
.
Group
),
ID
:
k
.
ID
,
UserID
:
k
.
UserID
,
Key
:
k
.
Key
,
Name
:
k
.
Name
,
GroupID
:
k
.
GroupID
,
Status
:
k
.
Status
,
IPWhitelist
:
k
.
IPWhitelist
,
IPBlacklist
:
k
.
IPBlacklist
,
CreatedAt
:
k
.
CreatedAt
,
UpdatedAt
:
k
.
UpdatedAt
,
User
:
UserFromServiceShallow
(
k
.
User
),
Group
:
GroupFromServiceShallow
(
k
.
Group
),
}
}
...
...
@@ -250,11 +252,12 @@ func AccountSummaryFromService(a *service.Account) *AccountSummary {
// usageLogFromServiceBase is a helper that converts service UsageLog to DTO.
// The account parameter allows caller to control what Account info is included.
func
usageLogFromServiceBase
(
l
*
service
.
UsageLog
,
account
*
AccountSummary
)
*
UsageLog
{
// The includeIPAddress parameter controls whether to include the IP address (admin-only).
func
usageLogFromServiceBase
(
l
*
service
.
UsageLog
,
account
*
AccountSummary
,
includeIPAddress
bool
)
*
UsageLog
{
if
l
==
nil
{
return
nil
}
re
turn
&
UsageLog
{
re
sult
:=
&
UsageLog
{
ID
:
l
.
ID
,
UserID
:
l
.
UserID
,
APIKeyID
:
l
.
APIKeyID
,
...
...
@@ -290,21 +293,26 @@ func usageLogFromServiceBase(l *service.UsageLog, account *AccountSummary) *Usag
Group
:
GroupFromServiceShallow
(
l
.
Group
),
Subscription
:
UserSubscriptionFromService
(
l
.
Subscription
),
}
// IP 地址仅对管理员可见
if
includeIPAddress
{
result
.
IPAddress
=
l
.
IPAddress
}
return
result
}
// UsageLogFromService converts a service UsageLog to DTO for regular users.
// It excludes Account details - users should not see
account information
.
// It excludes Account details
and IP address
- users should not see
these
.
func
UsageLogFromService
(
l
*
service
.
UsageLog
)
*
UsageLog
{
return
usageLogFromServiceBase
(
l
,
nil
)
return
usageLogFromServiceBase
(
l
,
nil
,
false
)
}
// UsageLogFromServiceAdmin converts a service UsageLog to DTO for admin users.
// It includes minimal Account info (ID, Name only).
// It includes minimal Account info (ID, Name only)
and IP address
.
func
UsageLogFromServiceAdmin
(
l
*
service
.
UsageLog
)
*
UsageLog
{
if
l
==
nil
{
return
nil
}
return
usageLogFromServiceBase
(
l
,
AccountSummaryFromService
(
l
.
Account
))
return
usageLogFromServiceBase
(
l
,
AccountSummaryFromService
(
l
.
Account
)
,
true
)
}
func
SettingFromService
(
s
*
service
.
Setting
)
*
Setting
{
...
...
backend/internal/handler/dto/settings.go
View file @
61f55674
...
...
@@ -17,6 +17,11 @@ type SystemSettings struct {
TurnstileSiteKey
string
`json:"turnstile_site_key"`
TurnstileSecretKeyConfigured
bool
`json:"turnstile_secret_key_configured"`
LinuxDoConnectEnabled
bool
`json:"linuxdo_connect_enabled"`
LinuxDoConnectClientID
string
`json:"linuxdo_connect_client_id"`
LinuxDoConnectClientSecretConfigured
bool
`json:"linuxdo_connect_client_secret_configured"`
LinuxDoConnectRedirectURL
string
`json:"linuxdo_connect_redirect_url"`
SiteName
string
`json:"site_name"`
SiteLogo
string
`json:"site_logo"`
SiteSubtitle
string
`json:"site_subtitle"`
...
...
@@ -50,5 +55,6 @@ type PublicSettings struct {
APIBaseURL
string
`json:"api_base_url"`
ContactInfo
string
`json:"contact_info"`
DocURL
string
`json:"doc_url"`
LinuxDoOAuthEnabled
bool
`json:"linuxdo_oauth_enabled"`
Version
string
`json:"version"`
}
backend/internal/handler/dto/types.go
View file @
61f55674
...
...
@@ -20,14 +20,16 @@ type User struct {
}
type
APIKey
struct
{
ID
int64
`json:"id"`
UserID
int64
`json:"user_id"`
Key
string
`json:"key"`
Name
string
`json:"name"`
GroupID
*
int64
`json:"group_id"`
Status
string
`json:"status"`
CreatedAt
time
.
Time
`json:"created_at"`
UpdatedAt
time
.
Time
`json:"updated_at"`
ID
int64
`json:"id"`
UserID
int64
`json:"user_id"`
Key
string
`json:"key"`
Name
string
`json:"name"`
GroupID
*
int64
`json:"group_id"`
Status
string
`json:"status"`
IPWhitelist
[]
string
`json:"ip_whitelist"`
IPBlacklist
[]
string
`json:"ip_blacklist"`
CreatedAt
time
.
Time
`json:"created_at"`
UpdatedAt
time
.
Time
`json:"updated_at"`
User
*
User
`json:"user,omitempty"`
Group
*
Group
`json:"group,omitempty"`
...
...
@@ -187,6 +189,9 @@ type UsageLog struct {
// User-Agent
UserAgent
*
string
`json:"user_agent"`
// IP 地址(仅管理员可见)
IPAddress
*
string
`json:"ip_address,omitempty"`
CreatedAt
time
.
Time
`json:"created_at"`
User
*
User
`json:"user,omitempty"`
...
...
backend/internal/handler/gateway_handler.go
View file @
61f55674
...
...
@@ -15,6 +15,7 @@ import (
"github.com/Wei-Shaw/sub2api/internal/pkg/antigravity"
"github.com/Wei-Shaw/sub2api/internal/pkg/claude"
pkgerrors
"github.com/Wei-Shaw/sub2api/internal/pkg/errors"
"github.com/Wei-Shaw/sub2api/internal/pkg/ip"
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
middleware2
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
"github.com/Wei-Shaw/sub2api/internal/service"
...
...
@@ -114,6 +115,9 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
// 获取 User-Agent
userAgent
:=
c
.
Request
.
UserAgent
()
// 获取客户端 IP
clientIP
:=
ip
.
GetClientIP
(
c
)
// 0. 检查wait队列是否已满
maxWait
:=
service
.
CalculateMaxWait
(
subject
.
Concurrency
)
canWait
,
err
:=
h
.
concurrencyHelper
.
IncrementWaitCount
(
c
.
Request
.
Context
(),
subject
.
UserID
,
maxWait
)
...
...
@@ -273,7 +277,7 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
}
// 异步记录使用量(subscription已在函数开头获取)
go
func
(
result
*
service
.
ForwardResult
,
usedAccount
*
service
.
Account
,
ua
string
)
{
go
func
(
result
*
service
.
ForwardResult
,
usedAccount
*
service
.
Account
,
ua
string
,
cip
string
)
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
defer
cancel
()
if
err
:=
h
.
gatewayService
.
RecordUsage
(
ctx
,
&
service
.
RecordUsageInput
{
...
...
@@ -283,10 +287,11 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
Account
:
usedAccount
,
Subscription
:
subscription
,
UserAgent
:
ua
,
IPAddress
:
cip
,
});
err
!=
nil
{
log
.
Printf
(
"Record usage failed: %v"
,
err
)
}
}(
result
,
account
,
userAgent
)
}(
result
,
account
,
userAgent
,
clientIP
)
return
}
}
...
...
@@ -401,7 +406,7 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
}
// 异步记录使用量(subscription已在函数开头获取)
go
func
(
result
*
service
.
ForwardResult
,
usedAccount
*
service
.
Account
,
ua
string
)
{
go
func
(
result
*
service
.
ForwardResult
,
usedAccount
*
service
.
Account
,
ua
string
,
cip
string
)
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
defer
cancel
()
if
err
:=
h
.
gatewayService
.
RecordUsage
(
ctx
,
&
service
.
RecordUsageInput
{
...
...
@@ -411,10 +416,11 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
Account
:
usedAccount
,
Subscription
:
subscription
,
UserAgent
:
ua
,
IPAddress
:
cip
,
});
err
!=
nil
{
log
.
Printf
(
"Record usage failed: %v"
,
err
)
}
}(
result
,
account
,
userAgent
)
}(
result
,
account
,
userAgent
,
clientIP
)
return
}
}
...
...
backend/internal/handler/gemini_v1beta_handler.go
View file @
61f55674
...
...
@@ -12,6 +12,7 @@ import (
"github.com/Wei-Shaw/sub2api/internal/pkg/antigravity"
"github.com/Wei-Shaw/sub2api/internal/pkg/gemini"
"github.com/Wei-Shaw/sub2api/internal/pkg/googleapi"
"github.com/Wei-Shaw/sub2api/internal/pkg/ip"
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
"github.com/Wei-Shaw/sub2api/internal/service"
...
...
@@ -167,6 +168,9 @@ func (h *GatewayHandler) GeminiV1BetaModels(c *gin.Context) {
// 获取 User-Agent
userAgent
:=
c
.
Request
.
UserAgent
()
// 获取客户端 IP
clientIP
:=
ip
.
GetClientIP
(
c
)
// For Gemini native API, do not send Claude-style ping frames.
geminiConcurrency
:=
NewConcurrencyHelper
(
h
.
concurrencyHelper
.
concurrencyService
,
SSEPingFormatNone
,
0
)
...
...
@@ -307,7 +311,7 @@ func (h *GatewayHandler) GeminiV1BetaModels(c *gin.Context) {
}
// 6) record usage async
go
func
(
result
*
service
.
ForwardResult
,
usedAccount
*
service
.
Account
,
ua
string
)
{
go
func
(
result
*
service
.
ForwardResult
,
usedAccount
*
service
.
Account
,
ua
string
,
cip
string
)
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
defer
cancel
()
if
err
:=
h
.
gatewayService
.
RecordUsage
(
ctx
,
&
service
.
RecordUsageInput
{
...
...
@@ -317,10 +321,11 @@ func (h *GatewayHandler) GeminiV1BetaModels(c *gin.Context) {
Account
:
usedAccount
,
Subscription
:
subscription
,
UserAgent
:
ua
,
IPAddress
:
cip
,
});
err
!=
nil
{
log
.
Printf
(
"Record usage failed: %v"
,
err
)
}
}(
result
,
account
,
userAgent
)
}(
result
,
account
,
userAgent
,
clientIP
)
return
}
}
...
...
backend/internal/handler/openai_gateway_handler.go
View file @
61f55674
...
...
@@ -11,6 +11,7 @@ import (
"time"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/Wei-Shaw/sub2api/internal/pkg/ip"
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
middleware2
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
"github.com/Wei-Shaw/sub2api/internal/service"
...
...
@@ -94,6 +95,10 @@ func (h *OpenAIGatewayHandler) Responses(c *gin.Context) {
// For non-Codex CLI requests, set default instructions
userAgent
:=
c
.
GetHeader
(
"User-Agent"
)
// 获取客户端 IP
clientIP
:=
ip
.
GetClientIP
(
c
)
if
!
openai
.
IsCodexCLIRequest
(
userAgent
)
{
reqBody
[
"instructions"
]
=
openai
.
DefaultInstructions
// Re-serialize body
...
...
@@ -242,7 +247,7 @@ func (h *OpenAIGatewayHandler) Responses(c *gin.Context) {
}
// Async record usage
go
func
(
result
*
service
.
OpenAIForwardResult
,
usedAccount
*
service
.
Account
,
ua
string
)
{
go
func
(
result
*
service
.
OpenAIForwardResult
,
usedAccount
*
service
.
Account
,
ua
string
,
cip
string
)
{
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
defer
cancel
()
if
err
:=
h
.
gatewayService
.
RecordUsage
(
ctx
,
&
service
.
OpenAIRecordUsageInput
{
...
...
@@ -252,10 +257,11 @@ func (h *OpenAIGatewayHandler) Responses(c *gin.Context) {
Account
:
usedAccount
,
Subscription
:
subscription
,
UserAgent
:
ua
,
IPAddress
:
cip
,
});
err
!=
nil
{
log
.
Printf
(
"Record usage failed: %v"
,
err
)
}
}(
result
,
account
,
userAgent
)
}(
result
,
account
,
userAgent
,
clientIP
)
return
}
}
...
...
backend/internal/handler/setting_handler.go
View file @
61f55674
...
...
@@ -42,6 +42,7 @@ func (h *SettingHandler) GetPublicSettings(c *gin.Context) {
APIBaseURL
:
settings
.
APIBaseURL
,
ContactInfo
:
settings
.
ContactInfo
,
DocURL
:
settings
.
DocURL
,
LinuxDoOAuthEnabled
:
settings
.
LinuxDoOAuthEnabled
,
Version
:
h
.
version
,
})
}
backend/internal/pkg/ip/ip.go
0 → 100644
View file @
61f55674
// Package ip 提供客户端 IP 地址提取工具。
package
ip
import
(
"net"
"strings"
"github.com/gin-gonic/gin"
)
// GetClientIP 从 Gin Context 中提取客户端真实 IP 地址。
// 按以下优先级检查 Header:
// 1. CF-Connecting-IP (Cloudflare)
// 2. X-Real-IP (Nginx)
// 3. X-Forwarded-For (取第一个非私有 IP)
// 4. c.ClientIP() (Gin 内置方法)
func
GetClientIP
(
c
*
gin
.
Context
)
string
{
// 1. Cloudflare
if
ip
:=
c
.
GetHeader
(
"CF-Connecting-IP"
);
ip
!=
""
{
return
normalizeIP
(
ip
)
}
// 2. Nginx X-Real-IP
if
ip
:=
c
.
GetHeader
(
"X-Real-IP"
);
ip
!=
""
{
return
normalizeIP
(
ip
)
}
// 3. X-Forwarded-For (多个 IP 时取第一个公网 IP)
if
xff
:=
c
.
GetHeader
(
"X-Forwarded-For"
);
xff
!=
""
{
ips
:=
strings
.
Split
(
xff
,
","
)
for
_
,
ip
:=
range
ips
{
ip
=
strings
.
TrimSpace
(
ip
)
if
ip
!=
""
&&
!
isPrivateIP
(
ip
)
{
return
normalizeIP
(
ip
)
}
}
// 如果都是私有 IP,返回第一个
if
len
(
ips
)
>
0
{
return
normalizeIP
(
strings
.
TrimSpace
(
ips
[
0
]))
}
}
// 4. Gin 内置方法
return
normalizeIP
(
c
.
ClientIP
())
}
// normalizeIP 规范化 IP 地址,去除端口号和空格。
func
normalizeIP
(
ip
string
)
string
{
ip
=
strings
.
TrimSpace
(
ip
)
// 移除端口号(如 "192.168.1.1:8080" -> "192.168.1.1")
if
host
,
_
,
err
:=
net
.
SplitHostPort
(
ip
);
err
==
nil
{
return
host
}
return
ip
}
// isPrivateIP 检查 IP 是否为私有地址。
func
isPrivateIP
(
ipStr
string
)
bool
{
ip
:=
net
.
ParseIP
(
ipStr
)
if
ip
==
nil
{
return
false
}
// 私有 IP 范围
privateBlocks
:=
[]
string
{
"10.0.0.0/8"
,
"172.16.0.0/12"
,
"192.168.0.0/16"
,
"127.0.0.0/8"
,
"::1/128"
,
"fc00::/7"
,
}
for
_
,
block
:=
range
privateBlocks
{
_
,
cidr
,
err
:=
net
.
ParseCIDR
(
block
)
if
err
!=
nil
{
continue
}
if
cidr
.
Contains
(
ip
)
{
return
true
}
}
return
false
}
// MatchesPattern 检查 IP 是否匹配指定的模式(支持单个 IP 或 CIDR)。
// pattern 可以是:
// - 单个 IP: "192.168.1.100"
// - CIDR 范围: "192.168.1.0/24"
func
MatchesPattern
(
clientIP
,
pattern
string
)
bool
{
ip
:=
net
.
ParseIP
(
clientIP
)
if
ip
==
nil
{
return
false
}
// 尝试解析为 CIDR
if
strings
.
Contains
(
pattern
,
"/"
)
{
_
,
cidr
,
err
:=
net
.
ParseCIDR
(
pattern
)
if
err
!=
nil
{
return
false
}
return
cidr
.
Contains
(
ip
)
}
// 作为单个 IP 处理
patternIP
:=
net
.
ParseIP
(
pattern
)
if
patternIP
==
nil
{
return
false
}
return
ip
.
Equal
(
patternIP
)
}
// MatchesAnyPattern 检查 IP 是否匹配任意一个模式。
func
MatchesAnyPattern
(
clientIP
string
,
patterns
[]
string
)
bool
{
for
_
,
pattern
:=
range
patterns
{
if
MatchesPattern
(
clientIP
,
pattern
)
{
return
true
}
}
return
false
}
// CheckIPRestriction 检查 IP 是否被 API Key 的 IP 限制允许。
// 返回值:(是否允许, 拒绝原因)
// 逻辑:
// 1. 先检查黑名单,如果在黑名单中则直接拒绝
// 2. 如果白名单不为空,IP 必须在白名单中
// 3. 如果白名单为空,允许访问(除非被黑名单拒绝)
func
CheckIPRestriction
(
clientIP
string
,
whitelist
,
blacklist
[]
string
)
(
bool
,
string
)
{
// 规范化 IP
clientIP
=
normalizeIP
(
clientIP
)
if
clientIP
==
""
{
return
false
,
"access denied"
}
// 1. 检查黑名单
if
len
(
blacklist
)
>
0
&&
MatchesAnyPattern
(
clientIP
,
blacklist
)
{
return
false
,
"access denied"
}
// 2. 检查白名单(如果设置了白名单,IP 必须在其中)
if
len
(
whitelist
)
>
0
&&
!
MatchesAnyPattern
(
clientIP
,
whitelist
)
{
return
false
,
"access denied"
}
return
true
,
""
}
// ValidateIPPattern 验证 IP 或 CIDR 格式是否有效。
func
ValidateIPPattern
(
pattern
string
)
bool
{
if
strings
.
Contains
(
pattern
,
"/"
)
{
_
,
_
,
err
:=
net
.
ParseCIDR
(
pattern
)
return
err
==
nil
}
return
net
.
ParseIP
(
pattern
)
!=
nil
}
// ValidateIPPatterns 验证多个 IP 或 CIDR 格式。
// 返回无效的模式列表。
func
ValidateIPPatterns
(
patterns
[]
string
)
[]
string
{
var
invalid
[]
string
for
_
,
p
:=
range
patterns
{
if
!
ValidateIPPattern
(
p
)
{
invalid
=
append
(
invalid
,
p
)
}
}
return
invalid
}
backend/internal/repository/account_repo.go
View file @
61f55674
...
...
@@ -675,6 +675,40 @@ func (r *accountRepository) SetRateLimited(ctx context.Context, id int64, resetA
return
err
}
func
(
r
*
accountRepository
)
SetAntigravityQuotaScopeLimit
(
ctx
context
.
Context
,
id
int64
,
scope
service
.
AntigravityQuotaScope
,
resetAt
time
.
Time
)
error
{
now
:=
time
.
Now
()
.
UTC
()
payload
:=
map
[
string
]
string
{
"rate_limited_at"
:
now
.
Format
(
time
.
RFC3339
),
"rate_limit_reset_at"
:
resetAt
.
UTC
()
.
Format
(
time
.
RFC3339
),
}
raw
,
err
:=
json
.
Marshal
(
payload
)
if
err
!=
nil
{
return
err
}
path
:=
"{antigravity_quota_scopes,"
+
string
(
scope
)
+
"}"
client
:=
clientFromContext
(
ctx
,
r
.
client
)
result
,
err
:=
client
.
ExecContext
(
ctx
,
"UPDATE accounts SET extra = jsonb_set(COALESCE(extra, '{}'::jsonb), $1::text[], $2::jsonb, true), updated_at = NOW() WHERE id = $3 AND deleted_at IS NULL"
,
path
,
raw
,
id
,
)
if
err
!=
nil
{
return
err
}
affected
,
err
:=
result
.
RowsAffected
()
if
err
!=
nil
{
return
err
}
if
affected
==
0
{
return
service
.
ErrAccountNotFound
}
return
nil
}
func
(
r
*
accountRepository
)
SetOverloaded
(
ctx
context
.
Context
,
id
int64
,
until
time
.
Time
)
error
{
_
,
err
:=
r
.
client
.
Account
.
Update
()
.
Where
(
dbaccount
.
IDEQ
(
id
))
.
...
...
@@ -718,6 +752,27 @@ func (r *accountRepository) ClearRateLimit(ctx context.Context, id int64) error
return
err
}
func
(
r
*
accountRepository
)
ClearAntigravityQuotaScopes
(
ctx
context
.
Context
,
id
int64
)
error
{
client
:=
clientFromContext
(
ctx
,
r
.
client
)
result
,
err
:=
client
.
ExecContext
(
ctx
,
"UPDATE accounts SET extra = COALESCE(extra, '{}'::jsonb) - 'antigravity_quota_scopes', updated_at = NOW() WHERE id = $1 AND deleted_at IS NULL"
,
id
,
)
if
err
!=
nil
{
return
err
}
affected
,
err
:=
result
.
RowsAffected
()
if
err
!=
nil
{
return
err
}
if
affected
==
0
{
return
service
.
ErrAccountNotFound
}
return
nil
}
func
(
r
*
accountRepository
)
UpdateSessionWindow
(
ctx
context
.
Context
,
id
int64
,
start
,
end
*
time
.
Time
,
status
string
)
error
{
builder
:=
r
.
client
.
Account
.
Update
()
.
Where
(
dbaccount
.
IDEQ
(
id
))
.
...
...
@@ -831,6 +886,11 @@ func (r *accountRepository) BulkUpdate(ctx context.Context, ids []int64, updates
args
=
append
(
args
,
*
updates
.
Status
)
idx
++
}
if
updates
.
Schedulable
!=
nil
{
setClauses
=
append
(
setClauses
,
"schedulable = $"
+
itoa
(
idx
))
args
=
append
(
args
,
*
updates
.
Schedulable
)
idx
++
}
// JSONB 需要合并而非覆盖,使用 raw SQL 保持旧行为。
if
len
(
updates
.
Credentials
)
>
0
{
payload
,
err
:=
json
.
Marshal
(
updates
.
Credentials
)
...
...
backend/internal/repository/api_key_repo.go
View file @
61f55674
...
...
@@ -26,13 +26,21 @@ func (r *apiKeyRepository) activeQuery() *dbent.APIKeyQuery {
}
func
(
r
*
apiKeyRepository
)
Create
(
ctx
context
.
Context
,
key
*
service
.
APIKey
)
error
{
created
,
er
r
:=
r
.
client
.
APIKey
.
Create
()
.
builde
r
:=
r
.
client
.
APIKey
.
Create
()
.
SetUserID
(
key
.
UserID
)
.
SetKey
(
key
.
Key
)
.
SetName
(
key
.
Name
)
.
SetStatus
(
key
.
Status
)
.
SetNillableGroupID
(
key
.
GroupID
)
.
Save
(
ctx
)
SetNillableGroupID
(
key
.
GroupID
)
if
len
(
key
.
IPWhitelist
)
>
0
{
builder
.
SetIPWhitelist
(
key
.
IPWhitelist
)
}
if
len
(
key
.
IPBlacklist
)
>
0
{
builder
.
SetIPBlacklist
(
key
.
IPBlacklist
)
}
created
,
err
:=
builder
.
Save
(
ctx
)
if
err
==
nil
{
key
.
ID
=
created
.
ID
key
.
CreatedAt
=
created
.
CreatedAt
...
...
@@ -108,6 +116,18 @@ func (r *apiKeyRepository) Update(ctx context.Context, key *service.APIKey) erro
builder
.
ClearGroupID
()
}
// IP 限制字段
if
len
(
key
.
IPWhitelist
)
>
0
{
builder
.
SetIPWhitelist
(
key
.
IPWhitelist
)
}
else
{
builder
.
ClearIPWhitelist
()
}
if
len
(
key
.
IPBlacklist
)
>
0
{
builder
.
SetIPBlacklist
(
key
.
IPBlacklist
)
}
else
{
builder
.
ClearIPBlacklist
()
}
affected
,
err
:=
builder
.
Save
(
ctx
)
if
err
!=
nil
{
return
err
...
...
@@ -268,14 +288,16 @@ func apiKeyEntityToService(m *dbent.APIKey) *service.APIKey {
return
nil
}
out
:=
&
service
.
APIKey
{
ID
:
m
.
ID
,
UserID
:
m
.
UserID
,
Key
:
m
.
Key
,
Name
:
m
.
Name
,
Status
:
m
.
Status
,
CreatedAt
:
m
.
CreatedAt
,
UpdatedAt
:
m
.
UpdatedAt
,
GroupID
:
m
.
GroupID
,
ID
:
m
.
ID
,
UserID
:
m
.
UserID
,
Key
:
m
.
Key
,
Name
:
m
.
Name
,
Status
:
m
.
Status
,
IPWhitelist
:
m
.
IPWhitelist
,
IPBlacklist
:
m
.
IPBlacklist
,
CreatedAt
:
m
.
CreatedAt
,
UpdatedAt
:
m
.
UpdatedAt
,
GroupID
:
m
.
GroupID
,
}
if
m
.
Edges
.
User
!=
nil
{
out
.
User
=
userEntityToService
(
m
.
Edges
.
User
)
...
...
Prev
1
2
3
4
5
6
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment