fix TCP deadlines, update logging
This commit is contained in:
parent
da34e64e07
commit
f15355c19e
|
@ -18,11 +18,11 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
telebit "git.rootprojects.org/root/telebit"
|
||||||
"git.rootprojects.org/root/telebit/dbg"
|
"git.rootprojects.org/root/telebit/dbg"
|
||||||
|
tbDns01 "git.rootprojects.org/root/telebit/dns01"
|
||||||
"git.rootprojects.org/root/telebit/mgmt"
|
"git.rootprojects.org/root/telebit/mgmt"
|
||||||
"git.rootprojects.org/root/telebit/mgmt/authstore"
|
"git.rootprojects.org/root/telebit/mgmt/authstore"
|
||||||
telebit "git.rootprojects.org/root/telebit"
|
|
||||||
tbDns01 "git.rootprojects.org/root/telebit/dns01"
|
|
||||||
"git.rootprojects.org/root/telebit/table"
|
"git.rootprojects.org/root/telebit/table"
|
||||||
httpshim "git.rootprojects.org/root/telebit/tunnel"
|
httpshim "git.rootprojects.org/root/telebit/tunnel"
|
||||||
legoDns01 "github.com/go-acme/lego/v3/challenge/dns01"
|
legoDns01 "github.com/go-acme/lego/v3/challenge/dns01"
|
||||||
|
|
20
routemux.go
20
routemux.go
|
@ -81,20 +81,36 @@ func (m *RouteMux) Serve(client net.Conn) error {
|
||||||
port = ":" + parts[len(parts)-1]
|
port = ":" + parts[len(parts)-1]
|
||||||
servername = strings.Join(parts[:len(parts)-1], ":")
|
servername = strings.Join(parts[:len(parts)-1], ":")
|
||||||
}
|
}
|
||||||
fmt.Println("Addr:", fam, servername, port)
|
fmt.Println("\nAddr:", fam, servername, port)
|
||||||
|
|
||||||
for _, meta := range m.routes {
|
for _, meta := range m.routes {
|
||||||
// TODO '*.example.com'
|
// TODO '*.example.com'
|
||||||
if meta.terminate {
|
if meta.terminate {
|
||||||
servername = wconn.Servername()
|
servername = wconn.Servername()
|
||||||
}
|
}
|
||||||
fmt.Println("\nMeta:", meta.comment, "meta.addr="+meta.addr, "servername="+servername)
|
|
||||||
if servername == meta.addr || "*" == meta.addr || port == meta.addr {
|
if servername == meta.addr || "*" == meta.addr || port == meta.addr {
|
||||||
//fmt.Fprintf(os.Stderr, "[debug] test of route: %v\n", meta)
|
//fmt.Fprintf(os.Stderr, "[debug] test of route: %v\n", meta)
|
||||||
// Only keep trying handlers if ErrNotHandled was returned
|
// Only keep trying handlers if ErrNotHandled was returned
|
||||||
if err := meta.handler.Serve(wconn); ErrNotHandled != err {
|
if err := meta.handler.Serve(wconn); ErrNotHandled != err {
|
||||||
|
fmt.Printf(
|
||||||
|
"[mux] Match: %s\n\tmeta.addr=%s\n\tservername=%s\n",
|
||||||
|
meta.comment, meta.addr, servername,
|
||||||
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if dbg.Debug {
|
||||||
|
fmt.Fprintf(
|
||||||
|
os.Stderr,
|
||||||
|
"[debug] [mux] Failed match: %s meta.addr=%s servername=%s\n",
|
||||||
|
meta.comment, meta.addr, servername,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else if dbg.Debug {
|
||||||
|
fmt.Fprintf(
|
||||||
|
os.Stderr,
|
||||||
|
"[debug] [mux] Skip (no match): %s meta.addr=%s servername=%s\n",
|
||||||
|
meta.comment, meta.addr, servername,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
31
telebit.go
31
telebit.go
|
@ -25,6 +25,7 @@ import (
|
||||||
// but even 1024b could work well.
|
// but even 1024b could work well.
|
||||||
var defaultBufferSize = 8192
|
var defaultBufferSize = 8192
|
||||||
var defaultPeekerSize = 1024
|
var defaultPeekerSize = 1024
|
||||||
|
var defaultWriteTimeout = 10 * time.Second
|
||||||
|
|
||||||
// ErrBadGateway means that the target did not accept the connection
|
// ErrBadGateway means that the target did not accept the connection
|
||||||
var ErrBadGateway = errors.New("EBADGATEWAY")
|
var ErrBadGateway = errors.New("EBADGATEWAY")
|
||||||
|
@ -58,7 +59,8 @@ func NewForwarder(target string, timeout time.Duration) HandlerFunc {
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Forward(client, tconn, timeout)
|
go Forward(client, tconn, timeout)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +77,12 @@ func Forward(client net.Conn, target net.Conn, timeout time.Duration) error {
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
defer target.Close()
|
defer target.Close()
|
||||||
|
|
||||||
|
noDeadline := time.Time{}
|
||||||
|
writeTimeout := defaultWriteTimeout
|
||||||
|
if timeout < defaultWriteTimeout {
|
||||||
|
writeTimeout = timeout
|
||||||
|
}
|
||||||
|
|
||||||
srcCh := make(chan []byte)
|
srcCh := make(chan []byte)
|
||||||
dstCh := make(chan []byte)
|
dstCh := make(chan []byte)
|
||||||
srcErrCh := make(chan error)
|
srcErrCh := make(chan error)
|
||||||
|
@ -84,6 +92,8 @@ func Forward(client net.Conn, target net.Conn, timeout time.Duration) error {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
b := make([]byte, defaultBufferSize)
|
b := make([]byte, defaultBufferSize)
|
||||||
|
client.SetReadDeadline(time.Now().Add(timeout))
|
||||||
|
target.SetReadDeadline(time.Now().Add(timeout))
|
||||||
n, err := client.Read(b)
|
n, err := client.Read(b)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
srcCh <- b[:n]
|
srcCh <- b[:n]
|
||||||
|
@ -101,6 +111,8 @@ func Forward(client net.Conn, target net.Conn, timeout time.Duration) error {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
b := make([]byte, defaultBufferSize)
|
b := make([]byte, defaultBufferSize)
|
||||||
|
target.SetReadDeadline(time.Now().Add(timeout))
|
||||||
|
client.SetReadDeadline(time.Now().Add(timeout))
|
||||||
n, err := target.Read(b)
|
n, err := target.Read(b)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
dstCh <- b[:n]
|
dstCh <- b[:n]
|
||||||
|
@ -115,10 +127,10 @@ func Forward(client net.Conn, target net.Conn, timeout time.Duration) error {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
fmt.Println(
|
fmt.Printf(
|
||||||
"Forwarding TCP connection",
|
"[mux] Forwarding TCP connection\n\t%s => %s\n\t(%s => %s)\n",
|
||||||
client.LocalAddr(),
|
|
||||||
client.RemoteAddr(),
|
client.RemoteAddr(),
|
||||||
|
client.LocalAddr(),
|
||||||
target.LocalAddr(),
|
target.LocalAddr(),
|
||||||
target.RemoteAddr(),
|
target.RemoteAddr(),
|
||||||
)
|
)
|
||||||
|
@ -131,15 +143,19 @@ ForwardData:
|
||||||
//case <-ctx.Done():
|
//case <-ctx.Done():
|
||||||
// break
|
// break
|
||||||
case b := <-srcCh:
|
case b := <-srcCh:
|
||||||
client.SetDeadline(time.Now().Add(timeout))
|
//fmt.Println("Read(): ", len(b))
|
||||||
|
target.SetWriteDeadline(time.Now().Add(writeTimeout))
|
||||||
_, err = target.Write(b)
|
_, err = target.Write(b)
|
||||||
|
target.SetWriteDeadline(noDeadline)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
fmt.Printf("write to target failed: %q\n", err.Error())
|
fmt.Printf("write to target failed: %q\n", err.Error())
|
||||||
break ForwardData
|
break ForwardData
|
||||||
}
|
}
|
||||||
case b := <-dstCh:
|
case b := <-dstCh:
|
||||||
target.SetDeadline(time.Now().Add(timeout))
|
//fmt.Println("Write(): ", len(b))
|
||||||
|
client.SetWriteDeadline(time.Now().Add(writeTimeout))
|
||||||
_, err = client.Write(b)
|
_, err = client.Write(b)
|
||||||
|
client.SetWriteDeadline(noDeadline)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
fmt.Printf("write to remote failed: %q\n", err.Error())
|
fmt.Printf("write to remote failed: %q\n", err.Error())
|
||||||
break ForwardData
|
break ForwardData
|
||||||
|
@ -168,7 +184,6 @@ ForwardData:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.Close()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -306,7 +321,7 @@ func NewCertMagic(acme *ACME) (*certmagic.Config, error) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
// yes, a circular reference, passing `magic` to its own Issuer
|
// yes, a circular reference, passing `magic` to its own Issuer
|
||||||
fmt.Printf("[debug] ACME Email: %q\n", acme.Email)
|
fmt.Printf("ACME Email: %q\n", acme.Email)
|
||||||
magic.Issuer = certmagic.NewACMEManager(magic, certmagic.ACMEManager{
|
magic.Issuer = certmagic.NewACMEManager(magic, certmagic.ACMEManager{
|
||||||
DNSProvider: acme.DNSProvider,
|
DNSProvider: acme.DNSProvider,
|
||||||
DNSChallengeOption: acme.DNSChallengeOption,
|
DNSChallengeOption: acme.DNSChallengeOption,
|
||||||
|
|
Loading…
Reference in New Issue