diff --git a/common/config.go b/common/config.go index 7ed9987..c7d0a3d 100644 --- a/common/config.go +++ b/common/config.go @@ -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: diff --git a/common/const.go b/common/const.go index 33d25f7..1b65da3 100644 --- a/common/const.go +++ b/common/const.go @@ -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" +) diff --git a/common/emit-options.go b/common/emit-options.go index 973ad06..c65de70 100644 --- a/common/emit-options.go +++ b/common/emit-options.go @@ -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)") } } diff --git a/common/log.go b/common/log.go index a08b612..592513f 100644 --- a/common/log.go +++ b/common/log.go @@ -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() diff --git a/datagram/log.go b/datagram/log.go index a1ea712..41ba34d 100644 --- a/datagram/log.go +++ b/datagram/log.go @@ -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() diff --git a/log.go b/log.go index bb8616a..f6b3fe0 100644 --- a/log.go +++ b/log.go @@ -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 != "" { diff --git a/log/log.go b/logger/log.go similarity index 93% rename from log/log.go rename to logger/log.go index b186257..4df2c51 100644 --- a/log/log.go +++ b/logger/log.go @@ -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 != "" { diff --git a/primary.go b/primary.go index fcd0f99..5001a77 100644 --- a/primary.go +++ b/primary.go @@ -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 diff --git a/primary/log.go b/primary/log.go index ea6a3a0..c4cf94a 100644 --- a/primary/log.go +++ b/primary/log.go @@ -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() diff --git a/primary/stream.go b/primary/stream.go index dc35831..d455cbe 100644 --- a/primary/stream.go +++ b/primary/stream.go @@ -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 + } diff --git a/raw/log.go b/raw/log.go index cf55c3d..068cee8 100644 --- a/raw/log.go +++ b/raw/log.go @@ -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() diff --git a/sam3.go b/sam3.go index 860a5a0..6c7b125 100644 --- a/sam3.go +++ b/sam3.go @@ -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" diff --git a/stream.go b/stream.go index 58e7881..dd30506 100644 --- a/stream.go +++ b/stream.go @@ -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 -} diff --git a/stream/log.go b/stream/log.go index 01e5219..5f4e8c5 100644 --- a/stream/log.go +++ b/stream/log.go @@ -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()