netflag

package module
v0.0.0-...-f43cf22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

Package netflag implements flags for network client and server

GoDoc

Server Example
package main

import (
	"errors"
	"fmt"
	"io"
	"os"
	"time"

	"github.com/takumakei/go-delint"
	"github.com/takumakei/go-exit"
	"github.com/takumakei/go-urfave-cli/clix"
	"github.com/takumakei/go-urfave-cli/netflag"
	"github.com/urfave/cli/v2"
)

func main() {
	server := netflag.NewServer(clix.FlagPrefix("SERVER_"))

	app := cli.NewApp()
	app.Flags = server.Flags()
	app.Before = server.Before
	app.Action = func(c *cli.Context) error {
		lis, err := server.Listen()
		if err != nil {
			return err
		}
		defer delint.AnywayFunc(lis.Close)

		for {
			conn, err := lis.Accept()
			if err != nil {
				fmt.Fprintln(c.App.ErrWriter, err)
				conn.Close()
				continue
			}
			_, err = conn.Write([]byte(time.Now().Format(time.RFC3339Nano) + "\n"))
			if err != nil && !errors.Is(err, io.EOF) {
				fmt.Fprintln(c.App.ErrWriter, err)
			}
			err = conn.Close()
			if err != nil {
				fmt.Fprintln(c.App.ErrWriter, err)
			}
		}
	}
	exit.Exit(app.Run(os.Args))
}
output
$ go run ./examples/server
NAME:
   server - A new cli application

USAGE:
   server [global options] command [command options] [arguments...]

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --address value, --addr value            address to listen [$SERVER_ADDRESS, $SERVER_ADDR]
   --tls-cert file, --tlscrt file           certificate file [$SERVER_TLS_CERT, $SERVER_TLSCRT]
   --tls-cert-key file, --tlskey file       private key file of certificate [$SERVER_TLS_CERT_KEY, $SERVER_TLSKEY]
   --tls-gen-cert, --tlsgen                 generate self-signed certificate (default: false) [$SERVER_TLS_GEN_CERT, $SERVER_TLSGEN]
   --tls-ca file, --tlsca file              root CA file for client auth [$SERVER_TLS_CA, $SERVER_TLSCA]
   --tls-min-version value, --tlsmin value  TLS minimum version (default: 1.2) [$SERVER_TLS_MIN_VERSION, $SERVER_TLSMIN]
   --tls-max-version value, --tlsmax value  TLS maximum version (default: 1.3) [$SERVER_TLS_MAX_VERSION, $SERVER_TLSMAX]
   --help, -h                               show help (default: false)
error: Required flag "addr" not set
exit status 1
$
Client Example
package main

import (
	"errors"
	"io"
	"os"

	"github.com/takumakei/go-delint"
	"github.com/takumakei/go-exit"
	"github.com/takumakei/go-urfave-cli/clix"
	"github.com/takumakei/go-urfave-cli/netflag"
	"github.com/urfave/cli/v2"
)

func main() {
	client := netflag.NewClient(clix.FlagPrefix("CLIENT_"))

	app := cli.NewApp()
	app.Flags = client.Flags()
	app.Before = client.Before
	app.Action = func(c *cli.Context) error {
		conn, err := client.Dial()
		if err != nil {
			return err
		}
		defer delint.AnywayFunc(conn.Close)

		p := make([]byte, 2048)
		for {
			n, err := conn.Read(p)
			if err != nil {
				if errors.Is(err, io.EOF) {
					return nil
				}
				return err
			}
			c.App.Writer.Write(p[:n])
		}
	}
	exit.Exit(app.Run(os.Args))
}
output
$ go run ./examples/client
NAME:
   client - A new cli application

USAGE:
   client [global options] command [command options] [arguments...]

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --address value, --addr value            address to connect [$CLIENT_ADDRESS, $CLIENT_ADDR]
   --tls-cert file, --tlscrt file           certificate file [$CLIENT_TLS_CERT, $CLIENT_TLSCRT]
   --tls-cert-key file, --tlskey file       private key file of certificate [$CLIENT_TLS_CERT_KEY, $CLIENT_TLSKEY]
   --tls-ca file, --tlsca file              root CA file of server [$CLIENT_TLS_CA, $CLIENT_TLSCA]
   --tls-server-name value, --tlssrv value  server name for verification [$CLIENT_TLS_SERVER_NAME, $CLIENT_TLSSRV]
   --tls-skip-verify, --tlsinsecure         TLS insecure skip verify (default: false) [$CLIENT_TLS_SKIP_VERIFY, $CLIENT_TLSINSECURE]
   --tls-min-version value, --tlsmin value  TLS minimum version (default: 1.2) [$CLIENT_TLS_MIN_VERSION, $CLIENT_TLSMIN]
   --tls-max-version value, --tlsmax value  TLS maximum version (default: 1.3) [$CLIENT_TLS_MAX_VERSION, $CLIENT_TLSMAX]
   --help, -h                               show help (default: false)
error: Required flag "addr" not set
exit status 1
$

Documentation

Overview

Package netflag implements functions to use network with github.com/urfave/cli/v2.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableGenCert

func DisableGenCert(c *config)

DisableGenCert is the option not to use FlagTLSGenCert.

func DisableTLS

func DisableTLS(c *config)

DisableTLS is the option not use flags related to TLS.

func EnableGenCert

func EnableGenCert(c *config)

EnableGenCert is the option to use FlagTLSGenCert.

func EnableTLS

func EnableTLS(c *config)

EnableTLS is the option to use flags related to TLS.

Types

type Client

type Client struct {
	// Name is the name of the Client, may be empty string.
	Name string

	// PredeterminedFlagNetwork is true if the value of FlagNetwork is predetermined by the option.
	PredeterminedFlagNetwork bool

	// DisableTLS is true if TLS is disabled.
	DisableTLS bool

	// FlagNetwork is the network to connect.
	FlagNetwork *cli.StringFlag

	// FlagAddress is the address to connect.
	FlagAddress *cli.StringFlag

	// FlagTLSCerts is the certificate filepath of the client.
	FlagTLSCerts *cli.StringSliceFlag

	// FlagTLSKeys is the private key filepath of the certificate.
	FlagTLSKeys *cli.StringSliceFlag

	// FlagTLSCAs is the certificate filepath of the RootCAs.
	FlagTLSCAs *cli.StringSliceFlag

	// FlagTLSServerName is the value of server name.
	FlagTLSServerName *cli.StringFlag

	// FlagTLSSkipVerify is value to InsecureSkipVerify.
	FlagTLSSkipVerify *cli.BoolFlag

	// FlagTLSMinVer is the minimum TLS version that is acceptable.
	FlagTLSMinVer *cli.GenericFlag

	// FlagTLSMaxVer is the maximum TLS version that is acceptable.
	FlagTLSMaxVer *cli.GenericFlag

	// FlagSet is clix.FlagSet.
	FlagSet clix.FlagSet
}

Client represents the flags related to a client to connect a server.

Example
clientFlags := netflag.NewClientName(
	clix.FlagPrefix("EXAMPLE_"), "echo",
	netflag.Network("udp", "*"),
	netflag.Address("127.0.0.1:9000"),
)

app := cli.NewApp()
app.Name = "example"
app.HelpName = "example"
app.Flags = clientFlags.Flags()
app.Before = clientFlags.Before
app.Action = func(c *cli.Context) error {
	fmt.Println("network[" + clientFlags.Network() + "]")
	fmt.Println("address[" + clientFlags.Address() + "]")
	return nil
}

_ = app.Run([]string{"example", "--help"})
Output:

NAME:
   example - A new cli application

USAGE:
   example [global options] command [command options] [arguments...]

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --echo-network value, --echo-net value             network to connect (default: "udp") [$EXAMPLE_ECHO_NETWORK, $EXAMPLE_ECHO_NET]
   --echo-address value, --echo-addr value            address to connect (default: "127.0.0.1:9000") [$EXAMPLE_ECHO_ADDRESS, $EXAMPLE_ECHO_ADDR]
   --echo-tls-cert file, --echo-tlscrt file           certificate file [$EXAMPLE_ECHO_TLS_CERT, $EXAMPLE_ECHO_TLSCRT]
   --echo-tls-cert-key file, --echo-tlskey file       private key file of certificate [$EXAMPLE_ECHO_TLS_CERT_KEY, $EXAMPLE_ECHO_TLSKEY]
   --echo-tls-ca file, --echo-tlsca file              root CA file of server [$EXAMPLE_ECHO_TLS_CA, $EXAMPLE_ECHO_TLSCA]
   --echo-tls-server-name value, --echo-tlssrv value  server name for verification [$EXAMPLE_ECHO_TLS_SERVER_NAME, $EXAMPLE_ECHO_TLSSRV]
   --echo-tls-skip-verify, --echo-tlsinsecure         TLS insecure skip verify (default: false) [$EXAMPLE_ECHO_TLS_SKIP_VERIFY, $EXAMPLE_ECHO_TLSINSECURE]
   --echo-tls-min-version value, --echo-tlsmin value  TLS minimum version (default: 1.2) [$EXAMPLE_ECHO_TLS_MIN_VERSION, $EXAMPLE_ECHO_TLSMIN]
   --echo-tls-max-version value, --echo-tlsmax value  TLS maximum version (default: 1.3) [$EXAMPLE_ECHO_TLS_MAX_VERSION, $EXAMPLE_ECHO_TLSMAX]
   --help, -h                                         show help (default: false)

func NewClient

func NewClient(prefix clix.FlagPrefix, opts ...Option) *Client

NewClient returns NewClient(prefix, "", opts...).

func NewClientName

func NewClientName(prefix clix.FlagPrefix, name string, opts ...Option) *Client

NewClientName returns *Client.

func (*Client) Address

func (f *Client) Address() string

Address returns the value of FlagAddress.

func (*Client) Before

func (f *Client) Before(c *cli.Context) error

Before calls f.FlagSet.Init(c). Before is intended to be used as cli.BeforeFunc.

func (*Client) Dial

func (f *Client) Dial() (net.Conn, error)

Dial returns the result of calling f.DialNetwork(f.Network()).

func (*Client) DialNetwork

func (f *Client) DialNetwork(network string) (net.Conn, error)

DialNetwork returns the result of calling tls.Dial if f.UseTLS() returns true, otherwise returns the result of calling net.Dial. f.Address() and f.TLSConfig() are used.

func (*Client) Flags

func (f *Client) Flags() []cli.Flag

Flags returns []cli.Flag.

It includes the following.

f.FlagNetwork  (if not predetermined)
f.FlagAddress

It also includes the following if TLS is enabled.

f.FlagTLSCerts
f.FlagTLSKeys
f.FlagTLSCAs
f.FlagTLSServerName
f.FlagTLSSkipVerify
f.FlagTLSMinVer
f.FlagTLSMaxVer

func (*Client) Network

func (f *Client) Network() string

Network returns the value of FlagNetwork.

func (*Client) TLSCAs

func (f *Client) TLSCAs() []string

TLSCAs returns the value of FlagTLSCAs.

func (*Client) TLSCerts

func (f *Client) TLSCerts() []string

TLSCerts returns the value of FlagTLSCerts.

func (*Client) TLSConfig

func (f *Client) TLSConfig() (*tls.Config, error)

TLSConfig returns *tls.Config.

func (*Client) TLSKeys

func (f *Client) TLSKeys() []string

TLSKeys returns the value of FlagTLSKeys.

func (*Client) TLSMaxVersion

func (f *Client) TLSMaxVersion() uint16

TLSMaxVersion returns the value of FlagTLSMaxVer.

func (*Client) TLSMinVersion

func (f *Client) TLSMinVersion() uint16

TLSMinVersion returns the value of FlagTLSMinVer.

func (*Client) TLSServerName

func (f *Client) TLSServerName() string

func (*Client) TLSSkipVerify

func (f *Client) TLSSkipVerify() bool

TLSSkipVerify returns the value of FlagTLSSkipVerify.

func (*Client) UseTLS

func (f *Client) UseTLS() bool

UseTLS returns true if TLS related flags are presented.

type Option

type Option func(*config)

Option represents options for Client and Server.

func Address

func Address(a string) Option

Address returns the option setting default address. FlagAddress is set required if the default address is not set.

func GenCert

func GenCert(v bool) Option

GenCert returns the option whether using self-signed certificate.

func Network

func Network(net ...string) Option

Network returns the option shows acceptable networks. FlagNetwork is available in command line options if more than one networks is set. Otherwise FlagNetwork is not shown in the command line options. Default value of network is "tcp".

func SkipVerify

func SkipVerify(v bool) Option

SkipVerify returns the option to set default value of FlagSkipVerify.

func TLS

func TLS(v bool) Option

TLS returns the option whether using TLS.

func TLSMaxVersion

func TLSMaxVersion(v uint16) Option

TLSMaxVersion returns the option to set default value of FlagTLSMaxVer.

func TLSMinVersion

func TLSMinVersion(v uint16) Option

TLSMinVersion returns the option to set default value of FlagTLSMinVer.

type Server

type Server struct {
	// Name is the name of Server, may be empty string.
	Name string

	// PredeterminedFlagNetwork is true if the value of FlagNetwork is predetermined by the option.
	PredeterminedFlagNetwork bool

	// DisableTLS is true if TLS is disabled.
	DisableTLS bool

	// DisableFlagTLSGenCert is true if FlagTLSGenCert would be included in the result of Flags().
	DisableFlagTLSGenCert bool

	// FlagNetwork is the network to listen.
	FlagNetwork *cli.StringFlag

	// FlagAddress is the address to listen.
	FlagAddress *cli.StringFlag

	// FlagTLSCerts is the certificate filepath of the server.
	FlagTLSCerts *cli.StringSliceFlag

	// FlagTLSKeys is the private key filepath of the certificate.
	FlagTLSKeys *cli.StringSliceFlag

	// FlagTLSGenCert specifies whether to generate a self signed certificate.
	FlagTLSGenCert *cli.BoolFlag

	// FlagTLSCAs is the certificate filepath of the ClientCAs.
	FlagTLSCAs *cli.StringSliceFlag

	// FlagTLSMinVer is the minimum TLS version that is acceptable.
	FlagTLSMinVer *cli.GenericFlag

	// FlagTLSMaxVer is the maximum TLS version that is acceptable.
	FlagTLSMaxVer *cli.GenericFlag

	// FlagSet is clix.FlagSet.
	FlagSet clix.FlagSet
}

Server represents the flags related to a server to listen and accept clients.

func NewServer

func NewServer(prefix clix.FlagPrefix, opts ...Option) *Server

NewServer returns NewServer(prefix, "", opts...).

func NewServerName

func NewServerName(prefix clix.FlagPrefix, name string, opts ...Option) *Server

NewServerName returns *Server.

func (*Server) Address

func (f *Server) Address() string

Address returns the value of FlagAddress.

func (*Server) Before

func (f *Server) Before(c *cli.Context) error

Before calls f.FlagSet.Init(c), validates exclusive flags. Before is intended to be used as cli.BeforeFunc.

func (*Server) Flags

func (f *Server) Flags() []cli.Flag

Flags returns []cli.Flag.

It includes the following.

f.FlagNetwork  (if not predetermined)
f.FlagAddress

It also includes the following if TLS is enabled.

f.FlagTLSCerts
f.FlagTLSKeys
f.FlagTLSGenCert  (if not disabled)
f.FlagTLSCAs
f.FlagTLSMinVer
f.FlagTLSMaxVer

func (*Server) Listen

func (f *Server) Listen() (net.Listener, error)

Listen returns the result of calling f.ListenNetwork(f.Network()).

func (*Server) ListenNetwork

func (f *Server) ListenNetwork(network string) (net.Listener, error)

ListenNetwork returns the result of calling tls.Listen if f.UseTLS() returns true, otherwise returns the result of calling net.Listen. f.Address() and f.TLSConfig() are used.

func (*Server) Network

func (f *Server) Network() string

Network returns the value of FlagNetwork.

func (*Server) TLSCAs

func (f *Server) TLSCAs() []string

TLSCAs returns the value of FlagTLSCAs.

func (*Server) TLSCerts

func (f *Server) TLSCerts() []string

TLSCerts returns the value of FlagTLSCerts.

func (*Server) TLSConfig

func (f *Server) TLSConfig() (*tls.Config, error)

TLSConfig returns *tls.Config.

func (*Server) TLSGenCert

func (f *Server) TLSGenCert() bool

TLSGenCert returns the value of FlagTLSGenCert.

func (*Server) TLSKeys

func (f *Server) TLSKeys() []string

TLSKeys returns the value of FlagTLSKeys.

func (*Server) TLSMaxVersion

func (f *Server) TLSMaxVersion() uint16

TLSMaxVersion returns the value of FlagTLSMaxVer.

func (*Server) TLSMinVersion

func (f *Server) TLSMinVersion() uint16

TLSMinVersion returns the value of FlagTLSMinVer.

func (*Server) UseTLS

func (f *Server) UseTLS() bool

UseTLS returns true if TLS related flags are presented.

type TLSVersion

type TLSVersion struct {
	// contains filtered or unexported fields
}

TLSVersion wraps a uint16 as tls.Version* to satisfy flag.Value.

func NewTLSVersion

func NewTLSVersion(value uint16) *TLSVersion

NewTLSVersion creates a *TLSVersion with a default value.

func (*TLSVersion) Set

func (tv *TLSVersion) Set(value string) error

Set parses value as TLS version string, sets it.

func (*TLSVersion) String

func (tv *TLSVersion) String() string

String returns a readable representation of this value (for usage defaults)

func (*TLSVersion) Value

func (tv *TLSVersion) Value() uint16

Value returns an uint16 as TLS version set by this flag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL