Merge pull request #1 from urgentquest/refactor-clean-up

Clean up and fixes
This commit is contained in:
idk
2025-03-21 13:47:00 -04:00
committed by GitHub
14 changed files with 48 additions and 75 deletions

View File

@ -404,13 +404,13 @@ func (f *I2PConfig) Print() []string {
// Accesslisttype returns the I2CP access list configuration string based on the AccessListType setting
func (f *I2PConfig) Accesslisttype() string {
switch f.AccessListType {
case "whitelist":
case ACCESS_TYPE_WHITELIST:
log.Debug("Access list type set to whitelist")
return fmt.Sprintf("i2cp.enableAccessList=true")
case "blacklist":
case ACCESS_TYPE_BLACKLIST:
log.Debug("Access list type set to blacklist")
return fmt.Sprintf("i2cp.enableBlackList=true")
case "none":
case ACCESS_TYPE_NONE:
log.Debug("Access list type set to none")
return ""
default:

View File

@ -32,3 +32,15 @@ const (
HELLO_REPLY_OK = "HELLO REPLY RESULT=OK"
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,15 @@ type Option func(*SAMEmit) error
// SetType sets the type of the forwarder server
func SetType(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if s == "STREAM" {
if s == SESSION_STYLE_STREAM {
c.Style = s
log.WithField("style", s).Debug("Set session style")
return nil
} else if s == "DATAGRAM" {
} else if s == SESSION_STYLE_DATAGRAM {
c.Style = s
log.WithField("style", s).Debug("Set session style")
return nil
} else if s == "RAW" {
} else if s == SESSION_STYLE_RAW {
c.Style = s
log.WithField("style", s).Debug("Set session style")
return nil
@ -399,24 +399,20 @@ func SetCloseIdleTimeMs(u int) func(*SAMEmit) error {
// SetAccessListType tells the system to treat the AccessList as a whitelist
func SetAccessListType(s string) func(*SAMEmit) error {
return func(c *SAMEmit) error {
if s == "whitelist" {
c.I2PConfig.AccessListType = "whitelist"
if s == ACCESS_TYPE_WHITELIST {
c.I2PConfig.AccessListType = ACCESS_TYPE_WHITELIST
log.Debug("Set access list type to whitelist")
return nil
} else if s == "blacklist" {
c.I2PConfig.AccessListType = "blacklist"
} else if s == ACCESS_TYPE_BLACKLIST {
c.I2PConfig.AccessListType = ACCESS_TYPE_BLACKLIST
log.Debug("Set access list type to blacklist")
return nil
} else if s == "none" {
c.I2PConfig.AccessListType = ""
log.Debug("Set access list type to none")
return nil
} else if s == "" {
} else if s == ACCESS_TYPE_NONE || s == "" {
c.I2PConfig.AccessListType = ""
log.Debug("Set access list type to none")
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,6 @@
package common
import logger "github.com/go-i2p/go-sam-go/log"
import logger "github.com/go-i2p/go-sam-go/logger"
var log = logger.GetSAM3Logger()

View File

@ -1,6 +1,6 @@
package datagram
import logger "github.com/go-i2p/go-sam-go/log"
import logger "github.com/go-i2p/go-sam-go/logger"
var log = logger.GetSAM3Logger()

4
log.go
View File

@ -2,7 +2,7 @@
package sam3
import (
"io/ioutil"
"io"
"os"
"strings"
"sync"
@ -19,7 +19,7 @@ func InitializeSAM3Logger() {
once.Do(func() {
log = logrus.New()
// We do not want to log by default
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
log.SetLevel(logrus.PanicLevel)
// Check if DEBUG_I2P is set
if logLevel := os.Getenv("DEBUG_I2P"); logLevel != "" {

View File

@ -1,7 +1,7 @@
package log
package logger
import (
"io/ioutil"
"io"
"os"
"strings"
"sync"
@ -18,7 +18,7 @@ func InitializeSAM3Logger() {
once.Do(func() {
log = logrus.New()
// We do not want to log by default
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
log.SetLevel(logrus.PanicLevel)
// Check if DEBUG_I2P is set
if logLevel := os.Getenv("DEBUG_I2P"); logLevel != "" {

View File

@ -2,10 +2,6 @@
package sam3
import (
"math/rand"
"strconv"
"time"
"github.com/go-i2p/go-sam-go/primary"
)
@ -13,15 +9,6 @@ const (
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.
type PrimarySession struct {
*primary.PrimarySession

View File

@ -1,6 +1,6 @@
package primary
import logger "github.com/go-i2p/go-sam-go/log"
import logger "github.com/go-i2p/go-sam-go/logger"
var log = logger.GetSAM3Logger()

View File

@ -1,6 +1,8 @@
package primary
import (
"net"
"github.com/go-i2p/go-sam-go/common"
"github.com/go-i2p/go-sam-go/stream"
"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")
return nil, err
}
streamSession := &stream.StreamSession{
SAM: (*stream.SAM)(sam.SAM),
}
streamSession.Conn = conn
return streamSession, nil
return newFromPrimary(sam, conn), nil
}
// Creates a new stream.StreamSession with the I2CP- and streaminglib options as
@ -33,11 +31,7 @@ func (sam *PrimarySession) NewUniqueStreamSubSession(id string) (*stream.StreamS
}
fromPort, toPort := common.RandPort(), common.RandPort()
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
return newFromPrimary(sam, conn), nil
}
// Creates a new stream.StreamSession with the I2CP- and streaminglib options as
@ -49,9 +43,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")
return nil, err
}
return newFromPrimary(sam, conn), nil
}
func newFromPrimary(sam *PrimarySession, conn net.Conn) *stream.StreamSession {
streamSession := &stream.StreamSession{
SAM: (*stream.SAM)(sam.SAM),
SAM: &stream.SAM{
SAM: (*common.SAM)(sam.SAM),
},
}
streamSession.Conn = conn
return streamSession, nil
return streamSession
}

View File

@ -1,6 +1,6 @@
package raw
import logger "github.com/go-i2p/go-sam-go/log"
import logger "github.com/go-i2p/go-sam-go/logger"
var log = logger.GetSAM3Logger()

12
sam3.go
View File

@ -11,10 +11,6 @@ import (
"github.com/go-i2p/i2pkeys"
)
func init() {
InitializeSAM3Logger()
}
// Used for controlling I2Ps SAMv3.
type SAM struct {
*common.SAM
@ -60,14 +56,6 @@ func (s *SAM) NewPrimarySession(id string, keys i2pkeys.I2PKeys, options []strin
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"

View File

@ -2,8 +2,6 @@
package sam3
import (
"time"
"github.com/go-i2p/go-sam-go/stream"
)
@ -18,13 +16,3 @@ func (s *StreamSession) Cancel() chan *StreamSession {
ch <- s
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
}

View File

@ -1,6 +1,6 @@
package stream
import logger "github.com/go-i2p/go-sam-go/log"
import logger "github.com/go-i2p/go-sam-go/logger"
var log = logger.GetSAM3Logger()