mirror of
https://github.com/mmatczuk/go-http-tunnel.git
synced 2026-05-15 14:16:17 -06:00
Exposes "tunnel" and "tunneld" to be embedded
This commit is contained in:
parent
863bcb3784
commit
d69513b0ff
6 changed files with 67 additions and 29 deletions
|
|
@ -51,7 +51,7 @@ type ClientConfig struct {
|
|||
Tunnels map[string]*Tunnel `yaml:"tunnels"`
|
||||
}
|
||||
|
||||
func loadClientConfigFromFile(file string) (*ClientConfig, error) {
|
||||
func LoadClientConfigFromFile(file string) (*ClientConfig, error) {
|
||||
buf, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read file %q: %s", file, err)
|
||||
|
|
|
|||
|
|
@ -67,24 +67,44 @@ func init() {
|
|||
}
|
||||
|
||||
type options struct {
|
||||
config string
|
||||
logLevel int
|
||||
version bool
|
||||
command string
|
||||
args []string
|
||||
config string
|
||||
logLevel int
|
||||
version bool
|
||||
command string
|
||||
args []string
|
||||
}
|
||||
|
||||
func parseArgs(args ...string) (*options, error) {
|
||||
config := flag.String("config", "tunnel.yaml", "Path to tunnel configuration file")
|
||||
logLevel := flag.Int("log-level", 1, "Level of messages to log, 0-3")
|
||||
version := flag.Bool("version", false, "Prints tunnel version")
|
||||
flag.CommandLine.Parse(args)
|
||||
func (opt options) Command() string {
|
||||
return opt.command
|
||||
}
|
||||
|
||||
func (opt options) LogLevel() int {
|
||||
return opt.logLevel
|
||||
}
|
||||
|
||||
func (opt options) Args() []string {
|
||||
return opt.args
|
||||
}
|
||||
|
||||
func ParseArgs(hasConfig bool, args ...string) (*options, error) {
|
||||
var config *string
|
||||
cli := flag.NewFlagSet(args[0], flag.ExitOnError)
|
||||
args = args[1:]
|
||||
if hasConfig {
|
||||
config = cli.String("config", "tunnel.yaml", "Path to tunnel configuration file")
|
||||
} else {
|
||||
var s = ""
|
||||
config = &s
|
||||
}
|
||||
logLevel := cli.Int("log-level", 1, "Level of messages to log, 0-3")
|
||||
version := cli.Bool("version", false, "Prints tunnel version")
|
||||
cli.Parse(args)
|
||||
|
||||
opts := &options{
|
||||
config: *config,
|
||||
logLevel: *logLevel,
|
||||
version: *version,
|
||||
command: flag.Arg(0),
|
||||
config: *config,
|
||||
logLevel: *logLevel,
|
||||
version: *version,
|
||||
command: cli.Arg(0),
|
||||
}
|
||||
|
||||
if opts.version {
|
||||
|
|
@ -93,20 +113,20 @@ func parseArgs(args ...string) (*options, error) {
|
|||
|
||||
switch opts.command {
|
||||
case "":
|
||||
flag.Usage()
|
||||
cli.Usage()
|
||||
os.Exit(2)
|
||||
case "id", "list":
|
||||
opts.args = flag.Args()[1:]
|
||||
opts.args = cli.Args()[1:]
|
||||
if len(opts.args) > 0 {
|
||||
return nil, fmt.Errorf("list takes no arguments")
|
||||
}
|
||||
case "start":
|
||||
opts.args = flag.Args()[1:]
|
||||
opts.args = cli.Args()[1:]
|
||||
if len(opts.args) == 0 {
|
||||
return nil, fmt.Errorf("you must specify at least one tunnel to start")
|
||||
}
|
||||
case "start-all":
|
||||
opts.args = flag.Args()[1:]
|
||||
opts.args = cli.Args()[1:]
|
||||
if len(opts.args) > 0 {
|
||||
return nil, fmt.Errorf("start-all takes no arguments")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,25 +28,36 @@ func Main() {
|
|||
}
|
||||
|
||||
func MainArgs(args ...string) {
|
||||
opts, err := parseArgs(args...)
|
||||
opts, err := ParseArgs(true, args...)
|
||||
if err != nil {
|
||||
fatal(err.Error())
|
||||
}
|
||||
MainOpts(opts)
|
||||
}
|
||||
|
||||
func MainOpts(opts *options) {
|
||||
if opts.version {
|
||||
fmt.Println(version)
|
||||
return
|
||||
}
|
||||
|
||||
logger := log.NewFilterLogger(log.NewStdLogger(), opts.logLevel)
|
||||
|
||||
// read configuration file
|
||||
config, err := loadClientConfigFromFile(opts.config)
|
||||
config, err := LoadClientConfigFromFile(opts.config)
|
||||
if err != nil {
|
||||
fatal("configuration error: %s", err)
|
||||
}
|
||||
|
||||
switch opts.command {
|
||||
MainConfigOptions(config, opts)
|
||||
}
|
||||
|
||||
func MainConfigOptions(config *ClientConfig, options *options) {
|
||||
MainConfig(config, options.logLevel, options.command, options.args...)
|
||||
}
|
||||
|
||||
func MainConfig(config *ClientConfig, logLevel int, command string, args ...string) {
|
||||
logger := log.NewFilterLogger(log.NewStdLogger(), logLevel)
|
||||
|
||||
switch command {
|
||||
case "id":
|
||||
cert, err := tls.LoadX509KeyPair(config.TLSCrt, config.TLSKey)
|
||||
if err != nil {
|
||||
|
|
@ -74,7 +85,7 @@ func MainArgs(args ...string) {
|
|||
return
|
||||
case "start":
|
||||
tunnels := make(map[string]*Tunnel)
|
||||
for _, arg := range opts.args {
|
||||
for _, arg := range args {
|
||||
t, ok := config.Tunnels[arg]
|
||||
if !ok {
|
||||
fatal("no such tunnel %q", arg)
|
||||
|
|
|
|||
|
|
@ -5,3 +5,8 @@
|
|||
package tunnel
|
||||
|
||||
var version = "snapshot"
|
||||
|
||||
// Return the tunnel version
|
||||
func GetVersion() string {
|
||||
return version
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,9 @@ type options struct {
|
|||
version bool
|
||||
}
|
||||
|
||||
func parseArgs(args ...string) *options {
|
||||
func ParseArgs(args ...string) *options {
|
||||
flag := flag.NewFlagSet(args[0], flag.ExitOnError)
|
||||
args = args[1:]
|
||||
httpAddr := flag.String("httpAddr", ":80", "Public address for HTTP connections, empty string to disable")
|
||||
httpsAddr := flag.String("httpsAddr", ":443", "Public address listening for HTTPS connections, emptry string to disable")
|
||||
tunnelAddr := flag.String("tunnelAddr", ":5223", "Public address listening for tunnel client")
|
||||
|
|
@ -60,7 +62,7 @@ func parseArgs(args ...string) *options {
|
|||
clientsDir := flag.String("registeredClientsDB", "", "Database directory of registered clients")
|
||||
logLevel := flag.Int("log-level", 1, "Level of messages to log, 0-3")
|
||||
version := flag.Bool("version", false, "Prints tunneld version")
|
||||
flag.CommandLine.Parse(args)
|
||||
flag.Parse(args)
|
||||
|
||||
return &options{
|
||||
httpAddr: *httpAddr,
|
||||
|
|
|
|||
|
|
@ -21,11 +21,11 @@ import (
|
|||
)
|
||||
|
||||
func Main() {
|
||||
MainArgs(os.Args[1:]...)
|
||||
MainArgs(os.Args...)
|
||||
}
|
||||
|
||||
func MainArgs(args ...string) {
|
||||
opts := parseArgs(args...)
|
||||
opts := ParseArgs(args...)
|
||||
|
||||
if opts.version {
|
||||
fmt.Println(version)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue