Debian Codegraph @

control/cmd_protocolinfo.go

raw symbols SCIP
1 package control
2
3 import (
4 "strings"
5
6 "github.com/cretz/bine/torutil"
7 )
8
9 // ProtocolInfo is the protocol info result of Conn.ProtocolInfo.
10 type ProtocolInfo struct {
11 AuthMethods []string
12 CookieFile string
13 TorVersion string
14 RawResponse *Response
15 }
16
17 // HasAuthMethod checks if ProtocolInfo contains the requested auth method.
18 func (p *ProtocolInfo) HasAuthMethod(authMethod string) bool {
19 for _, m := range p.AuthMethods {
20 if m == authMethod {
21 return true
22 }
23 }
24 return false
25 }
26
27 // ProtocolInfo invokes PROTOCOLINFO on first invocation and returns a cached
28 // result on all others.
29 func (c *Conn) ProtocolInfo() (*ProtocolInfo, error) {
30 var err error
31 if c.protocolInfo == nil {
32 c.protocolInfo, err = c.sendProtocolInfo()
33 }
34 return c.protocolInfo, err
35 }
36
37 func (c *Conn) sendProtocolInfo() (*ProtocolInfo, error) {
38 resp, err := c.SendRequest("PROTOCOLINFO")
39 if err != nil {
40 return nil, err
41 }
42 // Check data vals
43 ret := &ProtocolInfo{RawResponse: resp}
44 for _, piece := range resp.Data {
45 key, val, ok := torutil.PartitionString(piece, ' ')
46 if !ok {
47 continue
48 }
49 switch key {
50 case "PROTOCOLINFO":
51 if val != "1" {
52 return nil, c.protoErr("Invalid PIVERSION: %v", val)
53 }
54 case "AUTH":
55 methods, cookieFile, _ := torutil.PartitionString(val, ' ')
56 if !strings.HasPrefix(methods, "METHODS=") {
57 continue
58 }
59 if cookieFile != "" {
60 if !strings.HasPrefix(cookieFile, "COOKIEFILE=") {
61 continue
62 }
63 if ret.CookieFile, err = torutil.UnescapeSimpleQuotedString(cookieFile[11:]); err != nil {
64 continue
65 }
66 }
67 ret.AuthMethods = strings.Split(methods[8:], ",")
68 case "VERSION":
69 torVersion, _, _ := torutil.PartitionString(val, ' ')
70 if strings.HasPrefix(torVersion, "Tor=") {
71 ret.TorVersion, err = torutil.UnescapeSimpleQuotedString(torVersion[4:])
72 }
73 }
74 }
75 return ret, nil
76 }