From 40ad754b47fe9bd8f59e040f64788f5b1d8ecf7c Mon Sep 17 00:00:00 2001 From: idk Date: Thu, 28 Apr 2022 11:23:54 -0400 Subject: [PATCH] Add support for AUTH commands --- auth.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ auth/main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ client.go | 5 ++++- options.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 auth.go create mode 100644 auth/main.go diff --git a/auth.go b/auth.go new file mode 100644 index 0000000..a74a23f --- /dev/null +++ b/auth.go @@ -0,0 +1,47 @@ +package goSam + +import "fmt" + +// SetupAuth sends the AUTH ENABLE command and immediately sets up a new Username and +// Password from the arguments +func (c *Client) SetupAuth(user, password string) error { + r, err := c.sendCmd("AUTH ENABLE\n") + if err != nil { + return err + } + if r.Topic != "AUTH" { + return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r) + } + r, err = c.sendCmd("AUTH %s %s\n", user, password) + if err != nil { + return err + } + if r.Topic != "AUTH" { + return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r) + } + return nil +} + +// TeardownAuth sends the AUTH DISABLE command but does not remove the Username and +// Password from the client PasswordManager +func (c *Client) TeardownAuth() error { + r, err := c.sendCmd("AUTH DISABLE\n") + if err != nil { + return err + } + if r.Topic != "AUTH" { + return fmt.Errorf("TeardownAuth Unknown Reply: %+v\n", r) + } + return nil +} + +func (c *Client) RemoveAuthUser(user string) error { + r, err := c.sendCmd("AUTH REMOVE %s\n", user) + if err != nil { + return err + } + if r.Topic != "AUTH" { + return fmt.Errorf("RemoveAuthUser Unknown Reply: %+v\n", r) + } + return nil +} diff --git a/auth/main.go b/auth/main.go new file mode 100644 index 0000000..95b2d2d --- /dev/null +++ b/auth/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + + "github.com/eyedeekay/goSam" +) + +/** +THIS is a freestanding test for SAMv3.2 AUTH commands using goSam. It's +intended to be run separate from the other tests so that you don't accidentally end +up setting SAM session passwords and leaving them in the PasswordManager if a test +fails for some reason before you can remove them. +**/ + +func main() { + client, err := goSam.NewDefaultClient() + if err != nil { + panic(err) + } + err = client.SetupAuth("user", "password") + if err != nil { + panic(err) + } + client2, err := goSam.NewDefaultClient() + if err != nil { + panic(err) + } + conn, err := client2.Dial("", "idk.i2p") + if err != nil { + fmt.Println(err) + } + conn.Close() + err = client.RemoveAuthUser("user") + if err != nil { + panic(err) + } + //fmt.Println(r) + err = client.TeardownAuth() + if err != nil { + panic(err) + } + //r, err = client.NewDestination() +} diff --git a/client.go b/client.go index 4294c27..fb49404 100644 --- a/client.go +++ b/client.go @@ -24,6 +24,8 @@ type Client struct { port string fromport string toport string + user string + pass string SamConn net.Conn // Control socket SamDGConn DatagramConn // Datagram socket @@ -211,7 +213,8 @@ func (c *Client) samaddr() string { // send the initial handshake command and check that the reply is ok func (c *Client) hello() error { - r, err := c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d\n", c.sammin, c.sammax) + + r, err := c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d %s %s\n", c.sammin, c.sammax, c.getUser(), c.getPass()) if err != nil { return err } diff --git a/options.go b/options.go index 4558659..c1ad4ea 100644 --- a/options.go +++ b/options.go @@ -62,6 +62,22 @@ func SetHost(s string) func(*Client) error { } } +//SetUser sets the username for authentication during the SAM HELLO phase +func SetUser(s string) func(*Client) error { + return func(c *Client) error { + c.user = s + return nil + } +} + +//SetUser sets the password for authentication during the SAM HELLO phase +func SetPass(s string) func(*Client) error { + return func(c *Client) error { + c.pass = s + return nil + } +} + func SetSAMMinVersion(i int) func(*Client) error { return func(c *Client) error { if i < 0 { @@ -555,3 +571,17 @@ func (c *Client) Print() string { c.closeidletime() + c.compresion() } + +func (c *Client) getUser() string { + if c.user == "" { + return "" + } + return fmt.Sprintf("USER=%s", c.user) +} + +func (c *Client) getPass() string { + if c.pass == "" { + return "" + } + return fmt.Sprintf("PASSWORD=%s", c.pass) +}