Files
Reims-Go/internal/app/qqgroup.go
T
Nwflower 181c616909 feat(plugin,ui): consolidate builtin.core, enforce Guest Tools toggle, and add QQ group entry
- Merge essential builtins (driver, default theme, start prep, host workarounds) into always-on system plugin builtin.core, with state.json and arg-ledger migration plus app-scoped workaround settings honored by both GUI and CLI.
- Ensure disabling builtin.bridge.guest-tools stops the localhost bridge without being restarted by feature callers, fix guesttools.Server Start/Close race, and reflect the disabled state in VMPage and SettingsModal.
- Add QQ community group entry in About settings with Windows tencent:// protocol detection, cached invite redirect resolution, and copy-ID fallback.
- Update docs/plugin-architecture.md, Wails bindings, and i18n strings.
2026-11-29 09:54:51 -07:00

199 lines
4.8 KiB
Go

package app
import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/Nwflower/reims-go/internal/netproxy"
)
const (
qqGroupID = "1136042744"
qqGroupShortURL = "https://qm.qq.com/q/TBmRJlVbSo"
qqGroupAuthCacheTTL = 10 * time.Minute
qqGroupLookupTimeout = 5 * time.Second
)
var errQQNotInstalled = errors.New("qq not installed")
type qqGroupAuth struct {
groupCode string
authKey string
}
type qqGroupWPAParam struct {
GroupUin string `json:"groupUin"`
TimeStamp int64 `json:"timeStamp"`
AuthKey string `json:"authKey"`
Auth string `json:"auth"`
}
var qqGroupAuthCache struct {
sync.Mutex
at time.Time
auth *qqGroupAuth
}
// CanOpenQQGroup reports whether a local QQ protocol handler is installed and
// ready to be launched directly without opening a browser.
func (a *App) CanOpenQQGroup() bool {
return hasQQProtocolHandler()
}
// OpenQQGroup launches the local QQ desktop client directly to the community
// group join dialog via the tencent://groupwpa/ protocol. It returns an error
// rather than falling back to a web browser when QQ is not installed or the
// invite token cannot be resolved.
func (a *App) OpenQQGroup() error {
if !hasQQProtocolHandler() {
return errQQNotInstalled
}
auth, err := resolveQQGroupAuth(a.baseCtx())
if err != nil {
return err
}
schemeURL := buildQQGroupSchemeURL(auth.groupCode, auth.authKey, time.Now())
return launchQQProtocol(schemeURL)
}
func resolveQQGroupAuth(ctx context.Context) (qqGroupAuth, error) {
qqGroupAuthCache.Lock()
if qqGroupAuthCache.auth != nil && time.Since(qqGroupAuthCache.at) < qqGroupAuthCacheTTL {
cached := *qqGroupAuthCache.auth
qqGroupAuthCache.Unlock()
return cached, nil
}
qqGroupAuthCache.Unlock()
if ctx == nil {
ctx = context.Background()
}
reqCtx, cancel := context.WithTimeout(ctx, qqGroupLookupTimeout)
defer cancel()
req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, qqGroupShortURL, nil)
if err != nil {
return qqGroupAuth{}, err
}
client := &http.Client{
Transport: netproxy.NewTransport(),
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
if err != nil {
return qqGroupAuth{}, fmt.Errorf("resolve QQ group invite: %w", err)
}
defer resp.Body.Close()
location := strings.TrimSpace(resp.Header.Get("Location"))
if location == "" {
return qqGroupAuth{}, fmt.Errorf("QQ group invite did not redirect (HTTP %d)", resp.StatusCode)
}
auth, err := parseQQGroupRedirect(location)
if err != nil {
return qqGroupAuth{}, err
}
qqGroupAuthCache.Lock()
copyAuth := auth
qqGroupAuthCache.at = time.Now()
qqGroupAuthCache.auth = &copyAuth
qqGroupAuthCache.Unlock()
return auth, nil
}
func parseQQGroupRedirect(location string) (qqGroupAuth, error) {
u, err := url.Parse(strings.TrimSpace(location))
if err != nil {
return qqGroupAuth{}, fmt.Errorf("parse QQ group redirect: %w", err)
}
if !strings.EqualFold(u.Scheme, "https") {
return qqGroupAuth{}, errors.New("unexpected non-HTTPS QQ group redirect")
}
q := u.Query()
authKey := strings.TrimSpace(q.Get("authKey"))
if authKey == "" {
return qqGroupAuth{}, errors.New("QQ group redirect missing authKey")
}
groupCode := qqGroupID
if busiData := strings.TrimSpace(q.Get("busi_data")); busiData != "" {
if decoded, ok := decodeBase64Flexible(busiData); ok {
var payload struct {
GroupCode string `json:"groupCode"`
}
if json.Unmarshal(decoded, &payload) == nil && isDigits(payload.GroupCode) {
groupCode = payload.GroupCode
}
}
}
return qqGroupAuth{
groupCode: groupCode,
authKey: authKey,
}, nil
}
func buildQQGroupSchemeURL(groupUin, authKey string, now time.Time) string {
raw, _ := json.Marshal(qqGroupWPAParam{
GroupUin: groupUin,
TimeStamp: now.UnixMilli(),
AuthKey: authKey,
Auth: "",
})
return "tencent://groupwpa/?subcmd=all&param=" + strings.ToUpper(hex.EncodeToString(raw)) + "&jump_from="
}
func decodeBase64Flexible(s string) ([]byte, bool) {
for _, enc := range []*base64.Encoding{
base64.StdEncoding,
base64.RawStdEncoding,
base64.URLEncoding,
base64.RawURLEncoding,
} {
if b, err := enc.DecodeString(s); err == nil {
return b, true
}
}
return nil, false
}
func isDigits(s string) bool {
if s == "" {
return false
}
for i := 0; i < len(s); i++ {
if s[i] < '0' || s[i] > '9' {
return false
}
}
return true
}
func extractCommandExecutable(cmd string) string {
cmd = strings.TrimSpace(cmd)
if cmd == "" {
return ""
}
if cmd[0] == '"' {
if end := strings.IndexByte(cmd[1:], '"'); end > 0 {
return strings.TrimSpace(cmd[1 : 1+end])
}
return ""
}
if sp := strings.IndexAny(cmd, " \t"); sp > 0 {
return cmd[:sp]
}
return cmd
}