Skip to content

Commit

Permalink
End-to-end FTP working.
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed Feb 15, 2018
1 parent 110bb85 commit 052af7d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
8 changes: 7 additions & 1 deletion cmd/marionette-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/redjack/marionette"
"github.com/redjack/marionette/mar"
_ "github.com/redjack/marionette/plugins"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -82,8 +83,11 @@ func run() error {
}
defer ln.Close()

streamSet := marionette.NewStreamSet()
defer streamSet.Close()

// Create dialer to remote server.
dialer, err := marionette.NewDialer(doc, config.Server.IP)
dialer, err := marionette.NewDialer(doc, config.Server.IP, streamSet)
if err != nil {
return err
}
Expand All @@ -96,6 +100,8 @@ func run() error {
}
defer proxy.Close()

fmt.Printf("listening on %s\n", config.Client.Bind)

// Wait for signal.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
Expand Down
3 changes: 3 additions & 0 deletions cmd/marionette-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/redjack/marionette"
"github.com/redjack/marionette/mar"
_ "github.com/redjack/marionette/plugins"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -88,6 +89,8 @@ func run() error {
}
defer proxy.Close()

fmt.Printf("listening on %s, proxying to %s\n", ln.Addr().String(), config.Server.Proxy)

// Wait for signal.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
Expand Down
4 changes: 2 additions & 2 deletions fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func (fsm *fsm) ensureClientConn(ctx context.Context) error {
}

fsm.conn = NewBufferedConn(conn, MaxCellLength)
fsm.closeFuncs = append(fsm.closeFuncs, conn.Close)
// fsm.closeFuncs = append(fsm.closeFuncs, conn.Close)

return nil
}
Expand All @@ -406,7 +406,7 @@ func (fsm *fsm) ensureServerConn(ctx context.Context) error {
}

fsm.conn = NewBufferedConn(conn, MaxCellLength)
fsm.closeFuncs = append(fsm.closeFuncs, conn.Close)
// fsm.closeFuncs = append(fsm.closeFuncs, conn.Close)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion mar/formats/20150701/ftp_pasv_transfer.mar
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ connection(tcp, ftp_pasv_port):
ftp_pasv_transfer end NULL 1

action do_ftp_pasv_transfer:
server fte.send("ID3.*", 512)
server fte.send_async("ID3.*", 512)
4 changes: 2 additions & 2 deletions mar/formats/20150701/ftp_simple_blocking.mar
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ action do_pasv_mode_ok:
server tg.send("ftp_entering_passive")

action do_ftp_get_file_request:
client io.puts("get MyFile.mp3\n")
client fte.send_async("get ([a-zA-Z0-9]*).mp3\n", 128)

action do_ftp_get_file_response_started:
server io.puts("150 Data connection starting for MyFile.mp3.\n")
server io.puts("150 Opening data channel for file transfer\n")

action do_ftp_pasv_transfer:
server model.spawn("ftp_pasv_transfer", 1)
Expand Down
20 changes: 10 additions & 10 deletions mar/mar.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plugins/model/spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ func Spawn(fsm marionette.FSM, args ...interface{}) error {

// Execute a sub-FSM multiple times.
for i := 0; i < n; i++ {
logger.Debug("spawning", zap.Int("i", i))
logger.Debug("spawn begin", zap.Int("i", i))
child := fsm.Clone(doc)
if err := child.Execute(context.TODO()); err != nil {
logger.Error("child execution failed", zap.Error(err))
child.Reset()
return err
}
child.Reset()
logger.Debug("spawn end", zap.Int("i", i))
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions plugins/tg/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Send(fsm marionette.FSM, args ...interface{}) error {
ciphertext = strings.Replace(ciphertext, "%%SERVER_LISTEN_IP%%", fsm.Host(), -1)
for _, cipher := range grammar.Ciphers {
var err error
if ciphertext, err = encryptTo(fsm, cipher, ciphertext); err != nil {
if ciphertext, err = encryptTo(fsm, cipher, ciphertext, logger); err != nil {
logger.Error("cannot encrypt", zap.String("key", cipher.Key()), zap.Error(err))
return fmt.Errorf("cannot encrypt: %q", err)
}
Expand All @@ -58,15 +58,15 @@ func Send(fsm marionette.FSM, args ...interface{}) error {
return nil
}

func encryptTo(fsm marionette.FSM, cipher Cipher, template string) (_ string, err error) {
func encryptTo(fsm marionette.FSM, cipher Cipher, template string, logger *zap.Logger) (_ string, err error) {
// Encode data from streams if there is capacity in the handler.
var data []byte
if capacity, err := cipher.Capacity(); err != nil {
return "", err
} else if capacity > 0 {
cell := fsm.StreamSet().Dequeue(capacity)
if cell == nil {
cell = marionette.NewCell(0, 0, 0, marionette.NORMAL)
cell = marionette.NewCell(0, 0, capacity, marionette.NORMAL)
}

// Assign ids and marshal to bytes.
Expand Down

0 comments on commit 052af7d

Please sign in to comment.