go unioffice

unioffice
inernal/license/license.go:

package license

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"time"

	"github.com/unidoc/unioffice/common"
)

func GetMeteredState() (MeteredStatus, error) {
	status := MeteredStatus{OK: true, Credits: 100, Used: 0}
	return status, nil
}

func TrackUse(useKey string) {
}

func (lk *LicenseKey) TypeToString() string {
	if lk.Subscription {
		return "Metered subscription"
	}
	if lk.Tier == LicenseTierUnlicensed {
		return "Unlicensed"
	}
	if lk.Tier == LicenseTierCommunity {
		return "AGPLv3 Open Source Community License"
	}
	if lk.Tier == LicenseTierIndividual || lk.Tier == "indie" {
		return "Commercial License - Individual"
	}
	return "Commercial License - Business"
}

func MakeUnlicensedKey() *LicenseKey {
	lk := LicenseKey{}
	lk.CustomerName = "Licensed"
	lk.Tier = LicenseTierBusiness
	lk.CreatedAt = time.Now().UTC()
	lk.CreatedAtInt = lk.CreatedAt.Unix()
	lk.UniHTML = true
	lk.UniPDF = true
	lk.UniOffice = true
	return &lk
}

type MeteredStatus struct {
	OK      bool
	Credits int64
	Used    int64
}

func SetLicenseKey(content string, customerName string) error {
	return nil
}

type LegacyLicenseType byte

const (
	LicenseTierUnlicensed = "unlicensed"
	LicenseTierCommunity  = "community"
	LicenseTierIndividual = "individual"
	LicenseTierBusiness   = "business"
)

var unlicenseKey = MakeUnlicensedKey()

func Track(docKey string, useKey string) error { return nil }

func SetMeteredKey(apiKey string) error {
	lk := &LicenseKey{Subscription: true, ApiKey: apiKey, IsPersitentCache: false}
	unlicenseKey = lk
	return nil
}

func (ll LegacyLicense) Verify(pubKey *rsa.PublicKey) error {
	return nil
}

func GetLicenseKey() *LicenseKey {
	return unlicenseKey
}

func SetMeteredKeyPersistentCache(val bool) { unlicenseKey.IsPersitentCache = val }

type meteredStatusResp struct {
	Valid        bool  `json:"valid"`
	OrgCredits   int64 `json:"org_credits"`
	OrgUsed      int64 `json:"org_used"`
	OrgRemaining int64 `json:"org_remaining"`
}

type LegacyLicense struct {
	Name        string
	Signature   string `json:",omitempty"`
	Expiration  time.Time
	LicenseType LegacyLicenseType
}

func GenRefId(prefix string) (string, error) {
	var buffer bytes.Buffer
	buffer.WriteString(prefix)
	bytes := make([]byte, 8+16)
	now := time.Now().UTC().UnixNano()
	binary.BigEndian.PutUint64(bytes, uint64(now))
	_, last := rand.Read(bytes[8:])
	if last != nil {
		return "", last
	}
	buffer.WriteString(hex.EncodeToString(bytes))
	return buffer.String(), nil
}

func SetLegacyLicenseKey(s string) error {
	return nil
}

type LicenseKey struct {
	LicenseId        string    `json:"license_id"`
	CustomerId       string    `json:"customer_id"`
	CustomerName     string    `json:"customer_name"`
	Tier             string    `json:"tier"`
	CreatedAt        time.Time `json:"-"`
	CreatedAtInt     int64     `json:"created_at"`
	ExpiresAt        time.Time `json:"-"`
	ExpiresAtInt     int64     `json:"expires_at"`
	CreatedBy        string    `json:"created_by"`
	CreatorName      string    `json:"creator_name"`
	CreatorEmail     string    `json:"creator_email"`
	UniPDF           bool      `json:"unipdf"`
	UniOffice        bool      `json:"unioffice"`
	UniHTML          bool      `json:"unihtml"`
	Trial            bool      `json:"trial"`
	Subscription     bool
	ApiKey           string
	IsPersitentCache bool
}

func (lk *LicenseKey) ToString() string {
	if lk.Subscription {
		return "Metered subscription"
	}
	result := fmt.Sprintf("License Id: %s\n", lk.LicenseId)
	result += fmt.Sprintf("Customer Id: %s\n", lk.CustomerId)
	result += fmt.Sprintf("Customer Name: %s\n", lk.CustomerName)
	result += fmt.Sprintf("Tier: %s\n", lk.Tier)
	result += fmt.Sprintf("Created At: %s\n", common.UtcTimeFormat(lk.CreatedAt))
	if lk.ExpiresAt.IsZero() {
		result += "Expires At: Never\n"
	} else {
		result += fmt.Sprintf("Expires At: %s\n", common.UtcTimeFormat(lk.ExpiresAt))
	}
	result += fmt.Sprintf("Creator: %s <%s>\n", lk.CreatorName, lk.CreatorEmail)
	return result
}

func (lk *LicenseKey) IsLicensed() bool {
	return true
}
posted @ 2025-03-23 09:42  卓能文  阅读(72)  评论(0)    收藏  举报