Skip to content

Commit 8f44621

Browse files
authored
Expanded debug logging, fix deprecations (antoniomika#240)
* Add debug messages for aborted requests * Don't use %s for Println * Fix deprecated ioutil calls * Fix incorrect leading spaces for comment * Add debug-interval option * Align debug logging syntax with existing error logging * Fix linting errors, fix status logging * Ensure debug-interval is not zero
1 parent 4a28b9e commit 8f44621

9 files changed

Lines changed: 46 additions & 21 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ Flags:
341341
--cleanup-unbound-timeout duration Duration to wait before cleaning up an unbound (unforwarded) connection (default 5s)
342342
-c, --config string Config file (default "config.yml")
343343
--debug Enable debugging information
344+
--debug-interval duration The duration to wait between each debug loop output if debug is true (default 2s)
344345
-d, --domain string The root domain for HTTP(S) multiplexing that will be appended to subdomains (default "ssi.sh")
345346
--force-requested-aliases Force the aliases used to be the one that is requested. Will fail the bind if it exists already
346347
--force-requested-ports Force the ports used to be the one that is requested. Will fail the bind if it exists already

cmd/sish.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func init() {
131131
rootCmd.PersistentFlags().IntP("log-to-file-max-backups", "", 3, "The maxium number of rotated logs files to keep")
132132
rootCmd.PersistentFlags().IntP("log-to-file-max-age", "", 28, "The maxium number of days to store log output in a file")
133133

134+
rootCmd.PersistentFlags().DurationP("debug-interval", "", 2*time.Second, "Duration to wait between each debug loop output if debug is true")
134135
rootCmd.PersistentFlags().DurationP("idle-connection-timeout", "", 5*time.Second, "Duration to wait for activity before closing a connection for all reads and writes")
135136
rootCmd.PersistentFlags().DurationP("ping-client-interval", "", 5*time.Second, "Duration representing an interval to ping a client to ensure it is up")
136137
rootCmd.PersistentFlags().DurationP("ping-client-timeout", "", 5*time.Second, "Duration to wait for activity before closing a connection after sending a ping to a client")

config.example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cleanup-unbound: false
2727
cleanup-unbound-timeout: 5s
2828
config: config.yml
2929
debug: false
30+
debug-interval: 2s
3031
domain: ssi.sh
3132
force-requested-aliases: false
3233
force-requested-ports: false

httpmuxer/httpmuxer.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"bytes"
88
"encoding/base64"
99
"fmt"
10-
"io/ioutil"
10+
"io"
1111
"log"
1212
"net"
1313
"net/http"
@@ -49,8 +49,22 @@ func Start(state *utils.State) {
4949

5050
// Here is where we check whether or not an IP is blocked.
5151
clientIPAddr, _, err := net.SplitHostPort(c.Request.RemoteAddr)
52-
if state.IPFilter.Blocked(c.ClientIP()) || state.IPFilter.Blocked(clientIPAddr) || err != nil {
53-
c.AbortWithStatus(http.StatusForbidden)
52+
clientIPAddrBlocked := state.IPFilter.Blocked(clientIPAddr)
53+
cClientIP := c.ClientIP()
54+
cClientIPBlocked := state.IPFilter.Blocked(cClientIP)
55+
56+
if clientIPAddrBlocked || cClientIPBlocked || err != nil {
57+
status := http.StatusForbidden
58+
c.AbortWithStatus(status)
59+
if viper.GetBool("debug") {
60+
log.Println("Aborting with status", status)
61+
if clientIPAddrBlocked {
62+
log.Println("Blocked:", clientIPAddr)
63+
}
64+
if cClientIPBlocked {
65+
log.Println("Blocked:", cClientIP)
66+
}
67+
}
5468
return
5569
}
5670
c.Next()
@@ -165,7 +179,11 @@ func Start(state *utils.State) {
165179
return
166180
}
167181

168-
c.AbortWithStatus(http.StatusNotFound)
182+
status := http.StatusNotFound
183+
c.AbortWithStatus(status)
184+
if viper.GetBool("debug") {
185+
log.Println("Aborting with status", status)
186+
}
169187
return
170188
}
171189

@@ -181,7 +199,11 @@ func Start(state *utils.State) {
181199

182200
if authNeeded {
183201
c.Header("WWW-Authenticate", "Basic realm=\"sish\"")
184-
c.AbortWithStatus(http.StatusUnauthorized)
202+
status := http.StatusUnauthorized
203+
c.AbortWithStatus(status)
204+
if viper.GetBool("debug") {
205+
log.Println("Aborting with status", status)
206+
}
185207
return
186208
}
187209

@@ -234,13 +256,13 @@ func Start(state *utils.State) {
234256
return
235257
}
236258

237-
reqBody, err := ioutil.ReadAll(c.Request.Body)
259+
reqBody, err := io.ReadAll(c.Request.Body)
238260
if err != nil {
239261
log.Println("Error reading request body:", err)
240262
return
241263
}
242264

243-
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
265+
c.Request.Body = io.NopCloser(bytes.NewBuffer(reqBody))
244266

245267
err = forward.ResponseModifier(ResponseModifier(state, hostname, reqBody, c, currentListener))(currentListener.Forward)
246268
if err != nil {

httpmuxer/proxy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"crypto/tls"
77
"encoding/base64"
88
"encoding/json"
9-
"io/ioutil"
9+
"io"
1010
"log"
1111
"net"
1212
"net/http"
@@ -46,12 +46,12 @@ func RoundTripper() *http.Transport {
4646
func ResponseModifier(state *utils.State, hostname string, reqBody []byte, c *gin.Context, currentListener *utils.HTTPHolder) func(*http.Response) error {
4747
return func(response *http.Response) error {
4848
if viper.GetBool("admin-console") || viper.GetBool("service-console") {
49-
resBody, err := ioutil.ReadAll(response.Body)
49+
resBody, err := io.ReadAll(response.Body)
5050
if err != nil {
5151
log.Println("Error reading response for webconsole:", err)
5252
}
5353

54-
response.Body = ioutil.NopCloser(bytes.NewBuffer(resBody))
54+
response.Body = io.NopCloser(bytes.NewBuffer(resBody))
5555

5656
startTime := c.GetTime("startTime")
5757
currentTime := time.Now()
@@ -69,7 +69,7 @@ func ResponseModifier(state *utils.State, hostname string, reqBody []byte, c *gi
6969
log.Println("Error reading gzip data:", err)
7070
}
7171

72-
resBody, err = ioutil.ReadAll(gzReader)
72+
resBody, err = io.ReadAll(gzReader)
7373
if err != nil {
7474
log.Println("Error reading gzip data:", err)
7575
}

sshmuxer/handle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func handleChannels(chans <-chan ssh.NewChannel, sshConn *utils.SSHConnection, s
8181
}
8282
}
8383

84-
// handleChannel handles a SSH connection's channel request.
84+
// handleChannel handles a SSH connection's channel request.
8585
func handleChannel(newChannel ssh.NewChannel, sshConn *utils.SSHConnection, state *utils.State) {
8686
switch channel := newChannel.ChannelType(); channel {
8787
case "session":

sshmuxer/requests.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package sshmuxer
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"log"
76
"net"
87
"os"
@@ -92,7 +91,7 @@ func handleRemoteForward(newRequest *ssh.Request, sshConn *utils.SSHConnection,
9291
}
9392
}
9493

95-
tmpfile, err := ioutil.TempFile("", strings.ReplaceAll(sshConn.SSHConn.RemoteAddr().String()+":"+stringPort, ":", "_"))
94+
tmpfile, err := os.CreateTemp("", strings.ReplaceAll(sshConn.SSHConn.RemoteAddr().String()+":"+stringPort, ":", "_"))
9695
if err != nil {
9796
log.Println("Error creating temporary file:", err)
9897

sshmuxer/sshmuxer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ func Start() {
6666

6767
go httpmuxer.Start(state)
6868

69-
if viper.GetBool("debug") {
69+
debugInterval := viper.GetDuration("debug-interval")
70+
71+
if viper.GetBool("debug") && debugInterval > 0 {
7072
go func() {
7173
for {
7274
log.Println("=======Start=========")
@@ -138,7 +140,7 @@ func Start() {
138140
})
139141
log.Print("========End==========\n")
140142

141-
time.Sleep(2 * time.Second)
143+
time.Sleep(debugInterval)
142144
}
143145
}()
144146
}

utils/utils.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"fmt"
1313
"io"
1414
"io/fs"
15-
"io/ioutil"
1615
"log"
1716
mathrand "math/rand"
1817
"net"
@@ -271,7 +270,7 @@ func loadPrivateKeys(config *ssh.ServerConfig) {
271270
return nil
272271
}
273272

274-
i, e := ioutil.ReadFile(path)
273+
i, e := os.ReadFile(path)
275274
if e != nil {
276275
log.Printf("Can't read file %s as private key: %s\n", d.Name(), err)
277276
return nil
@@ -418,7 +417,7 @@ func loadKeys() {
418417
return nil
419418
}
420419

421-
i, e := ioutil.ReadFile(path)
420+
i, e := os.ReadFile(path)
422421
if e != nil {
423422
log.Printf("Can't read file %s as public key: %s\n", d.Name(), err)
424423
return nil
@@ -519,7 +518,7 @@ func generatePrivateKey(passphrase string) []byte {
519518
pemData = pem.EncodeToMemory(pemBlock)
520519
}
521520

522-
err = ioutil.WriteFile(filepath.Join(viper.GetString("private-keys-directory"), "ssh_key"), pemData, 0600)
521+
err = os.WriteFile(filepath.Join(viper.GetString("private-keys-directory"), "ssh_key"), pemData, 0600)
523522
if err != nil {
524523
log.Println("Error writing to file:", err)
525524
}
@@ -532,7 +531,7 @@ func generatePrivateKey(passphrase string) []byte {
532531
func loadPrivateKey(passphrase string) ssh.Signer {
533532
var signer ssh.Signer
534533

535-
pk, err := ioutil.ReadFile(filepath.Join(viper.GetString("private-keys-directory"), "ssh_key"))
534+
pk, err := os.ReadFile(filepath.Join(viper.GetString("private-keys-directory"), "ssh_key"))
536535
if err != nil {
537536
log.Println("Error loading private key, generating a new one:", err)
538537
pk = generatePrivateKey(passphrase)

0 commit comments

Comments
 (0)