Files
go-sam-go/DOC.md
eyedeekay a53acecb87 godoc
2025-05-29 19:44:45 -04:00

24 KiB

sam3

-- import "github.com/go-i2p/go-sam-go"

Package sam3 provides a compatibility layer for the go-i2p/sam3 library using go-sam-go as the backend

Usage

const (
	Sig_NONE                 = "SIGNATURE_TYPE=EdDSA_SHA512_Ed25519"
	Sig_DSA_SHA1             = "SIGNATURE_TYPE=DSA_SHA1"
	Sig_ECDSA_SHA256_P256    = "SIGNATURE_TYPE=ECDSA_SHA256_P256"
	Sig_ECDSA_SHA384_P384    = "SIGNATURE_TYPE=ECDSA_SHA384_P384"
	Sig_ECDSA_SHA512_P521    = "SIGNATURE_TYPE=ECDSA_SHA512_P521"
	Sig_EdDSA_SHA512_Ed25519 = "SIGNATURE_TYPE=EdDSA_SHA512_Ed25519"
)

Constants from original sam3

var (
	Options_Humongous = []string{
		"inbound.length=3", "outbound.length=3",
		"inbound.lengthVariance=1", "outbound.lengthVariance=1",
		"inbound.backupQuantity=3", "outbound.backupQuantity=3",
		"inbound.quantity=6", "outbound.quantity=6",
	}

	Options_Large = []string{
		"inbound.length=3", "outbound.length=3",
		"inbound.lengthVariance=1", "outbound.lengthVariance=1",
		"inbound.backupQuantity=1", "outbound.backupQuantity=1",
		"inbound.quantity=4", "outbound.quantity=4",
	}

	Options_Wide = []string{
		"inbound.length=1", "outbound.length=1",
		"inbound.lengthVariance=1", "outbound.lengthVariance=1",
		"inbound.backupQuantity=2", "outbound.backupQuantity=2",
		"inbound.quantity=3", "outbound.quantity=3",
	}

	Options_Medium = []string{
		"inbound.length=3", "outbound.length=3",
		"inbound.lengthVariance=1", "outbound.lengthVariance=1",
		"inbound.backupQuantity=0", "outbound.backupQuantity=0",
		"inbound.quantity=2", "outbound.quantity=2",
	}

	Options_Default = []string{
		"inbound.length=3", "outbound.length=3",
		"inbound.lengthVariance=0", "outbound.lengthVariance=0",
		"inbound.backupQuantity=1", "outbound.backupQuantity=1",
		"inbound.quantity=1", "outbound.quantity=1",
	}

	Options_Small = []string{
		"inbound.length=3", "outbound.length=3",
		"inbound.lengthVariance=1", "outbound.lengthVariance=1",
		"inbound.backupQuantity=0", "outbound.backupQuantity=0",
		"inbound.quantity=1", "outbound.quantity=1",
	}

	Options_Warning_ZeroHop = []string{
		"inbound.length=0", "outbound.length=0",
		"inbound.lengthVariance=0", "outbound.lengthVariance=0",
		"inbound.backupQuantity=0", "outbound.backupQuantity=0",
		"inbound.quantity=2", "outbound.quantity=2",
	}
)

Predefined option sets (keeping your existing definitions)

var (
	PrimarySessionSwitch string = PrimarySessionString()
	SAM_HOST                    = getEnv("sam_host", "127.0.0.1")
	SAM_PORT                    = getEnv("sam_port", "7656")
)

Global variables from original sam3

func ConvertOptionsToSlice

func ConvertOptionsToSlice(opts Options) []string

Additional utility functions that may be needed for compatibility

func ExtractDest

func ExtractDest(input string) string

ExtractDest extracts destination from input

func ExtractPairInt

func ExtractPairInt(input, value string) int

ExtractPairInt extracts integer value from key=value pair

func ExtractPairString

func ExtractPairString(input, value string) string

ExtractPairString extracts string value from key=value pair

func GenerateOptionString

func GenerateOptionString(opts []string) string

GenerateOptionString generates option string from slice

func GetSAM3Logger

func GetSAM3Logger() *logrus.Logger

GetSAM3Logger returns the initialized logger

func IgnorePortError

func IgnorePortError(err error) error

IgnorePortError ignores port-related errors

func InitializeSAM3Logger

func InitializeSAM3Logger()

InitializeSAM3Logger initializes the logger

func PrimarySessionString

func PrimarySessionString() string

PrimarySessionString returns primary session string

func RandString

func RandString() string

RandString generates a random string

func SAMDefaultAddr

func SAMDefaultAddr(fallforward string) string

SAMDefaultAddr returns default SAM address

func SetAccessListType

func SetAccessListType(s string) func(*I2PConfig) error

func SetCloseIdleTime

func SetCloseIdleTime(s string) func(*I2PConfig) error

func SetInAllowZeroHop

func SetInAllowZeroHop(s string) func(*I2PConfig) error

Configuration option setters for all the missing Set* functions

func SetInBackupQuantity

func SetInBackupQuantity(s string) func(*I2PConfig) error

func SetInLength

func SetInLength(s string) func(*I2PConfig) error

func SetInQuantity

func SetInQuantity(s string) func(*I2PConfig) error

func SetInVariance

func SetInVariance(s string) func(*I2PConfig) error

func SetOutAllowZeroHop

func SetOutAllowZeroHop(s string) func(*I2PConfig) error

func SetOutBackupQuantity

func SetOutBackupQuantity(s string) func(*I2PConfig) error

func SetOutLength

func SetOutLength(s string) func(*I2PConfig) error

func SetOutQuantity

func SetOutQuantity(s string) func(*I2PConfig) error

func SetOutVariance

func SetOutVariance(s string) func(*I2PConfig) error

func SetReduceIdleTime

func SetReduceIdleTime(s string) func(*I2PConfig) error

func SetUseCompression

func SetUseCompression(s string) func(*I2PConfig) error

type DatagramSession

type DatagramSession struct {
}

DatagramSession implements net.PacketConn for I2P datagrams

func (*DatagramSession) Accept

func (s *DatagramSession) Accept() (net.Conn, error)

Accept accepts connections (not applicable for datagrams)

func (*DatagramSession) Addr

func (s *DatagramSession) Addr() net.Addr

Addr returns the session address

func (*DatagramSession) B32

func (s *DatagramSession) B32() string

B32 returns the base32 address

func (*DatagramSession) Close

func (s *DatagramSession) Close() error

Close closes the datagram session

func (*DatagramSession) Dial

func (s *DatagramSession) Dial(net string, addr string) (*DatagramSession, error)

Dial dials a connection (returns self for datagrams)

func (*DatagramSession) DialI2PRemote

func (s *DatagramSession) DialI2PRemote(net string, addr net.Addr) (*DatagramSession, error)

DialI2PRemote dials to I2P remote

func (*DatagramSession) DialRemote

func (s *DatagramSession) DialRemote(net, addr string) (net.PacketConn, error)

DialRemote dials to remote address

func (*DatagramSession) LocalAddr

func (s *DatagramSession) LocalAddr() net.Addr

LocalAddr returns the local address

func (*DatagramSession) LocalI2PAddr

func (s *DatagramSession) LocalI2PAddr() i2pkeys.I2PAddr

LocalI2PAddr returns the I2P destination

func (*DatagramSession) Lookup

func (s *DatagramSession) Lookup(name string) (a net.Addr, err error)

Lookup performs name lookup

func (*DatagramSession) Read

func (s *DatagramSession) Read(b []byte) (n int, err error)

Read reads from the session

func (*DatagramSession) ReadFrom

func (s *DatagramSession) ReadFrom(b []byte) (n int, addr net.Addr, err error)

ReadFrom reads a datagram from the session

func (*DatagramSession) RemoteAddr

func (s *DatagramSession) RemoteAddr() net.Addr

RemoteAddr returns the remote address

func (*DatagramSession) SetDeadline

func (s *DatagramSession) SetDeadline(t time.Time) error

SetDeadline sets read and write deadlines

func (*DatagramSession) SetReadDeadline

func (s *DatagramSession) SetReadDeadline(t time.Time) error

SetReadDeadline sets read deadline

func (*DatagramSession) SetWriteBuffer

func (s *DatagramSession) SetWriteBuffer(bytes int) error

SetWriteBuffer sets write buffer size

func (*DatagramSession) SetWriteDeadline

func (s *DatagramSession) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets write deadline

func (*DatagramSession) Write

func (s *DatagramSession) Write(b []byte) (int, error)

Write writes to the session

func (*DatagramSession) WriteTo

func (s *DatagramSession) WriteTo(b []byte, addr net.Addr) (n int, err error)

WriteTo writes a datagram to the specified address

type I2PConfig

type I2PConfig struct {
	*common.I2PConfig
}

I2PConfig manages I2P configuration options

func NewConfig

func NewConfig(opts ...func(*I2PConfig) error) (*I2PConfig, error)

NewConfig creates a new I2PConfig

func (*I2PConfig) DestinationKey

func (f *I2PConfig) DestinationKey() string

func (*I2PConfig) ID

func (f *I2PConfig) ID() string

func (*I2PConfig) MaxSAM

func (f *I2PConfig) MaxSAM() string

func (*I2PConfig) MinSAM

func (f *I2PConfig) MinSAM() string

func (*I2PConfig) Print

func (f *I2PConfig) Print() []string

func (*I2PConfig) Reduce

func (f *I2PConfig) Reduce() string

func (*I2PConfig) Reliability

func (f *I2PConfig) Reliability() string

func (*I2PConfig) SAMAddress

func (f *I2PConfig) SAMAddress() string

func (*I2PConfig) Sam

func (f *I2PConfig) Sam() string

func (*I2PConfig) SessionStyle

func (f *I2PConfig) SessionStyle() string

func (*I2PConfig) SetSAMAddress

func (f *I2PConfig) SetSAMAddress(addr string)

All the configuration method forwards

func (*I2PConfig) SignatureType

func (f *I2PConfig) SignatureType() string

func (*I2PConfig) ToPort

func (f *I2PConfig) ToPort() string

type Option

type Option func(*SAMEmit) error

Option is a functional option for SAMEmit

type Options

type Options map[string]string

Options represents a map of configuration options

func ConvertSliceToOptions

func ConvertSliceToOptions(slice []string) Options

func (Options) AsList

func (opts Options) AsList() (ls []string)

AsList returns options as a list of strings

type PrimarySession

type PrimarySession struct {
	Timeout  time.Duration
	Deadline time.Time
	Config   SAMEmit
}

PrimarySession represents a primary session

func (*PrimarySession) Addr

func (ss *PrimarySession) Addr() i2pkeys.I2PAddr

Addr returns the I2P address

func (*PrimarySession) Close

func (ss *PrimarySession) Close() error

Close closes the session

func (*PrimarySession) Dial

func (sam *PrimarySession) Dial(network, addr string) (net.Conn, error)

Dial implements net.Dialer

func (*PrimarySession) DialTCP

func (sam *PrimarySession) DialTCP(network string, laddr, raddr net.Addr) (net.Conn, error)

DialTCP implements x/dialer

func (*PrimarySession) DialTCPI2P

func (sam *PrimarySession) DialTCPI2P(network string, laddr, raddr string) (net.Conn, error)

DialTCPI2P dials TCP over I2P

func (*PrimarySession) DialUDP

func (sam *PrimarySession) DialUDP(network string, laddr, raddr net.Addr) (net.PacketConn, error)

DialUDP implements x/dialer

func (*PrimarySession) DialUDPI2P

func (sam *PrimarySession) DialUDPI2P(network, laddr, raddr string) (*DatagramSession, error)

DialUDPI2P dials UDP over I2P

func (*PrimarySession) From

func (ss *PrimarySession) From() string

From returns from port

func (*PrimarySession) ID

func (ss *PrimarySession) ID() string

ID returns the session ID

func (*PrimarySession) Keys

func (ss *PrimarySession) Keys() i2pkeys.I2PKeys

Keys returns the session keys

func (*PrimarySession) LocalAddr

func (ss *PrimarySession) LocalAddr() net.Addr

LocalAddr returns local address

func (*PrimarySession) Lookup

func (s *PrimarySession) Lookup(name string) (a net.Addr, err error)

Lookup performs name lookup

func (*PrimarySession) NewDatagramSubSession

func (s *PrimarySession) NewDatagramSubSession(id string, udpPort int) (*DatagramSession, error)

NewDatagramSubSession creates a new datagram sub-session

func (*PrimarySession) NewRawSubSession

func (s *PrimarySession) NewRawSubSession(id string, udpPort int) (*RawSession, error)

NewRawSubSession creates a new raw sub-session

func (*PrimarySession) NewStreamSubSession

func (sam *PrimarySession) NewStreamSubSession(id string) (*StreamSession, error)

NewStreamSubSession creates a new stream sub-session

func (*PrimarySession) NewStreamSubSessionWithPorts

func (sam *PrimarySession) NewStreamSubSessionWithPorts(id, from, to string) (*StreamSession, error)

NewStreamSubSessionWithPorts creates a new stream sub-session with ports

func (*PrimarySession) NewUniqueStreamSubSession

func (sam *PrimarySession) NewUniqueStreamSubSession(id string) (*StreamSession, error)

NewUniqueStreamSubSession creates a unique stream sub-session

func (*PrimarySession) Resolve

func (sam *PrimarySession) Resolve(network, addr string) (net.Addr, error)

Resolve resolves network address

func (*PrimarySession) ResolveTCPAddr

func (sam *PrimarySession) ResolveTCPAddr(network, dest string) (net.Addr, error)

ResolveTCPAddr resolves TCP address

func (*PrimarySession) ResolveUDPAddr

func (sam *PrimarySession) ResolveUDPAddr(network, dest string) (net.Addr, error)

ResolveUDPAddr resolves UDP address

func (*PrimarySession) SignatureType

func (ss *PrimarySession) SignatureType() string

SignatureType returns signature type

func (*PrimarySession) To

func (ss *PrimarySession) To() string

To returns to port

type RawSession

type RawSession struct {
}

RawSession provides raw datagram messaging

func (*RawSession) Close

func (s *RawSession) Close() error

Close closes the raw session

func (*RawSession) LocalAddr

func (s *RawSession) LocalAddr() i2pkeys.I2PAddr

LocalAddr returns the local I2P destination

func (*RawSession) Read

func (s *RawSession) Read(b []byte) (n int, err error)

Read reads one raw datagram

func (*RawSession) SetDeadline

func (s *RawSession) SetDeadline(t time.Time) error

SetDeadline sets read and write deadlines

func (*RawSession) SetReadDeadline

func (s *RawSession) SetReadDeadline(t time.Time) error

SetReadDeadline sets read deadline

func (*RawSession) SetWriteDeadline

func (s *RawSession) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets write deadline

func (*RawSession) WriteTo

func (s *RawSession) WriteTo(b []byte, addr i2pkeys.I2PAddr) (n int, err error)

WriteTo sends one raw datagram to the destination

type SAM

type SAM struct {
	Config SAMEmit
}

SAM represents the main controller for I2P router's SAM bridge

func NewSAM

func NewSAM(address string) (*SAM, error)

NewSAM creates a new controller for the I2P routers SAM bridge

func (*SAM) Close

func (sam *SAM) Close() error

Close closes this sam session

func (*SAM) EnsureKeyfile

func (sam *SAM) EnsureKeyfile(fname string) (keys i2pkeys.I2PKeys, err error)

EnsureKeyfile ensures keyfile exists

func (*SAM) Keys

func (sam *SAM) Keys() (k *i2pkeys.I2PKeys)

Keys returns the keys associated with this SAM instance

func (*SAM) Lookup

func (sam *SAM) Lookup(name string) (i2pkeys.I2PAddr, error)

Lookup performs a name lookup

func (*SAM) NewDatagramSession

func (s *SAM) NewDatagramSession(id string, keys i2pkeys.I2PKeys, options []string, udpPort int) (*DatagramSession, error)

NewDatagramSession creates a new datagram session

func (*SAM) NewKeys

func (sam *SAM) NewKeys(sigType ...string) (i2pkeys.I2PKeys, error)

NewKeys creates the I2P-equivalent of an IP address

func (*SAM) NewPrimarySession

func (sam *SAM) NewPrimarySession(id string, keys i2pkeys.I2PKeys, options []string) (*PrimarySession, error)

NewPrimarySession creates a new PrimarySession

func (*SAM) NewPrimarySessionWithSignature

func (sam *SAM) NewPrimarySessionWithSignature(id string, keys i2pkeys.I2PKeys, options []string, sigType string) (*PrimarySession, error)

NewPrimarySessionWithSignature creates a new PrimarySession with signature

func (*SAM) NewRawSession

func (s *SAM) NewRawSession(id string, keys i2pkeys.I2PKeys, options []string, udpPort int) (*RawSession, error)

NewRawSession creates a new raw session

func (*SAM) NewStreamSession

func (sam *SAM) NewStreamSession(id string, keys i2pkeys.I2PKeys, options []string) (*StreamSession, error)

NewStreamSession creates a new StreamSession

func (*SAM) NewStreamSessionWithSignature

func (sam *SAM) NewStreamSessionWithSignature(id string, keys i2pkeys.I2PKeys, options []string, sigType string) (*StreamSession, error)

NewStreamSessionWithSignature creates a new StreamSession with custom signature

func (*SAM) NewStreamSessionWithSignatureAndPorts

func (sam *SAM) NewStreamSessionWithSignatureAndPorts(id, from, to string, keys i2pkeys.I2PKeys, options []string, sigType string) (*StreamSession, error)

NewStreamSessionWithSignatureAndPorts creates a new StreamSession with signature and ports

func (*SAM) ReadKeys

func (sam *SAM) ReadKeys(r io.Reader) (err error)

ReadKeys reads public/private keys from an io.Reader

type SAMConn

type SAMConn struct {
}

SAMConn implements net.Conn for I2P connections

func (*SAMConn) Close

func (sc *SAMConn) Close() error

Close closes the connection

func (*SAMConn) LocalAddr

func (sc *SAMConn) LocalAddr() net.Addr

LocalAddr returns the local address

func (*SAMConn) Read

func (sc *SAMConn) Read(buf []byte) (int, error)

Read reads data from the connection

func (*SAMConn) RemoteAddr

func (sc *SAMConn) RemoteAddr() net.Addr

RemoteAddr returns the remote address

func (*SAMConn) SetDeadline

func (sc *SAMConn) SetDeadline(t time.Time) error

SetDeadline sets read and write deadlines

func (*SAMConn) SetReadDeadline

func (sc *SAMConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets read deadline

func (*SAMConn) SetWriteDeadline

func (sc *SAMConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets write deadline

func (*SAMConn) Write

func (sc *SAMConn) Write(buf []byte) (int, error)

Write writes data to the connection

type SAMEmit

type SAMEmit struct {
	I2PConfig
}

SAMEmit handles SAM protocol message generation

func NewEmit

func NewEmit(opts ...func(*SAMEmit) error) (*SAMEmit, error)

NewEmit creates a new SAMEmit

func (*SAMEmit) Accept

func (e *SAMEmit) Accept() string

Accept generates accept message

func (*SAMEmit) AcceptBytes

func (e *SAMEmit) AcceptBytes() []byte

AcceptBytes generates accept message as bytes

func (*SAMEmit) Connect

func (e *SAMEmit) Connect(dest string) string

Connect generates connect message

func (*SAMEmit) ConnectBytes

func (e *SAMEmit) ConnectBytes(dest string) []byte

ConnectBytes generates connect message as bytes

func (*SAMEmit) Create

func (e *SAMEmit) Create() string

Create generates session create message

func (*SAMEmit) CreateBytes

func (e *SAMEmit) CreateBytes() []byte

CreateBytes generates session create message as bytes

func (*SAMEmit) GenerateDestination

func (e *SAMEmit) GenerateDestination() string

GenerateDestination generates destination message

func (*SAMEmit) GenerateDestinationBytes

func (e *SAMEmit) GenerateDestinationBytes() []byte

GenerateDestinationBytes generates destination message as bytes

func (*SAMEmit) Hello

func (e *SAMEmit) Hello() string

Hello generates hello message

func (*SAMEmit) HelloBytes

func (e *SAMEmit) HelloBytes() []byte

HelloBytes generates hello message as bytes

func (*SAMEmit) Lookup

func (e *SAMEmit) Lookup(name string) string

Lookup generates lookup message

func (*SAMEmit) LookupBytes

func (e *SAMEmit) LookupBytes(name string) []byte

LookupBytes generates lookup message as bytes

func (*SAMEmit) SamOptionsString

func (e *SAMEmit) SamOptionsString() string

SamOptionsString returns SAM options as string

type SAMResolver

type SAMResolver struct {
	*SAM
}

SAMResolver provides name resolution functionality

func NewFullSAMResolver

func NewFullSAMResolver(address string) (*SAMResolver, error)

NewFullSAMResolver creates a new full SAMResolver

func NewSAMResolver

func NewSAMResolver(parent *SAM) (*SAMResolver, error)

NewSAMResolver creates a new SAMResolver from existing SAM

func (*SAMResolver) Resolve

func (sam *SAMResolver) Resolve(name string) (i2pkeys.I2PAddr, error)

Resolve performs a lookup

type StreamListener

type StreamListener struct {
}

StreamListener implements net.Listener for I2P streams

func (*StreamListener) Accept

func (l *StreamListener) Accept() (net.Conn, error)

Accept accepts new inbound connections

func (*StreamListener) AcceptI2P

func (l *StreamListener) AcceptI2P() (*SAMConn, error)

AcceptI2P accepts a new inbound I2P connection

func (*StreamListener) Addr

func (l *StreamListener) Addr() net.Addr

Addr returns the listener's address

func (*StreamListener) Close

func (l *StreamListener) Close() error

Close closes the listener

func (*StreamListener) From

func (l *StreamListener) From() string

From returns the from port

func (*StreamListener) To

func (l *StreamListener) To() string

To returns the to port

type StreamSession

type StreamSession struct {
	Timeout  time.Duration
	Deadline time.Time
}

StreamSession represents a streaming session

func (*StreamSession) Addr

func (s *StreamSession) Addr() i2pkeys.I2PAddr

Addr returns the I2P destination address

func (*StreamSession) Close

func (s *StreamSession) Close() error

Close closes the session

func (*StreamSession) Dial

func (s *StreamSession) Dial(n, addr string) (c net.Conn, err error)

Dial establishes a connection to an address

func (*StreamSession) DialContext

func (s *StreamSession) DialContext(ctx context.Context, n, addr string) (net.Conn, error)

DialContext establishes a connection with context

func (*StreamSession) DialContextI2P

func (s *StreamSession) DialContextI2P(ctx context.Context, n, addr string) (*SAMConn, error)

DialContextI2P establishes an I2P connection with context

func (*StreamSession) DialI2P

func (s *StreamSession) DialI2P(addr i2pkeys.I2PAddr) (*SAMConn, error)

DialI2P dials to an I2P destination

func (*StreamSession) From

func (s *StreamSession) From() string

From returns the from port

func (*StreamSession) ID

func (s *StreamSession) ID() string

ID returns the local tunnel name

func (*StreamSession) Keys

func (s *StreamSession) Keys() i2pkeys.I2PKeys

Keys returns the keys associated with the session

func (*StreamSession) Listen

func (s *StreamSession) Listen() (*StreamListener, error)

Listen creates a new stream listener

func (*StreamSession) LocalAddr

func (s *StreamSession) LocalAddr() net.Addr

LocalAddr returns the local address

func (*StreamSession) Lookup

func (s *StreamSession) Lookup(name string) (i2pkeys.I2PAddr, error)

Lookup performs name lookup

func (*StreamSession) Read

func (s *StreamSession) Read(buf []byte) (int, error)

Read reads data from the stream

func (*StreamSession) RemoteAddr

func (s *StreamSession) RemoteAddr() net.Addr

RemoteAddr returns the remote address

func (*StreamSession) SetDeadline

func (s *StreamSession) SetDeadline(t time.Time) error

SetDeadline sets read and write deadlines

func (*StreamSession) SetReadDeadline

func (s *StreamSession) SetReadDeadline(t time.Time) error

SetReadDeadline sets read deadline

func (*StreamSession) SetWriteDeadline

func (s *StreamSession) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets write deadline

func (*StreamSession) SignatureType

func (s *StreamSession) SignatureType() string

SignatureType returns the signature type

func (*StreamSession) To

func (s *StreamSession) To() string

To returns the to port

func (*StreamSession) Write

func (s *StreamSession) Write(data []byte) (int, error)

Write sends data over the stream