protocol

package
v0.0.0-...-ba1e856 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MessageTypeSocks5     = MessageType("SOCKS5RESPONSE")
	MessageConnect        = MessageType("CONNECT")
	MessageConnectReverse = MessageType("CONNECTR")
)
View Source
const KindAnnouncementEvent int = 38333

KindAnnouncementEvent represents the unique identifier for announcement events.

View Source
const KindCertificateEvent int = 38334

KindCertificateEvent represents the unique identifier for certificate events.

View Source
const KindEphemeralEvent int = 28333

KindEphemeralEvent represents the unique identifier for ephemeral events.

View Source
const KindPrivateKeyEvent int = 38335

KindPrivateKeyEvent represents the unique identifier for private key events.

Variables

View Source
var ErrEmptyURL = errors.New("url to be parsed is empty")

Functions

func AddDefaultScheme

func AddDefaultScheme(s string) string

add default scheme if string does not include a scheme.

func AddScheme

func AddScheme(s, scheme string) string

func GetEncryptionKeys

func GetEncryptionKeys(privateKey, publicKey string) ([]byte, []byte, error)

func IsDomainName

func IsDomainName(name string) bool

IsDomainName checks if a string represents a valid domain name.

It follows the rules specified in RFC 1035 and RFC 3696 for domain name validation.

The input string is first processed with the RemoveScheme function to remove any scheme prefix. The domain name is then split into labels using the dot separator. The function checks that the number of labels is at least 2 and that the total length of the string is between 1 and 254 characters.

The function iterates over the characters of the string and performs checks based on the character type. Valid characters include letters (a-zA-Z), digits (0-9), underscore (_), and hyphen (-). Each label can contain up to 63 characters and the last label cannot end with a hyphen. The function also checks that the byte before a dot or a hyphen is not a dot or a hyphen, respectively. Non-numeric characters are tracked to ensure the presence of at least one non-numeric character in the domain name.

If any of the checks fail, the function returns false. Otherwise, it returns true.

Example usage: s := "mail.google.com" isValid := IsDomainName(s).

func MarshalJSON

func MarshalJSON(m *Message) ([]byte, error)

func RemoveScheme

func RemoveScheme(s string) string

RemoveScheme removes the scheme from a URL string. If the URL string includes a scheme (e.g., "http://"), the scheme will be removed and the remaining string will be returned. If the URL string includes a default scheme (e.g., "//"), the default scheme will be removed and the remaining string will be returned. If the URL string does not include a scheme, the original string will be returned unchanged.

Types

type EventSigner

type EventSigner struct {
	PublicKey string
	// contains filtered or unexported fields
}

EventSigner represents a signer that can create and sign events.

EventSigner provides methods for creating unsigned events, creating signed events.

func NewEventSigner

func NewEventSigner(privateKey string) (*EventSigner, error)

NewEventSigner creates a new EventSigner.

func (*EventSigner) CreateEvent

func (s *EventSigner) CreateEvent(kind int, tags nostr.Tags) nostr.Event

CreateEvent creates a new Event with the provided tags. The Public Key and the current timestamp are set automatically. The Kind is set to KindEphemeralEvent.

func (*EventSigner) CreateSignedEvent

func (s *EventSigner) CreateSignedEvent(
	targetPublicKey string,
	kind int,
	tags nostr.Tags,
	opts ...MessageOption,
) (nostr.Event, error)

CreateSignedEvent creates a signed Nostr event with the provided target public key, tags, and options. It computes the shared key between the target public key and the private key of the EventSigner. Then, it creates a new message with the provided options. The message is serialized to JSON and encrypted using the shared key. The method then calls CreateEvent to create a new unsigned event with the provided tags. The encrypted message is set as the content of the event. Finally, the event is signed with the private key of the EventSigner, setting the event ID and event Sig fields. The signed event is returned along with any error that occurs.

type Message

type Message struct {
	Key                uuid.UUID   `json:"key,omitempty"`                // unique identifier for the message
	Type               MessageType `json:"type,omitempty"`               // type of message
	Data               []byte      `json:"data,omitempty"`               // data to be sent
	Destination        string      `json:"destination,omitempty"`        // destination to send the message
	EntryPublicAddress string      `json:"entryPublicAddress,omitempty"` // public ip address of the entry node (used for reverse connect)
}

func NewMessage

func NewMessage(configs ...MessageOption) *Message

func UnmarshalJSON

func UnmarshalJSON(data []byte) (*Message, error)

type MessageOption

type MessageOption func(*Message)

func WithData

func WithData(data []byte) MessageOption

func WithDestination

func WithDestination(destination string) MessageOption

func WithEntryPublicAddress

func WithEntryPublicAddress(entryPublicAddress string) MessageOption

func WithType

func WithType(messageType MessageType) MessageOption

func WithUUID

func WithUUID(uuid uuid.UUID) MessageOption

type MessageType

type MessageType string

type URL

type URL struct {
	SubName, Name, TLD, Port string
	IsDomain                 bool
	*url.URL
}

URL represents a URL with additional fields and methods.

func FromParsed

func FromParsed(parsedURL *url.URL) (*URL, error)

FromParsed mirrors the net/url.Parse function, but instead of returning a *url.URL, it returns a *URL, which is a struct that contains additional fields.

The function first checks if the parsedUrl.Host field is empty. If it is empty, it returns a *URL with the URL field set to parsedUrl and all other fields set to their zero values.

If the parsedUrl.Host field is not empty, it extracts the domain and port using the domainPort function.

It then calculates the effective top-level domain plus one (etld+1) using the publicsuffix.EffectiveTLDPlusOne function.

The etld+1 is then split into the domain name (domName) and the top-level domain (tld).

It further determines the subdomain (sub) by checking if the domain is a subdomain of the etld+1.

The domain name (domName) is then converted to ASCII using the idna.ToASCII function.

Finally, it returns a *URL with the extracted values and the URL field set to parsedUrl. The IsDomain field is set to the result of the IsDomainName function called with the ASCII domain name. The SubName field is set to sub, the Name field is set to domName, and the T.

func Parse

func Parse(urlString string) (*URL, error)

Parse parses a string representation of a URL and returns a *URL and error. It mirrors the net/url.Parse function but returns a tld.URL, which contains extra fields.

func (URL) Domain

func (url URL) Domain(includeSub bool) string

Domain returns the domain name of the URL. If includeSub is true and there is a subdomain, it includes the subdomain in the returned string. Otherwise, it only includes the domain.

func (URL) HTTPS

func (url URL) HTTPS() string

HTTPS returns the URL with HTTPS Scheme but leaves the URL itself untouched.

func (URL) IsLocal

func (url URL) IsLocal() bool

IsLocal checks if the URL is a local address. It returns true if the URL's top-level domain (TLD) is "localhost" or if the URL's hostname resolves to a loopback IP address.

func (URL) NoWWW

func (url URL) NoWWW() string

NoWWW returns the domain name without the "www" subdomain. If the subdomain is not "www" or is empty, it returns the domain name as is. The returned domain name is a string in the format "subname.name.tld".

func (URL) String

func (url URL) String(includeScheme bool) string

String returns the string representation of the URL. It includes the scheme if `includeScheme` is true.

func (URL) StripQueryParams

func (url URL) StripQueryParams(includeScheme bool) string

StripQueryParams removes query parameters and fragments from the URL and returns the URL as a string. If includeScheme is true, it includes the scheme in the returned URL.

func (URL) StripWWW

func (url URL) StripWWW(includeScheme bool) string

StripWWW returns the URL without "www" subdomain, but leaves the URL itself untouched. This function returns the whole URL with its path, in contrast to NoWWW().

func (URL) WWW

func (url URL) WWW() string

WWW returns the domain name with the "www" subdomain. If the subdomain is not "www", it returns the domain name as is. The returned domain name is a string in the format "subname.name.tld".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL