fix semver. Check router type to determine whether to send PRIMARY or MASTER to SAM session

This commit is contained in:
idk
2022-01-10 11:19:39 -05:00
parent 0e87ddfa4b
commit 45106d2b70
12 changed files with 51 additions and 38 deletions

View File

@ -1,6 +1,6 @@
USER_GH=eyedeekay USER_GH=eyedeekay
VERSION=0.33.01 VERSION=0.33.1
packagename=sam3 packagename=sam3
echo: echo:

View File

@ -347,7 +347,7 @@ func (cfg *Config) DatagramSession() (session *DatagramSession, err error) {
// determine udp port // determine udp port
var portstr string var portstr string
_, portstr, err = net.SplitHostPort(cfg.Addr) _, portstr, err = net.SplitHostPort(cfg.Addr)
if err == nil { if IgnorePortError(err) == nil {
var port int var port int
port, err = strconv.Atoi(portstr) port, err = strconv.Atoi(portstr)
if err == nil && port > 0 { if err == nil && port > 0 {

View File

@ -56,6 +56,10 @@ func (s *SAM) NewDatagramSession(id string, keys i2pkeys.I2PKeys, options []stri
return nil, err return nil, err
} }
_, lport, err := net.SplitHostPort(udpconn.LocalAddr().String()) _, 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}) conn, err := s.newGenericSession("DATAGRAM", id, keys, options, []string{"PORT=" + lport})
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1,5 +1,3 @@
// +build nettest
package sam3 package sam3
import ( import (

11
emit.go
View File

@ -3,6 +3,7 @@ package sam3
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
) )
type SAMEmit struct { type SAMEmit struct {
@ -96,3 +97,13 @@ func NewEmit(opts ...func(*SAMEmit) error) (*SAMEmit, error) {
} }
return &emit, nil 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
}

View File

@ -137,9 +137,7 @@ func (sam *PrimarySession) DialUDPI2P(network, laddr, raddr string) (*DatagramSe
func (s *PrimarySession) Lookup(name string) (a net.Addr, err error) { func (s *PrimarySession) Lookup(name string) (a net.Addr, err error) {
var sam *SAM var sam *SAM
if len(strings.Split(name, ":")) <= 1 { name = strings.Split(name, ":")[0]
name += ":0"
}
sam, err = NewSAM(s.samAddr) sam, err = NewSAM(s.samAddr)
if err == nil { if err == nil {
defer sam.Close() 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 // Creates a new PrimarySession with the I2CP- and streaminglib options as
// specified. See the I2P documentation for a full list of options. // specified. See the I2P documentation for a full list of options.
func (sam *SAM) NewPrimarySession(id string, keys i2pkeys.I2PKeys, options []string) (*PrimarySession, error) { 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 { if err != nil {
return nil, err 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 // Creates a new PrimarySession with the I2CP- and PRIMARYinglib options as
// specified. See the I2P documentation for a full list of options. // 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) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -329,6 +327,10 @@ func (s *PrimarySession) NewDatagramSubSession(id string, udpPort int) (*Datagra
return nil, err return nil, err
} }
_, lport, err := net.SplitHostPort(udpconn.LocalAddr().String()) _, lport, err := net.SplitHostPort(udpconn.LocalAddr().String())
if err != nil {
s.Close()
return nil, err
}
conn, err := s.newGenericSubSession("DATAGRAM", id, []string{"PORT=" + lport}) conn, err := s.newGenericSubSession("DATAGRAM", id, []string{"PORT=" + lport})
if err != nil { if err != nil {
return nil, err return nil, err
@ -368,6 +370,10 @@ func (s *PrimarySession) NewRawSubSession(id string, udpPort int) (*RawSession,
return nil, err return nil, err
} }
_, lport, err := net.SplitHostPort(udpconn.LocalAddr().String()) _, 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, s.keys, options, []string{"PORT=" + lport})
conn, err := s.newGenericSubSession("RAW", id, []string{"PORT=" + lport}) conn, err := s.newGenericSubSession("RAW", id, []string{"PORT=" + lport})
if err != nil { if err != nil {

View File

@ -1,5 +1,3 @@
// +build nettest
package sam3 package sam3
import ( import (

View File

@ -1,5 +1,3 @@
// +build nettest
package sam3 package sam3
import ( import (
@ -99,7 +97,7 @@ func Test_PrimaryStreamingServerClient(t *testing.T) {
} }
defer sam.Close() defer sam.Close()
fmt.Println("\tServer: Creating tunnel") fmt.Println("\tServer: Creating tunnel")
ss, err := sam.NewUniqueStreamSubSession("primaryExampleServerTun") ss, err := sam.NewUniqueStreamSubSession("PrimaryServerClientTunnel")
if err != nil { if err != nil {
return return
} }
@ -184,26 +182,13 @@ func ExamplePrimaryStreamSession() {
return 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 { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
return return
} }
defer sam.Close() defer sam.Close()
// See the example Option_* variables. conn, err := sam.Dial("tcp", "idk.i2p") //someone.Base32())
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)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return return
@ -223,8 +208,6 @@ func ExamplePrimaryStreamSession() {
fmt.Println("Read HTTP/HTML from idk.i2p") fmt.Println("Read HTTP/HTML from idk.i2p")
log.Println("Read HTTP/HTML from idk.i2p") log.Println("Read HTTP/HTML from idk.i2p")
} }
return
// Output: // Output:
//Sending HTTP GET / //Sending HTTP GET /
//Read HTTP/HTML from idk.i2p //Read HTTP/HTML from idk.i2p
@ -260,7 +243,7 @@ func ExamplePrimaryStreamListener() {
// Client connecting to the server // Client connecting to the server
go func(server i2pkeys.I2PAddr) { go func(server i2pkeys.I2PAddr) {
cs, err := sam.NewStreamSubSession("client_example") cs, err := sam.NewUniqueStreamSubSession("PrimaryListenerTunnel")
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
quit <- false quit <- false
@ -284,7 +267,7 @@ func ExamplePrimaryStreamListener() {
quit <- true quit <- true
}(keys.Addr()) // end of client }(keys.Addr()) // end of client
ss, err := sam.NewStreamSubSession("server_example") ss, err := sam.NewUniqueStreamSubSession("PrimaryListenerTunnel")
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return return

View File

@ -1,5 +1,3 @@
// +build nettest
package sam3 package sam3
import ( import (

View File

@ -178,7 +178,7 @@ func (s *StreamSession) Dial(n, addr string) (c net.Conn, err error) {
var i2paddr i2pkeys.I2PAddr var i2paddr i2pkeys.I2PAddr
var host string var host string
host, _, err = net.SplitHostPort(addr) host, _, err = net.SplitHostPort(addr)
if err == nil { if err = IgnorePortError(err); err == nil {
// check for name // check for name
if strings.HasSuffix(host, ".b32.i2p") || strings.HasSuffix(host, ".i2p") { if strings.HasSuffix(host, ".b32.i2p") || strings.HasSuffix(host, ".i2p") {
// name lookup // name lookup

View File

@ -1,5 +1,3 @@
// +build nettest
package sam3 package sam3
import ( import (

View File

@ -1,5 +1,7 @@
package sam3 package sam3
import "net/http"
// Examples and suggestions for options when creating sessions. // Examples and suggestions for options when creating sessions.
var ( var (
// Suitable options if you are shuffling A LOT of traffic. If unused, this // 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.backupQuantity=0", "outbound.backupQuantity=0",
"inbound.quantity=2", "outbound.quantity=2"} "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()