Skip to content

Commit

Permalink
Started work on migrating to cobra and viper
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomika committed May 2, 2020
1 parent 2df6c4b commit 19a3fb7
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.14
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
Expand Down
6 changes: 4 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ before:
- go mod tidy
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
- ldflags:
- -s -w -X cmd.Version={{ .Version }} -X cmd.Commit={{ .Commit }} -X cmd.Date={{ .Date }}
env:
- CGO_ENABLED=0
goos:
- linux
- win
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.13.2-alpine as builder
FROM golang:1.14-alpine as builder
LABEL maintainer="Antonio Mika <me@antoniomika.me>"

ENV GOCACHE /gocache
Expand All @@ -19,7 +19,7 @@ ARG VERSION=dev
ARG COMMIT=none
ARG DATE=unknown

RUN go install -ldflags="-s -w -X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${DATE}"
RUN go install -ldflags="-s -w -X cmd.Version=${VERSION} -X cmd.Commit=${COMMIT} -X cmd.Date=${DATE}"
RUN go test -i ./...

FROM scratch
Expand Down
114 changes: 114 additions & 0 deletions cmd/sish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package cmd

import (
"fmt"
"log"

"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const (
longCommandInfo = `sish is a command line utility that implements an SSH server
that can handle HTTP(S)/WS(S)/TCP multiplexing and forwarding.
It can handle multiple vhosting and reverse tunneling.`

// Version describes the version of the current build
Version = "dev"

// Commit describes the commit of the current build
Commit = "none"

// Date describes the date of the current build
Date = "unknown"
)

var (
configFile string

rootCmd = &cobra.Command{
Use: "sish",
Short: "The sish command initializes and runs the sish ssh multiplexer",
Long: longCommandInfo,
Run: runCommand,
Version: Version,
}
)

func init() {
cobra.OnInitialize(initConfig)

rootCmd.SetVersionTemplate(fmt.Sprintf("Version: %v\nCommit: %v\nDate: %v\n", Version, Commit, Date))

rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "config.yml", "Config file")
rootCmd.PersistentFlags().StringP("ssh-addr", "a", "localhost:2222", "The address to listen for SSH connections")
rootCmd.PersistentFlags().StringP("http-addr", "i", "localhost:80", "The address to listen for HTTP connections")
rootCmd.PersistentFlags().StringP("https-addr", "t", "localhost:443", "The address to listen for HTTPS connections")
rootCmd.PersistentFlags().StringP("redirect-root-location", "r", "https://github.com/antoniomika/sish", "Where to redirect the root domain to")
rootCmd.PersistentFlags().StringP("cert-dir", "s", "ssl/", "The location of pem files for HTTPS (fullchain.pem and privkey.pem)")
rootCmd.PersistentFlags().StringP("domain", "d", "ssi.sh", "The domain for HTTP(S) multiplexing")
rootCmd.PersistentFlags().StringP("banned-subdomains", "b", "localhost", "A comma separated list of banned subdomains")
rootCmd.PersistentFlags().StringP("banned-ips", "x", "", "A comma separated list of banned ips")
rootCmd.PersistentFlags().StringP("banned-countries", "o", "", "A comma separated list of banned countries")
rootCmd.PersistentFlags().StringP("whitelisted-ips", "w", "", "A comma separated list of whitelisted ips")
rootCmd.PersistentFlags().StringP("whitelisted-countries", "y", "", "A comma separated list of whitelisted countries")
rootCmd.PersistentFlags().StringP("privkey-passphrase", "p", "S3Cr3tP4$$phrAsE", "Passphrase to use for the server private key")
rootCmd.PersistentFlags().StringP("privkey-location", "l", "keys/ssh_key", "SSH server private key")
rootCmd.PersistentFlags().StringP("auth-password", "u", "S3Cr3tP4$$W0rD", "Password to use for password auth")
rootCmd.PersistentFlags().StringP("auth-keys-dir", "k", "pubkeys/", "Directory for public keys for pubkey auth")
rootCmd.PersistentFlags().StringP("bind-range", "n", "0,1024-65535", "Ports that are allowed to be bound")
rootCmd.PersistentFlags().StringP("proxy-protocol-version", "q", "1", "What version of the proxy protocol to use.\nCan either be 1, 2, or userdefined. If userdefined, the user needs to add a command to SSH called proxyproto:version (ie proxyproto:1)")
rootCmd.PersistentFlags().StringP("admin-token", "j", "S3Cr3tP4$$W0rD", "The token to use for admin access")
rootCmd.PersistentFlags().StringP("service-console-token", "m", "", "The token to use for service access. Auto generated if empty.")

rootCmd.PersistentFlags().BoolP("append-user-to-subdomain", "", false, "Whether or not to append the user to the subdomain")
rootCmd.PersistentFlags().BoolP("admin-enabled", "", false, "Whether or not to enable the admin console")
rootCmd.PersistentFlags().BoolP("service-console-enabled", "", false, "Whether or not to enable the admin console for each service and send the info to users")
rootCmd.PersistentFlags().BoolP("force-random-subdomain", "", true, "Whether or not to force a random subdomain")
rootCmd.PersistentFlags().BoolP("verify-origin", "", true, "Whether or not to verify origin on websocket connection")
rootCmd.PersistentFlags().BoolP("verify-ssl", "", true, "Whether or not to verify SSL on proxy connection")
rootCmd.PersistentFlags().BoolP("https-enabled", "", false, "Whether or not to listen for HTTPS connections")
rootCmd.PersistentFlags().BoolP("redirect-root", "", true, "Whether or not to redirect the root domain")
rootCmd.PersistentFlags().BoolP("use-geodb", "", false, "Whether or not to use the maxmind geodb")
rootCmd.PersistentFlags().BoolP("authentication-enabled", "", false, "Whether or not to require auth on the SSH service")
rootCmd.PersistentFlags().BoolP("cleanup-unbound", "", true, "Whether or not to cleanup unbound (forwarded) SSH connections")
rootCmd.PersistentFlags().BoolP("bind-random", "", true, "Bind ports randomly (OS chooses)")
rootCmd.PersistentFlags().BoolP("proxy-protocol-enabled", "", false, "Whether or not to enable the use of the proxy protocol")
rootCmd.PersistentFlags().BoolP("debug", "", false, "Whether or not to print debug information")
rootCmd.PersistentFlags().BoolP("version", "", false, "Print version and exit")
rootCmd.PersistentFlags().BoolP("tcp-alias", "", false, "Whether or not to allow the use of TCP aliasing")
rootCmd.PersistentFlags().BoolP("log-to-client", "", false, "Whether or not to log http requests to the client")

rootCmd.PersistentFlags().IntP("http-port-override", "", 0, "The port to use for http command output")
rootCmd.PersistentFlags().IntP("https-port-override", "", 0, "The port to use for https command output")
rootCmd.PersistentFlags().IntP("max-subdomain-len", "", 3, "The length of the random subdomain to generate")
rootCmd.PersistentFlags().IntP("connection-idle-timeout", "", 5, "Number of seconds to wait for activity before closing a connection")
}

func initConfig() {
viper.SetConfigFile(configFile)

viper.BindPFlags(rootCmd.PersistentFlags())
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
log.Println("Using config file:", viper.ConfigFileUsed())
}

viper.WatchConfig()

viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Reloaded configuration file.")
})
}

// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}

func runCommand(cmd *cobra.Command, args []string) {
// log.Printf("%+v", viper.AllSettings())
// log.Println(cmd, args)
}
35 changes: 20 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
module github.com/antoniomika/sish

require (
github.com/fsnotify/fsnotify v1.4.7
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.4.0
github.com/golang/protobuf v1.3.2 // indirect
github.com/gorilla/websocket v1.4.1
github.com/jpillora/ipfilter v1.0.0
github.com/json-iterator/go v1.1.8 // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/gin-gonic/gin v1.6.2
github.com/golang/protobuf v1.4.0 // indirect
github.com/gorilla/websocket v1.4.2
github.com/jpillora/ipfilter v1.2.1
github.com/koding/websocketproxy v0.0.0-20181220232114-7ed82d81a28c
github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/oschwald/maxminddb-golang v1.5.0 // indirect
github.com/pires/go-proxyproto v0.0.0-20190615163442-2c19fd512994
github.com/ugorji/go v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20191108234033-bd318be0434a
golang.org/x/sys v0.0.0-20191105231009-c1f44814a5cd // indirect
gopkg.in/yaml.v2 v2.2.5 // indirect
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/mitchellh/mapstructure v1.3.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/pelletier/go-toml v1.7.0 // indirect
github.com/phuslu/geoip v1.0.20200411 // indirect
github.com/pires/go-proxyproto v0.0.0-20200408100809-62dfc1403b91
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.6.3
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 // indirect
gopkg.in/ini.v1 v1.55.0 // indirect
)

go 1.13
Loading

0 comments on commit 19a3fb7

Please sign in to comment.