mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-02-20 03:23:11 -06:00
Fall back to Tor when it is not an I2P host
This commit is contained in:
parent
b0d4d7c9a4
commit
0832209cbe
|
|
@ -19,7 +19,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
@ -27,6 +26,7 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/cretz/bine/tor"
|
||||||
"github.com/eyedeekay/goSam"
|
"github.com/eyedeekay/goSam"
|
||||||
"github.com/eyedeekay/onramp"
|
"github.com/eyedeekay/onramp"
|
||||||
sentryhttp "github.com/getsentry/sentry-go/http"
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
||||||
|
|
@ -49,12 +49,29 @@ func client() (*goSam.Client, error) {
|
||||||
return goSam.NewClient(*samAddr)
|
return goSam.NewClient(*samAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
var sam, err = client()
|
var sam, samError = client()
|
||||||
|
|
||||||
|
func start() (*tor.Tor, error) {
|
||||||
|
if skip {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return tor.Start(context.Background(), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dialer() (*tor.Dialer, error) {
|
||||||
|
if skip {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return t.Dialer(context.TODO(), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
var t, terr = start()
|
||||||
|
var tdialer, tderr = dialer()
|
||||||
|
|
||||||
// Dial a network connection to an I2P server or a unix socket. Fail for clearnet addresses.
|
// Dial a network connection to an I2P server or a unix socket. Fail for clearnet addresses.
|
||||||
func Dial(network, addr string) (net.Conn, error) {
|
func Dial(network, addr string) (net.Conn, error) {
|
||||||
if err != nil {
|
if samError != nil {
|
||||||
return nil, err
|
return nil, samError
|
||||||
}
|
}
|
||||||
if network == "unix" {
|
if network == "unix" {
|
||||||
return net.Dial(network, addr)
|
return net.Dial(network, addr)
|
||||||
|
|
@ -68,7 +85,7 @@ func Dial(network, addr string) (net.Conn, error) {
|
||||||
if strings.HasSuffix(url.Host, ".i2p") {
|
if strings.HasSuffix(url.Host, ".i2p") {
|
||||||
return sam.Dial(network, addr)
|
return sam.Dial(network, addr)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("unknown network %s or address %s", network, url)
|
return tdialer.Dial(network, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed static/*.gotmpl
|
//go:embed static/*.gotmpl
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue