bine/control/cmd_protocolinfo.go

65 lines
1.4 KiB
Go
Raw Normal View History

2018-05-10 18:11:18 +00:00
package control
import (
"strings"
2018-05-10 21:29:16 +00:00
"github.com/cretz/bine/util"
2018-05-10 18:11:18 +00:00
)
type ProtocolInfo struct {
AuthMethods []string
CookieFile string
TorVersion string
RawResponse *Response
}
func (p *ProtocolInfo) HasAuthMethod(authMethod string) bool {
for _, m := range p.AuthMethods {
if m == authMethod {
return true
}
}
return false
}
func (c *Conn) RequestProtocolInfo() (*ProtocolInfo, error) {
resp, err := c.SendRequest("PROTOCOLINFO")
if err != nil {
return nil, err
}
2018-05-10 21:29:16 +00:00
// Check data vals
2018-05-10 18:11:18 +00:00
ret := &ProtocolInfo{RawResponse: resp}
for _, piece := range resp.Data {
2018-05-10 21:29:16 +00:00
key, val, ok := util.PartitionString(piece, ' ')
2018-05-10 18:11:18 +00:00
if !ok {
continue
}
switch key {
2018-05-10 21:29:16 +00:00
case "PROTOCOLINFO":
if val != "1" {
return nil, newProtocolError("Invalid PIVERSION: %v", val)
}
2018-05-10 18:11:18 +00:00
case "AUTH":
2018-05-10 21:29:16 +00:00
methods, cookieFile, _ := util.PartitionString(val, ' ')
2018-05-10 18:11:18 +00:00
if !strings.HasPrefix(methods, "METHODS=") {
continue
}
if cookieFile != "" {
if !strings.HasPrefix(cookieFile, "COOKIEFILE=") {
continue
}
2018-05-10 21:29:16 +00:00
if ret.CookieFile, err = util.ParseSimpleQuotedString(cookieFile[11:]); err != nil {
2018-05-10 18:11:18 +00:00
continue
}
}
ret.AuthMethods = strings.Split(methods[8:], ",")
case "VERSION":
2018-05-10 21:29:16 +00:00
torVersion, _, _ := util.PartitionString(val, ' ')
2018-05-10 18:11:18 +00:00
if strings.HasPrefix(torVersion, "Tor=") {
2018-05-10 21:29:16 +00:00
ret.TorVersion, err = util.ParseSimpleQuotedString(torVersion[4:])
2018-05-10 18:11:18 +00:00
}
}
}
return ret, nil
}