more debugging

This commit is contained in:
AJ ONeal 2020-07-15 04:09:17 +00:00
parent e5f26a25da
commit ce09953167
3 changed files with 27 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package telebit package telebit
import ( import (
"fmt"
"io" "io"
) )
@ -29,12 +30,13 @@ func (d *Decoder) Decode(out Router) error {
go func() { go func() {
for { for {
b := make([]byte, d.bufferSize) b := make([]byte, d.bufferSize)
//fmt.Println("loopers gonna loop")
n, err := d.in.Read(b) n, err := d.in.Read(b)
fmt.Println("[debug] [decoder] [srv] Tunnel read", n)
if n > 0 { if n > 0 {
rx <- b[:n] rx <- b[:n]
} }
if nil != err { if nil != err {
fmt.Println("[debug] [decoder] [srv] Tunnel read err", err)
rxErr <- err rxErr <- err
return return
} }
@ -47,8 +49,10 @@ func (d *Decoder) Decode(out Router) error {
// TODO, do we actually need ctx here? // TODO, do we actually need ctx here?
// would it be sufficient to expect the reader to be closed by the caller instead? // would it be sufficient to expect the reader to be closed by the caller instead?
case b := <-rx: case b := <-rx:
fmt.Println("[debug] [decoder] [srv] Tunnel write", len(b), string(b))
_, err := p.Write(b) _, err := p.Write(b)
if nil != err { if nil != err {
fmt.Println("[debug] [decoder] [srv] Tunnel write error")
// an error to write represents an unrecoverable error, // an error to write represents an unrecoverable error,
// not just a downstream client error // not just a downstream client error
//d.in.Close() //d.in.Close()

View File

@ -2,7 +2,9 @@ package telebit
import ( import (
"context" "context"
"encoding/hex"
"errors" "errors"
"fmt"
"io" "io"
"sync" "sync"
) )
@ -71,10 +73,12 @@ func (enc *Encoder) Encode(rin io.Reader, src, dst Addr) error {
b := make([]byte, enc.bufferSize) b := make([]byte, enc.bufferSize)
//fmt.Println("loopers gonna loop") //fmt.Println("loopers gonna loop")
n, err := rin.Read(b) n, err := rin.Read(b)
fmt.Println("[debug] [encoder] [srv] Browser read", n)
if n > 0 { if n > 0 {
rx <- b[:n] rx <- b[:n]
} }
if nil != err { if nil != err {
fmt.Println("[debug] [encoder] [srv] Browser read error", err)
rxErr <- err rxErr <- err
return return
} }
@ -90,21 +94,27 @@ func (enc *Encoder) Encode(rin io.Reader, src, dst Addr) error {
case <-enc.ctx.Done(): case <-enc.ctx.Done():
// TODO: verify that closing the reader will cause the goroutine to be released // TODO: verify that closing the reader will cause the goroutine to be released
//rin.Close() //rin.Close()
fmt.Println("[debug] [encoder] [srv] Browser ctx.Done()")
return errors.New("cancelled by encoder read or parent context") return errors.New("cancelled by encoder read or parent context")
case <-enc.subctx.Done(): case <-enc.subctx.Done():
//rin.Close() //rin.Close()
fmt.Println("[debug] [encoder] [srv] Browser subctx.Done()")
return errors.New("cancelled by encoder write context") return errors.New("cancelled by encoder write context")
case b := <-rx: case b := <-rx:
header, _, err := Encode(b, src, Addr{scheme: src.scheme, addr: "", port: -1}) header, _, err := Encode(b, src, Addr{scheme: src.scheme, addr: dst.Hostname(), port: dst.Port()})
if nil != err { if nil != err {
//rin.Close() //rin.Close()
fmt.Println("[debug] [encoder] [srv] Browser Encode err", err)
return err return err
} }
//fmt.Println("[debug] encode header:", string(header)) //fmt.Println("[debug] encode header:", string(header))
//fmt.Println("[debug] encode payload:", string(b)) //fmt.Println("[debug] encode payload:", string(b))
_, err = enc.write(header, b) _, err = enc.write(header, b)
fmt.Println("[debug] [encoder] [srv] Browser-to-tun write", len(header), header)
fmt.Println("[debug] [encoder] [srv]", len(b), hex.EncodeToString(b))
if nil != err { if nil != err {
fmt.Println("[debug] [encoder] [srv] Browser-to-tun write err", err)
//rin.Close() //rin.Close()
return err return err
} }

View File

@ -50,7 +50,7 @@ func (p *Parser) unpackV1(b []byte) (int, error) {
z++ z++
n := len(b) n := len(b)
if n < 1 { if n < 1 {
//fmt.Println("[debug] v1 end", z, n) fmt.Printf("[debug] v1 message unpacked (%d loops) (%d bytes left)\n", z, n)
break break
} }
@ -141,7 +141,10 @@ func (p *Parser) unpackV1Header(b []byte, n int) ([]byte, error) {
port, _ := strconv.Atoi(parts[PortIndex]) port, _ := strconv.Atoi(parts[PortIndex])
service := parts[ServiceIndex] service := parts[ServiceIndex]
fmt.Printf("[debug] parts: %s\n", parts)
fmt.Printf("[debug] service: %s\n", service)
if "control" == service { if "control" == service {
fmt.Printf("[debug] control: %s\n", service)
return nil, errors.New("'control' messages not implemented") return nil, errors.New("'control' messages not implemented")
} }
@ -163,6 +166,7 @@ func (p *Parser) unpackV1Header(b []byte, n int) ([]byte, error) {
} }
p.state.srcAddr = src p.state.srcAddr = src
p.state.dstAddr = dst p.state.dstAddr = dst
/* /*
p.state.conn = p.conns[addr.Network()] p.state.conn = p.conns[addr.Network()]
if nil == p.state.conn { if nil == p.state.conn {
@ -180,11 +184,16 @@ func (p *Parser) unpackV1Header(b []byte, n int) ([]byte, error) {
} }
*/ */
p.parseState++ p.parseState++
fmt.Printf("[debug] parse state: %v\n", p.parseState)
if "end" == service {
p.handler.RouteBytes(p.state.srcAddr, p.state.dstAddr, []byte{})
}
return b, nil return b, nil
} }
func (p *Parser) unpackV1Payload(b []byte, n int) ([]byte, error) { func (p *Parser) unpackV1Payload(b []byte, n int) ([]byte, error) {
fmt.Printf("[debug] state: %+v\n", p.state)
// Handle "connect" and "end" // Handle "connect" and "end"
if 0 == p.state.payloadLen { if 0 == p.state.payloadLen {
/* /*
@ -203,6 +212,7 @@ func (p *Parser) unpackV1Payload(b []byte, n int) ([]byte, error) {
*/ */
//fmt.Printf("[debug] [2] payload written: %d | payload length: %d\n", p.state.payloadWritten, p.state.payloadLen) //fmt.Printf("[debug] [2] payload written: %d | payload length: %d\n", p.state.payloadWritten, p.state.payloadLen)
fmt.Printf("[debug] RouteBytes: %#v %#v %s\n", p.state.srcAddr, p.state.dstAddr, p.state.dstAddr.scheme)
p.handler.RouteBytes(p.state.srcAddr, p.state.dstAddr, []byte{}) p.handler.RouteBytes(p.state.srcAddr, p.state.dstAddr, []byte{})
return b, nil return b, nil
} }