Debian Codegraph @

control/cmd_onion.go

raw symbols SCIP
1 package controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/
2
3 import (
4 "crypto/rsascip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/"
5 "crypto/x509scip-go gomod github.com/golang/go/src go1.15 `crypto/x509`/"
6 "encoding/base64scip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/"
7 "fmtscip-go gomod github.com/golang/go/src go1.15 fmt/"
8 "strconvscip-go gomod github.com/golang/go/src go1.15 strconv/"
9 "stringsscip-go gomod github.com/golang/go/src go1.15 strings/"
10
11 "github.com/cretz/bine/torutilscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/"
12 "github.com/cretz/bine/torutil/ed25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/"
13 )
14
15 // KeyType is a key type for Key in AddOnion.
16 type KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType# string
17
18 const (
19 // KeyTypeNew is NEW.
20 KeyTypeNewscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeNew. KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType# = "NEW"
21 // KeyTypeRSA1024 is RSA1024.
22 KeyTypeRSA1024scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeRSA1024. KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType# = "RSA1024"
23 // KeyTypeED25519V3 is ED25519-V3.
24 KeyTypeED25519V3scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeED25519V3. KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType# = "ED25519-V3"
25 )
26
27 // KeyAlgo is a key algorithm for GenKey on AddOnion.
28 type KeyAlgoscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgo# string
29
30 const (
31 // KeyAlgoBest is BEST.
32 KeyAlgoBestscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgoBest. KeyAlgoscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgo# = "BEST"
33 // KeyAlgoRSA1024 is RSA1024.
34 KeyAlgoRSA1024scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgoRSA1024. KeyAlgoscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgo# = "RSA1024"
35 // KeyAlgoED25519V3 is ED25519-V3.
36 KeyAlgoED25519V3scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgoED25519V3. KeyAlgoscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgo# = "ED25519-V3"
37 )
38
39 // Key is a type of key to use for AddOnion. Implementations include GenKey,
40 // RSAKey, and ED25519Key.
41 type Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Key# interface {
42 // Type is the KeyType for AddOnion.
43 Typescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Key#Type.() KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType#
44 // Blob is the serialized key for AddOnion.
45 Blobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Key#Blob.() string
46 }
47
48 // KeyFromString creates a Key for AddOnion based on a response string.
49 func KeyFromStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyFromString().(str string) (Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Key#, error) {
50 typ, blob, _ := torutilscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/.PartitionStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PartitionString().(str, ':')
51 switch KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType#(typ) {
52 case KeyTypeNewscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeNew.:
53 return GenKeyFromBlobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKeyFromBlob().(blob), nil
54 case KeyTypeRSA1024scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeRSA1024.:
55 return RSA1024KeyFromBlobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSA1024KeyFromBlob().(blob)
56 case KeyTypeED25519V3scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeED25519V3.:
57 return ED25519KeyFromBlobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519KeyFromBlob().(blob)
58 default:
59 return nil, fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unrecognized key type: %v", typ)
60 }
61 }
62
63 // GenKey is a Key for AddOnion that asks Tor to generate a key for the given
64 // algorithm.
65 type GenKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKey# KeyAlgoscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgo#
66
67 // GenKeyFromBlob creates a GenKey for the given response blob which is a
68 // KeyAlgo.
69 func GenKeyFromBlobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKeyFromBlob().(blob string) GenKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKey# { return GenKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKey#(KeyAlgoscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyAlgo#(blob)) }
70
71 // Type implements Key.Type.
72 func (GenKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKey#) Typescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKey#Type().() KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType# { return KeyTypeNewscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeNew. }
73
74 // Blob implements Key.Blob.
75 func (g GenKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKey#) Blobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/GenKey#Blob().() string { return string(g) }
76
77 // RSAKey is a Key for AddOnion that is a RSA-1024 key (i.e. v2).
78 type RSAKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey# struct{ *rsascip-go gomod github.com/golang/go/src go1.15 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey#PrivateKey. }
79
80 // RSA1024KeyFromBlob creates a RSAKey for the given response blob.
81 func RSA1024KeyFromBlobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSA1024KeyFromBlob().(blob string) (*RSAKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey#, error) {
82 byts, err := base64scip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/Encoding#DecodeString().(blob)
83 if err != nil {
84 return nil, err
85 }
86 rsaKey, err := x509scip-go gomod github.com/golang/go/src go1.15 `crypto/x509`/.ParsePKCS1PrivateKeyscip-go gomod github.com/golang/go/src go1.15 `crypto/x509`/ParsePKCS1PrivateKey().(byts)
87 if err != nil {
88 return nil, err
89 }
90 return &RSAKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey#{rsaKey}, nil
91 }
92
93 // Type implements Key.Type.
94 func (*RSAKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey#) Typescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey#Type().() KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType# { return KeyTypeRSA1024scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeRSA1024. }
95
96 // Blob implements Key.Blob.
97 func (r *RSAKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey#) Blobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey#Blob().() string {
98 return base64scip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/Encoding#EncodeToString().(x509scip-go gomod github.com/golang/go/src go1.15 `crypto/x509`/.MarshalPKCS1PrivateKeyscip-go gomod github.com/golang/go/src go1.15 `crypto/x509`/MarshalPKCS1PrivateKey().(r.PrivateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/RSAKey#PrivateKey.))
99 }
100
101 // ED25519Key is a Key for AddOnion that is a ed25519 key (i.e. v3).
102 type ED25519Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519Key# struct{ ed25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/.KeyPairscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519Key#KeyPair. }
103
104 // ED25519KeyFromBlob creates a ED25519Key for the given response blob.
105 func ED25519KeyFromBlobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519KeyFromBlob().(blob string) (*ED25519Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519Key#, error) {
106 byts, err := base64scip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/Encoding#DecodeString().(blob)
107 if err != nil {
108 return nil, err
109 }
110 return &ED25519Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519Key#{ed25519scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/.PrivateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/PrivateKey#(byts).KeyPairscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/PrivateKey#KeyPair().()}, nil
111 }
112
113 // Type implements Key.Type.
114 func (*ED25519Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519Key#) Typescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519Key#Type().() KeyTypescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyType# { return KeyTypeED25519V3scip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyTypeED25519V3. }
115
116 // Blob implements Key.Blob.
117 func (e *ED25519Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519Key#) Blobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ED25519Key#Blob().() string { return base64scip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.15 `encoding/base64`/Encoding#EncodeToString().(e.PrivateKeyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil/ed25519`/KeyPair#PrivateKey.()) }
118
119 // AddOnionRequest is a set of request params for AddOnion.
120 type AddOnionRequestscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest# struct {
121 // Key is the key to use or GenKey if Tor should generate it.
122 Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Key. Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Key#
123 // Flags are ADD_ONION flags.
124 Flagsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Flags. []string
125 // MaxStreams is ADD_ONION MaxStreams.
126 MaxStreamsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#MaxStreams. int
127 // Ports are ADD_ONION Port values. Key is virtual port, Val is target
128 // port (or can be empty to use virtual port).
129 Portsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Ports. []*KeyValscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVal#
130 // ClientAuths are ADD_ONION ClientAuth values. If value is empty string,
131 // Tor will generate the password.
132 ClientAuthsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#ClientAuths. map[string]string
133 }
134
135 // AddOnionResponse is the response for AddOnion.
136 type AddOnionResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse# struct {
137 // ServiceID is the ADD_ONION response ServiceID value.
138 ServiceIDscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#ServiceID. string
139 // Key is the ADD_ONION response PrivateKey value.
140 Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#Key. Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Key#
141 // ClientAuths are the ADD_ONION response ClientAuth values.
142 ClientAuthsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#ClientAuths. map[string]string
143 // RawResponse is the raw ADD_ONION response.
144 RawResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#RawResponse. *Responsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#
145 }
146
147 // AddOnion invokes ADD_ONION and returns its response.
148 func (c *Connscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#) AddOnionscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#AddOnion().(req *AddOnionRequestscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#) (*AddOnionResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#, error) {
149 // Build command
150 if req.Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Key. == nil {
151 return nil, c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Key required")
152 }
153 cmd := "ADD_ONION " + string(req.Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Key..Typescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Key#Type.()) + ":" + req.Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Key..Blobscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Key#Blob.()
154 if len(req.Flagsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Flags.) > 0 {
155 cmd += " Flags=" + stringsscip-go gomod github.com/golang/go/src go1.15 strings/.Joinscip-go gomod github.com/golang/go/src go1.15 strings/Join().(req.Flagsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Flags., ",")
156 }
157 if req.MaxStreamsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#MaxStreams. > 0 {
158 cmd += " MaxStreams=" + strconvscip-go gomod github.com/golang/go/src go1.15 strconv/.Itoascip-go gomod github.com/golang/go/src go1.15 strconv/Itoa().(req.MaxStreamsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#MaxStreams.)
159 }
160 for _, port := range req.Portsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#Ports. {
161 cmd += " Port=" + port.Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVal#Key.
162 if port.Valscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVal#Val. != "" {
163 cmd += "," + port.Valscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVal#Val.
164 }
165 }
166 for name, blob := range req.ClientAuthsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionRequest#ClientAuths. {
167 cmd += " ClientAuth=" + name
168 if blob != "" {
169 cmd += ":" + blob
170 }
171 }
172 // Invoke and read response
173 resp, err := c.SendRequestscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#SendRequest().(cmd)
174 if err != nil {
175 return nil, err
176 }
177 ret := &AddOnionResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#{RawResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#RawResponse.: resp}
178 for _, data := range resp.Datascip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Data. {
179 key, val, _ := torutilscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/.PartitionStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PartitionString().(data, '=')
180 switch key {
181 case "ServiceID":
182 ret.ServiceIDscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#ServiceID. = val
183 case "PrivateKey":
184 if ret.Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#Key., err = KeyFromStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyFromString().(val); err != nil {
185 return nil, err
186 }
187 case "ClientAuth":
188 name, pass, _ := torutilscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/.PartitionStringscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/torutil`/PartitionString().(val, ':')
189 if ret.ClientAuthsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#ClientAuths. == nil {
190 ret.ClientAuthsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#ClientAuths. = map[string]string{}
191 }
192 ret.ClientAuthsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/AddOnionResponse#ClientAuths.[name] = pass
193 }
194 }
195 return ret, nil
196 }
197
198 // DelOnion invokes DELONION.
199 func (c *Connscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#) DelOnionscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#DelOnion().(serviceID string) error {
200 return c.sendRequestIgnoreResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#sendRequestIgnoreResponse().("DEL_ONION %v", serviceID)
201 }