| 1 | # <img src="logo/bine-logo.png" width="180px"> |
| 2 | |
| 3 | [](https://godoc.org/github.com/cretz/bine) |
| 4 | |
| 5 | Bine is a Go API for using and controlling Tor. It is similar to [Stem](https://stem.torproject.org/). |
| 6 | |
| 7 | Features: |
| 8 | |
| 9 | * Full support for the Tor controller API |
| 10 | * Support for `net.Conn` and `net.Listen` style APIs |
| 11 | * Supports statically compiled Tor to embed Tor into the binary |
| 12 | * Supports both v2 and v3 onion services |
| 13 | * Support for embedded control socket in Tor >= 0.3.5 (non-Windows) |
| 14 | |
| 15 | See info below, the [API docs](http://godoc.org/github.com/cretz/bine), and the [examples](examples). The project is |
| 16 | MIT licensed. The Tor docs/specs and https://github.com/yawning/bulb were great helps when building this. |
| 17 | |
| 18 | ## Example |
| 19 | |
| 20 | It is really easy to create an onion service. For example, assuming `tor` is on the `PATH`, this bit of code will show |
| 21 | a directory server of the current directory: |
| 22 | |
| 23 | ```go |
| 24 | package main |
| 25 | |
| 26 | import ( |
| 27 | "context" |
| 28 | "fmt" |
| 29 | "log" |
| 30 | "net/http" |
| 31 | "time" |
| 32 | |
| 33 | "github.com/cretz/bine/tor" |
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | // Start tor with default config (can set start conf's DebugWriter to os.Stdout for debug logs) |
| 38 | fmt.Println("Starting and registering onion service, please wait a couple of minutes...") |
| 39 | t, err := tor.Start(nil, nil) |
| 40 | if err != nil { |
| 41 | log.Panicf("Unable to start Tor: %v", err) |
| 42 | } |
| 43 | defer t.Close() |
| 44 | // Wait at most a few minutes to publish the service |
| 45 | listenCtx, listenCancel := context.WithTimeout(context.Background(), 3*time.Minute) |
| 46 | defer listenCancel() |
| 47 | // Create a v3 onion service to listen on any port but show as 80 |
| 48 | onion, err := t.Listen(listenCtx, &tor.ListenConf{Version3: true, RemotePorts: []int{80}}) |
| 49 | if err != nil { |
| 50 | log.Panicf("Unable to create onion service: %v", err) |
| 51 | } |
| 52 | defer onion.Close() |
| 53 | fmt.Printf("Open Tor browser and navigate to http://%v.onion\n", onion.ID) |
| 54 | fmt.Println("Press enter to exit") |
| 55 | // Serve the current folder from HTTP |
| 56 | errCh := make(chan error, 1) |
| 57 | go func() { errCh <- http.Serve(onion, http.FileServer(http.Dir("."))) }() |
| 58 | // End when enter is pressed |
| 59 | go func() { |
| 60 | fmt.Scanln() |
| 61 | errCh <- nil |
| 62 | }() |
| 63 | if err = <-errCh; err != nil { |
| 64 | log.Panicf("Failed serving: %v", err) |
| 65 | } |
| 66 | } |
| 67 | ``` |
| 68 | |
| 69 | If in `main.go` it can simply be run with `go run main.go`. Of course this uses a separate `tor` process. To embed Tor |
| 70 | statically in the binary, follow the [embedded package docs](https://godoc.org/github.com/cretz/bine/process/embedded) |
| 71 | which will require [building Tor statically](https://github.com/cretz/tor-static). Then with |
| 72 | `github.com/cretz/bine/process/embedded` imported, change the start line above to: |
| 73 | |
| 74 | ```go |
| 75 | t, err := tor.Start(nil, &tor.StartConf{ProcessCreator: embedded.NewCreator()}) |
| 76 | ``` |
| 77 | |
| 78 | This defaults to Tor 0.3.5.x versions but others can be used from different packages. In non-Windows environments, the |
| 79 | `UseEmbeddedControlConn` field in `StartConf` can be set to `true` to use an embedded socket that does not open a |
| 80 | control port. |
| 81 | |
| 82 | Tested on Windows, the original exe file is ~7MB. With Tor statically linked it comes to ~24MB, but Tor does not have to |
| 83 | be distributed separately. Of course take notice of all licenses in accompanying projects. |
| 84 | |
| 85 | ## Testing |
| 86 | |
| 87 | To test, a simple `go test ./...` from the base of the repository will work (add in a `-v` in there to see the tests). |
| 88 | The integration tests in `tests` however will be skipped. To execute those tests, `-tor` must be passed to the test. |
| 89 | Also, `tor` must be on the `PATH` or `-tor.path` must be set to the path of the `tor` executable. Even with those flags, |
| 90 | only the integration tests that do not connect to the Tor network are run. To also include the tests that use the Tor |
| 91 | network, add the `-tor.network` flag. For details Tor logs during any of the integration tests, use the `-tor.verbose` |
| 92 | flag. |