Skip to content

Commit 2149d1a

Browse files
author
hwh33
authored
Log when server sees alert record from tlsmasq origin (#495)
1 parent 59ab5f5 commit 2149d1a

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ require (
4646
github.com/getlantern/ring v0.0.0-20181206150603-dd46ce8faa01 // indirect
4747
github.com/getlantern/tinywss v0.0.0-20200121221108-851921f95ad7
4848
github.com/getlantern/tlsdefaults v0.0.0-20171004213447-cf35cfd0b1b4
49-
github.com/getlantern/tlsmasq v0.4.5
49+
github.com/getlantern/tlsmasq v0.4.6
50+
github.com/getlantern/tlsutil v0.5.1
5051
github.com/getlantern/waitforserver v1.0.1
5152
github.com/getlantern/withtimeout v0.0.0-20160829163843-511f017cd913
5253
github.com/go-redis/redis/v8 v8.10.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ github.com/getlantern/tinywss v0.0.0-20200121221108-851921f95ad7 h1:wVcJbQS7pf4h
290290
github.com/getlantern/tinywss v0.0.0-20200121221108-851921f95ad7/go.mod h1:ZLyPOKtNWU4vWnAiRiNQ7hbfLMqCEuj1DgQWBtHp7tQ=
291291
github.com/getlantern/tlsdefaults v0.0.0-20171004213447-cf35cfd0b1b4 h1:73U3J4msGw3cXeKtCEbY7hbOdD6aX8gJv8BOu+VagF8=
292292
github.com/getlantern/tlsdefaults v0.0.0-20171004213447-cf35cfd0b1b4/go.mod h1:f8WmDYKFOaC5/y0d3GWl6UKf1ZbSlIoMzkuC8x7pUhg=
293-
github.com/getlantern/tlsmasq v0.4.5 h1:dFh3AxZdicyo8Sqy1/iZLpWiGuJGew43SKaVSL7o6QA=
294-
github.com/getlantern/tlsmasq v0.4.5/go.mod h1:qgXekW+O2Eag1/hsAndpV/xdY1XXZaoIj7FhXskbxdY=
293+
github.com/getlantern/tlsmasq v0.4.6 h1:yk+XnAgB9XofhJ9leFR/SotRlLLtS2vElvjB43Xjn7E=
294+
github.com/getlantern/tlsmasq v0.4.6/go.mod h1:If80SpH0K1QvlZ5xeLlp3Vba73s8r1aCZzSmQNKN/pY=
295295
github.com/getlantern/tlsredis v0.0.0-20180308045249-5d4ed6dd3836/go.mod h1:1ZJE0mXEdPyyuF1daUTDBo2nVWB/6nuZy7IcNmRnHrc=
296-
github.com/getlantern/tlsutil v0.5.0 h1:VNQMXW3oMtPDSNyeTLk+MaU8FGkusNxwARJ30sR7yPw=
297-
github.com/getlantern/tlsutil v0.5.0/go.mod h1:lVgvr4nxuQ1ocOho90UB6LnHFlpP16TXAGpHR8Z0QnI=
296+
github.com/getlantern/tlsutil v0.5.1 h1:Cn19aDidw4+yufrQaCAYjZir3g1QaObs1xf4qzez3CA=
297+
github.com/getlantern/tlsutil v0.5.1/go.mod h1:lVgvr4nxuQ1ocOho90UB6LnHFlpP16TXAGpHR8Z0QnI=
298298
github.com/getlantern/utls v0.0.0-20200903013459-0c02248f7ce1 h1:+Egmu6VMMPm8/FHz8TOtQ1Usn3zg0gkS7ZHrLFycyok=
299299
github.com/getlantern/utls v0.0.0-20200903013459-0c02248f7ce1/go.mod h1:81/JblRrFcHdL/b50CIN3OuJmkt41KbgnjQu+mBSbgQ=
300300
github.com/getlantern/uuid v1.1.2-0.20190507182000-5c9436b8c718/go.mod h1:uX10hOzZUUDR+oYNSIks+RcozOEiwTNC/K2rw9SUi1k=

tlsmasq/tlsmasq.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import (
44
"context"
55
"crypto/tls"
66
"encoding/hex"
7+
"errors"
78
"fmt"
89
"net"
10+
"sync"
911

12+
"github.com/getlantern/golog"
1013
"github.com/getlantern/tlsmasq"
1114
"github.com/getlantern/tlsmasq/ptlshs"
15+
"github.com/getlantern/tlsutil"
1216
)
1317

18+
var log = golog.LoggerFor("tlsmasq-listener")
19+
1420
func Wrap(ll net.Listener, certFile string, keyFile string, originAddr string, secret string,
1521
tlsMinVersion uint16, tlsCipherSuites []uint16, onNonFatalErrors func(error)) (net.Listener, error) {
1622

@@ -51,5 +57,45 @@ func Wrap(ll net.Listener, certFile string, keyFile string, originAddr string, s
5157
},
5258
}
5359

54-
return tlsmasq.WrapListener(ll, listenerCfg), nil
60+
return wrapListener(ll, listenerCfg), nil
61+
}
62+
63+
type loggingListener struct {
64+
tlsmasqListener net.Listener
65+
}
66+
67+
func wrapListener(transportListener net.Listener, cfg tlsmasq.ListenerConfig) net.Listener {
68+
return loggingListener{tlsmasq.WrapListener(transportListener, cfg)}
69+
}
70+
71+
func (l loggingListener) Accept() (net.Conn, error) {
72+
conn, err := l.tlsmasqListener.Accept()
73+
if err != nil {
74+
return nil, err
75+
}
76+
return loggingConn{Conn: conn.(tlsmasq.Conn)}, nil
77+
}
78+
79+
func (l loggingListener) Addr() net.Addr { return l.tlsmasqListener.Addr() }
80+
func (l loggingListener) Close() error { return l.tlsmasqListener.Close() }
81+
82+
type loggingConn struct {
83+
tlsmasq.Conn
84+
handshakeOnce sync.Once
85+
}
86+
87+
func (conn loggingConn) Read(b []byte) (n int, err error) { return conn.doIO(b, conn.Conn.Read) }
88+
func (conn loggingConn) Write(b []byte) (n int, err error) { return conn.doIO(b, conn.Conn.Write) }
89+
90+
func (conn loggingConn) doIO(b []byte, io func([]byte) (int, error)) (n int, err error) {
91+
conn.handshakeOnce.Do(func() {
92+
var alertErr tlsutil.UnexpectedAlertError
93+
if err = conn.Handshake(); err != nil && errors.As(err, &alertErr) {
94+
log.Debugf("received alert from origin in tlsmasq handshake: %v", alertErr.Alert)
95+
}
96+
})
97+
if err != nil {
98+
return 0, err
99+
}
100+
return io(b)
55101
}

0 commit comments

Comments
 (0)