93 lines
2 KiB
Go
93 lines
2 KiB
Go
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
// that can be found in the LICENSE file.
|
|
|
|
// +build !nolimit
|
|
|
|
package license
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/drone/drone/core"
|
|
"github.com/drone/go-license/license"
|
|
"github.com/drone/go-license/license/licenseutil"
|
|
)
|
|
|
|
// embedded public key used to verify license signatures.
|
|
var publicKey = []byte("GB/hFnXEg63vDZ2W6mKFhLxZTuxMrlN/C/0iVZ2LfPQ=")
|
|
|
|
// License renewal endpoint.
|
|
const licenseEndpoint = "https://license.drone.io/api/v1/license/renew"
|
|
|
|
// Trial returns a default license with trial terms based
|
|
// on the source code management system.
|
|
func Trial(provider string) *core.License {
|
|
switch provider {
|
|
case "gitea", "gogs":
|
|
return &core.License{
|
|
Kind: core.LicenseTrial,
|
|
Repos: 0,
|
|
Users: 0,
|
|
Builds: 0,
|
|
Nodes: 0,
|
|
}
|
|
default:
|
|
return &core.License{
|
|
Kind: core.LicenseTrial,
|
|
Repos: 0,
|
|
Users: 0,
|
|
Builds: 15000,
|
|
Nodes: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
// Load loads the license from file.
|
|
func Load(path string) (*core.License, error) {
|
|
pub, err := licenseutil.DecodePublicKey(publicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
decoded, err := license.DecodeFile(path, pub)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if decoded.Expired() {
|
|
// if the license is expired we should check the license
|
|
// server to see if the license has been renewed. If yes
|
|
// we will load the renewed license.
|
|
|
|
buf := new(bytes.Buffer)
|
|
json.NewEncoder(buf).Encode(decoded)
|
|
res, err := http.Post(licenseEndpoint, "application/json", buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
raw, err := ioutil.ReadAll(res.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
decoded, err = license.Decode(raw, pub)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
license := new(core.License)
|
|
license.Expires = decoded.Exp
|
|
err = json.Unmarshal(decoded.Dat, license)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return license, err
|
|
}
|