diff options
author | kali kaneko (leap communications) <kali@leap.se> | 2020-01-24 22:34:09 -0600 |
---|---|---|
committer | kali kaneko (leap communications) <kali@leap.se> | 2020-01-24 22:35:54 -0600 |
commit | 1c9220e04016d035c3c688c315ceabe274f45dfc (patch) | |
tree | 796488fe592e117fd97781a018bc4b2060a0672d /pkg/auth/sip2/client.go | |
parent | d3b21e5adc27cbb472e688b7c602e3bd721dec31 (diff) |
initial sip implementation
Diffstat (limited to 'pkg/auth/sip2/client.go')
-rw-r--r-- | pkg/auth/sip2/client.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/pkg/auth/sip2/client.go b/pkg/auth/sip2/client.go new file mode 100644 index 0000000..fbdeded --- /dev/null +++ b/pkg/auth/sip2/client.go @@ -0,0 +1,74 @@ +package sip2 + +import ( + "fmt" + "github.com/reiver/go-telnet" + "log" + "time" +) + +const loginRequestTemplate string = "9300CN%s|CO%s|CP%s|" +const statusRequestTemplate string = "23000%s %sAO%s|AA%s|AD%s|" + +type Client struct { + Host string + Port string + location string + conn *telnet.Conn + parser *Parser +} + +func NewClient(host, port, location string) Client { + c := Client{host, port, location, nil, nil} + c.parser = getParser() + return c +} + +func (c *Client) Connect() (bool, error) { + conn, err := telnet.DialTo(c.Host + ":" + c.Port) + if nil != err { + log.Println(log.Printf("error: %v", err)) + return false, err + } + c.conn = conn + return true, nil +} + +func (c *Client) Login(user, pass string) bool { + loginStr := fmt.Sprintf(loginRequestTemplate, user, pass, c.location) + if nil == c.conn { + fmt.Println("error! null connection") + } + telnetSend(c.conn, loginStr) + loginResp := telnetRead(c.conn) + msg := c.parseResponse(loginResp) + if value, ok := c.parser.getFixedFieldValue(msg, Ok); ok && value == TRUE { + return true + } + return false +} + +func (c *Client) CheckCredentials(user, passwd string) bool { + currentTime := time.Now() + statusRequest := fmt.Sprintf( + statusRequestTemplate, + currentTime.Format("20060102"), + currentTime.Format("150102"), + c.location, user, passwd) + telnetSend(c.conn, statusRequest) + + statusMsg := c.parseResponse(telnetRead(c.conn)) + if value, ok := c.parser.getFieldValue(statusMsg, ValidPatron); ok && value == YES { + if value, ok := c.parser.getFieldValue(statusMsg, ValidPatronPassword); ok && value == YES { + return true + } + } + + // TODO log whatever error we can find (AF, Screen Message, for instance) + return false +} + +func (c *Client) parseResponse(txt string) *Message { + msg := c.parser.parseMessage(txt) + return msg +} |