tordam

A library for peer discovery inside the Tor network
git clone https://git.parazyd.org/tordam
Log | Files | Refs | README | LICENSE

tor.go (2205B)


      1 // Copyright (c) 2017-2021 Ivan Jelincic <parazyd@dyne.org>
      2 //
      3 // This file is part of tordam
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program. If not, see <https://www.gnu.org/licenses/>.
     17 
     18 package tordam
     19 
     20 import (
     21 	"fmt"
     22 	"net"
     23 	"os"
     24 	"os/exec"
     25 	"strings"
     26 )
     27 
     28 // newtorrc returns a torrc string that is fed as standard input to the Tor
     29 // binary for its configuration.
     30 func newtorrc(listener, torlistener *net.TCPAddr, portmap []string) string {
     31 	var pm []string
     32 
     33 	for _, i := range pm {
     34 		p := strings.Split(i, ":")
     35 		pm = append(pm, fmt.Sprintf("HiddenServicePort %s %s",
     36 			p[0], strings.Join([]string{"127.0.0.1", p[1]}, ":")))
     37 	}
     38 
     39 	return fmt.Sprintf(`
     40 Log warn syslog
     41 RunAsDaemon 0
     42 DataDirectory tor
     43 SocksPort %s
     44 HiddenServiceDir hs
     45 HiddenServicePort %d %s
     46 %s
     47 `, torlistener.String(),
     48 		listener.Port, listener.String(), strings.Join(pm, "\n"))
     49 }
     50 
     51 // SpawnTor runs the system's Tor binary with the torrc created by newtorrc.
     52 // It takes listener (which is the local JSON-RPC server net.TCPAddr),
     53 // portmap (to map HiddenServicePort entries) and datadir (to store Tor files)
     54 // as parameters. Returns exec.Cmd pointer and/or error.
     55 func SpawnTor(listener *net.TCPAddr, portmap []string, datadir string) (*exec.Cmd, error) {
     56 	var err error
     57 
     58 	if err = ValidatePortmap(portmap); err != nil {
     59 		return nil, err
     60 	}
     61 
     62 	Cfg.TorAddr, err = GetAvailableListener()
     63 	if err != nil {
     64 		return nil, err
     65 	}
     66 
     67 	if err := os.MkdirAll(datadir, 0700); err != nil {
     68 		return nil, err
     69 	}
     70 
     71 	cmd := exec.Command("tor", "-f", "-")
     72 	cmd.Stdin = strings.NewReader(newtorrc(listener, Cfg.TorAddr, portmap))
     73 	cmd.Dir = datadir
     74 	return cmd, cmd.Start()
     75 }