Browse Source

style: 修复一些代码格式问题

pull/64/head
zhengkunwang223 2 years ago committed by zhengkunwang223
parent
commit
bfbd56de0c
  1. 4
      backend/utils/docker/docker.go
  2. 7
      backend/utils/files/fileinfo.go
  3. 5
      backend/utils/ssl/acme.go
  4. 96
      backend/utils/ssl/acme_test.go
  5. 29
      backend/utils/ssl/obtain_err.go

4
backend/utils/docker/docker.go

@ -66,12 +66,10 @@ func (c Client) CreateNetwork(name string) error {
func (c Client) NetworkExist(name string) bool {
var options types.NetworkListOptions
var array []filters.KeyValuePair
array = append(array, filters.Arg("name", name))
options.Filters = filters.NewArgs(filters.Arg("name", name))
networks, err := c.cli.NetworkList(context.Background(), options)
if err != nil {
return false
}
return len(networks) > 0
}

7
backend/utils/files/fileinfo.go

@ -182,7 +182,7 @@ func (f *FileInfo) getContent() error {
}
func detectBinary(buf []byte) bool {
var whiteByte int = 0
whiteByte := 0
n := min(1024, len(buf))
for i := 0; i < n; i++ {
if (buf[i] >= 0x20) || buf[i] == 9 || buf[i] == 10 || buf[i] == 13 {
@ -192,10 +192,7 @@ func detectBinary(buf []byte) bool {
}
}
if whiteByte >= 1 {
return false
}
return true
return whiteByte < 1
}
func min(x, y int) int {

5
backend/utils/ssl/acme.go

@ -11,6 +11,11 @@ import (
"github.com/go-acme/lego/v4/registration"
)
type domainError struct {
Domain string
Error error
}
func GetPrivateKey(priKey crypto.PrivateKey) []byte {
rsaKey := priKey.(*rsa.PrivateKey)
derStream := x509.MarshalPKCS1PrivateKey(rsaKey)

96
backend/utils/ssl/acme_test.go

@ -6,18 +6,13 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/cenkalti/backoff/v4"
"github.com/go-acme/lego/v4/acme"
"github.com/go-acme/lego/v4/acme/api"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate"
@ -38,6 +33,11 @@ func (p *plainDnsProvider) Present(domain, token, keyAuth string) error {
return nil
}
func (p *plainDnsProvider) CleanUp(domain, token, keyAuth string) error {
fmt.Printf("%s,%s,%s", domain, token, keyAuth)
return nil
}
func TestCreatePrivate(t *testing.T) {
priKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
@ -57,92 +57,6 @@ func TestCreatePrivate(t *testing.T) {
}
}
func checkChallengeStatus(chlng acme.ExtendedChallenge) (bool, error) {
switch chlng.Status {
case acme.StatusValid:
return true, nil
case acme.StatusPending, acme.StatusProcessing:
return false, nil
case acme.StatusInvalid:
return false, chlng.Error
default:
return false, errors.New("the server returned an unexpected state")
}
}
func checkAuthorizationStatus(authz acme.Authorization) (bool, error) {
switch authz.Status {
case acme.StatusValid:
return true, nil
case acme.StatusPending, acme.StatusProcessing:
return false, nil
case acme.StatusDeactivated, acme.StatusExpired, acme.StatusRevoked:
return false, fmt.Errorf("the authorization state %s", authz.Status)
case acme.StatusInvalid:
for _, chlg := range authz.Challenges {
if chlg.Status == acme.StatusInvalid && chlg.Error != nil {
return false, chlg.Error
}
}
return false, fmt.Errorf("the authorization state %s", authz.Status)
default:
return false, errors.New("the server returned an unexpected state")
}
}
func validate(core *api.Core, domain string, chlg acme.Challenge) error {
chlng, err := core.Challenges.New(chlg.URL)
if err != nil {
return fmt.Errorf("failed to initiate challenge: %w", err)
}
valid, err := checkChallengeStatus(chlng)
if err != nil {
return err
}
if valid {
return nil
}
ra, err := strconv.Atoi(chlng.RetryAfter)
if err != nil {
// The ACME server MUST return a Retry-After.
// If it doesn't, we'll just poll hard.
// Boulder does not implement the ability to retry challenges or the Retry-After header.
// https://github.com/letsencrypt/boulder/blob/master/docs/acme-divergences.md#section-82
ra = 5
}
initialInterval := time.Duration(ra) * time.Second
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = initialInterval
bo.MaxInterval = 10 * initialInterval
bo.MaxElapsedTime = 100 * initialInterval
// After the path is sent, the ACME server will access our server.
// Repeatedly check the server for an updated status on our request.
operation := func() error {
authz, err := core.Authorizations.Get(chlng.AuthorizationURL)
if err != nil {
return backoff.Permanent(err)
}
valid, err := checkAuthorizationStatus(authz)
if err != nil {
return backoff.Permanent(err)
}
if valid {
return nil
}
return errors.New("the server didn't respond to our request")
}
return backoff.Retry(operation, bo)
}
func TestSSL(t *testing.T) {
//// 本地ACME测试用

29
backend/utils/ssl/obtain_err.go

@ -1,29 +0,0 @@
package ssl
import (
"bytes"
"fmt"
"sort"
)
type obtainError map[string]error
func (e obtainError) Error() string {
buffer := bytes.NewBufferString("error: one or more domains had a problem:\n")
var domains []string
for domain := range e {
domains = append(domains, domain)
}
sort.Strings(domains)
for _, domain := range domains {
buffer.WriteString(fmt.Sprintf("[%s] %s\n", domain, e[domain]))
}
return buffer.String()
}
type domainError struct {
Domain string
Error error
}
Loading…
Cancel
Save