2020-04-06 12:07:16 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-04-09 15:37:46 +00:00
|
|
|
"github.com/cbeuw/Cloak/internal/common"
|
2020-04-06 12:07:16 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-10-15 20:32:38 +00:00
|
|
|
func RouteUDP(bindFunc func() (*net.UDPConn, error), streamTimeout time.Duration, newSeshFunc func() *mux.Session) {
|
2020-04-06 12:07:16 +00:00
|
|
|
var sesh *mux.Session
|
2020-05-27 12:40:36 +00:00
|
|
|
localConn, err := bindFunc()
|
2020-04-06 12:07:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:32:38 +00:00
|
|
|
streams := make(map[string]*mux.Stream)
|
2020-04-06 12:07:16 +00:00
|
|
|
|
2020-05-23 14:44:24 +00:00
|
|
|
data := make([]byte, 8192)
|
|
|
|
for {
|
|
|
|
i, addr, err := localConn.ReadFrom(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to read first packet from proxy client: %v", err)
|
|
|
|
continue
|
2020-04-06 12:07:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 20:32:38 +00:00
|
|
|
if sesh == nil || sesh.IsClosed() || sesh.Singleplex {
|
2020-05-23 14:44:24 +00:00
|
|
|
sesh = newSeshFunc()
|
2020-04-06 12:07:16 +00:00
|
|
|
}
|
2020-05-23 14:44:24 +00:00
|
|
|
|
|
|
|
stream, ok := streams[addr.String()]
|
|
|
|
if !ok {
|
2020-10-15 20:32:38 +00:00
|
|
|
stream, err = sesh.OpenStream()
|
2020-05-23 14:44:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to open stream: %v", err)
|
2020-10-15 20:32:38 +00:00
|
|
|
if sesh.Singleplex {
|
|
|
|
sesh.Close()
|
2020-07-06 16:33:52 +00:00
|
|
|
}
|
2020-05-23 14:44:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-07-06 16:33:52 +00:00
|
|
|
|
2020-05-23 14:44:24 +00:00
|
|
|
streams[addr.String()] = stream
|
|
|
|
proxyAddr := addr
|
|
|
|
go func() {
|
|
|
|
buf := make([]byte, 8192)
|
|
|
|
for {
|
|
|
|
n, err := stream.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Tracef("copying stream to proxy client: %v", err)
|
|
|
|
stream.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = localConn.WriteTo(buf[:n], proxyAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Tracef("copying stream to proxy client: %v", err)
|
|
|
|
stream.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-04-06 12:07:16 +00:00
|
|
|
}
|
2020-05-23 14:44:24 +00:00
|
|
|
|
|
|
|
_, err = stream.Write(data[:i])
|
2020-04-06 12:07:16 +00:00
|
|
|
if err != nil {
|
2020-05-23 14:44:24 +00:00
|
|
|
log.Tracef("copying proxy client to stream: %v", err)
|
|
|
|
delete(streams, addr.String())
|
2020-04-06 12:07:16 +00:00
|
|
|
stream.Close()
|
2020-05-23 14:44:24 +00:00
|
|
|
continue
|
2020-04-06 12:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:32: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
|
|
|
|
}
|
2020-10-15 20:32:38 +00:00
|
|
|
if sesh == nil || sesh.IsClosed() || sesh.Singleplex {
|
2020-04-06 12:07:16 +00:00
|
|
|
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
|
|
|
|
}
|
2020-07-06 16:33:52 +00:00
|
|
|
|
2020-10-15 20:32:38 +00:00
|
|
|
stream, err := sesh.OpenStream()
|
2020-04-06 12:07:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to open stream: %v", err)
|
|
|
|
localConn.Close()
|
2020-10-15 20:32:38 +00:00
|
|
|
if sesh.Singleplex {
|
|
|
|
sesh.Close()
|
2020-07-06 16:33:52 +00:00
|
|
|
}
|
2020-04-06 12:07:16 +00:00
|
|
|
return
|
|
|
|
}
|
2020-07-06 16:33:52 +00:00
|
|
|
|
2020-04-06 12:07:16 +00:00
|
|
|
_, err = stream.Write(data[:i])
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to write to stream: %v", err)
|
|
|
|
localConn.Close()
|
|
|
|
stream.Close()
|
|
|
|
return
|
|
|
|
}
|
2020-04-14 12:31:24 +00:00
|
|
|
|
|
|
|
stream.SetReadFromTimeout(streamTimeout) // if localConn hasn't sent anything to stream to a period of time, stream closes
|
2020-04-09 15:37:46 +00:00
|
|
|
go func() {
|
2020-04-14 12:31:24 +00:00
|
|
|
if _, err := common.Copy(localConn, stream); err != nil {
|
2020-04-09 22:52:08 +00:00
|
|
|
log.Tracef("copying stream to proxy client: %v", err)
|
2020-04-09 15:37:46 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-04-14 12:31:24 +00:00
|
|
|
if _, err = common.Copy(stream, localConn); err != nil {
|
2020-04-09 22:52:08 +00:00
|
|
|
log.Tracef("copying proxy client to stream: %v", err)
|
2020-04-09 15:37:46 +00:00
|
|
|
}
|
2020-04-06 12:07:16 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|