Skip to content

Commit

Permalink
Forward command implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
eminmuhammadi committed Sep 18, 2022
1 parent 2c17706 commit 4824c2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

xtunnel is a simple tunneling tool that allows you to create a tunnel in different ways.

Note: This tool is still in development and is not ready for production use. All connection is done over insecure channels.

## Installation

You can download binary files for each platform from [the latest releases](https://github.com/eminmuhammadi/xtunnel/releases).

## Usage

### Port Forwarding
```bash
xtunnel start --master <local-ip>:9090 --target example.com:80 --protocol tcp4
xtunnel forward --local <local-ip>:<local-port> --remote <remote-ip>:<tcp-port> --protocol tcp
```
48 changes: 24 additions & 24 deletions cmd/start.go → cmd/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ import (
cli "github.com/urfave/cli/v2"
)

func Start() *cli.Command {
func Forward() *cli.Command {
return &cli.Command{
Name: "start",
Aliases: []string{"s"},
Name: "forward",
Aliases: []string{"f"},
Usage: "Forwards remote connections to local port",
Flags: []cli.Flag{
// -m, --master flag
// -r, --remote flag
&cli.StringFlag{
Name: "master",
Aliases: []string{"m"},
Usage: "Master node",
Name: "remote",
Aliases: []string{"r"},
Usage: "Remote node",
Required: true,
},
// -t, --target flag
// -l, --local flag
&cli.StringFlag{
Name: "target",
Aliases: []string{"t"},
Usage: "Target node",
Name: "local",
Aliases: []string{"l"},
Usage: "Local node",
Required: true,
},
// -p, --protocol flag
Expand All @@ -56,36 +56,36 @@ func Start() *cli.Command {
Action: func(ctx *cli.Context) error {
log.Println("Starting tunnel...")

master := ctx.String("master")
log.Printf("Using %s as master node\n", master)
local := ctx.String("local")
log.Printf("Using %s as local node\n", local)

target := ctx.String("target")
log.Printf("Dialling %s, and using it as target node\n", target)
remote := ctx.String("remote")
log.Printf("Dialling %s, and using it as remote node\n", remote)

protocol := ctx.String("protocol")
log.Printf("Using %s as protocol\n", protocol)

masterHost, masterPort := strings.Split(master, ":")[0], strings.Split(master, ":")[1]
log.Printf("Master info: %s, port: %s\n", masterHost, masterPort)
localHost, localPort := strings.Split(local, ":")[0], strings.Split(local, ":")[1]
log.Printf("Local info: %s, port: %s\n", localHost, localPort)

targetHost, targetPort := strings.Split(target, ":")[0], strings.Split(target, ":")[1]
log.Printf("Target info: %s, port: %s\n", targetHost, targetPort)
remoteHost, remotePort := strings.Split(remote, ":")[0], strings.Split(remote, ":")[1]
log.Printf("Remote info: %s, port: %s\n", remoteHost, remotePort)

masterPortN, err := strconv.Atoi(masterPort)
localPortN, err := strconv.Atoi(localPort)
if err != nil {
return err
}

targetPortN, err := strconv.Atoi(targetPort)
remotePortN, err := strconv.Atoi(remotePort)
if err != nil {
return err
}

masterNode := pkg.NewNode(masterHost, masterPortN)
targetNode := pkg.NewNode(targetHost, targetPortN)
localNode := pkg.NewNode(localHost, localPortN)
remoteNode := pkg.NewNode(remoteHost, remotePortN)

log.Println("Connection established")
if err := pkg.CreateTunnel(protocol, masterNode, targetNode); err != nil {
if err := pkg.CreateTunnel(protocol, localNode, remoteNode); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
)

var commands = []*cli.Command{
cmd.Start(),
cmd.Forward(),
}

// Metadata for current release
Expand Down
16 changes: 7 additions & 9 deletions pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type Node struct {
type Tunnel struct {
Protocol string
Listener net.Listener
Target *Node
}

// Creates a new node
Expand All @@ -45,7 +44,7 @@ func NewNode(host string, port int) *Node {
}

// Listen announces on the local network address.
func (node *Node) Listen(protocol string, target *Node) (Tunnel, error) {
func (node *Node) Listen(protocol string) (Tunnel, error) {
listener, err := net.Listen(protocol, fmt.Sprintf("%s:%d", node.Host, node.Port))

if err != nil {
Expand All @@ -55,7 +54,6 @@ func (node *Node) Listen(protocol string, target *Node) (Tunnel, error) {
return Tunnel{
Protocol: protocol,
Listener: listener,
Target: target,
}, nil
}

Expand Down Expand Up @@ -100,9 +98,9 @@ func Handshake(local net.Conn, remote net.Conn) {
}

// Creates a new reverse tunnel
func CreateTunnel(protocol string, master *Node, target *Node) error {
// Listen on master
tunnel, err := master.Listen(protocol, target)
func CreateTunnel(protocol string, local *Node, remote *Node) error {
// Listen on local
tunnel, err := local.Listen(protocol)

if err != nil {
return err
Expand All @@ -111,14 +109,14 @@ func CreateTunnel(protocol string, master *Node, target *Node) error {
defer tunnel.Listener.Close()

for {
// Accept connections from master
// Accept connections from local
local, err := tunnel.Listener.Accept()
if err != nil {
return err
}

// Dial to target
remote, err := target.Dial(protocol)
// Dial to remote
remote, err := remote.Dial(protocol)
if err != nil {
return err
}
Expand Down

0 comments on commit 4824c2e

Please sign in to comment.