github.com/annwntech/go-micro/v2@v2.9.5/helper/helper.go (about)

     1  package helper
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"crypto/x509"
     7  	"errors"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"net/http"
    11  	"strings"
    12  
    13  	"github.com/annwntech/go-micro/v2/metadata"
    14  	"github.com/urfave/cli/v2"
    15  )
    16  
    17  func ACMEHosts(ctx *cli.Context) []string {
    18  	var hosts []string
    19  	for _, host := range strings.Split(ctx.String("acme_hosts"), ",") {
    20  		if len(host) > 0 {
    21  			hosts = append(hosts, host)
    22  		}
    23  	}
    24  	return hosts
    25  }
    26  
    27  func RequestToContext(r *http.Request) context.Context {
    28  	ctx := context.Background()
    29  	md := make(metadata.Metadata)
    30  	for k, v := range r.Header {
    31  		md[k] = strings.Join(v, ",")
    32  	}
    33  	return metadata.NewContext(ctx, md)
    34  }
    35  
    36  func TLSConfig(ctx *cli.Context) (*tls.Config, error) {
    37  	cert := ctx.String("tls_cert_file")
    38  	key := ctx.String("tls_key_file")
    39  	ca := ctx.String("tls_client_ca_file")
    40  
    41  	if len(cert) > 0 && len(key) > 0 {
    42  		certs, err := tls.LoadX509KeyPair(cert, key)
    43  		if err != nil {
    44  			return nil, err
    45  		}
    46  
    47  		if len(ca) > 0 {
    48  			caCert, err := ioutil.ReadFile(ca)
    49  			if err != nil {
    50  				return nil, err
    51  			}
    52  
    53  			caCertPool := x509.NewCertPool()
    54  			caCertPool.AppendCertsFromPEM(caCert)
    55  
    56  			return &tls.Config{
    57  				Certificates: []tls.Certificate{certs},
    58  				ClientCAs:    caCertPool,
    59  				ClientAuth:   tls.RequireAndVerifyClientCert,
    60  				NextProtos:   []string{"h2", "http/1.1"},
    61  			}, nil
    62  		}
    63  
    64  		return &tls.Config{
    65  			Certificates: []tls.Certificate{certs}, NextProtos: []string{"h2", "http/1.1"},
    66  		}, nil
    67  	}
    68  
    69  	return nil, errors.New("TLS certificate and key files not specified")
    70  }
    71  
    72  // UnexpectedSubcommand checks for erroneous subcommands and prints help and returns error
    73  func UnexpectedSubcommand(ctx *cli.Context) error {
    74  	if first := Subcommand(ctx); first != "" {
    75  		// received something that isn't a subcommand
    76  		return cli.Exit(fmt.Sprintf("Unrecognized subcommand for %s: %s. Please refer to '%s --help'", ctx.App.Name, first, ctx.App.Name), 1)
    77  	}
    78  	return cli.ShowSubcommandHelp(ctx)
    79  }
    80  
    81  func UnexpectedCommand(ctx *cli.Context) error {
    82  	commandName := ctx.Args().First()
    83  	return cli.Exit(fmt.Sprintf("Unrecognized micro command: %s. Please refer to 'micro --help'", commandName), 1)
    84  }
    85  
    86  func MissingCommand(ctx *cli.Context) error {
    87  	return cli.Exit(fmt.Sprintf("No command provided to micro. Please refer to 'micro --help'"), 1)
    88  }
    89  
    90  // MicroSubcommand returns the subcommand name
    91  func Subcommand(ctx *cli.Context) string {
    92  	return ctx.Args().First()
    93  }