telebit/mplexer/connwrap.go

237 lines
5.9 KiB
Go
Raw Normal View History

2020-05-22 10:41:24 +00:00
package telebit
2020-05-22 10:07:35 +00:00
import (
2020-06-06 09:43:56 +00:00
"bufio"
2020-06-09 10:41:38 +00:00
"fmt"
2020-05-22 10:07:35 +00:00
"net"
"time"
2020-06-09 10:41:38 +00:00
"git.coolaj86.com/coolaj86/go-telebitd/sni"
2020-05-22 10:07:35 +00:00
)
// ConnWrap is just a cheap way to DRY up some switch conn.(type) statements to handle special features of Conn
type ConnWrap struct {
// TODO use io.MultiReader to unbuffer the peeker
2020-06-06 09:43:56 +00:00
//Conn net.Conn
2020-06-09 10:41:38 +00:00
peeker *bufio.Reader
servername string
scheme string
Conn net.Conn
Plain net.Conn
2020-06-22 06:34:42 +00:00
encrypted *bool
2020-06-06 09:43:56 +00:00
}
type Peeker interface {
Peek(n int) ([]byte, error)
}
func (c *ConnWrap) Peek(n int) ([]byte, error) {
if nil != c.peeker {
return c.peeker.Peek(n)
}
switch conn := c.Conn.(type) {
case *ConnWrap:
return conn.Peek(n)
case *Conn:
return conn.Peek(n)
default:
// *net.UDPConn,*net.TCPConn,*net.IPConn,*net.UnixConn
if nil == c.peeker {
c.peeker = bufio.NewReaderSize(c.Conn, defaultPeekerSize)
2020-06-06 09:43:56 +00:00
}
return c.peeker.Peek(n)
}
2020-05-22 10:07:35 +00:00
}
func (c *ConnWrap) Read(b []byte) (n int, err error) {
2020-06-06 09:43:56 +00:00
if nil != c.peeker {
return c.peeker.Read(b)
}
2020-05-22 10:07:35 +00:00
return c.Conn.Read(b)
}
// Write writes data to the connection.
// Write can be made to time out and return an Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetWriteDeadline.
func (c *ConnWrap) Write(b []byte) (n int, err error) {
return c.Conn.Write(b)
}
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (c *ConnWrap) Close() error {
return c.Conn.Close()
}
// Scheme returns one of "https", "http", "tcp", "tls", or ""
func (c *ConnWrap) Scheme() string {
2020-06-09 10:41:38 +00:00
if "" != c.scheme {
return c.scheme
2020-05-22 10:07:35 +00:00
}
2020-06-09 10:41:38 +00:00
/*
if nil != c.Plain {
tlsConn := &ConnWrap{Conn: c.Plain}
// TODO upgrade tls+http => https
c.scheme = tlsConn.Scheme()
return c.scheme
}
*/
2020-05-22 10:07:35 +00:00
switch conn := c.Conn.(type) {
case *ConnWrap:
return conn.Scheme()
case *Conn:
return string(conn.relayTargetAddr.scheme)
}
return ""
}
2020-06-09 10:41:38 +00:00
/*
func (c *ConnWrap) SetServername(name string) {
c.servername = name
}
*/
2020-05-22 10:07:35 +00:00
// Servername may return Servername or Hostname as hinted by a tunnel or buffered peeking
func (c *ConnWrap) Servername() string {
2020-06-09 10:41:38 +00:00
if "" != c.servername {
return c.servername
}
2020-05-22 10:07:35 +00:00
if nil != c.Plain {
tlsConn := &ConnWrap{Conn: c.Plain}
2020-06-09 10:41:38 +00:00
c.servername = tlsConn.Servername()
return c.servername
2020-05-22 10:07:35 +00:00
}
switch conn := c.Conn.(type) {
case *ConnWrap:
2020-06-09 10:41:38 +00:00
//c.servername = conn.Servername()
return conn.Servername()
2020-05-22 10:07:35 +00:00
case *Conn:
2020-06-09 10:41:38 +00:00
// TODO XXX
//c.servername = string(conn.relayTargetAddr.addr)
return string(conn.relayTargetAddr.addr)
2020-05-22 10:07:35 +00:00
}
2020-06-22 06:34:42 +00:00
// this will get the servername
c.isTerminated()
2020-06-09 10:41:38 +00:00
return c.servername
2020-05-22 10:07:35 +00:00
}
// isTerminated returns true if net.Conn is either a ConnWrap{ tls.Conn },
// or a telebit.Conn with a non-encrypted `scheme` such as "tcp" or "http".
2020-05-22 10:07:35 +00:00
func (c *ConnWrap) isTerminated() bool {
2020-06-06 09:43:56 +00:00
// TODO look at SNI, may need context for Peek() timeout
/*
if nil != c.Plain {
return true
}
*/
2020-06-22 06:34:42 +00:00
if nil != c.encrypted {
return !*c.encrypted
}
2020-05-22 10:07:35 +00:00
2020-06-09 00:58:52 +00:00
// how to know how many bytes to read? really needs timeout
c.SetDeadline(time.Now().Add(5 * time.Second))
n := 6
b, _ := c.Peek(n)
2020-07-06 09:56:29 +00:00
fmt.Println("Peek(n)", b, string(b))
defer c.SetDeadline(time.Time{})
2020-07-06 09:56:29 +00:00
var encrypted bool
if len(b) >= n {
2020-06-09 00:58:52 +00:00
// SSL v3.x / TLS v1.x
// 0: TLS Byte
// 1: Major Version
2020-06-09 10:41:38 +00:00
// 2: Minor Version - 1
// 3-4: Header Length
2020-06-09 10:41:38 +00:00
// Payload
// 5: TLS Client Hello Marker Byte
if 0x16 == b[0] && 0x03 == b[1] && 0x01 == b[5] {
2020-06-09 10:41:38 +00:00
length := (int(b[3]) << 8) + int(b[4])
b, err := c.Peek(n - 1 + length)
if nil != err {
2020-07-06 09:56:29 +00:00
c.encrypted = &encrypted
2020-06-22 06:34:42 +00:00
return !*c.encrypted
2020-06-09 10:41:38 +00:00
}
c.servername, _ = sni.GetHostname(b)
2020-07-06 09:56:29 +00:00
encrypted = true
c.encrypted = &encrypted
2020-06-22 06:34:42 +00:00
return !*c.encrypted
2020-06-09 00:58:52 +00:00
}
}
2020-07-06 09:56:29 +00:00
c.encrypted = &encrypted
2020-06-22 06:34:42 +00:00
return !*c.encrypted
/*
if nil != err {
return true
}
2020-06-09 00:58:52 +00:00
switch conn := c.Conn.(type) {
case *ConnWrap:
return conn.isTerminated()
case *Conn:
_, ok := encryptedSchemes[string(conn.relayTargetAddr.scheme)]
return !ok
}
return false
*/
2020-05-22 10:07:35 +00:00
}
// LocalAddr returns the local network address.
func (c *ConnWrap) LocalAddr() net.Addr {
// TODO is this the right one?
return c.Conn.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (c *ConnWrap) RemoteAddr() net.Addr {
// TODO is this the right one?
return c.Conn.RemoteAddr()
}
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future and pending
// I/O, not just the immediately following call to Read or
// Write. After a deadline has been exceeded, the connection
// can be refreshed by setting a deadline in the future.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
//
// Note that if a TCP connection has keep-alive turned on,
// which is the default unless overridden by Dialer.KeepAlive
// or ListenConfig.KeepAlive, then a keep-alive failure may
// also return a timeout error. On Unix systems a keep-alive
// failure on I/O can be detected using
// errors.Is(err, syscall.ETIMEDOUT).
func (c *ConnWrap) SetDeadline(t time.Time) error {
return c.Conn.SetDeadline(t)
}
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
func (c *ConnWrap) SetReadDeadline(t time.Time) error {
return c.Conn.SetReadDeadline(t)
}
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
func (c *ConnWrap) SetWriteDeadline(t time.Time) error {
return c.Conn.SetWriteDeadline(t)
}