diff --git a/control/cmd_event.go b/control/cmd_event.go index 2500b0b..578a800 100644 --- a/control/cmd_event.go +++ b/control/cmd_event.go @@ -270,12 +270,14 @@ func (c *Conn) relayAsyncEvents(resp *Response) { var code, data string var dataArray []string if len(resp.Data) == 1 { - // If there is a single line of data, first line of it is the code, rest of the first line is data - firstNewline := strings.Index(resp.Data[0], "\r\n") - if firstNewline == -1 { - return + // On single line, part up to space, newline, or EOL is the code, rest is data + if index := strings.Index(resp.Data[0], " "); index != -1 { + code, data = resp.Data[0][:index], resp.Data[0][index+1:] + } else if index := strings.Index(resp.Data[0], "\r\n"); index != -1 { + code, data = resp.Data[0][:index], resp.Data[0][index+2:] + } else { + code, data = resp.Data[0], "" } - code, data = resp.Data[0][:firstNewline], resp.Data[0][firstNewline+2:] } else if len(resp.Data) > 0 { // If there are multiple lines, the entire first line is the code code, dataArray = resp.Data[0], resp.Data[1:] diff --git a/examples/embeddedfileserver/main.go b/examples/embeddedfileserver/main.go index 83acda2..96924b6 100644 --- a/examples/embeddedfileserver/main.go +++ b/examples/embeddedfileserver/main.go @@ -48,8 +48,9 @@ func run() error { // Wait at most a few minutes to publish the service listenCtx, listenCancel := context.WithTimeout(context.Background(), 3*time.Minute) defer listenCancel() - // Create an onion service to listen on a random local port but show as 80 - onion, err := t.Listen(listenCtx, &tor.ListenConf{RemotePorts: []int{80}}) + // Create an onion service to listen on a random local port but show as + // Do version 3, it's faster to set up + onion, err := t.Listen(listenCtx, &tor.ListenConf{RemotePorts: []int{80}, Version3: true}) if err != nil { return err } diff --git a/process/embedded/process.go b/process/embedded/process.go index 7aa0784..3f05ea6 100644 --- a/process/embedded/process.go +++ b/process/embedded/process.go @@ -9,8 +9,9 @@ // $GOPATH/src/github.com/cretz/tor-static as if it was fetched with go get. To // build the needed static libs, follow the README in that project. Once the // static libs are built, this uses CGO to statically link them here. For -// Windows this means something like http://tdm-gcc.tdragon.net/ needs to be -// present with gcc.exe on the PATH. +// Windows this means something like http://www.msys2.org/ needs to be +// installed with gcc.exe on the PATH (i.e. the same gcc that was used to build +// the static Tor lib). package embedded import ( diff --git a/tests/control_cmd_hiddenservice_test.go b/tests/control_cmd_hiddenservice_test.go new file mode 100644 index 0000000..270909b --- /dev/null +++ b/tests/control_cmd_hiddenservice_test.go @@ -0,0 +1,38 @@ +package tests + +import ( + "context" + "strings" + "testing" + "time" + + "github.com/cretz/bine/control" +) + +func TestHSFetch(t *testing.T) { + ctx := GlobalEnabledNetworkContext(t) + // Add listener + eventCh := make(chan control.Event) + defer close(eventCh) + err := ctx.Control.AddEventListener(eventCh, control.EventCodeHSDescContent) + ctx.Require.NoError(err) + defer ctx.Control.RemoveEventListener(eventCh, control.EventCodeHSDescContent) + // Lookup HS + err = ctx.Control.GetHiddenServiceDescriptorAsync("facebookcorewwwi", "") + ctx.Require.NoError(err) + // Grab events + eventCtx, eventCancel := context.WithTimeout(ctx, 45*time.Second) + defer eventCancel() + errCh := make(chan error, 1) + go func() { errCh <- ctx.Control.HandleEvents(eventCtx) }() + select { + case <-eventCtx.Done(): + ctx.Require.NoError(eventCtx.Err()) + case err := <-errCh: + ctx.Require.NoError(err) + case event := <-eventCh: + hsEvent := event.(*control.HSDescContentEvent) + ctx.Require.Equal("facebookcorewwwi", hsEvent.Address) + ctx.Require.True(strings.HasPrefix(hsEvent.Descriptor, "rendezvous-service-descriptor "+hsEvent.DescID)) + } +} diff --git a/tor/tor.go b/tor/tor.go index f736b41..d486169 100644 --- a/tor/tor.go +++ b/tor/tor.go @@ -207,6 +207,8 @@ func (t *Tor) startProcess(ctx context.Context, conf *StartConf) error { return err } args = append(args, "--ControlPort", "auto", "--ControlPortWriteToFile", controlPortFile.Name()) + } else { + args = append(args, "--ControlPort", strconv.Itoa(conf.ControlPort)) } // Start process with the args var processCtx context.Context