mosers
/
eaas-vde-proxy
Archiviert
1
0
Fork 0

Proxy forwards and displays binary traffic

main
Simon Moser vor 3 Jahren
Ursprung 00cc5a0cad
Commit 037a9d69a4
Signiert von: mosers
GPG-Schlüssel-ID: 96B3365A234B500C

@ -1,33 +1,42 @@
package cmd package cmd
import ( import (
"bytes"
"io" "io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
) )
// Overloads the exec.Cmd class to save the full command
// and adds custom input/output pipes
type cmd struct { type cmd struct {
fullCommand string fullCommand string
*exec.Cmd *exec.Cmd
bufOut bytes.Buffer inReader io.Reader
bufErr bytes.Buffer InWriter io.Writer
OutReader io.Reader
outWriter io.Writer
} }
// New creates a new cmd object with given arguments and returns it
func New(name string, args string) *cmd{ func New(name string, args string) *cmd{
var bO, bE bytes.Buffer ir, iw := io.Pipe()
or, ow := io.Pipe()
c := cmd { c := cmd {
name + " " + args, name + " " + args,
exec.Command(name, args), exec.Command(name, args),
bO, ir,
bE, iw,
} or,
c.Stdout = io.MultiWriter(os.Stdout, &c.bufOut) ow,
c.Stderr = io.MultiWriter(os.Stderr, &c.bufErr) }
c.Stdout = c.outWriter
c.Stdin = c.inReader
c.Stderr = os.Stderr
return &c return &c
} }
// Execute runs Cmd.Start() and catches the possible error
func (c *cmd) Execute() { func (c *cmd) Execute() {
err := c.Start() err := c.Start()
if err != nil { if err != nil {
@ -35,17 +44,15 @@ func (c *cmd) Execute() {
} }
} }
func (c *cmd) GetOut() string { // WaitH runs Cmd.Wait() and catches the possible error
return string(c.bufOut.Bytes())
}
func (c *cmd) GetErr() string {
return string(c.bufErr.Bytes())
}
func (c *cmd) WaitH() { func (c *cmd) WaitH() {
err := c.Wait() err := c.Wait()
if err != nil { if err != nil {
log.Printf("%s failed with %s\n", c.fullCommand, err) log.Printf("%s failed with %s\n", c.fullCommand, err)
} }
} }
const (
Out = "\u001b[31m>> " // Prefix for traffic from VM
In = "\u001b[32m<< " // Prefix for traffic to VM
)

@ -1,31 +1,48 @@
package main package main
import ( import (
"fmt"
"io"
"os"
"proxy/cmd" "proxy/cmd"
"unicode"
) )
// Start the two plugs and run two concurrent forward methods
func main() { func main() {
//c1 := cmd.New("sh", "-c \"vde_plug /run/vde/sw_main.sock\"") c1 := cmd.New("vde_plug", "/run/vde/sw_main.sock")
c2 := cmd.New("vde_plug", "/run/vde/sw_proxy.sock") c2 := cmd.New("vde_plug", "/run/vde/sw_proxy.sock")
//c1 := cmd.New("ping", "localhost", "-c 10") c1.Execute()
//c2 := cmd.New("nc", "google.com", "80")
//stdin, err := c2.StdinPipe()
//if err != nil {
// log.Fatal(err)
//}
//c1.Execute()
c2.Execute() c2.Execute()
//time.Sleep(time.Second) go pipeForward(c1.OutReader, c2.InWriter, cmd.In)
//io.WriteString(stdin, "GET / HTTP/1.0\n\n") go pipeForward(c2.OutReader, c1.InWriter, cmd.Out)
//time.Sleep(time.Second) c1.WaitH()
//stdin.Close()
//c1.WaitH()
c2.WaitH() c2.WaitH()
}
/* cmds := []*cmd.cmd{c1} // Reads from an input and writes to and output,
for _, x := range cmds { // do things to the content in between.
go func(cmd *exec.Cmd) { // For now only output it in xxd format.
fmt.Printf(x.String()) // Is meant to be run concurrently with "go pipeForward(...)"
}(cmd) func pipeForward(reader io.Reader, writer io.Writer, prefix string) {
}*/ i := 0
for {
bytes := make([]byte, 16)
bytesReadable := make([]byte, 16)
_, err := reader.Read(bytes)
if err == io.EOF {
break
}
for i, ch := range bytes {
if ch > unicode.MaxASCII || ch < '\u0020' {
bytesReadable[i] = '\u002E'
} else {
bytesReadable[i] = ch
}
}
xxdString := fmt.Sprintf("%s%08x: %04x %04x %04x %04x %04x %04x %04x %04x %s\n", prefix, i, bytes[0:1], bytes[2:3], bytes[4:5], bytes[6:7], bytes[8:9], bytes[10:11], bytes[12:13], bytes[14:15], bytesReadable)
os.Stdout.WriteString(xxdString)
writer.Write(bytes)
i += 16
}
} }

@ -12,10 +12,11 @@ case "$1" in
vde_switch -daemon -s $RUN/sw_main.sock -p $RUN/sw_main.pid vde_switch -daemon -s $RUN/sw_main.sock -p $RUN/sw_main.pid
# Proxy switch -.- # Proxy switch -.-
vde_switch -daemon -s $RUN/sw_proxy.sock -p $RUN/sw_proxy.pid vde_switch -daemon -s $RUN/sw_proxy.sock -p $RUN/sw_proxy.pid
# Slirp NAT # Slirp NAT + Port forwarding SSH
slirpvde --dhcp --daemon -s $RUN/sw_main.sock -p $RUN/slirp.pid slirpvde -D -H 10.0.0.2 --daemon -s $RUN/sw_main.sock -p $RUN/slirp.pid
echo "Run:\nqemu -m 512 -nic vde,mac='52:54:00:12:34:56',sock=$RUN/sw_proxy.sock -hda alpine1.qcow2 -nographic" #echo "Run:\nqemu -m 512 -nic vde,mac='52:54:00:12:34:56',sock=$RUN/sw_proxy.sock -hda alpine1.qcow2 -nographic"
$qemu -m 512 -nic vde,mac='52:54:00:12:34:66',sock=$RUN/sw_main.sock -hda alpine2.qcow2 -daemonize -vnc :1 -pidfile $RUN/vm_2.pid $qemu -m 512 -nic vde,mac='52:54:00:12:34:56',sock=$RUN/sw_proxy.sock -hda alpine1.qcow2 -daemonize -vnc :1 -pidfile $RUN/vm_1.pid
$qemu -m 512 -nic vde,mac='52:54:00:12:34:66',sock=$RUN/sw_main.sock -hda alpine2.qcow2 -daemonize -vnc :2 -pidfile $RUN/vm_2.pid
;; ;;
stop) stop)
echo "Stopping VDE network for QEMU: " echo "Stopping VDE network for QEMU: "
@ -29,6 +30,9 @@ case "$1" in
status) status)
ps -fq `cat $RUN/*.pid | xargs | sed 's/ /,/g'` ps -fq `cat $RUN/*.pid | xargs | sed 's/ /,/g'`
;; ;;
ssh)
ssh -p 2222 root@localhost
;;
*) *)
echo "Usage: $0 {start|stop|restart|reload}" echo "Usage: $0 {start|stop|restart|reload}"
exit 1 exit 1