Skip to content

Commit 4b1780d

Browse files
authored
Support logging.split_host_port for all addresses
1 parent c97893f commit 4b1780d

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

logging.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ type logEntry interface {
1515
eventType() string
1616
}
1717

18+
type addressLog struct {
19+
Host string `json:"host"`
20+
Port int `json:"port"`
21+
}
22+
23+
func (entry addressLog) String() string {
24+
return net.JoinHostPort(entry.Host, fmt.Sprint(entry.Port))
25+
}
26+
27+
func getAddressLog(host string, port int, cfg *config) interface{} {
28+
entry := addressLog{
29+
Host: host,
30+
Port: port,
31+
}
32+
if cfg.Logging.SplitHostPort {
33+
return entry
34+
}
35+
return entry.String()
36+
}
37+
1838
type authAccepted bool
1939

2040
func (accepted authAccepted) String() string {
@@ -98,7 +118,7 @@ func (entry connectionCloseLog) eventType() string {
98118
}
99119

100120
type tcpipForwardLog struct {
101-
Address string `json:"address"`
121+
Address interface{} `json:"address"`
102122
}
103123

104124
func (entry tcpipForwardLog) String() string {
@@ -109,7 +129,7 @@ func (entry tcpipForwardLog) eventType() string {
109129
}
110130

111131
type cancelTCPIPForwardLog struct {
112-
Address string `json:"address"`
132+
Address interface{} `json:"address"`
113133
}
114134

115135
func (entry cancelTCPIPForwardLog) String() string {
@@ -184,8 +204,8 @@ func (entry sessionInputLog) eventType() string {
184204

185205
type directTCPIPLog struct {
186206
channelLog
187-
From string `json:"from"`
188-
To string `json:"to"`
207+
From interface{} `json:"from"`
208+
To interface{} `json:"to"`
189209
}
190210

191211
func (entry directTCPIPLog) String() string {
@@ -366,19 +386,8 @@ func (context connContext) logEvent(entry logEntry) {
366386
}
367387
if context.cfg.Logging.JSON {
368388
var jsonEntry interface{}
369-
var source interface{}
370-
if context.cfg.Logging.SplitHostPort {
371-
tcpSource := context.RemoteAddr().(*net.TCPAddr)
372-
source = struct {
373-
Host string `json:"host"`
374-
Port int `json:"port"`
375-
}{
376-
Host: tcpSource.IP.String(),
377-
Port: tcpSource.Port,
378-
}
379-
} else {
380-
source = context.RemoteAddr().String()
381-
}
389+
tcpSource := context.RemoteAddr().(*net.TCPAddr)
390+
source := getAddressLog(tcpSource.IP.String(), tcpSource.Port, context.cfg)
382391
if context.cfg.Logging.Timestamps {
383392
jsonEntry = struct {
384393
Time string `json:"time"`

request.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"encoding/binary"
77
"errors"
88
mathRand "math/rand"
9-
"net"
10-
"strconv"
119

1210
"github.com/prometheus/client_golang/prometheus"
1311
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -34,7 +32,7 @@ func (request tcpipRequest) reply(context *connContext) []byte {
3432
}
3533
func (request tcpipRequest) logEntry(context *connContext) logEntry {
3634
return tcpipForwardLog{
37-
Address: net.JoinHostPort(request.Address, strconv.Itoa(int(request.Port))),
35+
Address: getAddressLog(request.Address, int(request.Port), context.cfg),
3836
}
3937
}
4038

@@ -48,7 +46,7 @@ func (request cancelTCPIPRequest) reply(context *connContext) []byte {
4846
}
4947
func (request cancelTCPIPRequest) logEntry(context *connContext) logEntry {
5048
return cancelTCPIPForwardLog{
51-
Address: net.JoinHostPort(request.Address, strconv.Itoa(int(request.Port))),
49+
Address: getAddressLog(request.Address, int(request.Port), context.cfg),
5250
}
5351
}
5452

tcpip.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import (
55
"bytes"
66
"fmt"
77
"io"
8-
"net"
98
"net/http"
109
"net/http/httputil"
11-
"strconv"
1210
"strings"
1311

1412
"github.com/prometheus/client_golang/prometheus"
@@ -71,8 +69,8 @@ func handleDirectTCPIPChannel(newChannel ssh.NewChannel, context channelContext)
7169
channelLog: channelLog{
7270
ChannelID: context.channelID,
7371
},
74-
From: net.JoinHostPort(channelData.OriginatorAddress, strconv.Itoa(int(channelData.OriginatorPort))),
75-
To: net.JoinHostPort(channelData.Address, strconv.Itoa(int(channelData.Port))),
72+
From: getAddressLog(channelData.OriginatorAddress, int(channelData.OriginatorPort), context.cfg),
73+
To: getAddressLog(channelData.Address, int(channelData.Port), context.cfg),
7674
})
7775
defer context.logEvent(directTCPIPCloseLog{
7876
channelLog: channelLog{

0 commit comments

Comments
 (0)