fix conflicts

This commit is contained in:
eyedeekay
2025-05-26 23:30:26 -04:00
13 changed files with 131 additions and 108 deletions

View File

@@ -226,6 +226,9 @@ func (sam *SAM) NewGenericSessionWithSignatureAndPorts(style, id, from, to strin
// close this sam session // close this sam session
func (sam *SAM) Close() error { func (sam *SAM) Close() error {
if sam.Conn != nil {
log.Debug("Closing SAM session") log.Debug("Closing SAM session")
return sam.Conn.Close() return sam.Conn.Close()
}
return nil
} }

View File

@@ -237,9 +237,9 @@ func (f *I2PConfig) SignatureType() string {
// EncryptLease returns the lease set encryption configuration string // EncryptLease returns the lease set encryption configuration string
// Returns "i2cp.encryptLeaseSet=true" if encryption is enabled, empty string otherwise // Returns "i2cp.encryptLeaseSet=true" if encryption is enabled, empty string otherwise
func (f *I2PConfig) EncryptLease() string { func (f *I2PConfig) EncryptLease() string {
if f.EncryptLeaseSet == true { if f.EncryptLeaseSet {
log.Debug("Lease set encryption enabled") log.Debug("Lease set encryption enabled")
return fmt.Sprintf(" i2cp.encryptLeaseSet=true ") return " i2cp.encryptLeaseSet=true "
} }
log.Debug("Lease set encryption not enabled") log.Debug("Lease set encryption not enabled")
return "" return ""
@@ -262,7 +262,7 @@ func (f *I2PConfig) Reliability() string {
// Reduce returns I2CP reduce-on-idle configuration settings as a string if enabled // Reduce returns I2CP reduce-on-idle configuration settings as a string if enabled
func (f *I2PConfig) Reduce() string { func (f *I2PConfig) Reduce() string {
// If reduce idle is enabled, return formatted configuration string // If reduce idle is enabled, return formatted configuration string
if f.ReduceIdle == true { if f.ReduceIdle {
// Log the reduce idle settings being applied // Log the reduce idle settings being applied
log.WithFields(logrus.Fields{ log.WithFields(logrus.Fields{
"reduceIdle": f.ReduceIdle, "reduceIdle": f.ReduceIdle,
@@ -287,7 +287,7 @@ func (f *I2PConfig) Reduce() string {
// Close returns I2CP close-on-idle configuration settings as a string if enabled // Close returns I2CP close-on-idle configuration settings as a string if enabled
func (f *I2PConfig) Close() string { func (f *I2PConfig) Close() string {
// If close idle is enabled, return formatted configuration string // If close idle is enabled, return formatted configuration string
if f.CloseIdle == true { if f.CloseIdle {
// Log the close idle settings being applied // Log the close idle settings being applied
log.WithFields(logrus.Fields{ log.WithFields(logrus.Fields{
"closeIdle": f.CloseIdle, "closeIdle": f.CloseIdle,
@@ -312,17 +312,17 @@ func (f *I2PConfig) DoZero() string {
var settings []string var settings []string
// Add inbound zero hop setting if enabled // Add inbound zero hop setting if enabled
if f.InAllowZeroHop == true { if f.InAllowZeroHop {
settings = append(settings, fmt.Sprintf("inbound.allowZeroHop=%t", f.InAllowZeroHop)) settings = append(settings, fmt.Sprintf("inbound.allowZeroHop=%t", f.InAllowZeroHop))
} }
// Add outbound zero hop setting if enabled // Add outbound zero hop setting if enabled
if f.OutAllowZeroHop == true { if f.OutAllowZeroHop {
settings = append(settings, fmt.Sprintf("outbound.allowZeroHop=%t", f.OutAllowZeroHop)) settings = append(settings, fmt.Sprintf("outbound.allowZeroHop=%t", f.OutAllowZeroHop))
} }
// Add fast receive setting if enabled // Add fast receive setting if enabled
if f.FastRecieve == true { if f.FastRecieve {
settings = append(settings, fmt.Sprintf("i2cp.fastRecieve=%t", f.FastRecieve)) settings = append(settings, fmt.Sprintf("i2cp.fastRecieve=%t", f.FastRecieve))
} }
@@ -404,13 +404,13 @@ func (f *I2PConfig) Print() []string {
// Accesslisttype returns the I2CP access list configuration string based on the AccessListType setting // Accesslisttype returns the I2CP access list configuration string based on the AccessListType setting
func (f *I2PConfig) Accesslisttype() string { func (f *I2PConfig) Accesslisttype() string {
switch f.AccessListType { switch f.AccessListType {
case "whitelist": case ACCESS_TYPE_WHITELIST:
log.Debug("Access list type set to whitelist") log.Debug("Access list type set to whitelist")
return fmt.Sprintf("i2cp.enableAccessList=true") return "i2cp.enableAccessList=true"
case "blacklist": case ACCESS_TYPE_BLACKLIST:
log.Debug("Access list type set to blacklist") log.Debug("Access list type set to blacklist")
return fmt.Sprintf("i2cp.enableBlackList=true") return "i2cp.enableBlackList=true"
case "none": case ACCESS_TYPE_NONE:
log.Debug("Access list type set to none") log.Debug("Access list type set to none")
return "" return ""
default: default:
@@ -469,7 +469,7 @@ func NewConfig(opts ...func(*I2PConfig) error) (*I2PConfig, error) {
config.SamMax = DEFAULT_SAM_MAX config.SamMax = DEFAULT_SAM_MAX
config.TunName = "" config.TunName = ""
config.TunType = "server" config.TunType = "server"
config.Style = "STREAM" config.Style = SESSION_STYLE_STREAM
config.InLength = 3 config.InLength = 3
config.OutLength = 3 config.OutLength = 3
config.InQuantity = 2 config.InQuantity = 2

View File

@@ -32,3 +32,15 @@ const (
HELLO_REPLY_OK = "HELLO REPLY RESULT=OK" HELLO_REPLY_OK = "HELLO REPLY RESULT=OK"
HELLO_REPLY_NOVERSION = "HELLO REPLY RESULT=NOVERSION\n" HELLO_REPLY_NOVERSION = "HELLO REPLY RESULT=NOVERSION\n"
) )
const (
SESSION_STYLE_STREAM = "STREAM"
SESSION_STYLE_DATAGRAM = "DATAGRAM"
SESSION_STYLE_RAW = "RAW"
)
const (
ACCESS_TYPE_WHITELIST = "whitelist"
ACCESS_TYPE_BLACKLIST = "blacklist"
ACCESS_TYPE_NONE = "none"
)

View File

@@ -14,15 +14,9 @@ type Option func(*SAMEmit) error
// SetType sets the type of the forwarder server // SetType sets the type of the forwarder server
func SetType(s string) func(*SAMEmit) error { func SetType(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error { return func(c *SAMEmit) error {
if s == "STREAM" { if s == SESSION_STYLE_STREAM ||
c.Style = s s == SESSION_STYLE_DATAGRAM ||
log.WithField("style", s).Debug("Set session style") s == SESSION_STYLE_RAW {
return nil
} else if s == "DATAGRAM" {
c.Style = s
log.WithField("style", s).Debug("Set session style")
return nil
} else if s == "RAW" {
c.Style = s c.Style = s
log.WithField("style", s).Debug("Set session style") log.WithField("style", s).Debug("Set session style")
return nil return nil
@@ -399,24 +393,20 @@ func SetCloseIdleTimeMs(u int) func(*SAMEmit) error {
// SetAccessListType tells the system to treat the AccessList as a whitelist // SetAccessListType tells the system to treat the AccessList as a whitelist
func SetAccessListType(s string) func(*SAMEmit) error { func SetAccessListType(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error { return func(c *SAMEmit) error {
if s == "whitelist" { if s == ACCESS_TYPE_WHITELIST {
c.I2PConfig.AccessListType = "whitelist" c.I2PConfig.AccessListType = ACCESS_TYPE_WHITELIST
log.Debug("Set access list type to whitelist") log.Debug("Set access list type to whitelist")
return nil return nil
} else if s == "blacklist" { } else if s == ACCESS_TYPE_BLACKLIST {
c.I2PConfig.AccessListType = "blacklist" c.I2PConfig.AccessListType = ACCESS_TYPE_BLACKLIST
log.Debug("Set access list type to blacklist") log.Debug("Set access list type to blacklist")
return nil return nil
} else if s == "none" { } else if s == ACCESS_TYPE_NONE || s == "" {
c.I2PConfig.AccessListType = ""
log.Debug("Set access list type to none")
return nil
} else if s == "" {
c.I2PConfig.AccessListType = "" c.I2PConfig.AccessListType = ""
log.Debug("Set access list type to none") log.Debug("Set access list type to none")
return nil return nil
} }
return fmt.Errorf("Invalid Access list type(whitelist, blacklist, none)") return fmt.Errorf("Invalid Access list type (whitelist, blacklist, none)")
} }
} }

View File

@@ -1,6 +1,7 @@
package common package common
import ( import (
"fmt"
"math/rand" "math/rand"
"net" "net"
"strconv" "strconv"
@@ -76,8 +77,9 @@ var (
randGen = rand.New(randSource) randGen = rand.New(randSource)
) )
func RandPort() string { func RandPort() (portNumber string, err error) {
for { maxAttempts := 30
for range maxAttempts {
p := randGen.Intn(55534) + 10000 p := randGen.Intn(55534) + 10000
port := strconv.Itoa(p) port := strconv.Itoa(p)
if l, e := net.Listen("tcp", net.JoinHostPort("localhost", port)); e != nil { if l, e := net.Listen("tcp", net.JoinHostPort("localhost", port)); e != nil {
@@ -88,8 +90,10 @@ func RandPort() string {
continue continue
} else { } else {
defer l.Close() defer l.Close()
return strconv.Itoa(l.Addr().(*net.UDPAddr).Port) return strconv.Itoa(l.Addr().(*net.UDPAddr).Port), nil
} }
} }
} }
return "", fmt.Errorf("unable to find a pair of available tcp and udp ports in %v attempts", maxAttempts)
} }

View File

@@ -16,11 +16,11 @@ func NewConfig(opts ...func(*I2PConfig) error) (*I2PConfig, error) {
var config I2PConfig var config I2PConfig
config.SamHost = "127.0.0.1" config.SamHost = "127.0.0.1"
config.SamPort = 7656 config.SamPort = 7656
config.SamMin = "3.0" config.SamMin = common.DEFAULT_SAM_MIN
config.SamMax = "3.2" config.SamMax = common.DEFAULT_SAM_MAX
config.TunName = "" config.TunName = ""
config.TunType = "server" config.TunType = "server"
config.Style = "STREAM" config.Style = common.SESSION_STYLE_STREAM
config.InLength = 3 config.InLength = 3
config.OutLength = 3 config.OutLength = 3
config.InQuantity = 2 config.InQuantity = 2

61
log.go
View File

@@ -1,7 +1,68 @@
package sam3 package sam3
<<<<<<< HEAD
import ( import (
"github.com/go-i2p/logger" "github.com/go-i2p/logger"
) )
var log = logger.GetGoI2PLogger() var log = logger.GetGoI2PLogger()
||||||| a681ab3
import (
"io/ioutil"
"os"
"strings"
"sync"
"github.com/sirupsen/logrus"
)
var (
log *logrus.Logger
once sync.Once
)
func InitializeSAM3Logger() {
once.Do(func() {
log = logrus.New()
// We do not want to log by default
log.SetOutput(ioutil.Discard)
log.SetLevel(logrus.PanicLevel)
// Check if DEBUG_I2P is set
if logLevel := os.Getenv("DEBUG_I2P"); logLevel != "" {
log.SetOutput(os.Stdout)
switch strings.ToLower(logLevel) {
case "debug":
log.SetLevel(logrus.DebugLevel)
case "warn":
log.SetLevel(logrus.WarnLevel)
case "error":
log.SetLevel(logrus.ErrorLevel)
default:
log.SetLevel(logrus.DebugLevel)
}
log.WithField("level", log.GetLevel()).Debug("Logging enabled.")
}
})
}
// GetSAM3Logger returns the initialized logger
func GetSAM3Logger() *logrus.Logger {
if log == nil {
InitializeSAM3Logger()
}
return log
}
func init() {
InitializeSAM3Logger()
}
=======
import "github.com/go-i2p/go-sam-go/logger"
var log = logger.GetSAM3Logger()
func init() {
logger.InitializeSAM3Logger()
log = logger.GetSAM3Logger()
}
>>>>>>> 11110ed5a2ac9ad702c0278886c0418c8cd95a16

View File

@@ -1 +0,0 @@
package log

View File

@@ -2,10 +2,6 @@
package sam3 package sam3
import ( import (
"math/rand"
"strconv"
"time"
"github.com/go-i2p/go-sam-go/primary" "github.com/go-i2p/go-sam-go/primary"
) )
@@ -13,15 +9,6 @@ const (
session_ADDOK = "SESSION STATUS RESULT=OK" session_ADDOK = "SESSION STATUS RESULT=OK"
) )
func randport() string {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
p := r.Intn(55534) + 10000
port := strconv.Itoa(p)
log.WithField("port", port).Debug("Generated random port")
return strconv.Itoa(p)
}
// Represents a primary session. // Represents a primary session.
type PrimarySession struct { type PrimarySession struct {
*primary.PrimarySession *primary.PrimarySession

View File

@@ -1,6 +1,8 @@
package primary package primary
import ( import (
"net"
"github.com/go-i2p/go-sam-go/common" "github.com/go-i2p/go-sam-go/common"
"github.com/go-i2p/go-sam-go/stream" "github.com/go-i2p/go-sam-go/stream"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -15,11 +17,7 @@ func (sam *PrimarySession) NewStreamSubSession(id string) (*stream.StreamSession
log.WithError(err).Error("Failed to create new generic sub-session") log.WithError(err).Error("Failed to create new generic sub-session")
return nil, err return nil, err
} }
streamSession := &stream.StreamSession{ return newFromPrimary(sam, conn), nil
SAM: (*stream.SAM)(sam.SAM),
}
streamSession.Conn = conn
return streamSession, nil
} }
// Creates a new stream.StreamSession with the I2CP- and streaminglib options as // Creates a new stream.StreamSession with the I2CP- and streaminglib options as
@@ -31,13 +29,7 @@ func (sam *PrimarySession) NewUniqueStreamSubSession(id string) (*stream.StreamS
log.WithError(err).Error("Failed to create new generic sub-session") log.WithError(err).Error("Failed to create new generic sub-session")
return nil, err return nil, err
} }
fromPort, toPort := common.RandPort(), common.RandPort() return newFromPrimary(sam, conn), nil
log.WithFields(logrus.Fields{"fromPort": fromPort, "toPort": toPort}).Debug("Generated random ports")
streamSession := &stream.StreamSession{
SAM: (*stream.SAM)(sam.SAM),
}
streamSession.Conn = conn
return streamSession, nil
} }
// Creates a new stream.StreamSession with the I2CP- and streaminglib options as // Creates a new stream.StreamSession with the I2CP- and streaminglib options as
@@ -49,9 +41,17 @@ func (sam *PrimarySession) NewStreamSubSessionWithPorts(id, from, to string) (*s
log.WithError(err).Error("Failed to create new generic sub-session with signature and ports") log.WithError(err).Error("Failed to create new generic sub-session with signature and ports")
return nil, err return nil, err
} }
return newFromPrimary(sam, conn), nil
}
func newFromPrimary(sam *PrimarySession, conn net.Conn) *stream.StreamSession {
streamSession := &stream.StreamSession{ streamSession := &stream.StreamSession{
SAM: (*stream.SAM)(sam.SAM), SAM: &stream.SAM{
SAM: (*common.SAM)(sam.SAM),
},
} }
streamSession.Conn = conn streamSession.Conn = conn
return streamSession, nil
return streamSession
} }

View File

@@ -1,10 +1,7 @@
package raw package raw
import logger "github.com/go-i2p/go-sam-go/log" import (
"github.com/go-i2p/logger"
)
var log = logger.GetSAM3Logger() var log = logger.GetGoI2PLogger()
func init() {
logger.InitializeSAM3Logger()
log = logger.GetSAM3Logger()
}

24
sam3.go
View File

@@ -17,11 +17,11 @@ type SAM struct {
} }
// Creates a new stream session by wrapping stream.NewStreamSession // Creates a new stream session by wrapping stream.NewStreamSession
func (s *SAM) NewStreamSession(param1 string, keys i2pkeys.I2PKeys, param3 []string) (*StreamSession, error) { func (s *SAM) NewStreamSession(id string, keys i2pkeys.I2PKeys, options []string) (*StreamSession, error) {
sam := &stream.SAM{ sam := &stream.SAM{
SAM: s.SAM, SAM: s.SAM,
} }
ss, err := sam.NewStreamSession(param1, keys, param3) ss, err := sam.NewStreamSession(id, keys, options)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -56,26 +56,8 @@ func (s *SAM) NewPrimarySession(id string, keys i2pkeys.I2PKeys, options []strin
return &primarySession, nil return &primarySession, nil
} }
const (
session_OK = "SESSION STATUS RESULT=OK DESTINATION="
session_DUPLICATE_ID = "SESSION STATUS RESULT=DUPLICATED_ID\n"
session_DUPLICATE_DEST = "SESSION STATUS RESULT=DUPLICATED_DEST\n"
session_INVALID_KEY = "SESSION STATUS RESULT=INVALID_KEY\n"
session_I2P_ERROR = "SESSION STATUS RESULT=I2P_ERROR MESSAGE="
)
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"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandString() string { func RandString() string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
n := 4 n := 4
b := make([]rune, n) b := make([]rune, n)
for i := range b { for i := range b {

View File

@@ -2,8 +2,6 @@
package sam3 package sam3
import ( import (
"time"
"github.com/go-i2p/go-sam-go/stream" "github.com/go-i2p/go-sam-go/stream"
) )
@@ -18,13 +16,3 @@ func (s *StreamSession) Cancel() chan *StreamSession {
ch <- s ch <- s
return ch return ch
}*/ }*/
func minNonzeroTime(a, b time.Time) time.Time {
if a.IsZero() {
return b
}
if b.IsZero() || a.Before(b) {
return a
}
return b
}