2020-05-22 10:41:24 +00:00
|
|
|
package telebit
|
2020-05-19 09:35:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-15 04:09:17 +00:00
|
|
|
"encoding/hex"
|
2020-05-19 09:35:22 +00:00
|
|
|
"errors"
|
2020-07-15 04:09:17 +00:00
|
|
|
"fmt"
|
2020-05-19 09:35:22 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2020-05-20 22:23:58 +00:00
|
|
|
// TODO: try to be more like encoding/csv, or more like encoding/pem and encoding/json?
|
|
|
|
|
|
|
|
// Encoder converts TCP to MPLEXY-TCP
|
2020-05-19 09:35:22 +00:00
|
|
|
type Encoder struct {
|
2020-05-20 22:52:06 +00:00
|
|
|
ctx context.Context
|
|
|
|
subctx context.Context
|
|
|
|
mux sync.Mutex
|
|
|
|
//out io.WriteCloser
|
|
|
|
out io.Writer
|
2020-05-20 22:23:58 +00:00
|
|
|
outErr chan error
|
|
|
|
bufferSize int
|
2020-05-19 09:35:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 22:23:58 +00:00
|
|
|
// NewEncoder returns an Encoder instance
|
2020-05-20 22:52:06 +00:00
|
|
|
func NewEncoder(ctx context.Context, wout io.Writer) *Encoder {
|
2020-05-19 09:35:22 +00:00
|
|
|
enc := &Encoder{
|
2020-05-20 22:23:58 +00:00
|
|
|
ctx: ctx,
|
|
|
|
out: wout,
|
|
|
|
outErr: make(chan error),
|
|
|
|
bufferSize: defaultBufferSize,
|
2020-05-19 09:35:22 +00:00
|
|
|
}
|
|
|
|
return enc
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:23:58 +00:00
|
|
|
// Run loops over a select of contexts and error channels
|
|
|
|
// to cancel and close south-side connections, if needed.
|
|
|
|
// TODO should this be pushed elsewhere to handled?
|
2020-05-19 09:35:22 +00:00
|
|
|
func (enc *Encoder) Run() error {
|
|
|
|
ctx, cancel := context.WithCancel(enc.ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
enc.subctx = ctx
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// TODO: do children respond to children cancelling?
|
|
|
|
case <-enc.ctx.Done():
|
|
|
|
// TODO
|
2020-05-20 22:52:06 +00:00
|
|
|
//_ = enc.out.Close()
|
2020-05-19 09:35:22 +00:00
|
|
|
return errors.New("context cancelled")
|
2020-05-20 22:23:58 +00:00
|
|
|
case err := <-enc.outErr:
|
|
|
|
// if a write fails for one, it fail for all
|
2020-05-19 09:35:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 09:03:04 +00:00
|
|
|
func (enc *Encoder) Start() error {
|
|
|
|
go enc.Run()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:23:58 +00:00
|
|
|
// Encode adds MPLEXY headers to raw net traffic, and is intended to be used on each client connection
|
2020-05-21 10:29:05 +00:00
|
|
|
func (enc *Encoder) Encode(rin io.Reader, src, dst Addr) error {
|
2020-05-19 09:35:22 +00:00
|
|
|
rx := make(chan []byte)
|
|
|
|
rxErr := make(chan error)
|
|
|
|
|
2020-05-22 10:34:37 +00:00
|
|
|
//fmt.Println("[debug] what's the source to encode?", src)
|
2020-05-21 10:29:05 +00:00
|
|
|
|
2020-05-19 09:35:22 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2020-05-20 22:23:58 +00:00
|
|
|
b := make([]byte, enc.bufferSize)
|
2020-05-19 09:35:22 +00:00
|
|
|
//fmt.Println("loopers gonna loop")
|
2020-05-20 22:23:58 +00:00
|
|
|
n, err := rin.Read(b)
|
2020-07-15 04:09:17 +00:00
|
|
|
fmt.Println("[debug] [encoder] [srv] Browser read", n)
|
2020-05-19 09:35:22 +00:00
|
|
|
if n > 0 {
|
|
|
|
rx <- b[:n]
|
|
|
|
}
|
|
|
|
if nil != err {
|
2020-07-15 04:09:17 +00:00
|
|
|
fmt.Println("[debug] [encoder] [srv] Browser read error", err)
|
2020-05-19 09:35:22 +00:00
|
|
|
rxErr <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-07-13 09:14:11 +00:00
|
|
|
// TODO set a timeout as a failsafe
|
2020-05-19 09:35:22 +00:00
|
|
|
for {
|
|
|
|
//fmt.Println("poopers gonna poop")
|
|
|
|
select {
|
|
|
|
// TODO, do we actually need ctx here?
|
|
|
|
// would it be sufficient to expect the reader to be closed by the caller instead?
|
|
|
|
case <-enc.ctx.Done():
|
|
|
|
// TODO: verify that closing the reader will cause the goroutine to be released
|
2020-05-20 22:52:06 +00:00
|
|
|
//rin.Close()
|
2020-07-15 04:09:17 +00:00
|
|
|
fmt.Println("[debug] [encoder] [srv] Browser ctx.Done()")
|
2020-07-13 09:14:11 +00:00
|
|
|
return errors.New("cancelled by encoder read or parent context")
|
2020-05-19 09:35:22 +00:00
|
|
|
case <-enc.subctx.Done():
|
2020-05-20 22:52:06 +00:00
|
|
|
//rin.Close()
|
2020-07-15 04:09:17 +00:00
|
|
|
fmt.Println("[debug] [encoder] [srv] Browser subctx.Done()")
|
2020-07-13 09:14:11 +00:00
|
|
|
return errors.New("cancelled by encoder write context")
|
2020-05-19 09:35:22 +00:00
|
|
|
case b := <-rx:
|
2020-07-15 04:09:17 +00:00
|
|
|
header, _, err := Encode(b, src, Addr{scheme: src.scheme, addr: dst.Hostname(), port: dst.Port()})
|
2020-05-19 09:35:22 +00:00
|
|
|
if nil != err {
|
2020-05-20 22:52:06 +00:00
|
|
|
//rin.Close()
|
2020-07-15 04:09:17 +00:00
|
|
|
fmt.Println("[debug] [encoder] [srv] Browser Encode err", err)
|
2020-05-19 09:35:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-22 10:34:37 +00:00
|
|
|
//fmt.Println("[debug] encode header:", string(header))
|
|
|
|
//fmt.Println("[debug] encode payload:", string(b))
|
2020-05-21 10:29:05 +00:00
|
|
|
|
2020-05-20 22:23:58 +00:00
|
|
|
_, err = enc.write(header, b)
|
2020-07-15 09:33:23 +00:00
|
|
|
fmt.Println("[debug] [encoder] [srv] Browser-to-tun write", len(header), string(header))
|
2020-07-15 04:09:17 +00:00
|
|
|
fmt.Println("[debug] [encoder] [srv]", len(b), hex.EncodeToString(b))
|
2020-05-19 09:35:22 +00:00
|
|
|
if nil != err {
|
2020-07-15 04:09:17 +00:00
|
|
|
fmt.Println("[debug] [encoder] [srv] Browser-to-tun write err", err)
|
2020-05-20 22:52:06 +00:00
|
|
|
//rin.Close()
|
2020-05-19 09:35:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case err := <-rxErr:
|
|
|
|
// it can be assumed that err will close though, right?
|
2020-05-20 22:52:06 +00:00
|
|
|
//rin.Close()
|
2020-05-19 09:35:22 +00:00
|
|
|
if io.EOF == err {
|
2020-05-21 10:29:05 +00:00
|
|
|
header, _, _ := Encode(nil, src, Addr{scheme: "end"})
|
2020-05-22 10:34:37 +00:00
|
|
|
//fmt.Println("[debug] encode end: ", string(header))
|
2020-05-19 09:35:22 +00:00
|
|
|
// ignore err, which may have already closed
|
2020-05-20 22:23:58 +00:00
|
|
|
_, _ = enc.write(header, nil)
|
2020-05-19 09:35:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-21 10:29:05 +00:00
|
|
|
// TODO transmit message , []byte(err.Error())
|
|
|
|
header, _, _ := Encode(nil, src, Addr{scheme: "error"})
|
2020-05-19 09:35:22 +00:00
|
|
|
// ignore err, which may have already closed
|
2020-05-20 22:23:58 +00:00
|
|
|
_, _ = enc.write(header, nil)
|
2020-05-19 09:35:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:23:58 +00:00
|
|
|
func (enc *Encoder) write(h, b []byte) (int, error) {
|
2020-05-19 09:35:22 +00:00
|
|
|
// mutex here so that we can get back error info
|
|
|
|
enc.mux.Lock()
|
2020-05-20 22:23:58 +00:00
|
|
|
var m int
|
2020-05-21 10:29:05 +00:00
|
|
|
n, err := enc.out.Write(append(h, b...))
|
|
|
|
/*
|
|
|
|
n, err := enc.out.Write(h)
|
|
|
|
if nil == err && len(b) > 0 {
|
|
|
|
m, err = enc.out.Write(b)
|
|
|
|
}
|
|
|
|
*/
|
2020-05-19 09:35:22 +00:00
|
|
|
enc.mux.Unlock()
|
|
|
|
if nil != err {
|
2020-05-20 22:23:58 +00:00
|
|
|
enc.outErr <- err
|
2020-05-19 09:35:22 +00:00
|
|
|
}
|
2020-05-20 22:23:58 +00:00
|
|
|
return n + m, err
|
2020-05-19 09:35:22 +00:00
|
|
|
}
|