Skip to content

Commit

Permalink
Fix issue when the remote socks server is close
Browse files Browse the repository at this point in the history
  • Loading branch information
nodauf committed Apr 27, 2021
1 parent 2bf3268 commit c922d8b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func FlushRoutes() {
fmt.Println("[*] Successfull route flushed")
}

func GetRoute(ip string) string {
func GetRoute(ip string) (string, string) {
for network, remoteSocks := range Routes {
if utils.CIDRContainsIP(network, ip) {
return remoteSocks
return network, remoteSocks
}
}
return ""
return "", ""
}
18 changes: 13 additions & 5 deletions socks/socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"strconv"
"strings"

socks5 "github.com/nodauf/Go-RouterSocks/go-socks5"
router "github.com/nodauf/Go-RouterSocks/router"
Expand Down Expand Up @@ -50,9 +51,15 @@ func listenAndAccept(address string, status chan error) {
if err != nil {
log.Println(err)
}
remoteSocks := router.GetRoute(dest)
network, remoteSocks := router.GetRoute(dest)
if remoteSocks != "" {
connectToSocks(firstBytes, secondBytes, conn, remoteSocks)
err := connectToSocks(firstBytes, secondBytes, conn, remoteSocks)
// If the socks server is no longer available, we have the error conneciton refused
if err != nil && strings.Contains(err.Error(), "connection refused") {
// The route is no longer valid and we delete it
fmt.Println("Remote socks server no longer available")
router.DeleteRoutes(network)
}
} else {
fmt.Println("\n[-] Unkown route for " + dest)
conn.Close()
Expand All @@ -61,13 +68,14 @@ func listenAndAccept(address string, status chan error) {
}
}

func connectToSocks(firstBytes []byte, secondBytes []byte, src net.Conn, remoteSocks string) {
func connectToSocks(firstBytes []byte, secondBytes []byte, src net.Conn, remoteSocks string) error {

var proxy net.Conn
//log.Println("Connecting to remote socks")
proxy, err := net.Dial("tcp", remoteSocks)
if err != nil {
fmt.Println(err)
log.Println(err)
return err
}
defer src.Close()
defer proxy.Close()
Expand All @@ -87,7 +95,7 @@ func connectToSocks(firstBytes []byte, secondBytes []byte, src net.Conn, remoteS
case <-chanToRemote:
//log.Println("Local program is terminated")
}

return nil
}

// Performs copy operation between streams: os and tcp streams
Expand Down

0 comments on commit c922d8b

Please sign in to comment.