2015-09-30 16:02:47 +00:00
|
|
|
// cmd_onion.go - various onion service commands: ADD_ONION, DEL_ONION...
|
|
|
|
//
|
|
|
|
// To the extent possible under law, David Stainton waived all copyright
|
|
|
|
// and related or neighboring rights to this module of bulb, using the creative
|
|
|
|
// commons "cc0" public domain dedication. See LICENSE or
|
|
|
|
// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
|
|
|
|
|
|
|
|
package bulb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OnionInfo is the result of the AddOnion command.
|
|
|
|
type OnionInfo struct {
|
2015-10-08 21:25:22 +00:00
|
|
|
OnionID string
|
2015-09-30 16:02:47 +00:00
|
|
|
KeyType string
|
|
|
|
Key string
|
|
|
|
|
|
|
|
RawResponse *Response
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddOnion issues an ADD_ONION command and returns the parsed response.
|
|
|
|
func (c *Conn) AddOnion(virtPort int, target, keyType, keyContent string, new bool) (*OnionInfo, error) {
|
|
|
|
var fields []string
|
|
|
|
request := "ADD_ONION "
|
|
|
|
onionInfo := OnionInfo{}
|
|
|
|
|
|
|
|
if new {
|
|
|
|
request += "NEW:BEST"
|
|
|
|
} else {
|
|
|
|
request += fmt.Sprintf("%s:%s", keyType, keyContent)
|
|
|
|
}
|
|
|
|
request += fmt.Sprintf(" Port=%d,%s\n", virtPort, target)
|
|
|
|
response, err := c.Request(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
onionInfo.RawResponse = response
|
|
|
|
fields = strings.Split(fmt.Sprintf("%s", response.Data), "ServiceID=")
|
|
|
|
fields = strings.Split(fields[1], " ")
|
2015-10-08 21:25:22 +00:00
|
|
|
onionInfo.OnionID = fields[0]
|
2015-09-30 16:02:47 +00:00
|
|
|
|
|
|
|
if new {
|
|
|
|
fields = strings.Split(fmt.Sprintf("%s", response.Data), "PrivateKey=")
|
|
|
|
fields = strings.Split(fields[1], ":")
|
|
|
|
onionInfo.KeyType = fields[0]
|
|
|
|
fields = strings.Split(fields[1], "\n")
|
|
|
|
onionInfo.Key = fields[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &onionInfo, nil
|
|
|
|
}
|
|
|
|
|
2015-10-08 21:25:22 +00:00
|
|
|
// DeleteOnion issues a DEL_ONION command and returns the parsed response.
|
|
|
|
func (c *Conn) DeleteOnion(serviceID string) error {
|
|
|
|
var deleteCmd string = fmt.Sprintf("DEL_ONION %s\n", serviceID)
|
2015-09-30 16:02:47 +00:00
|
|
|
_, err := c.Request(deleteCmd)
|
|
|
|
return err
|
|
|
|
}
|