From 45106d2b7062a690dfad30841163b510855469df Mon Sep 17 00:00:00 2001 From: idk Date: Mon, 10 Jan 2022 11:19:39 -0500 Subject: [PATCH] fix semver. Check router type to determine whether to send PRIMARY or MASTER to SAM session --- Makefile | 2 +- config.go | 2 +- datagram.go | 4 ++++ datagram_test.go | 2 -- emit.go | 11 +++++++++++ primary.go | 16 +++++++++++----- primary_datagram_test.go | 2 -- primary_stream_test.go | 27 +++++---------------------- sam3_test.go | 2 -- stream.go | 2 +- stream_test.go | 2 -- suggestedOptions.go | 17 +++++++++++++++++ 12 files changed, 51 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index fbdafca..5840bd8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ USER_GH=eyedeekay -VERSION=0.33.01 +VERSION=0.33.1 packagename=sam3 echo: diff --git a/config.go b/config.go index 1a1c505..0a7a887 100644 --- a/config.go +++ b/config.go @@ -347,7 +347,7 @@ func (cfg *Config) DatagramSession() (session *DatagramSession, err error) { // determine udp port var portstr string _, portstr, err = net.SplitHostPort(cfg.Addr) - if err == nil { + if IgnorePortError(err) == nil { var port int port, err = strconv.Atoi(portstr) if err == nil && port > 0 { diff --git a/datagram.go b/datagram.go index 005cc8c..90262d1 100644 --- a/datagram.go +++ b/datagram.go @@ -56,6 +56,10 @@ func (s *SAM) NewDatagramSession(id string, keys i2pkeys.I2PKeys, options []stri return nil, err } _, lport, err := net.SplitHostPort(udpconn.LocalAddr().String()) + if err != nil { + s.Close() + return nil, err + } conn, err := s.newGenericSession("DATAGRAM", id, keys, options, []string{"PORT=" + lport}) if err != nil { return nil, err diff --git a/datagram_test.go b/datagram_test.go index 74cafe7..4455642 100644 --- a/datagram_test.go +++ b/datagram_test.go @@ -1,5 +1,3 @@ -// +build nettest - package sam3 import ( diff --git a/emit.go b/emit.go index 3ec16cc..6f44403 100644 --- a/emit.go +++ b/emit.go @@ -3,6 +3,7 @@ package sam3 import ( "fmt" "log" + "strings" ) type SAMEmit struct { @@ -96,3 +97,13 @@ func NewEmit(opts ...func(*SAMEmit) error) (*SAMEmit, error) { } return &emit, nil } + +func IgnorePortError(err error) error { + if err == nil { + return nil + } + if strings.Contains(err.Error(), "missing port in address") { + err = nil + } + return err +} diff --git a/primary.go b/primary.go index 17cc28a..96e5299 100644 --- a/primary.go +++ b/primary.go @@ -137,9 +137,7 @@ func (sam *PrimarySession) DialUDPI2P(network, laddr, raddr string) (*DatagramSe func (s *PrimarySession) Lookup(name string) (a net.Addr, err error) { var sam *SAM - if len(strings.Split(name, ":")) <= 1 { - name += ":0" - } + name = strings.Split(name, ":")[0] sam, err = NewSAM(s.samAddr) if err == nil { defer sam.Close() @@ -163,7 +161,7 @@ func (sam *PrimarySession) ResolveUDPAddr(network, dest string) (net.Addr, error // Creates a new PrimarySession with the I2CP- and streaminglib options as // specified. See the I2P documentation for a full list of options. func (sam *SAM) NewPrimarySession(id string, keys i2pkeys.I2PKeys, options []string) (*PrimarySession, error) { - conn, err := sam.newGenericSession("PRIMARY", id, keys, options, []string{}) + conn, err := sam.newGenericSession(PrimarySessionSwitch, id, keys, options, []string{}) if err != nil { return nil, err } @@ -175,7 +173,7 @@ func (sam *SAM) NewPrimarySession(id string, keys i2pkeys.I2PKeys, options []str // Creates a new PrimarySession with the I2CP- and PRIMARYinglib options as // specified. See the I2P documentation for a full list of options. func (sam *SAM) NewPrimarySessionWithSignature(id string, keys i2pkeys.I2PKeys, options []string, sigType string) (*PrimarySession, error) { - conn, err := sam.newGenericSessionWithSignature("PRIMARY", id, keys, sigType, options, []string{}) + conn, err := sam.newGenericSessionWithSignature(PrimarySessionSwitch, id, keys, sigType, options, []string{}) if err != nil { return nil, err } @@ -329,6 +327,10 @@ func (s *PrimarySession) NewDatagramSubSession(id string, udpPort int) (*Datagra return nil, err } _, lport, err := net.SplitHostPort(udpconn.LocalAddr().String()) + if err != nil { + s.Close() + return nil, err + } conn, err := s.newGenericSubSession("DATAGRAM", id, []string{"PORT=" + lport}) if err != nil { return nil, err @@ -368,6 +370,10 @@ func (s *PrimarySession) NewRawSubSession(id string, udpPort int) (*RawSession, return nil, err } _, lport, err := net.SplitHostPort(udpconn.LocalAddr().String()) + if err != nil { + s.Close() + return nil, err + } // conn, err := s.newGenericSubSession("RAW", id, s.keys, options, []string{"PORT=" + lport}) conn, err := s.newGenericSubSession("RAW", id, []string{"PORT=" + lport}) if err != nil { diff --git a/primary_datagram_test.go b/primary_datagram_test.go index 35c2e75..6bd1ec2 100644 --- a/primary_datagram_test.go +++ b/primary_datagram_test.go @@ -1,5 +1,3 @@ -// +build nettest - package sam3 import ( diff --git a/primary_stream_test.go b/primary_stream_test.go index 64373ac..bc53e4a 100644 --- a/primary_stream_test.go +++ b/primary_stream_test.go @@ -1,5 +1,3 @@ -// +build nettest - package sam3 import ( @@ -99,7 +97,7 @@ func Test_PrimaryStreamingServerClient(t *testing.T) { } defer sam.Close() fmt.Println("\tServer: Creating tunnel") - ss, err := sam.NewUniqueStreamSubSession("primaryExampleServerTun") + ss, err := sam.NewUniqueStreamSubSession("PrimaryServerClientTunnel") if err != nil { return } @@ -184,26 +182,13 @@ func ExamplePrimaryStreamSession() { return } - sam, err := earlysam.NewPrimarySession("PrimaryServerClientTunnel", keys, []string{"inbound.length=0", "outbound.length=0", "inbound.lengthVariance=0", "outbound.lengthVariance=0", "inbound.quantity=1", "outbound.quantity=1"}) + sam, err := earlysam.NewPrimarySession("PrimaryStreamSessionTunnel", keys, []string{"inbound.length=0", "outbound.length=0", "inbound.lengthVariance=0", "outbound.lengthVariance=0", "inbound.quantity=1", "outbound.quantity=1"}) if err != nil { log.Fatal(err.Error()) return } defer sam.Close() - // See the example Option_* variables. - ss, err := sam.NewStreamSubSession("stream_example") - if err != nil { - fmt.Println(err.Error()) - return - } - ss.Close() - someone, err := earlysam.Lookup("idk.i2p") - if err != nil { - fmt.Println(err.Error()) - return - } - - conn, err := ss.DialI2P(someone) + conn, err := sam.Dial("tcp", "idk.i2p") //someone.Base32()) if err != nil { fmt.Println(err.Error()) return @@ -223,8 +208,6 @@ func ExamplePrimaryStreamSession() { fmt.Println("Read HTTP/HTML from idk.i2p") log.Println("Read HTTP/HTML from idk.i2p") } - return - // Output: //Sending HTTP GET / //Read HTTP/HTML from idk.i2p @@ -260,7 +243,7 @@ func ExamplePrimaryStreamListener() { // Client connecting to the server go func(server i2pkeys.I2PAddr) { - cs, err := sam.NewStreamSubSession("client_example") + cs, err := sam.NewUniqueStreamSubSession("PrimaryListenerTunnel") if err != nil { fmt.Println(err.Error()) quit <- false @@ -284,7 +267,7 @@ func ExamplePrimaryStreamListener() { quit <- true }(keys.Addr()) // end of client - ss, err := sam.NewStreamSubSession("server_example") + ss, err := sam.NewUniqueStreamSubSession("PrimaryListenerTunnel") if err != nil { fmt.Println(err.Error()) return diff --git a/sam3_test.go b/sam3_test.go index c49c9d0..847182f 100644 --- a/sam3_test.go +++ b/sam3_test.go @@ -1,5 +1,3 @@ -// +build nettest - package sam3 import ( diff --git a/stream.go b/stream.go index 11107cf..ad9cd43 100644 --- a/stream.go +++ b/stream.go @@ -178,7 +178,7 @@ func (s *StreamSession) Dial(n, addr string) (c net.Conn, err error) { var i2paddr i2pkeys.I2PAddr var host string host, _, err = net.SplitHostPort(addr) - if err == nil { + if err = IgnorePortError(err); err == nil { // check for name if strings.HasSuffix(host, ".b32.i2p") || strings.HasSuffix(host, ".i2p") { // name lookup diff --git a/stream_test.go b/stream_test.go index bf09b3b..b0dc9f3 100644 --- a/stream_test.go +++ b/stream_test.go @@ -1,5 +1,3 @@ -// +build nettest - package sam3 import ( diff --git a/suggestedOptions.go b/suggestedOptions.go index 604738a..c0723d2 100644 --- a/suggestedOptions.go +++ b/suggestedOptions.go @@ -1,5 +1,7 @@ package sam3 +import "net/http" + // Examples and suggestions for options when creating sessions. var ( // Suitable options if you are shuffling A LOT of traffic. If unused, this @@ -51,3 +53,18 @@ var ( "inbound.backupQuantity=0", "outbound.backupQuantity=0", "inbound.quantity=2", "outbound.quantity=2"} ) + +func PrimarySessionString() string { + _, err := http.Get("http://127.0.0.1:7070") + if err != nil { + _, err := http.Get("http://127.0.0.1:7657") + if err != nil { + return "MASTER" + } + return "PRIMARY" + } + return "MASTER" + +} + +var PrimarySessionSwitch string = PrimarySessionString()