summaryrefslogtreecommitdiff
path: root/vendor/github.com/cretz/bine/control/cmd_protocolinfo.go
blob: 346b36d746d3d4bcf4cf7f2d70bea1450ccb7305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package control

import (
	"strings"

	"github.com/cretz/bine/torutil"
)

// ProtocolInfo is the protocol info result of Conn.ProtocolInfo.
type ProtocolInfo struct {
	AuthMethods []string
	CookieFile  string
	TorVersion  string
	RawResponse *Response
}

// HasAuthMethod checks if ProtocolInfo contains the requested auth method.
func (p *ProtocolInfo) HasAuthMethod(authMethod string) bool {
	for _, m := range p.AuthMethods {
		if m == authMethod {
			return true
		}
	}
	return false
}

// ProtocolInfo invokes PROTOCOLINFO on first invocation and returns a cached
// result on all others.
func (c *Conn) ProtocolInfo() (*ProtocolInfo, error) {
	var err error
	if c.protocolInfo == nil {
		c.protocolInfo, err = c.sendProtocolInfo()
	}
	return c.protocolInfo, err
}

func (c *Conn) sendProtocolInfo() (*ProtocolInfo, error) {
	resp, err := c.SendRequest("PROTOCOLINFO")
	if err != nil {
		return nil, err
	}
	// Check data vals
	ret := &ProtocolInfo{RawResponse: resp}
	for _, piece := range resp.Data {
		key, val, ok := torutil.PartitionString(piece, ' ')
		if !ok {
			continue
		}
		switch key {
		case "PROTOCOLINFO":
			if val != "1" {
				return nil, c.protoErr("Invalid PIVERSION: %v", val)
			}
		case "AUTH":
			methods, cookieFile, _ := torutil.PartitionString(val, ' ')
			if !strings.HasPrefix(methods, "METHODS=") {
				continue
			}
			if cookieFile != "" {
				if !strings.HasPrefix(cookieFile, "COOKIEFILE=") {
					continue
				}
				if ret.CookieFile, err = torutil.UnescapeSimpleQuotedString(cookieFile[11:]); err != nil {
					continue
				}
			}
			ret.AuthMethods = strings.Split(methods[8:], ",")
		case "VERSION":
			torVersion, _, _ := torutil.PartitionString(val, ' ')
			if strings.HasPrefix(torVersion, "Tor=") {
				ret.TorVersion, err = torutil.UnescapeSimpleQuotedString(torVersion[4:])
			}
		}
	}
	return ret, nil
}