mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
fix: use canonical OpenCode session IDs for Zen free tier
Upstream began rejecting anonymous free-tier requests whose
x-opencode-session does not match ses_[0-9a-f]{12}[0-9A-Za-z]{14}
with 403 FreeTierError. Map every downstream identity into that
shape deterministically (preserving native sessions), and bump
the impersonated client version to 1.18.31.
This commit is contained in:
1 parent
46def7fedc
commit
8185202382
2 files changed
+38
-2
No files matched your search
@@ -6,5 +6,5 @@ import (
|
||||
)
|
||||
|
||||
func UserAgent() string {
|
||||
return fmt.Sprintf("opencode/1.18.21 (%s %s; %s)", runtime.GOOS, runtime.GOARCH, runtime.Version())
|
||||
return fmt.Sprintf("opencode/1.18.31 (%s %s; %s)", runtime.GOOS, runtime.GOARCH, runtime.Version())
|
||||
}
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"opencode2api/internal/jsonutil"
|
||||
)
|
||||
@@ -40,7 +42,7 @@ func DeriveRequestIDs(r *http.Request, body map[string]any) RequestIDs {
|
||||
if signal == "" || signal == `{}` {
|
||||
signal = RandomID("fallback", 16)
|
||||
}
|
||||
session := StableID("ses", signal)
|
||||
session := CanonicalSessionID(signal)
|
||||
projectSignal := jsonutil.FirstString(r.Header.Get("x-opencode-project"), jsonutil.StringAt(body, "metadata", "project_id"))
|
||||
if projectSignal == "" {
|
||||
projectSignal = "opencode2api:default-project"
|
||||
@@ -81,6 +83,40 @@ func StableID(prefix, value string) string {
|
||||
return prefix + "_" + hex.EncodeToString(sum[:12])
|
||||
}
|
||||
|
||||
// canonicalSessionPattern matches OpenCode's canonical session format:
|
||||
// "ses_" + 12 lowercase hex timestamp characters + 14 Base62 characters.
|
||||
// Since 2026-09-16 the Zen free tier (Authorization: Bearer public) rejects
|
||||
// any other session shape with 403 FreeTierError.
|
||||
var canonicalSessionPattern = regexp.MustCompile(`^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$`)
|
||||
|
||||
const base62Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
// CanonicalSessionID returns signal unchanged when it already carries an
|
||||
// official OpenCode session (preserving upstream prompt-cache affinity).
|
||||
// Any other downstream identity (UUIDs, foreign client sessions, legacy
|
||||
// gateway sessions, conversation seeds) is deterministically hashed into the
|
||||
// canonical shape so the same conversation keeps a stable session.
|
||||
func CanonicalSessionID(signal string) string {
|
||||
if canonicalSessionPattern.MatchString(signal) {
|
||||
return signal
|
||||
}
|
||||
sum := sha256.Sum256([]byte("ses\x00" + signal))
|
||||
timePart := hex.EncodeToString(sum[:6])
|
||||
randomPart := base62Fixed(new(big.Int).SetBytes(sum[6:16]), 14)
|
||||
return "ses_" + timePart + randomPart
|
||||
}
|
||||
|
||||
func base62Fixed(n *big.Int, width int) string {
|
||||
base := big.NewInt(62)
|
||||
out := make([]byte, width)
|
||||
remainder := new(big.Int)
|
||||
for i := width - 1; i >= 0; i-- {
|
||||
n.DivMod(n, base, remainder)
|
||||
out[i] = base62Alphabet[remainder.Int64()]
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
|
||||
func RandomID(prefix string, size int) string {
|
||||
buf := make([]byte, size)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
|
||||
Reference in new issue
Block a user