Fall back to Tor when it is not an I2P host

This commit is contained in:
eyedeekay 2024-09-12 00:48:35 -04:00
parent b0d4d7c9a4
commit 0832209cbe
No known key found for this signature in database
GPG key ID: D75C03B39B5E14E1

View file

@ -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