| 1 | package torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.15 context/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.15 fmt/" |
| 6 | "ioscip-go gomod github.com/golang/go/src go1.15 io/" |
| 7 | "io/ioutilscip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/" |
| 8 | "net/textprotoscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/" |
| 9 | "osscip-go gomod github.com/golang/go/src go1.15 os/" |
| 10 | "path/filepathscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/" |
| 11 | "strconvscip-go gomod github.com/golang/go/src go1.15 strconv/" |
| 12 | "timescip-go gomod github.com/golang/go/src go1.15 time/" |
| 13 | |
| 14 | "github.com/cretz/bine/controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/" |
| 15 | |
| 16 | "github.com/cretz/bine/processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/" |
| 17 | ) |
| 18 | |
| 19 | // Tor is the wrapper around the Tor process and control port connection. It |
| 20 | // should be created with Start and developers should always call Close when |
| 21 | // done. |
| 22 | type Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor# struct { |
| 23 | // Process is the Tor instance that is running. |
| 24 | Processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Process. processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/.Processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/Process# |
| 25 | |
| 26 | // Control is the Tor controller connection. |
| 27 | Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control. *controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/.Connscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn# |
| 28 | |
| 29 | // ProcessCancelFunc is the context cancellation func for the Tor process. |
| 30 | // It is used by Close and should not be called directly. This can be nil. |
| 31 | ProcessCancelFuncscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ProcessCancelFunc. contextscip-go gomod github.com/golang/go/src go1.15 context/.CancelFuncscip-go gomod github.com/golang/go/src go1.15 context/CancelFunc# |
| 32 | |
| 33 | // ControlPort is the port that Control is connected on. It is 0 if the |
| 34 | // connection is an embedded control connection. |
| 35 | ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ControlPort. int |
| 36 | |
| 37 | // DataDir is the path to the data directory that Tor is using. |
| 38 | DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir. string |
| 39 | |
| 40 | // DeleteDataDirOnClose is true if, when Close is invoked, the entire |
| 41 | // directory will be deleted. |
| 42 | DeleteDataDirOnClosescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DeleteDataDirOnClose. bool |
| 43 | |
| 44 | // DebugWriter is the writer used for debug logs, or nil if debug logs |
| 45 | // should not be emitted. |
| 46 | DebugWriterscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DebugWriter. ioscip-go gomod github.com/golang/go/src go1.15 io/.Writerscip-go gomod github.com/golang/go/src go1.15 io/Writer# |
| 47 | |
| 48 | // StopProcessOnClose, if true, will attempt to halt the process on close. |
| 49 | StopProcessOnClosescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#StopProcessOnClose. bool |
| 50 | |
| 51 | // GeoIPCreatedFile is the path, relative to DataDir, that was created from |
| 52 | // StartConf.GeoIPFileReader. It is empty if no file was created. |
| 53 | GeoIPCreatedFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#GeoIPCreatedFile. string |
| 54 | |
| 55 | // GeoIPv6CreatedFile is the path, relative to DataDir, that was created |
| 56 | // from StartConf.GeoIPFileReader. It is empty if no file was created. |
| 57 | GeoIPv6CreatedFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#GeoIPv6CreatedFile. string |
| 58 | } |
| 59 | |
| 60 | // StartConf is the configuration used for Start when starting a Tor instance. A |
| 61 | // default instance with no fields set is the default used for Start. |
| 62 | type StartConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf# struct { |
| 63 | // ExePath is the path to the Tor executable. If it is not present, "tor" is |
| 64 | // used either locally or on the PATH. This is ignored if ProcessCreator is |
| 65 | // set. |
| 66 | ExePathscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ExePath. string |
| 67 | |
| 68 | // ProcessCreator is the override to use a specific process creator. If set, |
| 69 | // ExePath is ignored. |
| 70 | ProcessCreatorscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ProcessCreator. processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/.Creatorscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/Creator# |
| 71 | |
| 72 | // UseEmbeddedControlConn can be set to true to use |
| 73 | // process.Process.EmbeddedControlConn() instead of creating a connection |
| 74 | // via ControlPort. Note, this only works when ProcessCreator is an |
| 75 | // embedded Tor creator with version >= 0.3.5.x. |
| 76 | UseEmbeddedControlConnscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#UseEmbeddedControlConn. bool |
| 77 | |
| 78 | // ControlPort is the port to use for the Tor controller. If it is 0, Tor |
| 79 | // picks a port for use. This is ignored if UseEmbeddedControlConn is true. |
| 80 | ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ControlPort. int |
| 81 | |
| 82 | // DataDir is the directory used by Tor. If it is empty, a temporary |
| 83 | // directory is created in TempDataDirBase. |
| 84 | DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#DataDir. string |
| 85 | |
| 86 | // TempDataDirBase is the parent directory that a temporary data directory |
| 87 | // will be created under for use by Tor. This is ignored if DataDir is not |
| 88 | // empty. If empty it is assumed to be the current working directory. |
| 89 | TempDataDirBasescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#TempDataDirBase. string |
| 90 | |
| 91 | // RetainTempDataDir, if true, will not set the created temporary data |
| 92 | // directory to be deleted on close. This is ignored if DataDir is not |
| 93 | // empty. |
| 94 | RetainTempDataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#RetainTempDataDir. bool |
| 95 | |
| 96 | // DisableCookieAuth, if true, will not use the default SAFECOOKIE |
| 97 | // authentication mechanism for the Tor controller. |
| 98 | DisableCookieAuthscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#DisableCookieAuth. bool |
| 99 | |
| 100 | // DisableEagerAuth, if true, will not authenticate on Start. |
| 101 | DisableEagerAuthscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#DisableEagerAuth. bool |
| 102 | |
| 103 | // EnableNetwork, if true, will connect to the wider Tor network on start. |
| 104 | EnableNetworkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#EnableNetwork. bool |
| 105 | |
| 106 | // ExtraArgs is the set of extra args passed to the Tor instance when |
| 107 | // started. |
| 108 | ExtraArgsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ExtraArgs. []string |
| 109 | |
| 110 | // TorrcFile is the torrc file to set on start. If empty, a blank torrc is |
| 111 | // created in the data directory and is used instead. |
| 112 | TorrcFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#TorrcFile. string |
| 113 | |
| 114 | // DebugWriter is the writer to use for debug logs, or nil for no debug |
| 115 | // logs. |
| 116 | DebugWriterscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#DebugWriter. ioscip-go gomod github.com/golang/go/src go1.15 io/.Writerscip-go gomod github.com/golang/go/src go1.15 io/Writer# |
| 117 | |
| 118 | // NoHush if true does not set --hush. By default --hush is set. |
| 119 | NoHushscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#NoHush. bool |
| 120 | |
| 121 | // NoAutoSocksPort if true does not set "--SocksPort auto" as is done by |
| 122 | // default. This means the caller could set their own or just let it |
| 123 | // default to 9050. |
| 124 | NoAutoSocksPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#NoAutoSocksPort. bool |
| 125 | |
| 126 | // GeoIPReader, if present, is called before start to copy geo IP files to |
| 127 | // the data directory. Errors are propagated. If the ReadCloser is present, |
| 128 | // it is copied to the data dir, overwriting as necessary, and then closed |
| 129 | // and the appropriate command line argument is added to reference it. If |
| 130 | // both the ReadCloser and error are nil, no copy or command line argument |
| 131 | // is used for that version. This is called twice, once with false and once |
| 132 | // with true for ipv6. |
| 133 | // |
| 134 | // This can be set to torutil/geoipembed.GeoIPReader to use an embedded |
| 135 | // source. |
| 136 | GeoIPFileReaderscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#GeoIPFileReader. func(ipv6 bool) (ioscip-go gomod github.com/golang/go/src go1.15 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.15 io/ReadCloser#, error) |
| 137 | } |
| 138 | |
| 139 | // Start a Tor instance and connect to it. If ctx is nil, context.Background() |
| 140 | // is used. If conf is nil, a default instance is used. |
| 141 | func Startscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Start().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, conf *StartConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#) (*Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#, error) { |
| 142 | if ctx == nil { |
| 143 | ctx = contextscip-go gomod github.com/golang/go/src go1.15 context/.Backgroundscip-go gomod github.com/golang/go/src go1.15 context/Background().() |
| 144 | } |
| 145 | if conf == nil { |
| 146 | conf = &StartConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#{} |
| 147 | } |
| 148 | tor := &Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#{DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir.: conf.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#DataDir., DebugWriterscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DebugWriter.: conf.DebugWriterscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#DebugWriter., StopProcessOnClosescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#StopProcessOnClose.: true} |
| 149 | // Create the data dir and make it absolute |
| 150 | if tor.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir. == "" { |
| 151 | tempBase := conf.TempDataDirBasescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#TempDataDirBase. |
| 152 | if tempBase == "" { |
| 153 | tempBase = "." |
| 154 | } |
| 155 | var err error |
| 156 | if tempBase, err = filepathscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/Abs().(tempBase); err != nil { |
| 157 | return nil, err |
| 158 | } |
| 159 | if tor.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., err = ioutilscip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/.TempDirscip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/TempDir().(tempBase, "data-dir-"); err != nil { |
| 160 | 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().("Unable to create temp data dir: %v", err) |
| 161 | } |
| 162 | tor.Debugfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Debugf().("Created temp data directory at: %v", tor.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir.) |
| 163 | tor.DeleteDataDirOnClosescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DeleteDataDirOnClose. = !conf.RetainTempDataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#RetainTempDataDir. |
| 164 | } else if err := osscip-go gomod github.com/golang/go/src go1.15 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.15 os/MkdirAll().(tor.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., 0700); err != nil { |
| 165 | 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().("Unable to create data dir: %v", err) |
| 166 | } |
| 167 | |
| 168 | // !!!! From this point on, we must close tor if we error !!!! |
| 169 | |
| 170 | // Copy geoip stuff if necessary |
| 171 | err := tor.copyGeoIPFilesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#copyGeoIPFiles().(conf) |
| 172 | // Start tor |
| 173 | if err == nil { |
| 174 | err = tor.startProcessscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#startProcess().(ctx, conf) |
| 175 | } |
| 176 | // Connect the controller |
| 177 | if err == nil { |
| 178 | err = tor.connectControllerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#connectController().(ctx, conf) |
| 179 | } |
| 180 | // Attempt eager auth w/ no password |
| 181 | if err == nil && !conf.DisableEagerAuthscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#DisableEagerAuth. { |
| 182 | err = tor.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..Authenticatescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#Authenticate().("") |
| 183 | } |
| 184 | // If there was an error, we have to try to close here but it may leave the process open |
| 185 | if err != nil { |
| 186 | if closeErr := tor.Closescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Close().(); closeErr != nil { |
| 187 | err = fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Error on start: %v (also got error trying to close: %v)", err, closeErr) |
| 188 | } |
| 189 | } |
| 190 | return tor, err |
| 191 | } |
| 192 | |
| 193 | func (t *Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#) copyGeoIPFilesscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#copyGeoIPFiles().(conf *StartConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#) error { |
| 194 | if conf.GeoIPFileReaderscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#GeoIPFileReader. == nil { |
| 195 | return nil |
| 196 | } |
| 197 | if r, err := conf.GeoIPFileReaderscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#GeoIPFileReader.(false); err != nil { |
| 198 | return fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to read geoip file: %v", err) |
| 199 | } else if r != nil { |
| 200 | t.GeoIPCreatedFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#GeoIPCreatedFile. = "geoip" |
| 201 | if err := createFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/createFile().(filepathscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/Join().(t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., "geoip"), r); err != nil { |
| 202 | return fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to create geoip file: %v", err) |
| 203 | } |
| 204 | } |
| 205 | if r, err := conf.GeoIPFileReaderscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#GeoIPFileReader.(true); err != nil { |
| 206 | return fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to read geoip6 file: %v", err) |
| 207 | } else if r != nil { |
| 208 | t.GeoIPv6CreatedFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#GeoIPv6CreatedFile. = "geoip6" |
| 209 | if err := createFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/createFile().(filepathscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/Join().(t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., "geoip6"), r); err != nil { |
| 210 | return fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to create geoip6 file: %v", err) |
| 211 | } |
| 212 | } |
| 213 | return nil |
| 214 | } |
| 215 | |
| 216 | func createFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/createFile().(to string, from ioscip-go gomod github.com/golang/go/src go1.15 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.15 io/ReadCloser#) error { |
| 217 | f, err := osscip-go gomod github.com/golang/go/src go1.15 os/.Createscip-go gomod github.com/golang/go/src go1.15 os/Create().(to) |
| 218 | if err == nil { |
| 219 | _, err = ioscip-go gomod github.com/golang/go/src go1.15 io/.Copyscip-go gomod github.com/golang/go/src go1.15 io/Copy().(f, from) |
| 220 | if closeErr := f.Closescip-go gomod github.com/golang/go/src go1.15 os/File#Close().(); err == nil { |
| 221 | err = closeErr |
| 222 | } |
| 223 | } |
| 224 | if closeErr := from.Closescip-go gomod github.com/golang/go/src go1.15 io/Closer#Close().(); err == nil { |
| 225 | err = closeErr |
| 226 | } |
| 227 | return err |
| 228 | } |
| 229 | |
| 230 | func (t *Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#) startProcessscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#startProcess().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, conf *StartConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#) error { |
| 231 | // Get the creator |
| 232 | creator := conf.ProcessCreatorscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ProcessCreator. |
| 233 | if creator == nil { |
| 234 | torPath := conf.ExePathscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ExePath. |
| 235 | if torPath == "" { |
| 236 | torPath = "tor" |
| 237 | } |
| 238 | creator = processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/.NewCreatorscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/NewCreator().(torPath) |
| 239 | } |
| 240 | // Build the args |
| 241 | args := []string{"--DataDirectory", t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir.} |
| 242 | if !conf.DisableCookieAuthscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#DisableCookieAuth. { |
| 243 | args = append(args, "--CookieAuthentication", "1") |
| 244 | } |
| 245 | if !conf.EnableNetworkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#EnableNetwork. { |
| 246 | args = append(args, "--DisableNetwork", "1") |
| 247 | } |
| 248 | if !conf.NoHushscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#NoHush. { |
| 249 | args = append(args, "--hush") |
| 250 | } |
| 251 | if !conf.NoAutoSocksPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#NoAutoSocksPort. { |
| 252 | args = append(args, "--SocksPort", "auto") |
| 253 | } |
| 254 | if t.GeoIPCreatedFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#GeoIPCreatedFile. != "" { |
| 255 | args = append(args, "--GeoIPFile", filepathscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/Join().(t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., t.GeoIPCreatedFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#GeoIPCreatedFile.)) |
| 256 | } |
| 257 | if t.GeoIPv6CreatedFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#GeoIPv6CreatedFile. != "" { |
| 258 | args = append(args, "--GeoIPv6File", filepathscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.15 `path/filepath`/Join().(t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., t.GeoIPv6CreatedFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#GeoIPv6CreatedFile.)) |
| 259 | } |
| 260 | // If there is no Torrc file, create a blank temp one |
| 261 | torrcFileName := conf.TorrcFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#TorrcFile. |
| 262 | if torrcFileName == "" { |
| 263 | torrcFile, err := ioutilscip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/.TempFilescip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/TempFile().(t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., "torrc-") |
| 264 | if err != nil { |
| 265 | return err |
| 266 | } |
| 267 | torrcFileName = torrcFile.Namescip-go gomod github.com/golang/go/src go1.15 os/File#Name().() |
| 268 | if err = torrcFile.Closescip-go gomod github.com/golang/go/src go1.15 os/File#Close().(); err != nil { |
| 269 | return err |
| 270 | } |
| 271 | } |
| 272 | args = append(args, "-f", torrcFileName) |
| 273 | // Create file for Tor to write the control port to if it's not told to us and we're not embedded |
| 274 | var controlPortFileName string |
| 275 | var err error |
| 276 | if !conf.UseEmbeddedControlConnscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#UseEmbeddedControlConn. { |
| 277 | if conf.ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ControlPort. == 0 { |
| 278 | controlPortFile, err := ioutilscip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/.TempFilescip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/TempFile().(t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., "control-port-") |
| 279 | if err != nil { |
| 280 | return err |
| 281 | } |
| 282 | controlPortFileName = controlPortFile.Namescip-go gomod github.com/golang/go/src go1.15 os/File#Name().() |
| 283 | if err = controlPortFile.Closescip-go gomod github.com/golang/go/src go1.15 os/File#Close().(); err != nil { |
| 284 | return err |
| 285 | } |
| 286 | args = append(args, "--ControlPort", "auto", "--ControlPortWriteToFile", controlPortFile.Namescip-go gomod github.com/golang/go/src go1.15 os/File#Name().()) |
| 287 | } else { |
| 288 | args = append(args, "--ControlPort", strconvscip-go gomod github.com/golang/go/src go1.15 strconv/.Itoascip-go gomod github.com/golang/go/src go1.15 strconv/Itoa().(conf.ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ControlPort.)) |
| 289 | } |
| 290 | } |
| 291 | // Create process creator with args |
| 292 | var processCtx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context# |
| 293 | processCtx, t.ProcessCancelFuncscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ProcessCancelFunc. = contextscip-go gomod github.com/golang/go/src go1.15 context/.WithCancelscip-go gomod github.com/golang/go/src go1.15 context/WithCancel().(ctx) |
| 294 | args = append(args, conf.ExtraArgsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ExtraArgs....) |
| 295 | p, err := creator.Newscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/Creator#New.(processCtx, args...) |
| 296 | if err != nil { |
| 297 | return err |
| 298 | } |
| 299 | // Use the embedded conn if requested |
| 300 | if conf.UseEmbeddedControlConnscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#UseEmbeddedControlConn. { |
| 301 | t.Debugfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Debugf().("Using embedded control connection") |
| 302 | conn, err := p.EmbeddedControlConnscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/Process#EmbeddedControlConn.() |
| 303 | if err != nil { |
| 304 | return fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to get embedded control conn: %v", err) |
| 305 | } |
| 306 | t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control. = controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/.NewConnscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/NewConn().(textprotoscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/.NewConnscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/NewConn().(conn)) |
| 307 | t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..DebugWriterscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#DebugWriter. = t.DebugWriterscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DebugWriter. |
| 308 | } |
| 309 | // Start process with the args |
| 310 | t.Debugfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Debugf().("Starting tor with args %v", args) |
| 311 | if err = p.Startscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/Process#Start.(); err != nil { |
| 312 | return err |
| 313 | } |
| 314 | t.Processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Process. = p |
| 315 | // If not embedded, try a few times to read the control port file if we need to |
| 316 | if !conf.UseEmbeddedControlConnscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#UseEmbeddedControlConn. { |
| 317 | t.ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ControlPort. = conf.ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#ControlPort. |
| 318 | if t.ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ControlPort. == 0 { |
| 319 | ControlPortCheck: |
| 320 | for i := 0; i < 10; i++ { |
| 321 | select { |
| 322 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.15 context/Context#Done().(): |
| 323 | err = ctx.Errscip-go gomod github.com/golang/go/src go1.15 context/Context#Err().() |
| 324 | break ControlPortCheck |
| 325 | default: |
| 326 | // Try to read the controlport file, or wait a bit |
| 327 | var byts []byte |
| 328 | if byts, err = ioutilscip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/ReadFile().(controlPortFileName); err != nil { |
| 329 | break ControlPortCheck |
| 330 | } else if t.ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ControlPort., err = processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/.ControlPortFromFileContentsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/ControlPortFromFileContents().(string(byts)); err == nil { |
| 331 | break ControlPortCheck |
| 332 | } |
| 333 | timescip-go gomod github.com/golang/go/src go1.15 time/.Sleepscip-go gomod github.com/golang/go/src go1.15 time/Sleep().(200 * timescip-go gomod github.com/golang/go/src go1.15 time/.Millisecondscip-go gomod github.com/golang/go/src go1.15 time/Millisecond.) |
| 334 | } |
| 335 | } |
| 336 | if err != nil { |
| 337 | return fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to read control port file: %v", err) |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | return nil |
| 342 | } |
| 343 | |
| 344 | func (t *Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#) connectControllerscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#connectController().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, conf *StartConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/StartConf#) error { |
| 345 | // This doesn't apply if already connected (e.g. using embedded conn) |
| 346 | if t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control. != nil { |
| 347 | return nil |
| 348 | } |
| 349 | t.Debugfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Debugf().("Connecting to control port %v", t.ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ControlPort.) |
| 350 | textConn, err := textprotoscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/.Dialscip-go gomod github.com/golang/go/src go1.15 `net/textproto`/Dial().("tcp", "127.0.0.1:"+strconvscip-go gomod github.com/golang/go/src go1.15 strconv/.Itoascip-go gomod github.com/golang/go/src go1.15 strconv/Itoa().(t.ControlPortscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ControlPort.)) |
| 351 | if err != nil { |
| 352 | return err |
| 353 | } |
| 354 | t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control. = controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/.NewConnscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/NewConn().(textConn) |
| 355 | t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..DebugWriterscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#DebugWriter. = t.DebugWriterscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DebugWriter. |
| 356 | return nil |
| 357 | } |
| 358 | |
| 359 | // EnableNetwork sets DisableNetwork to 0 and optionally waits for bootstrap to |
| 360 | // complete. The context can be nil. If DisableNetwork isnt 1, this does |
| 361 | // nothing. |
| 362 | func (t *Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#) EnableNetworkscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#EnableNetwork().(ctx contextscip-go gomod github.com/golang/go/src go1.15 context/.Contextscip-go gomod github.com/golang/go/src go1.15 context/Context#, wait bool) error { |
| 363 | if ctx == nil { |
| 364 | ctx = contextscip-go gomod github.com/golang/go/src go1.15 context/.Backgroundscip-go gomod github.com/golang/go/src go1.15 context/Background().() |
| 365 | } |
| 366 | // Only enable if DisableNetwork is 1 |
| 367 | if vals, err := t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..GetConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#GetConf().("DisableNetwork"); err != nil { |
| 368 | return err |
| 369 | } else if len(vals) == 0 || vals[0].Keyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVal#Key. != "DisableNetwork" || vals[0].Valscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVal#Val. != "1" { |
| 370 | return nil |
| 371 | } |
| 372 | // Enable the network |
| 373 | if err := t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..SetConfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#SetConf().(controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/.KeyValsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/KeyVals().("DisableNetwork", "0")...); err != nil { |
| 374 | return nil |
| 375 | } |
| 376 | // If not waiting, leave |
| 377 | if !wait { |
| 378 | return nil |
| 379 | } |
| 380 | // Wait for progress to hit 100 |
| 381 | _, err := t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..EventWaitscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#EventWait().(ctx, []controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/.EventCodescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/EventCode#{controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/.EventCodeStatusClientscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/EventCodeStatusClient.}, |
| 382 | func(evt controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/.Eventscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Event#) (bool, error) { |
| 383 | if status, _ := evt.(*controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/.StatusEventscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusEvent#); status != nil && status.Actionscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusEvent#Action. == "BOOTSTRAP" { |
| 384 | if status.Severityscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusEvent#Severity. == "NOTICE" && status.Argumentsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusEvent#Arguments.["PROGRESS"] == "100" { |
| 385 | return true, nil |
| 386 | } else if status.Severityscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusEvent#Severity. == "ERR" { |
| 387 | return false, fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Failing bootstrapping, Tor warning: %v", status.Argumentsscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/StatusEvent#Arguments.["WARNING"]) |
| 388 | } |
| 389 | } |
| 390 | return false, nil |
| 391 | }) |
| 392 | return err |
| 393 | } |
| 394 | |
| 395 | // Close sends a halt to the Tor process if it can, closes the controller |
| 396 | // connection, and stops the process. |
| 397 | func (t *Torscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#) Closescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Close().() error { |
| 398 | t.Debugfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Debugf().("Closing Tor") |
| 399 | errs := []error{} |
| 400 | // If controller is authenticated, send the quit signal to the process. Otherwise, just close the controller. |
| 401 | sentHalt := false |
| 402 | if t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control. != nil { |
| 403 | if t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..Authenticatedscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#Authenticated. && t.StopProcessOnClosescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#StopProcessOnClose. { |
| 404 | if err := t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..Signalscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#Signal().("HALT"); err != nil { |
| 405 | errs = append(errs, fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to signal halt: %v", err)) |
| 406 | } else { |
| 407 | sentHalt = true |
| 408 | } |
| 409 | } |
| 410 | // Now close the controller |
| 411 | if err := t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control..Closescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#Close().(); err != nil { |
| 412 | errs = append(errs, fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Unable to close contrlller: %v", err)) |
| 413 | } else { |
| 414 | t.Controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Control. = nil |
| 415 | } |
| 416 | } |
| 417 | if t.Processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Process. != nil { |
| 418 | // If we didn't halt, we have to force kill w/ the cancel func |
| 419 | if !sentHalt && t.StopProcessOnClosescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#StopProcessOnClose. { |
| 420 | t.ProcessCancelFuncscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#ProcessCancelFunc.() |
| 421 | } |
| 422 | // Wait for a bit to make sure it stopped |
| 423 | errCh := make(chan error, 1) |
| 424 | var waitErr error |
| 425 | go func() { errCh <- t.Processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Process..Waitscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/process`/Process#Wait.() }() |
| 426 | select { |
| 427 | case waitErr = <-errCh: |
| 428 | if waitErr != nil { |
| 429 | errs = append(errs, fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Process wait failed: %v", waitErr)) |
| 430 | } |
| 431 | case <-timescip-go gomod github.com/golang/go/src go1.15 time/.Afterscip-go gomod github.com/golang/go/src go1.15 time/After().(300 * timescip-go gomod github.com/golang/go/src go1.15 time/.Millisecondscip-go gomod github.com/golang/go/src go1.15 time/Millisecond.): |
| 432 | errs = append(errs, fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Process did not exit after 300 ms")) |
| 433 | } |
| 434 | if waitErr == nil { |
| 435 | t.Processscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Process. = nil |
| 436 | } |
| 437 | } |
| 438 | // Get rid of the entire data dir |
| 439 | if t.DeleteDataDirOnClosescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DeleteDataDirOnClose. { |
| 440 | if err := osscip-go gomod github.com/golang/go/src go1.15 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.15 os/RemoveAll().(t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir.); err != nil { |
| 441 | errs = append(errs, fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Failed to remove data dir %v: %v", t.DataDirscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#DataDir., err)) |
| 442 | } |
| 443 | } |
| 444 | // Combine the errors if present |
| 445 | if len(errs) == 0 { |
| 446 | return nil |
| 447 | } else if len(errs) == 1 { |
| 448 | t.Debugfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Debugf().("Error while closing Tor: %v", errs[0]) |
| 449 | return errs[0] |
| 450 | } |
| 451 | t.Debugfscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/tor`/Tor#Debugf().("Errors while closing Tor: %v", errs) |
| 452 | return fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.15 fmt/Errorf().("Got %v errors while closing - %v", len(errs), errs) |
| 453 | } |