Cloak/internal/client/piper.go

144 lines
3.1 KiB
Go
Raw Normal View History

2020-04-06 12:07:16 +00:00
package client
import (
"github.com/cbeuw/Cloak/internal/common"
2020-04-06 12:07:16 +00:00
"io"
"net"
"sync/atomic"
"time"
mux "github.com/cbeuw/Cloak/internal/multiplex"
log "github.com/sirupsen/logrus"
)
2020-04-11 21:37:15 +00:00
func RouteUDP(listen func(string, string) (net.PacketConn, error), localConfig LocalConnConfig, newSeshFunc func() *mux.Session) {
2020-04-06 12:07:16 +00:00
var sesh *mux.Session
start:
2020-04-11 21:37:15 +00:00
localConn, err := listen("udp", localConfig.LocalAddr)
2020-04-06 12:07:16 +00:00
if err != nil {
log.Fatal(err)
}
var otherEnd atomic.Value
data := make([]byte, 10240)
2020-04-11 21:37:15 +00:00
i, oe, err := localConn.ReadFrom(data)
2020-04-06 12:07:16 +00:00
if err != nil {
log.Errorf("Failed to read first packet from proxy client: %v", err)
localConn.Close()
return
}
otherEnd.Store(oe)
if sesh == nil || sesh.IsClosed() {
sesh = newSeshFunc()
}
2020-04-11 21:37:15 +00:00
log.Debugf("proxy local address %v", otherEnd.Load().(net.Addr).String())
2020-04-06 12:07:16 +00:00
stream, err := sesh.OpenStream()
if err != nil {
log.Errorf("Failed to open stream: %v", err)
localConn.Close()
//localConnWrite.Close()
return
}
_, err = stream.Write(data[:i])
if err != nil {
log.Errorf("Failed to write to stream: %v", err)
localConn.Close()
//localConnWrite.Close()
stream.Close()
return
}
// stream to proxy
go func() {
buf := make([]byte, 16380)
for {
i, err := io.ReadAtLeast(stream, buf, 1)
if err != nil {
log.Print(err)
localConn.Close()
stream.Close()
break
}
2020-04-11 21:37:15 +00:00
_, err = localConn.WriteTo(buf[:i], otherEnd.Load().(net.Addr))
2020-04-06 12:07:16 +00:00
if err != nil {
log.Print(err)
localConn.Close()
stream.Close()
break
}
}
}()
// proxy to stream
buf := make([]byte, 16380)
if localConfig.Timeout != 0 {
localConn.SetReadDeadline(time.Now().Add(localConfig.Timeout))
}
for {
if localConfig.Timeout != 0 {
localConn.SetReadDeadline(time.Now().Add(localConfig.Timeout))
}
2020-04-11 21:37:15 +00:00
i, oe, err := localConn.ReadFrom(buf)
2020-04-06 12:07:16 +00:00
if err != nil {
localConn.Close()
stream.Close()
break
}
otherEnd.Store(oe)
_, err = stream.Write(buf[:i])
if err != nil {
localConn.Close()
stream.Close()
break
}
}
goto start
}
2020-04-10 10:07:38 +00:00
func RouteTCP(listener net.Listener, streamTimeout time.Duration, newSeshFunc func() *mux.Session) {
2020-04-06 12:07:16 +00:00
var sesh *mux.Session
for {
2020-04-10 10:07:38 +00:00
localConn, err := listener.Accept()
2020-04-06 12:07:16 +00:00
if err != nil {
log.Fatal(err)
continue
}
if sesh == nil || sesh.IsClosed() {
sesh = newSeshFunc()
}
go func() {
data := make([]byte, 10240)
i, err := io.ReadAtLeast(localConn, data, 1)
if err != nil {
log.Errorf("Failed to read first packet from proxy client: %v", err)
localConn.Close()
return
}
stream, err := sesh.OpenStream()
if err != nil {
log.Errorf("Failed to open stream: %v", err)
localConn.Close()
return
}
_, err = stream.Write(data[:i])
if err != nil {
log.Errorf("Failed to write to stream: %v", err)
localConn.Close()
stream.Close()
return
}
go func() {
if _, err := common.Copy(localConn, stream, 0); err != nil {
2020-04-09 22:52:08 +00:00
log.Tracef("copying stream to proxy client: %v", err)
}
}()
//util.Pipe(stream, localConn, localConfig.Timeout)
2020-04-10 10:07:38 +00:00
if _, err = common.Copy(stream, localConn, streamTimeout); err != nil {
2020-04-09 22:52:08 +00:00
log.Tracef("copying proxy client to stream: %v", err)
}
2020-04-06 12:07:16 +00:00
}()
}
}