Files
sam3/emit.go

120 lines
2.7 KiB
Go
Raw Normal View History

2019-12-07 22:06:50 -05:00
package sam3
import (
"fmt"
"net"
"strings"
"github.com/sirupsen/logrus"
2019-12-07 22:06:50 -05:00
)
type SAMEmit struct {
2019-12-07 22:10:55 -05:00
I2PConfig
2019-12-07 22:06:50 -05:00
}
func (e *SAMEmit) OptStr() string {
2024-01-07 12:09:13 -05:00
optStr := strings.Join(e.I2PConfig.Print(), " ")
2024-10-15 12:54:17 -04:00
log.WithField("optStr", optStr).Debug("Generated option string")
2019-12-07 22:06:50 -05:00
return optStr
}
func (e *SAMEmit) Hello() string {
2024-10-15 12:54:17 -04:00
hello := fmt.Sprintf("HELLO VERSION MIN=%s MAX=%s \n", e.I2PConfig.MinSAM(), e.I2PConfig.MaxSAM())
log.WithField("hello", hello).Debug("Generated HELLO command")
return hello
2019-12-07 22:06:50 -05:00
}
func (e *SAMEmit) HelloBytes() []byte {
return []byte(e.Hello())
}
func (e *SAMEmit) GenerateDestination() string {
2024-10-15 12:54:17 -04:00
dest := fmt.Sprintf("DEST GENERATE %s \n", e.I2PConfig.SignatureType())
log.WithField("destination", dest).Debug("Generated DEST GENERATE command")
return dest
2019-12-07 22:06:50 -05:00
}
func (e *SAMEmit) GenerateDestinationBytes() []byte {
return []byte(e.GenerateDestination())
}
func (e *SAMEmit) Lookup(name string) string {
2024-10-15 12:54:17 -04:00
lookup := fmt.Sprintf("NAMING LOOKUP NAME=%s \n", name)
log.WithField("lookup", lookup).Debug("Generated NAMING LOOKUP command")
return lookup
2019-12-07 22:06:50 -05:00
}
func (e *SAMEmit) LookupBytes(name string) []byte {
return []byte(e.Lookup(name))
}
func (e *SAMEmit) Connect(dest string) string {
2024-10-15 12:54:17 -04:00
connect := fmt.Sprintf(
2019-12-07 22:06:50 -05:00
"STREAM CONNECT ID=%s %s %s DESTINATION=%s \n",
e.I2PConfig.ID(),
e.I2PConfig.FromPort(),
e.I2PConfig.ToPort(),
dest,
)
2024-10-15 12:54:17 -04:00
log.WithField("connect", connect).Debug("Generated STREAM CONNECT command")
return connect
2019-12-07 22:06:50 -05:00
}
func (e *SAMEmit) ConnectBytes(dest string) []byte {
return []byte(e.Connect(dest))
}
func (e *SAMEmit) Accept() string {
2024-10-15 12:54:17 -04:00
accept := fmt.Sprintf(
"STREAM ACCEPT ID=%s %s %s",
2019-12-07 22:06:50 -05:00
e.I2PConfig.ID(),
e.I2PConfig.FromPort(),
e.I2PConfig.ToPort(),
)
2024-10-15 12:54:17 -04:00
log.WithField("accept", accept).Debug("Generated STREAM ACCEPT command")
return accept
2019-12-07 22:06:50 -05:00
}
func (e *SAMEmit) AcceptBytes() []byte {
return []byte(e.Accept())
}
func NewEmit(opts ...func(*SAMEmit) error) (*SAMEmit, error) {
var emit SAMEmit
for _, o := range opts {
if err := o(&emit); err != nil {
2024-10-15 12:54:17 -04:00
log.WithError(err).Error("Failed to apply option")
2019-12-07 22:06:50 -05:00
return nil, err
}
}
2024-10-15 12:54:17 -04:00
log.Debug("New SAMEmit instance created")
2019-12-07 22:06:50 -05:00
return &emit, nil
}
func IgnorePortError(err error) error {
if err == nil {
return nil
}
if strings.Contains(err.Error(), "missing port in address") {
2024-10-15 12:54:17 -04:00
log.Debug("Ignoring 'missing port in address' error")
err = nil
}
return err
}
func SplitHostPort(hostport string) (string, string, error) {
host, port, err := net.SplitHostPort(hostport)
if err != nil {
if IgnorePortError(err) == nil {
2024-10-15 12:54:17 -04:00
log.WithField("host", hostport).Debug("Using full string as host, port set to 0")
host = hostport
port = "0"
}
}
2024-10-15 12:54:17 -04:00
log.WithFields(logrus.Fields{
"host": host,
"port": port,
}).Debug("Split host and port")
return host, port, nil
}