63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package application
|
|
|
|
import (
|
|
"crypto/rsa"
|
|
"crypto/sha512"
|
|
"encoding/base64"
|
|
"github.com/yawning/bulb"
|
|
"golang.org/x/crypto/ed25519"
|
|
"net"
|
|
)
|
|
|
|
// "127.0.0.1:9051" "tcp4"
|
|
// "/var/run/tor/control" "unix"
|
|
func SetupOnion(torControlAddress string, torControlSocketType string, authentication string, pk *rsa.PrivateKey, onionport uint16) (net.Listener, error) {
|
|
c, err := bulb.Dial(torControlSocketType, torControlAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := c.Authenticate(authentication); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cfg := &bulb.NewOnionConfig{
|
|
DiscardPK: true,
|
|
PrivateKey: pk,
|
|
}
|
|
|
|
return c.NewListener(cfg, onionport)
|
|
}
|
|
|
|
func SetupOnionV3(torControlAddress string, torControlSocketType string, authentication string, pk ed25519.PrivateKey, onionport uint16) (net.Listener, error) {
|
|
c, err := bulb.Dial(torControlSocketType, torControlAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := c.Authenticate(authentication); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
digest := sha512.Sum512(pk[:32])
|
|
digest[0] &= 248
|
|
digest[31] &= 127
|
|
digest[31] |= 64
|
|
|
|
var privkey [64]byte
|
|
copy(privkey[0:32], digest[:32])
|
|
copy(privkey[32:64], digest[32:])
|
|
|
|
onionPK := &bulb.OnionPrivateKey{
|
|
KeyType: "ED25519-V3",
|
|
Key: base64.StdEncoding.EncodeToString(privkey[0:64]),
|
|
}
|
|
|
|
cfg := &bulb.NewOnionConfig{
|
|
DiscardPK: true,
|
|
PrivateKey: onionPK,
|
|
}
|
|
|
|
return c.NewListener(cfg, onionport)
|
|
}
|