Skip to content

Commit

Permalink
all: return ptr instead of interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed May 23, 2024
1 parent 34a8a92 commit e13dc3e
Show file tree
Hide file tree
Showing 31 changed files with 60 additions and 44 deletions.
6 changes: 4 additions & 2 deletions intra/core/p2est.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ type P2QuantileEstimator interface {
P() float64
}

var _ P2QuantileEstimator = (*p2)(nil)

// NewP50Estimator returns a new P50 (median) estimator.
func NewP50Estimator() P2QuantileEstimator {
func NewP50Estimator() *p2 {
// calibrate: go.dev/play/p/Ry1i61XqzgB
// 31 worked best amid wild latency fluctuations
// using 11 for lower overhead; 5 is the default
return NewP2QuantileEstimator(11, 0.5)
}

// NewP90Estimator returns a new estimator with percentile p.
func NewP2QuantileEstimator(samples int, probability float64) P2QuantileEstimator {
func NewP2QuantileEstimator(samples int, probability float64) *p2 {
// total samples, typically 5; higher sample size improves accuracy for
// lower percentiles (p50) at the expense of computational cost;
// for higher percentiles (p90+), even sample size as low as 5 works fine.
Expand Down
6 changes: 4 additions & 2 deletions intra/dialers/direct_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ type splitter struct {
used bool // Initially false. Becomes true after the first write.
}

var _ DuplexConn = (*splitter)(nil)

// DialWithSplit returns a TCP connection that always splits the initial upstream segment.
// Like net.Conn, it is intended for two-threaded use, with one thread calling
// Read and CloseRead, and another calling Write, ReadFrom, and CloseWrite.
func DialWithSplit(d *protect.RDial, addr *net.TCPAddr) (DuplexConn, error) {
func DialWithSplit(d *protect.RDial, addr *net.TCPAddr) (*splitter, error) {
conn, err := d.DialTCP(addr.Network(), nil, addr)
if err != nil {
return nil, err
}
if conn == nil {
return nil, errNoConn
return nil, net.UnknownNetworkError("no conn")
}
return &splitter{TCPConn: conn}, nil
}
Expand Down
9 changes: 7 additions & 2 deletions intra/dialers/retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func calcTimeout(before, after time.Time) time.Duration {
// Read and CloseRead, and another calling Write, ReadFrom, and CloseWrite.
// `dialer` will be used to establish the connection.
// `addr` is the destination.
func DialWithSplitRetry(dial *protect.RDial, addr *net.TCPAddr) (DuplexConn, error) {
func DialWithSplitRetry(dial *protect.RDial, addr *net.TCPAddr) (*retrier, error) {
before := time.Now()
conn, err := dial.DialTCP(addr.Network(), nil, addr)
if err != nil {
Expand Down Expand Up @@ -402,9 +402,14 @@ func laddr(c net.Conn) net.Addr {
if c != nil && core.IsNotNil(c) {
return c.LocalAddr()
}
return nil
return NoNetAddr{}
}

type NoNetAddr struct{}

func (NoNetAddr) Network() string { return "no" }
func (NoNetAddr) String() string { return "none" }

func barrier(m *sync.Mutex) {
if m != nil {
m.Lock()
Expand Down
2 changes: 1 addition & 1 deletion intra/dns53/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type dot struct {
var _ dnsx.Transport = (*dot)(nil)

// NewTLSTransport returns a DNS over TLS transport, ready for use.
func NewTLSTransport(id, rawurl string, addrs []string, px ipn.Proxies, ctl protect.Controller) (t dnsx.Transport, err error) {
func NewTLSTransport(id, rawurl string, addrs []string, px ipn.Proxies, ctl protect.Controller) (t *dot, err error) {
tlscfg := &tls.Config{MinVersion: tls.VersionTLS12}
// rawurl is either tls:host[:port] or tls://host[:port] or host[:port]
parsedurl, err := url.Parse(rawurl)
Expand Down
2 changes: 1 addition & 1 deletion intra/dns53/goos.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type goosr struct {
var _ dnsx.Transport = (*transport)(nil)

// NewGoosTransport returns the default Go DNS resolver
func NewGoosTransport(pxs ipn.Proxies, ctl protect.Controller) (t dnsx.Transport, err error) {
func NewGoosTransport(pxs ipn.Proxies, ctl protect.Controller) (t *goosr, err error) {
// cannot be nil, see: ipn.Exit which the only proxy guaranteed to be connected to the internet;
// ex: ipn.Base routed back within the tunnel (rethink's traffic routed back into rethink)
// but it doesn't work for goos because the traffic to localhost:53 is routed back in as if
Expand Down
6 changes: 3 additions & 3 deletions intra/dns53/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type dnssd struct {
var _ dnsx.Transport = (*dnssd)(nil)

// NewMDNSTransport returns a DNS transport that sends all DNS queries to mDNS endpoint.
func NewMDNSTransport(protos string) (t dnsx.Transport) {
t = &dnssd{
func NewMDNSTransport(protos string) *dnssd {
t := &dnssd{
id: dnsx.Local,
use4: use4(protos),
use6: use6(protos),
Expand All @@ -58,7 +58,7 @@ func NewMDNSTransport(protos string) (t dnsx.Transport) {
est: core.NewP50Estimator(),
}
log.I("mdns: setup: %s", protos)
return
return t
}

func use4(l3 string) bool {
Expand Down
6 changes: 3 additions & 3 deletions intra/dns53/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type transport struct {
var _ dnsx.Transport = (*transport)(nil)

// NewTransportFromHostname returns a DNS53 transport serving from hostname, ready for use.
func NewTransportFromHostname(id, hostname string, ipcsv string, px ipn.Proxies, ctl protect.Controller) (t dnsx.Transport, err error) {
func NewTransportFromHostname(id, hostname string, ipcsv string, px ipn.Proxies, ctl protect.Controller) (t *transport, err error) {
// ipcsv may contain port, eg: 10.1.1.3:53
do, err := settings.NewDNSOptionsFromHostname(hostname, ipcsv)
if err != nil {
Expand All @@ -61,7 +61,7 @@ func NewTransportFromHostname(id, hostname string, ipcsv string, px ipn.Proxies,
}

// NewTransport returns a DNS53 transport serving from ip & port, ready for use.
func NewTransport(id, ip, port string, px ipn.Proxies, ctl protect.Controller) (t dnsx.Transport, err error) {
func NewTransport(id, ip, port string, px ipn.Proxies, ctl protect.Controller) (t *transport, err error) {
ipport := net.JoinHostPort(ip, port)
do, err := settings.NewDNSOptions(ipport)
if err != nil {
Expand All @@ -71,7 +71,7 @@ func NewTransport(id, ip, port string, px ipn.Proxies, ctl protect.Controller) (
return newTransport(id, do, px, ctl)
}

func newTransport(id string, do *settings.DNSOptions, px ipn.Proxies, ctl protect.Controller) (dnsx.Transport, error) {
func newTransport(id string, do *settings.DNSOptions, px ipn.Proxies, ctl protect.Controller) (*transport, error) {
var relay ipn.Proxy
// cannot be nil, see: ipn.Exit which the only proxy guaranteed to be connected to the internet;
// ex: ipn.Base routed back within the tunnel (rethink's traffic routed back into rethink).
Expand Down
2 changes: 1 addition & 1 deletion intra/dnscrypt/multiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func NewDcMult(px ipn.Proxies, ctl protect.Controller) *DcMulti {
}

// AddTransport creates and adds a dnscrypt transport to p
func AddTransport(p *DcMulti, id, serverstamp string) (dnsx.Transport, error) {
func AddTransport(p *DcMulti, id, serverstamp string) (*serverinfo, error) {
if p == nil {
return nil, dnsx.ErrNoDcProxy
}
Expand Down
2 changes: 1 addition & 1 deletion intra/dnsx/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type ctransport struct {
est core.P2QuantileEstimator
}

func NewDefaultCachingTransport(t Transport) (ct Transport) {
func NewDefaultCachingTransport(t Transport) Transport {
return NewCachingTransport(t, defttl)
}

Expand Down
2 changes: 1 addition & 1 deletion intra/dnsx/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ type resolver struct {

var _ Resolver = (*resolver)(nil)

func NewResolver(fakeaddrs string, tunmode *settings.TunMode, dtr x.DNSTransport, l x.DNSListener, pt NatPt) Resolver {
func NewResolver(fakeaddrs string, tunmode *settings.TunMode, dtr x.DNSTransport, l x.DNSListener, pt NatPt) *resolver {
r := &resolver{
NatPt: pt,
listener: l,
Expand Down
4 changes: 2 additions & 2 deletions intra/doh/doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (t *transport) dial(network, addr string) (net.Conn, error) {
// `rawurl` is the DoH template in string form.
// `addrs` is a list of IP addresses to bootstrap dialers.
// `px` is the proxy provider, may be nil (eg for id == dnsx.Default)
func NewTransport(id, rawurl string, addrs []string, px ipn.Proxies, ctl protect.Controller) (dnsx.Transport, error) {
func NewTransport(id, rawurl string, addrs []string, px ipn.Proxies, ctl protect.Controller) (*transport, error) {
return newTransport(dnsx.DOH, id, rawurl, "", addrs, px, ctl)
}

Expand All @@ -100,7 +100,7 @@ func NewTransport(id, rawurl string, addrs []string, px ipn.Proxies, ctl protect
// `target` is the ODoH resolver.
// `addrs` is a list of IP addresses to bootstrap endpoint dialers.
// `px` is the proxy provider, never nil.
func NewOdohTransport(id, endpoint, target string, addrs []string, px ipn.Proxies, ctl protect.Controller) (dnsx.Transport, error) {
func NewOdohTransport(id, endpoint, target string, addrs []string, px ipn.Proxies, ctl protect.Controller) (*transport, error) {
return newTransport(dnsx.ODOH, id, endpoint, target, addrs, px, ctl)
}

Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type base struct {
status int
}

func NewBaseProxy(c protect.Controller) Proxy {
func NewBaseProxy(c protect.Controller) *base {
d := protect.MakeNsRDial(Base, c)
h := &base{
addr: "127.3.4.5:6890",
Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type exit struct {
status int
}

func NewExitProxy(c protect.Controller) Proxy {
func NewExitProxy(c protect.Controller) *exit {
if c == nil {
log.W("proxy: exit: missing ctl; probably not what you want")
}
Expand Down
4 changes: 3 additions & 1 deletion intra/ipn/ground.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ type ground struct {
addr string
}

func NewGroundProxy() Proxy {
var _ Proxy = (*ground)(nil)

func NewGroundProxy() *ground {
h := &ground{
addr: "[::]:0",
}
Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/h1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type basicAuth struct {
}

// AuthBasic returns a ProxyAuthorization that implements "Basic" protocol while ignoring realm challanges.
func AuthBasic(username string, password string) ProxyAuthorization {
func AuthBasic(username string, password string) *basicAuth {
return &basicAuth{username: username, password: password}
}

Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/http1.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type http1 struct {
status int
}

func NewHTTPProxy(id string, c protect.Controller, po *settings.ProxyOptions) (Proxy, error) {
func NewHTTPProxy(id string, c protect.Controller, po *settings.ProxyOptions) (*http1, error) {
var err error
if po == nil {
log.W("proxy: err setting up http1 w(%v): %v", po, err)
Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/piph2.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (t *piph2) dial(network, addr string) (net.Conn, error) {
return dialers.SplitDial(t.proxydialer, network, addr)
}

func NewPipProxy(id string, ctl protect.Controller, po *settings.ProxyOptions) (Proxy, error) {
func NewPipProxy(id string, ctl protect.Controller, po *settings.ProxyOptions) (*piph2, error) {
if po == nil {
return nil, errMissingProxyOpt
}
Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/pipws.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (t *pipws) wsconn(rurl, msg string) (c net.Conn, res *http.Response, err er
// The proxy options must contain a valid URL, and the URL must have a path with the format "/ws/<sha256(rsasig)>".
// The proxy options must also contain a valid auth user (raw client token) and
// password (expiry + signed raw client token).
func NewPipWsProxy(id string, ctl protect.Controller, po *settings.ProxyOptions) (Proxy, error) {
func NewPipWsProxy(id string, ctl protect.Controller, po *settings.ProxyOptions) (*pipws, error) {
if po == nil {
return nil, errMissingProxyOpt
}
Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/proxies.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (w *gw) MTU() (int, error) { return NOMTU, errNoMtu }
func (w *gw) Stat() *x.Stats { return &w.stats }
func (w *gw) Contains(string) bool { return w.ok }

func NewProxifier(c protect.Controller, o x.ProxyListener) Proxies {
func NewProxifier(c protect.Controller, o x.ProxyListener) *proxifier {
if c == nil || o == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/celzero/firestack/intra/settings"
)

func (pxr *proxifier) NewSocks5Proxy(id, user, pwd, ip, port string) (p Proxy, err error) {
func (pxr *proxifier) NewSocks5Proxy(id, user, pwd, ip, port string) (p *socks5, err error) {
opts := settings.NewAuthProxyOptions("socks5", user, pwd, ip, port, nil)
return NewSocks5Proxy(id, pxr.ctl, opts)
}
Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *socks5udpconn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
return 0, nil, errNoProxyConn
}

func NewSocks5Proxy(id string, ctl protect.Controller, po *settings.ProxyOptions) (Proxy, error) {
func NewSocks5Proxy(id string, ctl protect.Controller, po *settings.ProxyOptions) (*socks5, error) {
var err error
if po == nil {
log.W("proxy: err setting up socks5(%v): %v", po, err)
Expand Down
2 changes: 1 addition & 1 deletion intra/ipn/wgproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func loadIPNets(out *[]netip.Prefix, v string) (err error) {
}

// ref: github.com/WireGuard/wireguard-android/blob/713947e432/tunnel/tools/libwg-go/api-android.go#L76
func NewWgProxy(id string, ctl protect.Controller, cfg string) (WgProxy, error) {
func NewWgProxy(id string, ctl protect.Controller, cfg string) (*wgproxy, error) {
ifaddrs, allowedaddrs, peers, dnsh, endpointh, mtu, err := wgIfConfigOf(id, &cfg)
uapicfg := cfg
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion intra/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ const (

const defaultLevel = INFO

var _ Logger = (*simpleLogger)(nil)

var defaultFlags = golog.Lshortfile
var defaultCallerDepth = 2
var _ = RegisterLogger(defaultLogger())
Expand All @@ -86,7 +88,7 @@ func defaultLogger() *simpleLogger {
}
}

func NewLogger(tag string) Logger {
func NewLogger(tag string) *simpleLogger {
l := defaultLogger()
if len(tag) <= 0 { // if tag is empty, leave it as is
return l
Expand Down
4 changes: 2 additions & 2 deletions intra/protect/ipmap/ipmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ type IPSet struct {
fails int // Number of times the confirmed IP has failed.
}

func NewIPMap() IPMap {
func NewIPMap() *ipmap {
return NewIPMapFor(nil)
}

// NewIPMapFor returns a fresh IPMap with r as its nameserver.
func NewIPMapFor(r IPMapper) IPMap {
func NewIPMapFor(r IPMapper) *ipmap {
return &ipmap{
m: make(map[string]*IPSet),
p: make(map[string]*IPSet),
Expand Down
6 changes: 3 additions & 3 deletions intra/protect/xdial.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
errAccept = errors.New("cannot accept network")
)

func (d *RDial) dial(network, addr string) (Conn, error) {
func (d *RDial) dial(network, addr string) (net.Conn, error) {
usedialer := d.Dialer != nil
userdialer := d.RDialer != nil
if usedialer {
Expand Down Expand Up @@ -88,7 +88,7 @@ func (d *RDial) DialContext(_ context.Context, network, addr string) (net.Conn,
}
}

func (d *RDial) Accept(network, local string) (Listener, error) {
func (d *RDial) Accept(network, local string) (net.Listener, error) {
if network != "tcp" && network != "tcp4" && network != "tcp6" {
return nil, errAccept
}
Expand All @@ -108,7 +108,7 @@ func (d *RDial) Accept(network, local string) (Listener, error) {
return d.RDialer.Accept(network, local)
}

func (d *RDial) Announce(network, local string) (PacketConn, error) {
func (d *RDial) Announce(network, local string) (net.PacketConn, error) {
if network != "udp" && network != "udp4" && network != "udp6" {
return nil, errAnnounce
}
Expand Down
2 changes: 1 addition & 1 deletion intra/rnet/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type httpxhandle struct {
px ipn.Proxy
}

func newHttpServer(id, x string, ctl protect.Controller, listener ServerListener) (Server, error) {
func newHttpServer(id, x string, ctl protect.Controller, listener ServerListener) (*httpx, error) {
var host string
var usr string
var pwd string
Expand Down
4 changes: 3 additions & 1 deletion intra/rnet/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type Services interface {
RefreshServers() (active string)
}

var _ Services = (*services)(nil)
var _ Server = (*httpx)(nil)
var _ Server = (*socks5)(nil)

type services struct {
Expand All @@ -94,7 +96,7 @@ type services struct {
ctl protect.Controller
}

func NewServices(proxies ipn.Proxies, ctl protect.Controller, listener ServerListener) Services {
func NewServices(proxies ipn.Proxies, ctl protect.Controller, listener ServerListener) *services {
if listener == nil || ctl == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion intra/rnet/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type socks5handler struct {
px ipn.Proxy
}

func newSocks5Server(id, x string, ctl protect.Controller, listener ServerListener) (Server, error) {
func newSocks5Server(id, x string, ctl protect.Controller, listener ServerListener) (*socks5, error) {
var host string
var usr string
var pwd string
Expand Down
2 changes: 1 addition & 1 deletion intra/x64/natpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var (
)

// NewNatPt returns a new NatPt.
func NewNatPt(tunmode *settings.TunMode) dnsx.NatPt {
func NewNatPt(tunmode *settings.TunMode) *natPt {
log.I("natpt: new; mode(%v)", tunmode)
return &natPt{
nat64: newNat64(),
Expand Down
Loading

1 comment on commit e13dc3e

@ignoramous
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#54

Please sign in to comment.