-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jorropo <[email protected]>
- Loading branch information
Showing
11 changed files
with
188 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package tor | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"github.com/cretz/bine/tor" | ||
|
||
"github.com/joomcode/errorx" | ||
"golang.org/x/net/proxy" | ||
|
||
"berty.tech/go-libp2p-tor-transport/config" | ||
"berty.tech/go-libp2p-tor-transport/internal/confStore" | ||
) | ||
|
||
// Builder is the type holding the starter node, it's used to fetch different ressources. | ||
type Builder struct { | ||
allowTcpDial bool | ||
setupTimeout time.Duration | ||
bridge *tor.Tor | ||
dialer ContextDialer | ||
} | ||
|
||
// ContextDialer is a dialler that also support contexted dials. | ||
type ContextDialer interface { | ||
proxy.Dialer | ||
DialContext(ctx context.Context, network string, addr string) (net.Conn, error) | ||
} | ||
|
||
func NewBuilder(cs ...config.Configurator) (*Builder, error) { | ||
var conf confStore.Config | ||
{ | ||
// Applying configuration | ||
conf = confStore.Config{ | ||
SetupTimeout: 5 * time.Minute, | ||
RunningContext: context.Background(), | ||
TorStart: &tor.StartConf{ | ||
EnableNetwork: true, // Do Fast Start | ||
}, | ||
} | ||
if err := config.Merge(cs...)(&conf); err != nil { | ||
return nil, errorx.Decorate(err, "Can't apply configuration to the tor node") | ||
} | ||
} | ||
t, err := tor.Start(conf.RunningContext, conf.TorStart) | ||
if err != nil { | ||
return nil, errorx.Decorate(err, "Can't start tor node") | ||
} | ||
|
||
// Up until this point, we don't need the starting configuration anymore. | ||
conf.TorStart = nil | ||
|
||
dialer, err := t.Dialer(conf.RunningContext, nil) | ||
if err != nil { | ||
return nil, errorx.Decorate(err, "Can't create a dialer.") | ||
} | ||
return &Builder{ | ||
allowTcpDial: conf.AllowTcpDial, | ||
setupTimeout: conf.SetupTimeout, | ||
bridge: t, | ||
dialer: dialer, | ||
}, nil | ||
} | ||
|
||
// GetDialer returns a shared dialer, it is closed once the transport closes. | ||
func (b *Builder) GetDialer() ContextDialer { | ||
return b.dialer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dns | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"net" | ||
"time" | ||
|
||
"github.com/joomcode/errorx" | ||
madns "github.com/multiformats/go-multiaddr-dns" | ||
"github.com/ncruces/go-dns" | ||
) | ||
|
||
func CreatDoTDNSResolverFromDialContext(dialFunc dns.DialFunc, hostname string, addresses ...string) (*net.Resolver, error) { | ||
certPool, err := x509.SystemCertPool() | ||
if err != nil { | ||
return nil, errorx.Decorate(err, "can't fetch system cert pool") | ||
} | ||
resolver, err := dns.NewDoTResolver( | ||
hostname, | ||
dns.DoTAddresses(addresses...), | ||
dns.DoTCache( | ||
dns.MaxCacheEntries(256), | ||
dns.MaxCacheTTL(time.Hour*24), | ||
dns.MinCacheTTL(time.Minute), | ||
), | ||
dns.DoTConfig(&tls.Config{ | ||
RootCAs: certPool, | ||
}), | ||
dns.DoTDialFunc(dialFunc), | ||
) | ||
if err != nil { | ||
return nil, errorx.Decorate(err, "can't create DoT resolver") | ||
} | ||
return resolver, nil | ||
} | ||
|
||
func CreateDoTMaDNSResolverFromDialContext(dialFunc dns.DialFunc, hostname string, addresses ...string) (*madns.Resolver, error) { | ||
netResolver, err := CreatDoTDNSResolverFromDialContext(dialFunc, hostname, addresses...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &madns.Resolver{ | ||
Backend: netResolver, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
package confStore | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cretz/bine/tor" | ||
) | ||
|
||
// `Config` stores the config, don't use it, you must use Configurator. | ||
type Config struct { | ||
AllowTcpDial bool | ||
SetupTimeout time.Duration | ||
AllowTcpDial bool | ||
SetupTimeout time.Duration | ||
RunningContext context.Context | ||
|
||
TorStart *tor.StartConf | ||
} |
Oops, something went wrong.