door5/main.go
2023-11-19 07:53:24 -08:00

75 lines
1.7 KiB
Go

//go:build linux
package main
import (
"context"
"time"
"github.com/cretz/bine/process/embedded"
"github.com/cretz/bine/tor"
"github.com/davecgh/go-spew/spew"
"git.tcp.direct/kayos/door5/logger"
)
func MemoryMapOSFolder() (path string, err error) {
// create memory mapped folder for tor data directory (linux)
// un
return "", nil
}
func NewOnion() (*tor.Tor, error) {
log := logger.Get()
log.Logger.Info().Msg("starting and registering onion service...")
t, err := tor.Start(nil, &tor.StartConf{
ProcessCreator: embedded.NewCreator(),
UseEmbeddedControlConn: true,
RetainTempDataDir: false,
DebugWriter: logger.Get().With().Str("module", "tor").Logger(),
NoAutoSocksPort: false,
GeoIPFileReader: nil,
})
if err != nil {
log.Panicf("Unable to start Tor: %v", err)
}
defer func(t *tor.Tor) {
if err = t.Close(); err != nil {
log.Panicf("Unable to close Tor: %v", err)
}
}(t)
// Wait at most a few minutes to publish the service
listenCtx, listenCancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer listenCancel()
// Create a v3 onion service to listen on any port but show as 80
onion, err := t.Listen(listenCtx, &tor.ListenConf{RemotePorts: []int{80}})
if err != nil {
log.Panicf("Unable to create onion service: %v", err)
}
defer func(onion *tor.OnionService) {
if err = onion.Close(); err != nil {
log.Panicf("Unable to close onion service: %v", err)
}
}(onion)
log.Logger.Info().Msgf("live: %v.onion", onion.ID)
return t, nil
}
func main() {
o, e := NewOnion()
if e != nil {
panic(e)
}
pi, err := o.Control.ProtocolInfo()
if err != nil {
panic(err)
}
spew.Dump(pi)
}