Skip to content

Commit

Permalink
chore!(proxy): add proxy support for broker (#4)
Browse files Browse the repository at this point in the history
* feat(dev/cli): initialize cli

* refactor(dev/cli): refactoring flag system

* refactor(dev/cli): simplify print usage

* feat(dev/cli): add custom http handler for verifying

* feat(dev/server): add isPublic property on variable

* feat(dev/cli): add status code validation for response

* feat(dev/cli): add status code validation for response

* feat(dev/cli): finalize auth command

* feat(dev/cli): create base command for get

* refactor(dev/cli): formatting auth and add remove default value

* feat(dev/cli): create http request for get token before spawning echo instance

* feat(dev/cli): implementing webserver on get command

* refactor(dev/cli): pruning one file

* feat(dev/image): add docker compose

* fix(dev/server): modify dockerfile

* fix(dev/server): pruning dockerfile and modify csproj

* feat(dev/cli): add message on command before sending file

* feat(dev/cli): add feature to upload

* feat(dev/cli): add progress while uploading

* feat(dev/cli): add slider for progressbar

* feat(dev/cli): add validator for duplicate file

* feat(dev/cli): set response header for keep-alive

* refactor(dev/cli): remove unnecessary file

* feat(dev/cli): create new base command for send

* refactor(dev/server): add tunnel metadata on verify

* feat(dev/server): improving entrypoint for creating base random passowrd for salting certificate (soon)

* refactor(dev/server): add environment on docker compose

* fix(dev/cli): change parsing and add missing parameter

* fix(dev/cli): fixing wrong pinning json

* feat(dev/cli): add ssh tunnel generator args and implement on get variable

* feat(dev/cli): make function to upload file
fix(dev/cli): create command for gracefully kill tunnel after transfer

* fix(dev/cli): fix error when ssh tunnel is up

* feat(dev/cli): progress bar for uploading
fix(dev/cli): make receiver only retrive one request from sender

* refactor(dev/server): create new output for ssh proxy

* feat(dev/cli): add proxy response on config fule

* fix(dev/cli): proxy command support and use shell instead of binary execution
  • Loading branch information
ikr4-m authored Jul 25, 2024
1 parent 9e7a6dd commit 5786966
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 41 deletions.
16 changes: 10 additions & 6 deletions client/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ func AuthCommand(args []string) {
URL: *url,
Token: *token,
Tunnel: internal.ConfigFileTunnel{
Host: verifyData.TunnelHost,
Port: verifyData.TunnelPort,
Host: verifyData.Tunnel.Host,
Port: verifyData.Tunnel.Port,
Proxy: verifyData.Tunnel.Proxy,
},
}
err = config.WriteConfig()
Expand All @@ -79,10 +80,13 @@ func AuthCommand(args []string) {
}

type VerifyJSONReturn struct {
Message string `json:"message"`
IsPublic bool `json:"isPublic"`
TunnelHost string `json:"tunnelHost"`
TunnelPort int `json:"tunnelPort"`
Message string `json:"message"`
IsPublic bool `json:"isPublic"`
Tunnel struct {
Host string `json:"host"`
Port int `json:"port"`
Proxy string `json:"proxy"`
} `json:"tunnel"`

// This respon fired when the API is failed
ErrorTitle string `json:"title"`
Expand Down
10 changes: 8 additions & 2 deletions client/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var sshErrGlobal chan error = make(chan error)
var sshPidGlobal chan int = make(chan int)

func StartShellTunnel(isRemote bool, c internal.ConfigFile, localPort int, remotePort int) {
args := strings.Split(internal.GenerateSSHArgs(isRemote, c, localPort, remotePort), " ")
cmd := exec.Command("ssh", args...)
args := internal.GenerateSSHArgs(isRemote, c, localPort, remotePort)
cmd := exec.Command("sh", "-c", args)

stdout, err := cmd.StdoutPipe()
stderr, err := cmd.StderrPipe()
Expand Down Expand Up @@ -48,6 +48,12 @@ func StartShellTunnel(isRemote bool, c internal.ConfigFile, localPort int, remot
if strings.Contains(m, "remote port forwarding failed") {
sshErrGlobal <- errors.New("Duplicate remote on bridge server")
}
if strings.Contains(m, "EXCEPTION") {
sshErrGlobal <- errors.New("Error from Server: "+m)
}
if strings.Contains(m, "exec") && strings.Contains(m, "not found") {
sshErrGlobal <- errors.New("Proxy app not found. Did you install it?")
}
fmt.Println(m)
}
}()
Expand Down
5 changes: 3 additions & 2 deletions client/internal/rw_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ var (
)

type ConfigFileTunnel struct {
Host string `json:"host"`
Port int `json:"port"`
Host string `json:"host"`
Port int `json:"port"`
Proxy string `json:"proxy"`
}

type ConfigFile struct {
Expand Down
62 changes: 37 additions & 25 deletions client/internal/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,47 @@ package internal

import (
"fmt"
"strconv"
"strings"
)

func GenerateSSHArgs(isRemote bool, c ConfigFile, localPort int, remotePort int) string {
args := fmt.Sprintf(
"-p %v -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tunnel@%v",
c.Tunnel.Port,
c.Tunnel.Host,
)

flag := [4]int{}
remoteFlag := ""
flag[2] = remotePort
func GenerateSSHArgs(isRemote bool, c ConfigFile, localPort int, remotePort int) string{
args := []string{
"-o",
"UserKnownHostsFile=/dev/null",
"-o",
"StrictHostKeyChecking=no",
"tunnel@"+c.Tunnel.Host,
strconv.Itoa(remotePort),
}
generateProxyCommand(&args, c)

if isRemote {
remoteFlag = "-R"
flag[0] = remotePort
flag[1] = localPort
args = append(
[]string{"-R", fmt.Sprintf("%v:127.0.0.1:%v", remotePort, localPort)},
args...,
)
} else {
remoteFlag = "-L"
flag[1] = remotePort
flag[0] = localPort
args = append(
[]string{"-L", fmt.Sprintf("%v:127.0.0.1:%v", localPort, remotePort)},
args...,
)
}

args = fmt.Sprintf(
"%v %v:127.0.0.1:%v %v %v",
remoteFlag,
flag[0],
flag[1],
args,
flag[2],
)
return args
return "ssh "+strings.Join(args, " ")
}

func generateProxyCommand(args* []string, c ConfigFile) {
switch c.Tunnel.Proxy {
case "cloudflared":
*args = append(
[]string{
"-o",
fmt.Sprintf("ProxyCommand=\"cloudflared access ssh --hostname %v\"", c.Tunnel.Host),
},
*args...
)
default:
*args = append([]string{"-p", strconv.Itoa(c.Tunnel.Port)}, *args...)
}
}
1 change: 1 addition & 0 deletions server/MDrop.Broker/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public static class Constant
{
public static readonly string TunnelHost = Environment.GetEnvironmentVariable("TUNNEL_HOST") ?? "127.0.0.1";
public static readonly int TunnelPort = int.Parse(Environment.GetEnvironmentVariable("TUNNEL_PORT") ?? "2222");
public static readonly string TunnelProxy = Environment.GetEnvironmentVariable("TUNNEL_PROXY") ?? "";
public static readonly string PrivateModeToken = Environment.GetEnvironmentVariable("PRIVATE_MODE_TOKEN") ?? "";
public static X509Certificate2 Certificate = X509Certificate2.CreateFromEncryptedPemFile(
"cert.pem",
Expand Down
20 changes: 14 additions & 6 deletions server/MDrop.Broker/Controllers/VerifyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ public class VerifyReturnJson
[JsonPropertyName("message")]
public string Message { get; set; } = "";

[JsonPropertyName("tunnelHost")]
public string TunnelHost { get; set; } = Constant.TunnelHost;

[JsonPropertyName("tunnelPort")]
public int TunnelPort { get; set; } = Constant.TunnelPort;

[JsonPropertyName("isPublic")]
public bool IsPublic { get; set; } = false;

[JsonPropertyName("tunnel")]
public TunnelProperty Tunnel { get; set; } = new TunnelProperty();
public class TunnelProperty
{
[JsonPropertyName("host")]
public string Host { get; set; } = Constant.TunnelHost;

[JsonPropertyName("port")]
public int Port { get; set; } = Constant.TunnelPort;

[JsonPropertyName("proxy")]
public string Proxy { get; set; } = Constant.TunnelProxy;
}
}
}
1 change: 1 addition & 0 deletions server/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ services:
PRIVATE_MODE_TOKEN: ""
TUNNEL_HOST: 127.0.0.1
TUNNEL_PORT: 2222
TUNNEL_PROXY: ""
ports:
- 5000:5000

0 comments on commit 5786966

Please sign in to comment.