Skip to content

Commit

Permalink
robust support of multiple agents + plain http
Browse files Browse the repository at this point in the history
  • Loading branch information
kost committed Dec 3, 2019
1 parent 7a2aa40 commit 163f4a9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
12 changes: 5 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ func main() {
flag.PrintDefaults()
fmt.Println("")
fmt.Println("Usage:")
fmt.Println("1) Start revsocks -listen :8080 -socks 127.0.0.1:1080 on the client.")
fmt.Println("2) Start revsocks -connect client:8080 on the server.")
fmt.Println("1) Start on the client: revsocks -listen :8080 -socks 127.0.0.1:1080 -pass test")
fmt.Println("2) Start on the server: revsocks -connect client:8080 -pass test")
fmt.Println("3) Connect to 127.0.0.1:1080 on the client with any socks5 client.")
fmt.Println("4) Start revsocks -connect client:8080 -proxy 1.2.3.4:3124 -proxyauth Domain/user:pass")
fmt.Println("X) Enjoy. :]")
}

flag.Parse()
Expand Down Expand Up @@ -72,7 +70,7 @@ func main() {
}

//listenForSocks(*listen, *certificate)
log.Fatal(listenForSocks(*listen, *socks, *certificate))
log.Fatal(listenForSocks(true, *listen, *socks, *certificate))
}

if *connect != "" {
Expand Down Expand Up @@ -115,7 +113,7 @@ func main() {
if *recn > 0 {
for i := 1; i <= *recn; i++ {
log.Printf("Connecting to the far end. Try %d of %d", i, *recn)
error1 := connectForSocks(*connect, *proxy)
error1 := connectForSocks(true, *connect, *proxy)
log.Print(error1)
log.Printf("Sleeping for %d sec...", *rect)
tsleep := time.Second * time.Duration(*rect)
Expand All @@ -125,7 +123,7 @@ func main() {
} else {
for {
log.Printf("Reconnecting to the far end... ")
error1 := connectForSocks(*connect, *proxy)
error1 := connectForSocks(true, *connect, *proxy)
log.Print(error1)
log.Printf("Sleeping for %d sec...", *rect)
tsleep := time.Second * time.Duration(*rect)
Expand Down
25 changes: 16 additions & 9 deletions rclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func connectviaproxy(proxyaddr string, connectaddr string) net.Conn {
return nil
}

func connectForSocks(address string, proxy string) error {
func connectForSocks(tlsenable bool, address string, proxy string) error {
var session *yamux.Session
server, err := socks5.New(&socks5.Config{})
if err != nil {
Expand All @@ -194,8 +194,11 @@ func connectForSocks(address string, proxy string) error {
//var conn tls.Conn
if proxy == "" {
log.Println("Connecting to far end")
//conn, err = net.Dial("tcp", address)
conn, err = tls.Dial("tcp", address, conf)
if tlsenable {
conn, err = tls.Dial("tcp", address, conf)
} else {
conn, err = net.Dial("tcp", address)
}
if err != nil {
return err
}
Expand All @@ -204,13 +207,17 @@ func connectForSocks(address string, proxy string) error {
connp = connectviaproxy(proxy, address)
if connp != nil {
log.Println("Proxy successfull. Connecting to far end")
conntls := tls.Client(connp, conf)
err := conntls.Handshake()
if err != nil {
log.Printf("Error connect: %v", err)
return err
if tlsenable {
conntls := tls.Client(connp, conf)
err := conntls.Handshake()
if err != nil {
log.Printf("Error connect: %v", err)
return err
}
newconn = net.Conn(conntls)
} else {
newconn = connp
}
newconn = net.Conn(conntls)
} else {
log.Println("Proxy NOT successfull. Exiting")
return nil
Expand Down
64 changes: 39 additions & 25 deletions rserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import (

"bufio"
"time"
//"encoding/hex"
"github.com/hashicorp/yamux"
"strings"
"strconv"
)

var proxytout = time.Millisecond * 1000 //timeout for wait magicbytes
// Catches yamux connecting to us
func listenForSocks(address string, clients string, certificate string) error {

// listen for agents
func listenForSocks(tlslisten bool, address string, clients string, certificate string) error {
var err, erry error
var cer tls.Certificate
var session *yamux.Session
var sessions []*yamux.Session
var ln net.Listener

if certificate == "" {
cer, err = getRandomTLS(2048)
Expand All @@ -37,9 +38,13 @@ func listenForSocks(address string, clients string, certificate string) error {
log.Printf("Listening for agents on %s", address)
log.Printf("Will start listening for clients on %s", clients)
config := &tls.Config{Certificates: []tls.Certificate{cer}}
//ln, err := net.Listen("tcp", address)
ln, err := tls.Listen("tcp", address, config)
if tlslisten {
ln, err = tls.Listen("tcp", address, config)
} else {
ln, err = net.Listen("tcp", address)
}
if err != nil {
log.Printf("Error listening on %s: %v", address, err)
return err
}
var listenstr = strings.Split(clients, ":")
Expand All @@ -51,7 +56,8 @@ func listenForSocks(address string, clients string, certificate string) error {
for {
conn, err := ln.Accept()
conn.RemoteAddr()
log.Printf("Got a SSL connection from %v: ", conn.RemoteAddr())
agentstr:=conn.RemoteAddr().String()
log.Printf("[%s] Got a SSL connection from %v: ", agentstr, conn.RemoteAddr())
if err != nil {
fmt.Fprintf(os.Stderr, "Errors accepting!")
}
Expand Down Expand Up @@ -91,63 +97,71 @@ func listenForSocks(address string, clients string, certificate string) error {
} else {
//magic bytes received.
//disable socket read timeouts
log.Printf("Got Client from %s", conn.RemoteAddr())
log.Printf("[%s] Got Client from %s", agentstr, conn.RemoteAddr())
conn.SetReadDeadline(time.Now().Add(100 * time.Hour))
listen4clients := fmt.Sprintf("%s:%d",listenstr[0],portnum+portinc)
log.Printf("Built listen string %s", listen4clients)
//Add connection to yamux
session, erry = yamux.Client(conn, nil)
if erry != nil {
log.Printf("Error creating client in yamux for %s: %v", conn.RemoteAddr(), erry)
log.Printf("[%s] Error creating client in yamux for %s: %v", agentstr, conn.RemoteAddr(), erry)
continue
}
sessions=append(sessions,session)
go listenForClients(listen4clients, session)
go listenForClients(agentstr, listenstr[0], portnum+portinc, session)
portinc = portinc + 1
}
}
return nil
}

// Catches clients and connects to yamux
func listenForClients(address string, session *yamux.Session) error {
log.Printf("Waiting for clients on %s", address)
ln, err := net.Listen("tcp", address)
if err != nil {
return err
// Catches local clients and connects to yamux
func listenForClients(agentstr string, listen string, port int, session *yamux.Session) error {
var ln net.Listener
var address string
var err error
portinc:=port
for {
address = fmt.Sprintf("%s:%d",listen,portinc)
log.Printf("[%s] Waiting for clients on %s", agentstr, address)
ln, err = net.Listen("tcp", address)
if err != nil {
log.Printf("[%s] Error listening on %s: %v", agentstr, address, err)
portinc = portinc +1
} else {
break
}
}
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("[%s] Error accepting on %s: %v", agentstr, address, err)
return err
}
// TODO dial socks5 through yamux and connect to conn

if session == nil {
log.Printf("[%s] Session on %s is nil", agentstr, address)
conn.Close()
continue
}
log.Printf("Got a client for %s", conn.RemoteAddr())
log.Printf("[%s] Got client. Opening stream for %s", agentstr, conn.RemoteAddr())

log.Printf("Opening a stream for %s", conn.RemoteAddr())
stream, err := session.Open()
if err != nil {
log.Printf("Error opening stream for %s: %v", conn.RemoteAddr(), err)
log.Printf("[%s] Error opening stream for %s: %v", agentstr, conn.RemoteAddr(), err)
return err
}

// connect both of conn and stream

go func() {
log.Printf("Starting to copy conn to stream for %s", conn.RemoteAddr())
log.Printf("[%s] Starting to copy conn to stream for %s", agentstr, conn.RemoteAddr())
io.Copy(conn, stream)
conn.Close()
log.Printf("[%s] Done copying conn to stream for %s", agentstr, conn.RemoteAddr())
}()
go func() {
log.Printf("Starting to copy stream to conn for %s", conn.RemoteAddr())
log.Printf("[%s] Starting to copy stream to conn for %s", agentstr, conn.RemoteAddr())
io.Copy(stream, conn)
stream.Close()
log.Printf("Done copying stream to conn for %s", conn.RemoteAddr())
log.Printf("[%s] Done copying stream to conn for %s", agentstr, conn.RemoteAddr())
}()
}
}

0 comments on commit 163f4a9

Please sign in to comment.