Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/mmar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ func main() {
utils.EnvVarOrDefault(constants.MMAR_ENV_VAR_TUNNEL_HOST, constants.TUNNEL_HOST),
constants.TUNNEL_HOST_HELP,
)
clientCustomDns := clientCmd.String(
"custom-dns",
utils.EnvVarOrDefault(constants.MMAR_ENV_VAR_CUSTOM_DNS, ""),
constants.CLIENT_CUSTOM_DNS_HELP,
)
clientCustomCert := clientCmd.String(
"custom-cert",
utils.EnvVarOrDefault(constants.MMAR_ENV_VAR_CUSTOM_CERT, ""),
constants.CLIENT_CUSTOM_CERT_HELP,
)

versionCmd := flag.NewFlagSet(constants.VERSION_CMD, flag.ExitOnError)
versionCmd.Usage = utils.MmarVersionUsage
Expand All @@ -71,6 +81,8 @@ func main() {
TunnelHttpPort: *clientTunnelHttpPort,
TunnelTcpPort: *clientTunnelTcpPort,
TunnelHost: *clientTunnelHost,
CustomDns: *clientCustomDns,
CustomCert: *clientCustomCert,
}
client.Run(mmarClientConfig)
case constants.VERSION_CMD:
Expand Down
12 changes: 8 additions & 4 deletions constants/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ const (
MMAR_ENV_VAR_TUNNEL_HTTP_PORT = "MMAR__TUNNEL_HTTP_PORT"
MMAR_ENV_VAR_TUNNEL_TCP_PORT = "MMAR__TUNNEL_TCP_PORT"
MMAR_ENV_VAR_TUNNEL_HOST = "MMAR__TUNNEL_HOST"
MMAR_ENV_VAR_CUSTOM_DNS = "MMAR__CUSTOM_DNS"
MMAR_ENV_VAR_CUSTOM_CERT = "MMAR__CUSTOM_CERT"

SERVER_STATS_DEFAULT_USERNAME = "admin"
SERVER_STATS_DEFAULT_PASSWORD = "admin"

SERVER_HTTP_PORT_HELP = "Define port where mmar will bind to and run on server for HTTP requests."
SERVER_TCP_PORT_HELP = "Define port where mmar will bind to and run on server for TCP connections."

CLIENT_LOCAL_PORT_HELP = "Define the port where your local dev server is running to expose through mmar."
CLIENT_HTTP_PORT_HELP = "Define port of mmar HTTP server to make requests through the tunnel."
CLIENT_TCP_PORT_HELP = "Define port of mmar TCP server for client to connect to, creating a tunnel."
TUNNEL_HOST_HELP = "Define host domain of mmar server for client to connect to."
CLIENT_LOCAL_PORT_HELP = "Define the port where your local dev server is running to expose through mmar."
CLIENT_HTTP_PORT_HELP = "Define port of mmar HTTP server to make requests through the tunnel."
CLIENT_TCP_PORT_HELP = "Define port of mmar TCP server for client to connect to, creating a tunnel."
TUNNEL_HOST_HELP = "Define host domain of mmar server for client to connect to."
CLIENT_CUSTOM_DNS_HELP = "Define a custom DNS server that the mmar client should use when accessing your local dev server. (eg: 8.8.8.8:53, defaults to DNS in OS)"
CLIENT_CUSTOM_CERT_HELP = "Define path to file custom TLS certificate containing complete ASN.1 DER content (certificate, signature algorithm and signature). Currently used for testing, but may be used to allow mmar client to work with a dev server using custom TLS certificate setups. (eg: /path/to/cert)"

TUNNEL_MESSAGE_PROTOCOL_VERSION = 3
TUNNEL_MESSAGE_DATA_DELIMITER = '\n'
Expand Down
52 changes: 50 additions & 2 deletions internal/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bufio"
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
Expand All @@ -26,6 +28,8 @@ type ConfigOptions struct {
TunnelHttpPort string
TunnelTcpPort string
TunnelHost string
CustomDns string
CustomCert string
}

type MmarClient struct {
Expand Down Expand Up @@ -58,6 +62,50 @@ func (mc *MmarClient) handleRequestMessage(tunnelMsg protocol.TunnelMessage) {
},
}

// Use custom DNS if set
if mc.CustomDns != "" {
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return net.Dial("udp", mc.CustomDns)
},
}
dialer := &net.Dialer{
Resolver: r,
}

tp := &http.Transport{
DialContext: dialer.DialContext,
}

fwdClient.Transport = tp
}

// Use custom TLS certificate if setup
if mc.CustomCert != "" {
certData, certFileErr := os.ReadFile(mc.CustomCert)
if certFileErr != nil {
logger.Log(
constants.RED,
fmt.Sprintf(
"Could not read certificate from file: %v",
certFileErr,
))
os.Exit(1)
}

cert, certErr := x509.ParseCertificate(certData)
if certErr != nil {
logger.Log(constants.YELLOW, "Warning: Could not load custom certificate")
} else {
fmt.Println("adding cert dawg..")
fwdClient.Transport.(*http.Transport).TLSClientConfig = &tls.Config{
RootCAs: x509.NewCertPool(),
}
fwdClient.Transport.(*http.Transport).TLSClientConfig.RootCAs.AddCert(cert)
}
}

reqReader := bufio.NewReader(bytes.NewReader(tunnelMsg.MsgData))
req, reqErr := http.ReadRequest(reqReader)

Expand Down Expand Up @@ -100,8 +148,6 @@ func (mc *MmarClient) handleRequestMessage(tunnelMsg protocol.TunnelMessage) {
return
}

logger.LogHTTP(req, resp.StatusCode, resp.ContentLength, false, true)

// Writing response to buffer to tunnel it back
var responseBuff bytes.Buffer
resp.Write(&responseBuff)
Expand All @@ -110,6 +156,8 @@ func (mc *MmarClient) handleRequestMessage(tunnelMsg protocol.TunnelMessage) {
if err := mc.SendMessage(respMessage); err != nil {
log.Fatal(err)
}

logger.LogHTTP(req, resp.StatusCode, resp.ContentLength, false, true)
}

// Keep attempting to reconnect the existing tunnel until successful
Expand Down
12 changes: 10 additions & 2 deletions simulations/devserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ type DevServer struct {
*httptest.Server
}

func NewDevServer() *DevServer {
func NewDevServer(proto string, addr string) *DevServer {
mux := setupMux()

var httpServer *httptest.Server
switch proto {
case "https":
httpServer = httptest.NewTLSServer(mux)
case "http":
httpServer = httptest.NewServer(mux)
}

return &DevServer{
httptest.NewServer(mux),
httpServer,
}
}

Expand Down
Loading
Loading