| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "github.com/cretz/bine/torutil" |
| 7 | ) |
| 8 | |
| 9 | // Signal invokes SIGNAL. |
| 10 | func (c *Conn) Signal(signal string) error { |
| 11 | return c.sendRequestIgnoreResponse("SIGNAL %v", signal) |
| 12 | } |
| 13 | |
| 14 | // Quit invokes QUIT. |
| 15 | func (c *Conn) Quit() error { |
| 16 | return c.sendRequestIgnoreResponse("QUIT") |
| 17 | } |
| 18 | |
| 19 | // MapAddresses invokes MAPADDRESS and returns mapped addresses. |
| 20 | func (c *Conn) MapAddresses(addresses ...*KeyVal) ([]*KeyVal, error) { |
| 21 | cmd := "MAPADDRESS" |
| 22 | for _, address := range addresses { |
| 23 | cmd += " " + address.Key + "=" + address.Val |
| 24 | } |
| 25 | resp, err := c.SendRequest(cmd) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | data := resp.DataWithReply() |
| 30 | ret := make([]*KeyVal, 0, len(data)) |
| 31 | for _, address := range data { |
| 32 | mappedAddress := &KeyVal{} |
| 33 | mappedAddress.Key, mappedAddress.Val, _ = torutil.PartitionString(address, '=') |
| 34 | ret = append(ret, mappedAddress) |
| 35 | } |
| 36 | return ret, nil |
| 37 | } |
| 38 | |
| 39 | // GetInfo invokes GETINTO and returns values for requested keys. |
| 40 | func (c *Conn) GetInfo(keys ...string) ([]*KeyVal, error) { |
| 41 | resp, err := c.SendRequest("GETINFO %v", strings.Join(keys, " ")) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | ret := make([]*KeyVal, 0, len(resp.Data)) |
| 46 | for _, val := range resp.Data { |
| 47 | infoVal := &KeyVal{} |
| 48 | infoVal.Key, infoVal.Val, _ = torutil.PartitionString(val, '=') |
| 49 | if infoVal.Val, err = torutil.UnescapeSimpleQuotedStringIfNeeded(infoVal.Val); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | ret = append(ret, infoVal) |
| 53 | } |
| 54 | return ret, nil |
| 55 | } |
| 56 | |
| 57 | // PostDescriptor invokes POSTDESCRIPTOR. |
| 58 | func (c *Conn) PostDescriptor(descriptor string, purpose string, cache string) error { |
| 59 | cmd := "+POSTDESCRIPTOR" |
| 60 | if purpose != "" { |
| 61 | cmd += " purpose=" + purpose |
| 62 | } |
| 63 | if cache != "" { |
| 64 | cmd += " cache=" + cache |
| 65 | } |
| 66 | cmd += "\r\n" + descriptor + "\r\n." |
| 67 | return c.sendRequestIgnoreResponse(cmd) |
| 68 | } |
| 69 | |
| 70 | // UseFeatures invokes USEFEATURE. |
| 71 | func (c *Conn) UseFeatures(features ...string) error { |
| 72 | return c.sendRequestIgnoreResponse("USEFEATURE " + strings.Join(features, " ")) |
| 73 | } |
| 74 | |
| 75 | // ResolveAsync invokes RESOLVE. |
| 76 | func (c *Conn) ResolveAsync(address string, reverse bool) error { |
| 77 | cmd := "RESOLVE " |
| 78 | if reverse { |
| 79 | cmd += "mode=reverse " |
| 80 | } |
| 81 | return c.sendRequestIgnoreResponse(cmd + address) |
| 82 | } |
| 83 | |
| 84 | // TakeOwnership invokes TAKEOWNERSHIP. |
| 85 | func (c *Conn) TakeOwnership() error { |
| 86 | return c.sendRequestIgnoreResponse("TAKEOWNERSHIP") |
| 87 | } |
| 88 | |
| 89 | // DropGuards invokes DROPGUARDS. |
| 90 | func (c *Conn) DropGuards() error { |
| 91 | return c.sendRequestIgnoreResponse("DROPGUARDS") |
| 92 | } |