Skip to content

Commit

Permalink
Added the ability to log to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomika committed May 17, 2020
1 parent 4538ecf commit c2619d6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 10 deletions.
43 changes: 38 additions & 5 deletions cmd/sish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"fmt"
"io"
"log"
"os"
"time"

"github.com/antoniomika/sish/sshmuxer"
Expand All @@ -11,6 +13,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/natefinch/lumberjack.v2"
)

var (
Expand All @@ -35,11 +38,12 @@ var (
)

type logWriter struct {
TimeFmt string
TimeFmt string
MultiWriter io.Writer
}

func (w logWriter) Write(bytes []byte) (int, error) {
return fmt.Printf("%v | %s", time.Now().Format(w.TimeFmt), string(bytes))
return fmt.Fprintf(w.MultiWriter, "%v | %s", time.Now().Format(w.TimeFmt), string(bytes))
}

func init() {
Expand Down Expand Up @@ -70,6 +74,7 @@ func init() {
rootCmd.PersistentFlags().StringP("service-console-token", "m", "", "The token to use for service console access. Auto generated if empty for each connected tunnel")
rootCmd.PersistentFlags().StringP("append-user-to-subdomain-separator", "", "-", "The token to use for separating username and subdomain selection in a virtualhost")
rootCmd.PersistentFlags().StringP("time-format", "", "2006/01/02 - 15:04:05", "The time format to use for both HTTP and general log messages.")
rootCmd.PersistentFlags().StringP("log-to-file-path", "", "/tmp/sish.log", "The file to write log output to.")

rootCmd.PersistentFlags().BoolP("bind-random-subdomains", "", true, "Force bound HTTP tunnels to use random subdomains instead of user provided ones")
rootCmd.PersistentFlags().BoolP("verify-ssl", "", true, "Verify SSL certificates made on proxied HTTP connections")
Expand All @@ -93,10 +98,16 @@ func init() {
rootCmd.PersistentFlags().BoolP("tcp-load-balancer", "", false, "Enable the TCP load balancer (multiple clients can bind the same port)")
rootCmd.PersistentFlags().BoolP("alias-load-balancer", "", false, "Enable the alias load balancer (multiple clients can bind the same alias)")
rootCmd.PersistentFlags().BoolP("localhost-as-all", "", true, "Enable forcing localhost to mean all interfaces for tcp listeners")
rootCmd.PersistentFlags().BoolP("log-to-stdout", "", true, "Whether or not to write log output to stdout")
rootCmd.PersistentFlags().BoolP("log-to-file", "", false, "Whether or not to write log output to file, specified by log-to-file-path")
rootCmd.PersistentFlags().BoolP("log-to-file-compress", "", false, "Whether or not to compress log output files")

rootCmd.PersistentFlags().IntP("http-port-override", "", 0, "The port to use for http command output. This does not effect ports used for connecting, it's for cosmetic use only")
rootCmd.PersistentFlags().IntP("https-port-override", "", 0, "The port to use for https command output. This does not effect ports used for connecting, it's for cosmetic use only")
rootCmd.PersistentFlags().IntP("bind-random-subdomains-length", "", 3, "The length of the random subdomain to generate if a subdomain is unavailable or if random subdomains are enforced")
rootCmd.PersistentFlags().IntP("log-to-file-max-size", "", 500, "The maximum size of outputed log files in megabytes.")
rootCmd.PersistentFlags().IntP("log-to-file-max-backups", "", 3, "The maxium number of rotated logs files to keep.")
rootCmd.PersistentFlags().IntP("log-to-file-max-age", "", 28, "The maxium number of days to store log output in a file.")

rootCmd.PersistentFlags().DurationP("idle-connection-timeout", "", 5*time.Second, "Duration to wait for activity before closing a connection for all reads and writes")
rootCmd.PersistentFlags().DurationP("ping-client-interval", "", 5*time.Second, "Duration representing an interval to ping a client to ensure it is up")
Expand All @@ -120,12 +131,31 @@ func initConfig() {

viper.WatchConfig()

writers := []io.Writer{}

if viper.GetBool("log-to-stdout") {
writers = append(writers, os.Stdout)
}

if viper.GetBool("log-to-file") {
writers = append(writers, &lumberjack.Logger{
Filename: viper.GetString("log-to-file-path"),
MaxSize: viper.GetInt("log-to-file-max-size"),
MaxBackups: viper.GetInt("log-to-file-max-backups"),
MaxAge: viper.GetInt("log-to-file-max-age"),
Compress: viper.GetBool("log-to-file-compress"),
})
}

multiWriter := io.MultiWriter(writers...)

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

log.SetFlags(0)
log.SetOutput(logWriter{
TimeFmt: viper.GetString("time-format"),
TimeFmt: viper.GetString("time-format"),
MultiWriter: multiWriter,
})

if viper.GetBool("debug") {
Expand All @@ -135,14 +165,17 @@ func initConfig() {

log.SetFlags(0)
log.SetOutput(logWriter{
TimeFmt: viper.GetString("time-format"),
TimeFmt: viper.GetString("time-format"),
MultiWriter: multiWriter,
})

if viper.GetBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}

utils.Setup()
logrus.SetOutput(multiWriter)

utils.Setup(multiWriter)
}

// Execute executes the root command.
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/antoniomika/sish

require (
github.com/antoniomika/oxy v1.1.1-0.20200517061651-1c954f761f98
github.com/antoniomika/oxy v1.1.1-0.20200517194743-bedd7c62c77e
github.com/fsnotify/fsnotify v1.4.9
github.com/gin-gonic/gin v1.6.3
github.com/golang/protobuf v1.4.2 // indirect
Expand All @@ -25,6 +25,7 @@ require (
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9 // indirect
gopkg.in/ini.v1 v1.56.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v2 v2.3.0 // indirect
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/antoniomika/oxy v1.1.1-0.20200517061651-1c954f761f98 h1:/9dcIinwUyoXUQ6zb05iJlpxPS6FSfYTTKOqu/hgZ3c=
github.com/antoniomika/oxy v1.1.1-0.20200517061651-1c954f761f98/go.mod h1:3+QhU5rjR3nA+fbjvrafIeU9Vix+j9MtLvnBZjF/N00=
github.com/antoniomika/oxy v1.1.1-0.20200517194743-bedd7c62c77e h1:II47DJPovACM4IndMelUd39qcOMDkZjvxJM+Ed5gojk=
github.com/antoniomika/oxy v1.1.1-0.20200517194743-bedd7c62c77e/go.mod h1:3+QhU5rjR3nA+fbjvrafIeU9Vix+j9MtLvnBZjF/N00=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down Expand Up @@ -408,6 +410,8 @@ gopkg.in/ini.v1 v1.56.0 h1:DPMeDvGTM54DXbPkVIZsp19fp/I2K7zwA/itHYHKo8Y=
gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
2 changes: 1 addition & 1 deletion httpmuxer/httpmuxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Start(state *utils.State) {
releaseMode = gin.DebugMode
}
gin.SetMode(releaseMode)

gin.DefaultWriter = state.LogWriter
gin.ForceConsoleColor()

r := gin.New()
Expand Down
3 changes: 1 addition & 2 deletions sshmuxer/httphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func handleHTTPListener(check *channelForwardMsg, stringPort string, requestMess
fwd, err := forward.New(
forward.PassHostHeader(true),
forward.RoundTripper(rT),
//lint:ignore SA1019 Use dialer for now
forward.WebsocketDialer(rT.Dial),
forward.WebsocketRoundTripper(rT),
)

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions utils/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"encoding/base64"
"fmt"
"io"
"log"
"net"
"sync"
Expand Down Expand Up @@ -136,6 +137,7 @@ type State struct {
AliasListeners *sync.Map
TCPListeners *sync.Map
IPFilter *ipfilter.IPFilter
LogWriter io.Writer
}

// NewState returns a new state struct
Expand All @@ -148,5 +150,6 @@ func NewState() *State {
TCPListeners: &sync.Map{},
IPFilter: Filter,
Console: NewWebConsole(),
LogWriter: multiWriter,
}
}
6 changes: 5 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"log"
mathrand "math/rand"
Expand Down Expand Up @@ -38,10 +39,13 @@ var (
certHolder = make([]ssh.PublicKey, 0)
holderLock = sync.Mutex{}
bannedSubdomainList = []string{""}
multiWriter io.Writer
)

// Setup main utils
func Setup() {
func Setup(logWriter io.Writer) {
multiWriter = logWriter

upperList := func(stringList string) []string {
list := strings.FieldsFunc(stringList, CommaSplitFields)
for k, v := range list {
Expand Down

0 comments on commit c2619d6

Please # to comment.