github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/lnp2p/util.go (about)

     1  package lnp2p
     2  
     3  import (
     4  	"github.com/mit-dci/lit/bech32"
     5  	"github.com/mit-dci/lit/crypto/fastsha256"
     6  	"github.com/mit-dci/lit/crypto/koblitz"
     7  	"github.com/mit-dci/lit/lncore"
     8  	"golang.org/x/net/proxy"
     9  	"net"
    10  	"strings"
    11  )
    12  
    13  // ParseAdrString splits a string like
    14  // "ln1yrvw48uc3atg8e2lzs43mh74m39vl785g4ehem@myhost.co:8191 into a separate
    15  // pkh part and network part, adding the network part if needed
    16  func splitAdrString(adr string) (string, string) {
    17  
    18  	if !strings.ContainsRune(adr, ':') && strings.ContainsRune(adr, '@') {
    19  		adr += ":2448"
    20  	}
    21  
    22  	idHost := strings.Split(adr, "@")
    23  
    24  	if len(idHost) == 1 {
    25  		return idHost[0], ""
    26  	}
    27  
    28  	return idHost[0], idHost[1]
    29  }
    30  
    31  // Given a raw pubkey, returns the lit addr.  Stolen from `lnutil/litadr.go`.
    32  func convertPubkeyToLitAddr(pk pubkey) lncore.LnAddr {
    33  	b := (*koblitz.PublicKey)(pk).SerializeCompressed()
    34  	doubleSha := fastsha256.Sum256(b[:])
    35  
    36  	lnaddr, e := lncore.ParseLnAddr(bech32.Encode("ln", doubleSha[:20]))
    37  
    38  	// Should never happen.
    39  	if e != nil {
    40  		panic("this should never happen, lit addr gen code has a bug: " + e.Error())
    41  	}
    42  
    43  	return lnaddr
    44  }
    45  
    46  func connectToProxyTCP(addr string, auth *string) (func(string, string) (net.Conn, error), error) {
    47  	// Authentication is good.  Use it if it's there.
    48  	var pAuth *proxy.Auth
    49  	if auth != nil {
    50  		parts := strings.SplitN(*auth, ":", 2)
    51  		pAuth = &proxy.Auth{
    52  			User:     parts[0],
    53  			Password: parts[1],
    54  		}
    55  	}
    56  
    57  	// Actually attempt to connect to the SOCKS Proxy.
    58  	d, err := proxy.SOCKS5("tcp", addr, pAuth, proxy.Direct)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return d.Dial, nil
    64  }