improve the dialer
This commit is contained in:
parent
842be5eadd
commit
78075c3e32
3 changed files with 45 additions and 12 deletions
45
conn/conn.go
45
conn/conn.go
|
@ -6,20 +6,51 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
|
sam3.I2PKeys
|
||||||
|
*sam3.StreamSession
|
||||||
*sam3.SAMConn
|
*sam3.SAMConn
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Conn) SaveKeys() {
|
func (c Conn) FindKeys() bool {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConn(conn *sam3.SAMConn, path string) (*Conn, error) {
|
func (c Conn) SaveKeys() (*sam3.I2PKeys, error) {
|
||||||
return GenConn(conn, path), nil
|
return &c.I2PKeys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenConn(conn *sam3.SAMConn, path string) *Conn {
|
func (c Conn) LoadKeys() (*sam3.I2PKeys, error) {
|
||||||
var c = Conn{SAMConn: conn, path: path}
|
return &c.I2PKeys, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Conn) Keys() (*sam3.I2PKeys, error) {
|
||||||
|
if c.FindKeys() {
|
||||||
|
return c.LoadKeys()
|
||||||
|
}
|
||||||
|
return c.SaveKeys()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConn(sam *sam3.SAM, addr, path string, opts []string) (*Conn, error) {
|
||||||
|
var c Conn
|
||||||
|
var err error
|
||||||
|
c.I2PKeys, err = sam.NewKeys()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.path = path + c.I2PKeys.Addr().Base32()
|
||||||
c.SaveKeys()
|
c.SaveKeys()
|
||||||
return &c
|
c.StreamSession, err = sam.NewStreamSession("stream_example", c.I2PKeys, sam3.Options_Small)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenConn(sam *sam3.SAM, addr, path string, opts []string) *Conn {
|
||||||
|
c, err := NewConn(sam, addr, path, opts)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,12 +17,12 @@ import (
|
||||||
type Manager struct {
|
type Manager struct {
|
||||||
resolver.Resolver
|
resolver.Resolver
|
||||||
socks5.Config
|
socks5.Config
|
||||||
sam3.StreamSession
|
*sam3.SAM
|
||||||
conns []*conn.Conn
|
conns []*conn.Conn
|
||||||
datadir string
|
datadir string
|
||||||
samhost string
|
samhost string
|
||||||
samport string
|
samport string
|
||||||
samopts string
|
samopts []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Manager) Serve() error {
|
func (m Manager) Serve() error {
|
||||||
|
@ -40,11 +40,11 @@ func (m Manager) DialI2P(ctx context.Context, addr string) (*sam3.SAMConn, error
|
||||||
return c.SAMConn, nil
|
return c.SAMConn, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newconn, err := m.StreamSession.DialI2P(i2paddr)
|
newconn, err := conn.NewConn(m.SAM, addr, m.datadir, m.samopts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m.conns = append(m.conns, conn.GenConn(newconn, m.datadir))
|
m.conns = append(m.conns, newconn)
|
||||||
log.Println("Generated destination for address:", i2paddr.Base32(), "at position", len(m.conns)-1)
|
log.Println("Generated destination for address:", i2paddr.Base32(), "at position", len(m.conns)-1)
|
||||||
return m.conns[len(m.conns)-1].SAMConn, nil
|
return m.conns[len(m.conns)-1].SAMConn, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,9 @@ func SetPort(v string) func(*Manager) error {
|
||||||
func SetSAMOpts(s []string) func(*Manager) error {
|
func SetSAMOpts(s []string) func(*Manager) error {
|
||||||
return func(c *Manager) error {
|
return func(c *Manager) error {
|
||||||
for _, i := range s {
|
for _, i := range s {
|
||||||
c.samopts += i + " "
|
if i != "" {
|
||||||
|
c.samopts = append(c.samopts, i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue